Login  Register

Passing CubicSplines

Posted by Giancarlo Pfeifer-2 on Jul 11, 2006; 5:49pm
URL: http://quantlib.414.s1.nabble.com/Passing-CubicSplines-tp4913.html

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