gpt4 book ai didi

python - (如何)scipy.integrate.odeint 加速功能评估?

转载 作者:太空宇宙 更新时间:2023-11-04 10:37:34 24 4
gpt4 key购买 nike

通常是纯 python is ~50x slower than native code (C,Fortran)如果它由带有简单算法的紧密循环组成。当您使用 scipy.odeint就像这个 tutorial 中描述的那样你只需像这样编写函数并集成到纯 python 中:

def f(y, t):
Si = y[0]
Zi = y[1]
Ri = y[2]
# the model equations (see Munz et al. 2009)
f0 = P - B*Si*Zi - d*Si
f1 = B*Si*Zi + G*Ri - A*Si*Zi
f2 = d*Si + A*Si*Zi - G*Ri
return [f0, f1, f2]

这个函数必须被评估很多次,所以我预计它会造成巨大的性能瓶颈,考虑到 odeint 集成器本身是用 FORTRAN/ODPACK 制作的

它是否使用某些东西将函数 f(y,t) 从 python 转换为 native 代码?(像 f2py、scipy.weave、cython ...) 据我所知,odeint 不需要任何 C/C++ 或 Fortran 编译器,它不会增加我的 python 脚本的初始化时间,所以可能是 f2py 和 scipy。未使用 weave。

我之所以问这个问题,是因为在我自己的代码中使用与 scipy.integrate.odeint 相同的方法来加速紧密循环可能是个好主意。使用 odeint 比使用 f2py 或 scipy.weave 更方便。

最佳答案

不,它没有,odeint 代码在没有任何优化的情况下调用您的 python 函数。

它将您的函数包装在 ode_function 中(参见 here ),后者又使用 call_python_function 调用您的 python 函数。然后c函数ode_function将被odepack模块使用。

如果热衷于支持 pythonC 代码转换/加速的 ODE/PDE 集成器,请查看 pydelay (link) .实际上使用 weave

关于python - (如何)scipy.integrate.odeint 加速功能评估?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22608223/

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