gpt4 book ai didi

python - 将 numba.jit 与 scipy.integrate.ode 结合使用

转载 作者:太空狗 更新时间:2023-10-30 00:13:56 25 4
gpt4 key购买 nike

使用 numba.jitscipy.integrate 加速 odeint 的右侧计算工作正常:

from scipy.integrate import ode, odeint
from numba import jit

@jit
def rhs(t, X):
return 1

X = odeint(rhs, 0, np.linspace(0, 1, 11))

然而,像这样使用 integrate.ode:

solver = ode(rhs)
solver.set_initial_value(0, 0)
while solver.successful() and solver.t < 1:
solver.integrate(solver.t + 0.1)

使用装饰器 @jit 产生以下错误:

capi_return is NULL
Call-back cb_f_in_dvode__user__routines failed.
Traceback (most recent call last):
File "sandbox/numba_cubic.py", line 15, in <module>
solver.integrate(solver.t + 0.1)
File "/home/pgermann/Software/anaconda3/lib/python3.4/site-packages/scipy/integrate/_ode.py", line 393, in integrate
self.f_params, self.jac_params)
File "/home/pgermann/Software/anaconda3/lib/python3.4/site-packages/scipy/integrate/_ode.py", line 848, in run
y1, t, istate = self.runner(*args)
TypeError: not enough arguments: expected 2, got 1

有什么办法可以克服这个问题吗?

最佳答案

您可以使用包装函数,但我认为它不会提高小型 rhs 函数的性能。

@jit(nopython=True)
def rhs(t, X):
return 1

def wrapper(t, X):
return rhs(t, X)

solver = ode(wrapper)
solver.set_initial_value(0, 0)
while solver.successful() and solver.t < 1:
solver.integrate(solver.t + 0.1)

关于python - 将 numba.jit 与 scipy.integrate.ode 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32744658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com