使用 scipy.integrate.complex_ode 而不是 scipy.integrate.ode
Using scipy.integrate.complex_ode instead of scipy.integrate.ode
我正在尝试使用 complex_ode 方法而不是 scipy.integrate 中的 ode 方法。 complex_ode 的帮助页面没有提供示例,所以我可能做错了。
此代码适用于 scipy.integrate.ode:
from scipy.integrate import ode
y0, t0 = [1.0j, 2.0], 0
def f(t, y, arg1):
return [1j*arg1*y[0] + y[1], -arg1*y[1]**2]
def jac(t, y, arg1):
return [[1j*arg1, 1], [0, -arg1*2*y[1]]]
r = ode(f, jac).set_integrator('zvode', method='bdf', with_jacobian=True)
r.set_initial_value(y0, t0).set_f_params(2.0).set_jac_params(2.0)
t1 = 10
dt = 1
while r.successful() and r.t < t1:
r.integrate(r.t+dt)
print(r.t, r.y)
现在这段代码试图对 complex_ode 做同样的事情。
from scipy.integrate import complex_ode
y0, t0 = [1.0j, 2.0], 0
def f(t, y, arg1):
return [1j*arg1*y[0] + y[1], -arg1*y[1]**2]
def jac(t, y, arg1):
return [[1j*arg1, 1], [0, -arg1*2*y[1]]]
r = complex_ode(f, jac)
r.set_initial_value(y0, t0).set_f_params(2.0).set_jac_params(2.0)
t1 = 10
dt = 1
while r.successful() and r.t < t1:
r.integrate(r.t+dt)
print(r.t, r.y)
但是 r.integrate 行抱怨这个错误:'float' object has no attribute 'getitem'.
有人可以告诉我我做错了什么吗?
这似乎是 known bug in scipy.integrate. It seems that additional argument passing is broken in complex_ode
. You could try and see if they have fixed it in a newer release (although this bug report 暗示他们没有),或者在使用 complex_ode
时将自己限制在没有附加参数的情况下使用自己的包装函数。例如,您的示例的 hacky 解决方案可能类似于:
from scipy.integrate import complex_ode
class myfuncs(object):
def __init__(self, f, jac, fargs=[], jacargs=[]):
self._f = f
self._jac = jac
self.fargs=fargs
self.jacargs=jacargs
def f(self, t, y):
return self._f(t, y, *self.fargs)
def jac(self, t, y):
return self._jac(t, y, *self.jacargs)
def f(t, y, arg1):
return [1j*arg1*y[0] + y[1], -arg1*y[1]**2]
def jac(t, y, arg1):
return [[1j*arg1, 1], [0, -arg1*2*y[1]]]
y0, t0 = [1.0j, 2.0], 0
case = myfuncs(f, jac, fargs=[2.], jacargs=[2.])
r = complex_ode(case.f, case.jac)
r.set_initial_value(y0, t0)
t1 = 10
dt = 1
while r.successful() and r.t < t1:
r.integrate(r.t+dt)
print(r.t, r.y)
我正在尝试使用 complex_ode 方法而不是 scipy.integrate 中的 ode 方法。 complex_ode 的帮助页面没有提供示例,所以我可能做错了。
此代码适用于 scipy.integrate.ode:
from scipy.integrate import ode
y0, t0 = [1.0j, 2.0], 0
def f(t, y, arg1):
return [1j*arg1*y[0] + y[1], -arg1*y[1]**2]
def jac(t, y, arg1):
return [[1j*arg1, 1], [0, -arg1*2*y[1]]]
r = ode(f, jac).set_integrator('zvode', method='bdf', with_jacobian=True)
r.set_initial_value(y0, t0).set_f_params(2.0).set_jac_params(2.0)
t1 = 10
dt = 1
while r.successful() and r.t < t1:
r.integrate(r.t+dt)
print(r.t, r.y)
现在这段代码试图对 complex_ode 做同样的事情。
from scipy.integrate import complex_ode
y0, t0 = [1.0j, 2.0], 0
def f(t, y, arg1):
return [1j*arg1*y[0] + y[1], -arg1*y[1]**2]
def jac(t, y, arg1):
return [[1j*arg1, 1], [0, -arg1*2*y[1]]]
r = complex_ode(f, jac)
r.set_initial_value(y0, t0).set_f_params(2.0).set_jac_params(2.0)
t1 = 10
dt = 1
while r.successful() and r.t < t1:
r.integrate(r.t+dt)
print(r.t, r.y)
但是 r.integrate 行抱怨这个错误:'float' object has no attribute 'getitem'.
有人可以告诉我我做错了什么吗?
这似乎是 known bug in scipy.integrate. It seems that additional argument passing is broken in complex_ode
. You could try and see if they have fixed it in a newer release (although this bug report 暗示他们没有),或者在使用 complex_ode
时将自己限制在没有附加参数的情况下使用自己的包装函数。例如,您的示例的 hacky 解决方案可能类似于:
from scipy.integrate import complex_ode
class myfuncs(object):
def __init__(self, f, jac, fargs=[], jacargs=[]):
self._f = f
self._jac = jac
self.fargs=fargs
self.jacargs=jacargs
def f(self, t, y):
return self._f(t, y, *self.fargs)
def jac(self, t, y):
return self._jac(t, y, *self.jacargs)
def f(t, y, arg1):
return [1j*arg1*y[0] + y[1], -arg1*y[1]**2]
def jac(t, y, arg1):
return [[1j*arg1, 1], [0, -arg1*2*y[1]]]
y0, t0 = [1.0j, 2.0], 0
case = myfuncs(f, jac, fargs=[2.], jacargs=[2.])
r = complex_ode(case.f, case.jac)
r.set_initial_value(y0, t0)
t1 = 10
dt = 1
while r.successful() and r.t < t1:
r.integrate(r.t+dt)
print(r.t, r.y)