【发布时间】:2015-01-13 21:54:23
【问题描述】:
我要从c++ boost 库中了解odeint,我需要知道哪个部分做什么。
在boost/numeric/odeint/integrate/integrate_adaptive.hpp 中有一个名为integrate_adaptive 的函数。这个函数有一些重载。我的一些操作的简化文件如下:
integrate_adaptive_minimal.hpp
#define BOOST_NUMERIC_ODEINT_INTEGRATE_INTEGRATE_ADAPTIVE_HPP_INCLUDED
#include <boost/type_traits/is_same.hpp>
#include <boost/numeric/odeint/stepper/stepper_categories.hpp>
#include <boost/numeric/odeint/integrate/null_observer.hpp>
#include <boost/numeric/odeint/integrate/detail/integrate_adaptive.hpp>
using namespace std;
namespace boost {
namespace numeric {
namespace odeint {
/*
* the two overloads are needed in order to solve the forwarding problem
*/
template< class Stepper , class System , class State , class Time , class Observer >
size_t integrate_adaptive(
Stepper stepper , System system , State &start_state ,
Time start_time , Time end_time , Time dt ,
Observer observer )
{
cout<<"type one"<<endl; //added by me ***************************************
typedef typename odeint::unwrap_reference< Stepper >::type::stepper_category stepper_category;
return detail::integrate_adaptive(
stepper , system , start_state ,
start_time , end_time , dt ,
observer , stepper_category() );
/*
* Suggestion for a new extendable version:
*
* integrator_adaptive< Stepper , System, State , Time , Observer , typename Stepper::stepper_category > integrator;
* return integrator.run( stepper , system , start_state , start_time , end_time , dt , observer );
*/
}
/**
* \brief Second version to solve the forwarding problem,
* can be called with Boost.Range as start_state.
*/
template< class Stepper , class System , class State , class Time , class Observer >
size_t integrate_adaptive(
Stepper stepper , System system , const State &start_state ,
Time start_time , Time end_time , Time dt ,
Observer observer )
{
cout<<"type two"<<endl; //added by me ***************************************
typedef typename odeint::unwrap_reference< Stepper >::type::stepper_category stepper_category;
return detail::integrate_adaptive(
stepper , system , start_state ,
start_time , end_time , dt ,
observer , stepper_category() );
}
} // namespace odeint
} // namespace numeric
} // namespace boost
这两个函数的区别与参数start_state有关。在第二个函数中,它包含一个 const 关键字。我想知道调用了哪个重载。因此,我在每个函数中添加了一个 cout。然后我从这个测试文件中调用它们:
minimal.cpp
#include "integrate_adaptive_minimal.hpp"
#include <boost/numeric/odeint.hpp>
#include <armadillo>
using namespace arma;
using namespace boost::numeric::odeint;
typedef vec::fixed<2> state_type;
void sys( const state_type &x , state_type &dxdt , const double t)
{
mat A;
A<<-1<<0<<endr<<0<<-1;
mat B;
B<<1<<endr<<1;
dxdt=A*x+B*(t>0?1:0);
}
void observer( const state_type &x , const double t )
{
}
typedef runge_kutta_dopri5<state_type> stepper_type;
int main()
{
state_type x;
x(0) = 0.0;
x(1) = 0.0;
integrate_adaptive(make_controlled(1E-10,1E-10,stepper_type()),
sys,x,0.0,11.0,0.1,observer);
return 0;
}
当我编译运行时:
g++ -std=c++11 minimal.cpp -larmadillo -lboost_thread -lboost_filesystem -lboost_system -Wfatal-errors
./a.out
结果显示第一个函数integrate_adaptive被调用,它没有const。现在,我想知道调用第二个函数的正确方法是什么?对象 x 不能是const!
如果有人知道boost 库的机制,请告诉我第二个函数的优势。
更新:
【问题讨论】:
-
添加
state_type const cx = x;,然后在对integrate_adaptive的调用中使用cx而不是x。您还可以通过调用vec::fixex<2>构造函数来直接初始化cx...类似于state_type const cx(0.0, 0.0); -
“结果显示”。坦率地说,我没有看到任何结果,我不认为那是因为我做错了。你指的是什么结果?
-
@sehe
cout<<"type one"<<endl; //added by me
标签: c++ templates boost overloading odeint