使用scipy.integrate.complex_ode而不是scipy.integrate.ode [英] Using scipy.integrate.complex_ode instead of scipy.integrate.ode

查看:219
本文介绍了使用scipy.integrate.complex_ode而不是scipy.integrate.ode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在scipy.integrate中使用complex_ode方法而不是ode方法. complex_ode的帮助页面没有提供示例,因此我可能做错了一些事情.

I am trying to use complex_ode method instead of ode method in scipy.integrate. The help page for complex_ode does not provide example, so I may have done something wrong.

此代码可与scipy.integrate.ode一起正常工作:

This code works properly with 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进行相同的操作.

Now this other code tries to do the same with 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'对象没有属性' getitem '.

But the r.integrate line complains with this error: 'float' object has no attribute 'getitem'.

有人可以告诉我我在做什么错吗?

Could someone please tell me what am I doing wrong?

推荐答案

这似乎是 scipy.integrate中的已知错误.似乎其他参数传递在complex_ode中被破坏了.您可以尝试看看他们是否已在较新的版本中对其进行了修复(尽管此错误报告提示他们没有),或者在使用complex_ode时仅将自己限制为自己的包装函数,而无需其他参数.例如,针对您的示例的hacky解决方案可能类似于:

This would appear to be a 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 suggests they haven't), or just restrict yourself to your own wrapper functions without additional arguments when using complex_ode. For example, a hacky solution for your example could be something like:

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)

这篇关于使用scipy.integrate.complex_ode而不是scipy.integrate.ode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆