interp_1d spline crashes program


ianbe
09-01-2009, 08:18 AM
Hi

i used Dave's test spline program in this thread

http://www.nr.com/forum/showthread.php?t=1693&highlight=spline_interp

which worked fine in a VS C++ project on its own. However, ramp up the samples and it fails beyond 2^10 ( 1024). I also implemented the struct in my existing project which compiled ok, and cout'd the ff double being passed to it before the return, but fails to seek the value in the input VecDoub xx failing at Base_interp.locate(Base_interp* , Double x)
line 25. with a memory violation error.

Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other me
mory is corrupt.


Below is the struct

struct Spline_wrapper {

Spline_interp* my_func;
Doub lam;
Spline_wrapper(VecDoub xx, VecDoub yy) {
static Spline_interp my_funcc(xx,yy);
my_func = &my_funcc;
}
double operator()(const double ff ) {
return log(exp(my_func->interp(ff)))+ lam ;
}
};


and below is the implementation extract in my main


for (int k=0; k < n ; k++) {
spline.lam = lambda[n+1-k] ;
pactual = log(exp(x[k]))+lambda[n+1-k];
pinterp = spline(x[k]) ;
perror = pinterp - pactual ;


the funny things with this error was that when i change the length of the VecDoub x(2^16) to x(2^8) I get a different error

ERROR: Bad input to routine splint
in file c:\dissertation\code\cppdev\dissertation_07050466\ dissertation_07050466\interp_1d.h at line 214

Unhandled Exception: System.Runtime.InteropServices.SEHException: External component has thrown an exception.
at _CxxThrowException(Void* , _s__ThrowInfo* )
at Spline_interp.rawinterp(Spline_interp* , Int32 jl, Double xv) in c:\dissertation\code\cppdev\dissertation_07050466\ dissertation_070504
66\interp_1d.h:line 214
at Base_interp.interp(Base_interp* , Double x) in c:\dissertation\code\cppdev\dissertation_07050466\ dissertation_07050466\interp_1d.h:lin
e 12
at Spline_wrapper.()(Spline_wrapper* , Double ff) in c:\dissertation\code\cppdev\dissertation_07050466\ dissertation_07050466\dissertation
_07050466.cpp:line 75
at main(String[] args) in c:\dissertation\code\cppdev\dissertation_07050466\ dissertation_07050466\dissertation_07050466.cpp:li ne 454
at mainCRTStartupStrArray(String[] arguments) in f:\dd\vctools\crt_bld\self_x86\crt\src\mcrtexe.cpp :line 309


it just doesn't like this line

spline(x[k])

and complains about access to the value. probably because the Spline routine cannot read in the 2^16 values in the VecDoub "x" given in the call to the struct

Spline_wrapper spline(x,q);

any ideas on how I can fix this ?

Thanks

Ian

ianbe
09-01-2009, 01:12 PM
its ok problem fixed. I just got rid of the struct.

davekw7x
09-01-2009, 05:03 PM
Hi

i used Dave's test spline program in this thread

http://www.nr.com/forum/showthread.php?t=1693&highlight=spline_interp

which worked fine in a VS C++ project on its own.

However...There was a bug in my example that resulted in "undefined behavior." Appeared to work for some numbers of samples with some compilers but not with others.

Change the signature of the wrapper class constructor from the following:


Spline_wrapper(VecDoub xx, VecDoub yy)



to the following:


Spline_wrapper(VecDoub_I & xx, VecDoub & yy)



I regret the error.

Regards,

Dave

ianbe
09-01-2009, 06:10 PM
Thanks for that Dave,

I'm using interp1d but couldn't get it to produce correct results. I keep getting high interpolated values. even with your example on just x*x removing the sin function I get these values.

x interpolated actual
1.000000e+000 -3.023152e+075 1.000000e+000
1.008798e+000 -5.855462e+036 1.017673e+000
1.017595e+000 -1.692189e+051 1.035500e+000
1.026393e+000 -7.460353e+060 1.053483e+000
1.035191e+000 -3.288320e+070 1.071620e+000
1.043988e+000 -6.755712e+036 1.089912e+000
1.052786e+000 -2.976492e+046 1.108358e+000
1.061584e+000 -1.311135e+056 1.126960e+000
1.070381e+000 -3.784256e+070 1.145716e+000
1.079179e+000 -1.189261e+032 1.164627e+000

after amending your implementation it now works, so double thanks

Ian


x interpolated actual
1.000000e+000 1.000000e+000 1.000000e+000
1.008798e+000 1.017673e+000 1.017673e+000
1.017595e+000 1.035500e+000 1.035500e+000
1.026393e+000 1.053483e+000 1.053483e+000
1.035191e+000 1.071620e+000 1.071620e+000
1.043988e+000 1.089912e+000 1.089912e+000
1.052786e+000 1.108358e+000 1.108358e+000
1.061584e+000 1.126960e+000 1.126960e+000
1.070381e+000 1.145716e+000 1.145716e+000
1.079179e+000 1.164627e+000 1.164627e+000
1.087977e+000 1.183693e+000 1.183693e+000
1.096774e+000 1.202914e+000 1.202914e+000
1.105572e+000 1.222289e+000 1.222289e+000
1.114370e+000 1.241819e+000 1.241819e+000
1.123167e+000 1.261504e+000 1.261504e+000