I have trouble compiling code to integrate a cubic spline.
I have given a list of numbers from which I compute a cubic spline via a function in boost. This works so far. Now I want to calculate for example the length of the spline or just the area underneath it, therefore I need to integrate. But how do I pass the spline function to the integrator? I use the integrator for ODEs available in boost. I tried passing the integrator as a class with () operator, but the code fails to compile with the error message:
no type named 'type' in 'struct boost::enable_if<mpl_::bool_<false>, long long unsigned int>'
no matching function for call to 'integrate(integration_of_spline&, double&, double&, double&, double&, boost::numeric::odeint::null_observer)'
My code:
#include <iostream>
#include <vector>
#include <boost/math/interpolators/cardinal_cubic_b_spline.hpp>
#include <boost/numeric/odeint.hpp>
int main()
{
//create vector
std::vector<double> y_values = {0.0, 1.5, 5.0, 11.0, 13.0};
double step = 0.25;
//create spline
boost::math::interpolators::cardinal_cubic_b_spline<double> spline(y_values.data(), y_values .size(), 0, step);
//create rhs of ODE
class integration_of_spline {
public:
integration_of_spline ( boost::math::interpolators::cardinal_cubic_b_spline<double> &spline ) : spline(spline) { }
void operator()( const double &L , double &dLdt , const double t ) const
{
dLdt = this->spline(t);
return;
}
private:
boost::math::interpolators::cardinal_cubic_b_spline<double> spline;
};
//initial condition
double L = 0.0;
// Test: these all work
integration_of_spline integrator(spline);
double dLdt = 0;
integrator(L,dLdt,0.8);
std::cout << "dLdt test " << dLdt << std::endl;
std::cout << "spline(0.8) " << spline(0.8) << std::endl; //dLdt=spline(0.8) accordingly
//Integration. This step fails
boost::numeric::odeint::integrate( integrator, L , 0.0 , 1.0 , step );
//This also fails
boost::numeric::odeint::integrate( integration_of_spline(spline) , L , 0.0 , 1.0 , step );
return 0;
}
I have the feeling that it has something to do with how I pass the spline to the class, but I can't figure it out. It looks like the () operator is working correctly.
Documentation for the splines is here boost spline. I want to do something similar to boost odeint example, but even simpler in the sense that the state type is a single value.