file i/o with numerical recipes routines


andbeck
03-17-2005, 09:54 AM
I have written a program that uses the levenberg-marquardt routines and I have managed to get it to compile and build. I want to use the program to fit a set of data from a file that will contain my x, y and sig values.

However, whenever I try to use the >> operator to read in, say i values of x etc from a file e.g.

int n = x.size();
for (int i = 0; i < n i++) {
file >> x[i]; .......

this will only work if I define the x, y and sig vectors as double, not const double as in nrtypes_nr.h. When it does this, my program executes but misses out all the parts where it is crucial that the corresponding values are given! Also whenever I try to use cout << to print the values of the data read in from the file to the screen my program crashes!

Please help!!!!

Saul Teukolsky
03-17-2005, 12:57 PM
You should be doing your i/o in the calling program, not inside the recipe. In the calling program, declare vectors with Vec_DP, which is not const. You then pass them as Vec_I_DP to the recipe, which guarantees the recipe doesn't change them. This is further discussed in Chapter 1 of the book.

andbeck
03-21-2005, 01:49 AM
Thanks for the advice Saul,

I was doing my i/o in the calling program already but I tried declaring my vectors as Vec_DP as you suggested - unfortunately it's still not working!

I think my problem may be to do with scope - I'm reading in the vectors x, y and sig to the calling program but I don't think the program is passing them to the routines. Perhaps I need to define the vectors as global variables? I'm not sure how to go about doing this for this particular program though.

Saul Teukolsky
03-21-2005, 01:20 PM
Only relevant parts of code shown:

#include "nr.h"
...
int main(void)
{
const int NPT=
...
Vec_DP x(NPT),y(NPT),sig(NPT);
...
for (i=0;i<NPT;i++) {
x[i]=
...
NR::mrqmin(x,y,sig,a,ia...