8. Using this Software with Numerical Recipes in C++.

This software is designed to be compatible with the Numerical Recipes in C++ software. Using it with NR is not difficult. Just modify the following NR header files "nrtypes.h" and "nrutil.h". Each of these header files just includes two other header files. By default, these are "nrtypes_nr.h" and "nrutil_nr.h". Simply modify these two files to include "vm/nrtypes_vm.h" and "vm/nrutil_vm.h" instead. This produces the following two #define statements:

#define NRVec Vector

#define NRMat Matrix

These header files also make sure that nothing is duplicated by the two sets of header files. For example, some of the typedef declarations described previously are included in the NR software.

These classes should be completely compatible with the NR software, but that does not mean that they are 100% compatible with the NRVec<T> and NRMat<T> classes. The following few issues should be kept in mind if you are porting a program that uses these classes (including any of the NR sample programs).

8.1. The Copy Constructor.

The NR copy constructors create deep copies of the vector or matrix's data. The VecMat copy constructors make shallow copies. This means that if you attempt to use the copy constructor to create a "working copy" of data that should not be altered, the results will not be as expected when using the VecMat classes.

The workaround is simple. If you use something like the following, to create a working copy of a vector,

Vec_DP y = x;

Replace it with the statement,

Vec_DP y = x.copy();

Likewise, one should keep in mind that sending a vector or matrix to a function by value, rather than by reference, indirectly calls the copy constructor, as does returning by value.

8.2. The Assignment Operator.

The NR assignment operator will resize a vector or matrix to the same size as the right hand expression, if they are not already the same size. The VecMat assignment operators are not defined for different size objects. If you use the assignment operator to resize vectors or matrices in your code, then when porting to the VecMat classes, simply use the reshape() member function instead.

8.3. Indexing of Matrices.

In the NR software, the [] operator returns a pointer to the appropriate row of a matrix, which can then be indexed with a second [] operator. In the VecMat software, the use of strides makes this impossible. The [] operator instead returns an iterator to the row, which can then be indexed for the individual elements. This is provided to maintain compatibility with the NR software. The VecMat software also provides an () operator, for direct indexing, which should be more efficient.

The place where the [] operator could cause compatibility issues is if you explicitly use it to get a pointer to a matrix's data. Instead, use the data() member function.

For more information on the Numerical Recipes in C++ software, please refer to their webpage: www.nr.com.

Next Section

Back to Index