你能给我提供一个用 odeint 进行数值积分的简单例子吗?在 C++
中?
我想使用方便的集成功能,documented作为:
integrate( system , x0 , t0 , t1 , dt )
我也不确定,如果可能的话,如何传递它而不是函数或仿函数,类方法。
最佳答案
在 C++11 中,您可以使用一个简单的 lambda 函数包装对您的成员方法的调用
Class c;
auto f = [&c]( const state_type & x , state_type &dxdt , double t ) {
c.system_func( x , dxdt , t ); };
integrate( f , x0 , t0 , t1 , dt );
std::bind
也可能有效,但是您必须注意值是按引用还是按值传递的。
在 C++03 中,您需要围绕您的类方法编写一个简单的包装器
struct wrapper
{
Class &c;
wrapper( Class &c_ ) : c( c_ ) { }
template< typename State , typename Time >
void operator()( State const &x , State &dxdt , Time t ) const
{
c.system_func( x , dxdt , t );
}
};
// ...
integrate( wrapper( c ) , x0 , t0 , t1 , dt );
(Boost.Bind 将无法在超过两个参数的情况下正常工作)。
关于c++ - 如何在 C++ 中使用 odeint 执行简单的数值积分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18769254/