问题描述
我有一个可以在 scipy.integrate.ode
和 scipy.integrate.odeint
之间互换的求解器.这是代码.
I have a made a solver which can interchange between scipy.integrate.ode
and scipy.integrate.odeint
. Here is the code.
def f(y,s,C,u,v):
y0 = y[0] # u
y1 = y[1] # u'
y2 = y[2] # v
y3 = y[3] # v'
dy = np.zeros_like(y)
dy[0] = y1
dy[2] = y3
C = C.subs({u:y0,v:y2})
dy[1] = -C[0,0][0]*dy[0]**2\
-2*C[0,0][1]*dy[0]*dy[2]\
-C[0,1][1]*dy[2]**2
dy[3] = -C[1,0][0]*dy[0]**2\
-2*C[1,0][1]*dy[0]*dy[2]\
-C[1,1][1]*dy[2]**2
return dy
def solve(C,u0,s0,s1,ds,solver=None):
from sympy.abc import u,v
if solver == None: # use lsoda from scipy.integrate.odeint
s = np.arange(s0,s1+ds,ds)
print 'Running solver ...'
return sc.odeint(f,u0,s,args=(C,u,v))
else: # use any other solver from scipy.integrate.ode
r = sc.ode(f).set_integrator(solver) # vode,zvode,lsoda,dopri5,dop853
r.set_f_params(C,u,v)
r.set_initial_value(u0)
#t = []
y = []
print 'Running solver ...'
while r.successful() and r.t <= s1:
r.integrate(r.t + ds)
y.append(r.y)#; t.append(r.t)
return np.array(y)
我遇到的问题如下.如果我决定使用 scipy.integrate.odeint
中的求解器,那么 f
的参数必须按照它们在代码中的顺序指定.但是,如果我决定使用 scipy.integrate.ode
中的求解器,我必须更改函数 f(y,s,C,u,v) 到
f(s,y,C,u,v)
,否则我得到错误
The problem I experience is as following. If I decide to use the solver from scipy.integrate.odeint
then the parameters of f
have to be specified in the order as they are in the code. However, if I decide to use the solvers from scipy.integrate.ode
I have to change the order of the parameters of the function f(y,s,C,u,v)
to f(s,y,C,u,v)
, otherwise I get the error
TypeError: 'float' object has no attribute '__getitem__'
如果我这样做,那么 scipy.integrate.odeint
会为 f
生成相同的错误,定义为 f(s,y,C,u,v)
.无论参数顺序如何,如何统一f
操作?
If I do this, then scipy.integrate.odeint
generates the same error for f
defined as f(s,y,C,u,v)
. How can I operate with a unified f
regardless of the order of the parameters ?
总结一下问题:
scipy.integrate.ode
求解器工作.如果函数 f 定义为 f(y,s,C,u,v)
,integrate.odeint 求解器就可以工作.为什么会发生这种情况,我该如何解决?
scipy.integrate.ode
solvers work if the function f is defined as f(s,y,C,u,v)
, and scipy.integrate.odeint
solver works if the function f is defined as f(y,s,C,u,v)
. Why is this occurring, and how I can I fix this?
Scipy -- 版本 0.16.0
Scipy -- version 0.16.0
推荐答案
发生这种情况是因为多年前做出的一个不幸的 API 设计决定.odeint
和 ode
类需要不同的签名来解决系统问题.
It is occurring because of an unfortunate API design decision made years ago. odeint
and the ode
class require different signatures for the system to be solved.
当您使用例如 ode
类时,您可以通过添加一个包装器来更改前两个参数的顺序来修复它.例如,您可以更改此内容:
You can fix it by adding a wrapper that changes the order of the first two arguments when you use, say, the ode
class. For example, you could change this:
r = sc.ode(f).set_integrator(solver)
到
r = sc.ode(lambda t, x, *args: f(x, t, *args)).set_integrator(solver)
更新:在 SciPy 1.1.0 中,参数 tfirst
被添加到 scipy.integrate.odeint
.默认值 tfirst=False
保持旧行为.使用 tfirst=True
,odeint
需要 first 参数func
为 t
(即自变量).通过使用 tfirst=True
,相同的 func
可以用于 ode
、odeint
和更新的 solver_ivp
.
Update: In SciPy 1.1.0, the argument tfirst
was added to scipy.integrate.odeint
. The default, tfirst=False
, maintains the old behavior. With tfirst=True
, odeint
expects the first argument offunc
to be t
(i.e. the independent variable). By using tfirst=True
, the same func
can be used with ode
, odeint
and the newer solver_ivp
.
这篇关于不同 scipy ode 求解器之间的互换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!