Saul Teukolsky
02-28-2002, 12:27 PM
On p.27 of the C++ text it says you can easily use valarray for vectors instead of NRVec just by saying
#define NRVec valarray
This is not quite correct. The declaration for operator[] for a const valarray is
T operator[](size_t) const;
whereas for a const NRVec the return type is T &, not T. The only place this makes a difference in the recipes is when an argument of a function is Vec_I (which is typedef'd to const NRVec) and its reference is then explicitly taken. This fails for the valarray implementation. There are 2 recipes that are affected: sprstp and shootf.
A workaround is to remove the const:
typedef NRVec<INT> Vec_I_INT;
typedef NRVEC<DP> Vec_I_DP;
to override the corresponding statements in nrtypes.h. Then operator[] for a non-const is invoked, and it is a reference in the valarray implementation too.
#define NRVec valarray
This is not quite correct. The declaration for operator[] for a const valarray is
T operator[](size_t) const;
whereas for a const NRVec the return type is T &, not T. The only place this makes a difference in the recipes is when an argument of a function is Vec_I (which is typedef'd to const NRVec) and its reference is then explicitly taken. This fails for the valarray implementation. There are 2 recipes that are affected: sprstp and shootf.
A workaround is to remove the const:
typedef NRVec<INT> Vec_I_INT;
typedef NRVEC<DP> Vec_I_DP;
to override the corresponding statements in nrtypes.h. Then operator[] for a non-const is invoked, and it is a reference in the valarray implementation too.