Using scipy.integrate.complex_ode instead of scipy.integrate.ode

后端 未结 1 1904
予麋鹿
予麋鹿 2020-12-19 08:52

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.

相关标签:
1条回答
  • 2020-12-19 09:10

    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)
    
    0 讨论(0)
提交回复
热议问题