scipy.integrate.ode.set_solout 工作吗? [英] Does scipy.integrate.ode.set_solout work?
问题描述
集成例程的 scipy.integrate.ode
接口提供了一种在任何步骤违反约束时停止集成的方法,set_solout
.但是,即使在最简单的示例中,我也无法使用此方法.这是一次尝试:
The scipy.integrate.ode
interface to integration routines provides a method for stopping the integration if a constraint is violated at any step, set_solout
. However, I cannot get this method to work, even in the simplest examples. Here's one attempt:
import numpy as np
from scipy.integrate import ode
def f(t, y):
"""Exponential decay."""
return -y
def solout(t, y):
if y[0] < 0.5:
return -1
else:
return 0
y_initial = 1
t_initial = 0
r = ode(f).set_integrator('dopri5') # Integrator that supports solout
r.set_initial_value(y_initial, t_initial)
r.set_solout(solout)
# Integrate until t = 5, but stop when solout constraint violated
r.integrate(5)
# The time when solout should have terminated integration:
intersection_time = np.log(2)
当 t = log(2) = 0.693...
时,集成应该被 solout 停止,但反而愉快地继续到 t = 5
,当 >y = 0.007
.
The integration should have been stopped by solout when t = log(2) = 0.693...
, but instead happily continues until t = 5
, when y = 0.007
.
这是 scipy
中的错误,还是我没有正确使用 set_solout
?
Is this a bug in scipy
, or am I not using set_solout
correctly?
推荐答案
事实证明你需要在调用 set_initial_value
之前调用 set_solout
.(我通过研究 scipy
测试套件中的 set_solout 测试.)因此,在我的问题代码中颠倒两次调用的顺序会产生正确的结果.
It turns out you need to call set_solout
before calling set_initial_value
. (I figured this out by studying the set_solout
tests in the scipy
test suite.) So, reversing the order of the two calls in my question code produces the correct result.
即使这种行为是正确的,也应该在 set_solout
的文档中提及.我已经在 GitHub 上发布了 SciPy 的问题.
Even if this behavior is correct, it ought to be mentioned in the documentation for set_solout
. I've posted an issue with SciPy on GitHub.
更新:此问题已在 SciPy 0.17.0 中修复;即使在set_initial_value
之后调用set_solout
也会起作用,并且问题代码会产生正确的结果.
UPDATE: This issue is fixed in SciPy 0.17.0; set_solout
will work even if called after set_initial_value
, and the question code will produce the correct result.
这篇关于scipy.integrate.ode.set_solout 工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!