Re: Passing CubicSplines

Posted by Ferdinando M. Ametrano-3 on
URL: http://quantlib.414.s1.nabble.com/Passing-CubicSplines-tp4913p4914.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
>