C++の答えがあります。投稿するのに最適な場所ではないかもしれませんが、それでも興味深いかもしれません。(これを配置するのに適した場所が見つかりませんでした。これは、C++ ソリューションを探しているときに着陸した場所です)。
以下は、変数がゼロ以下になったときに統合を停止する C++ の例です。
#include <iostream>
#include <boost/range/algorithm.hpp>
#include <boost/numeric/odeint.hpp>
using namespace std;
using namespace boost::numeric::odeint;
typedef double state_type;
typedef runge_kutta_cash_karp54< state_type > error_stepper_type;
class Myode{
public:
void operator()(const state_type& x, state_type& dxdt, const double t){dxdt=-1-x;}
static bool done(const state_type &x){return x<=0.0;}
};
int main(int argc, char* argv[]){
Myode ode;
state_type x=10.0;
auto stepper = make_controlled< error_stepper_type >( 1.0e-10 , 1.0e-6 );
auto iter= boost::find_if(make_adaptive_range(stepper,ode,x, 0.0 , 20.0 , 0.01 ),
Myode::done);
cout<<"integration stopped at"<<x<<endl;
return 1;
}
積分は、ゼロ以下の値 x に初めて達したときに停止します (done 関数を参照)。そのため、現在のステップ サイズによっては、ゼロをはるかに下回る可能性があります。
これは c++11 コンストラクトを使用するため、コンパイラでこれを有効にする必要があることに注意してください。私の場合 (gcc 4.4) は、コンパイル コマンドに -std=gnu++0x を追加することで達成されました。