Hi all,
I'd like to create a NaturalCubicSpline object and use it outside the function in which it is generated. I build the spline populating the array of x values and the array of y values, and passing the reference to their first and last elements into the constructor of NaturalCubicSpline. The problem that i notice is that if i pass the NaturalCubicSpline generated to another routine, but i let the arrays with the x and y values go out of scope, the program fails with an assertion error: Expression: vector iterator not dereferencable. I think that this is simply the way it is: the constructor copies the begin and end of the vectors but doesn't perform a copy of the vectors, so if I want to persist the spline, i should package a struct{} that contais the spline with the two vectors. Is this the case? Or is there another way to construct the spline so that i can treat it as an object on its own? Thanks, gc P.S. I'm adding a snipped of code that reproduce the error i'm receiving #include <ql/quantlib.hpp> #include <boost/shared_ptr.hpp> #include <iostream> int _tmain(int argc, _TCHAR* argv[]) { boost::shared_ptr<QuantLib::NaturalCubicSpline> curve2; { std::vector<double> x; std::vector<double> y; x.push_back(1); y.push_back(10); x.push_back(2); y.push_back(7); x.push_back(3); y.push_back(2); boost::shared_ptr<QuantLib::NaturalCubicSpline> curve (new QuantLib::NaturalCubicSpline( x.begin(), x.end(), y.begin() ) ); curve2 = curve; } for ( double i = 1; i<= 8; i+= 0.25 ) { std::cout << i << "\t" << curve2->operator ()( i, true ) << std::endl; } return 0; } ___________________________________________________________ The all-new Yahoo! Mail goes wherever you go - free your email address from your Internet provider. http://uk.docs.yahoo.com/nowyoucan.html |
Hi Giancarlo,
you're right: in the current design the interpolation classes do not copy the x,y arrays, so it's up to the user to ensure they do not get destroyed while still needed. I'm not in favour of this behaviour, and this might change if Luigi agrees. ciao -- Nando On 7/11/06, Giancarlo Pfeifer <[hidden email]> wrote: > Hi all, > > I'd like to create a NaturalCubicSpline object and use it > outside the function in which it is generated. > I build the spline populating the array of x values and the array > of y values, and passing the reference to their first and last elements > into the constructor of NaturalCubicSpline. > The problem that i notice is that if i pass the NaturalCubicSpline > generated to another routine, but i let the arrays with the x and y values > go out of scope, the program fails with an assertion error: Expression: > vector iterator not dereferencable. > > I think that this is simply the way it is: the constructor > copies the begin and end of the vectors but doesn't perform a copy of the > vectors, so if I want to persist the spline, i should package a struct{} > that contais the spline with the two vectors. > > Is this the case? Or is there another way to construct the spline so that > i can treat it as an object on its own? > > Thanks, > gc > > P.S. I'm adding a snipped of code that reproduce the error i'm receiving > > > #include <ql/quantlib.hpp> > #include <boost/shared_ptr.hpp> > #include <iostream> > > > > int _tmain(int argc, _TCHAR* argv[]) > { > boost::shared_ptr<QuantLib::NaturalCubicSpline> curve2; > { > std::vector<double> x; > std::vector<double> y; > > x.push_back(1); > y.push_back(10); > x.push_back(2); > y.push_back(7); > x.push_back(3); > y.push_back(2); > > boost::shared_ptr<QuantLib::NaturalCubicSpline> curve > (new QuantLib::NaturalCubicSpline( x.begin(), x.end(), y.begin() ) ); > curve2 = curve; > } > > for ( double i = 1; i<= 8; i+= 0.25 ) { > std::cout << i << "\t" << curve2->operator ()( i, true ) > << std::endl; > } > return 0; > } > > > ___________________________________________________________ > The all-new Yahoo! Mail goes wherever you go - free your email address from your Internet provider. http://uk.docs.yahoo.com/nowyoucan.html > > > > ------------------------------------------------------------------------- > Using Tomcat but need to do more? Need to support web services, security? > Get stuff done quickly with pre-integrated technology to make your job easier > Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo > http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642 > _______________________________________________ > QuantLib-users mailing list > [hidden email] > https://lists.sourceforge.net/lists/listinfo/quantlib-users > |
On 07/12/2006 10:30:22 AM, Ferdinando Ametrano wrote: > you're right: in the current design the interpolation classes do not > copy the x,y arrays, so it's up to the user to ensure they do not get > destroyed while still needed. > I'm not in favour of this behaviour, and this might change if Luigi > agrees. I'm not extremely happy about it either. On the other hand, storing iterators allows the interpolation to maintain a link to the original values and to update itself when they change; copying the values would prevent this. Luigi ---------------------------------------- The rule on staying alive as a forecaster is to give 'em a number or give 'em a date, but never give 'em both at once. -- Jane Bryant Quinn |
> I'm not extremely happy about it either. On the other hand, storing iterators
> allows the interpolation to maintain a link to the original values and to > update itself when they change; copying the values would prevent this. > > Luigi Thanks for the replies. It's only a stylistic issue, so now that i know that this is the correct way to use the spline i'll adjust my coding. My only thought in favour of copying the arrays is that while it is true that by maintaining the link to the vectors allows the curve to update itself, it is also true that you need to invoke the "calculate()" method explicitly to recompute the coefficients and the derivatives. Therefore i'm thinking that a possible alternative is to copy the vectors during the initialisation, and modify the method "calculate()" to pass in the updated vectors. But it's probably not worth doing, and there probably are other considerations i haven't though about... Thanks a lot for your explanations, and thanks for your wonderful work! gc ___________________________________________________________ Does your mail provider give you FREE antivirus protection? Get Yahoo! Mail http://uk.mail.yahoo.com |
In reply to this post by Luigi Ballabio
Hi, The solution I found in my code was to create a struct or class to hold the interpolation object as well as the X and Y arrays. I can pass an object created from this to my functions instead of the interpolation object alone. Thus I can still update the arrays whenever I want and when the object goes out of scope, everything is destroyed. Toy out. >From: Luigi Ballabio <[hidden email]> >To: Ferdinando Ametrano <[hidden email]> >CC: Giancarlo Pfeifer ><[hidden email]>,[hidden email] >Subject: Re: [Quantlib-users] Passing CubicSplines >Date: Wed, 12 Jul 2006 11:43:36 +0200 > > >On 07/12/2006 10:30:22 AM, Ferdinando Ametrano wrote: > > you're right: in the current design the interpolation classes do not > > copy the x,y arrays, so it's up to the user to ensure they do not get > > destroyed while still needed. > > I'm not in favour of this behaviour, and this might change if Luigi > > agrees. > >I'm not extremely happy about it either. On the other hand, storing >iterators allows the interpolation to maintain a link to the original >values and to update itself when they change; copying the values would >prevent this. > >Luigi > > > >---------------------------------------- > >The rule on staying alive as a forecaster is to give 'em a number or >give 'em a date, but never give 'em both at once. >-- Jane Bryant Quinn > > >------------------------------------------------------------------------- >Using Tomcat but need to do more? Need to support web services, security? >Get stuff done quickly with pre-integrated technology to make your job >easier >Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo >http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642 >_______________________________________________ >QuantLib-users mailing list >[hidden email] >https://lists.sourceforge.net/lists/listinfo/quantlib-users |
Free forum by Nabble | Edit this page |