矢量化 SciPy ode 求解器 [英] Vectorized SciPy ode solver

查看:76
本文介绍了矢量化 SciPy ode 求解器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是关于当前的 scipy ode 求解器.来自 scipy 文档页面,它们的用法是:

My question is with respect to the current scipy ode solver. From the scipy doc page, their usage is:

# A problem to integrate and the corresponding jacobian:

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]]]

# The integration:
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("%g %g" % (r.t, r.y))

我的问题是:它使用了很多 python 循环(while 循环),这实质上减慢了程序的运行速度.我可以尝试编写 C 代码并使用 ctypes 使其更快,但我将无法在 scipy 中访问像 dopri5 这样的好算法(除非我自己实现它).

My problem is: it is using a lot of the python loops (the while-loop) which essentially slows the program down. I can try to write a C code and use ctypes to make it faster, but I won't be able to access the nice algorithms like dopri5 in scipy (unless I implement it myself).

是否有任何矢量化编码方式可以加快速度?

Is there any vectorized way of coding to make this faster?

谢谢!

推荐答案

'vectorization' 意味着同时并行执行一堆计算.是的,详细的实现将涉及迭代,但它是在 C 中,顺序对您来说无关紧要,Python 程序员.

'vectorization' means doing a bunch of calculations in parallel, all at once. Yes, the detailed implementation will involve iteration, but it's in C and the order does not matter to you, the Python programmer.

但是像这样的 ode 解决方案本质上是一个串行操作.你必须在 t 时间解决问题,然后才能在 t+dt 时间解决它.您无法随时间矢量化解决方案.您能做的最好的事情是选择一个 ode 求解器,它可以为时间步长 (dt) 做出明智的选择,尽可能大的步长,需要捕捉快速变化时的小步.

But an ode solution like this is essentially a serial operation. You have to solve the problem at time t before you solve it at time t+dt. You can't vectorize the solution through time. The best you can do is choose an ode solver that makes intelligent choices for the time steps (dt), big steps where possible, small ones when needed to capture rapid changes.

一个好的 ode 求解器可以让您向量化空间维度 - 即并行求解 10 个 ode.您也可以对计算雅可比行列式进行矢量化,同时返回 y+dyy-dy.基本上,您希望尽可能快地计算 fjac.

A good ode solver lets you vectorize the spatial dimension - i.e. solving 10 odes in parallel. You might also be able to vectorize calculating the Jacobian, returning both y+dy and y-dy at once. Basically you want to make the calculation of f and jac as fast as possible.

这篇关于矢量化 SciPy ode 求解器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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