使用odeint的简单2d系统(使用数组)无法编译

Simple 2d system (using array) with odeint does not compile

本文关键字:数组 编译 odeint 简单 2d 系统 使用      更新时间:2023-10-16

我在Mint 12上运行g++ 4.7, boost 1.55。我试着解决一个简单的二维ode系统,用odeint,下面是一维的例子,一维。这个例子在原始版本和根据答案修改的版本中都可以正常编译。现在,如果我想要一个2d系统,我使用double[2],事情就行不通了:

#include <iostream>
#include <boost/numeric/odeint.hpp>
using namespace std;
using namespace boost::numeric::odeint;
void rhs( const double *x, double *dxdt, const double t )
{
    dxdt[0] = 3.0/(2.0*t*t) + x[0]/(2.0*t);
    dxdt[1] = 3.0/(2.0*t*t) + x[1]/(2.0*t);
}
void write_cout( double *x, const double t )
{
    cout << t << 't' << x[0] << 't' << 2*x[1] << endl;
}
typedef runge_kutta_cash_karp54< double[2] > stepper_type;
int main()
{
    double x[2] = {0.0,0.0};
    integrate_adaptive( make_controlled( 1E-12, 1E-12, stepper_type() ), rhs, x, 1.0, 10.0, 0.1, write_cout );
}

错误信息很混乱,但是以:

结尾

/usr/include/boost/numeric/odeint/algebra/range_algebra.hpp:129:47: error: function return a array

是数组double[2]的问题吗?我该怎么解决呢?也许用矢量?顺便说一下,我试过同时使用

typedef runge_kutta_cash_karp54< double > stepper_type;
typedef runge_kutta_cash_karp54< double , double , double , double , vector_space_algebra > stepper_type;

的建议在1d答案中,但无济于事。我还应该提到,在具有较旧boost的旧机器上(不记得是哪个版本),所有编译都没有问题。谢谢你的建议!

使用std::array<Double,2>

#include <array>
typedef std::array< double , 2 > state_type;
void rhs( state_type const &x, state_type &dxdt, const double t )
{
    dxdt[0] = 3.0/(2.0*t*t) + x[0]/(2.0*t);
    dxdt[1] = 3.0/(2.0*t*t) + x[1]/(2.0*t);
}
void write_cout( state_type const& x, const double t )
{
    cout << t << 't' << x[0] << 't' << 2*x[1] << endl;
}
typedef runge_kutta_cash_karp54< state_type > stepper_type;