python打包无法识别numba_python – Numba的jit无法编译具有另一个函数作为输入的函数...

我试图在数字上解决一个允许离散跳跃的ODE.我正在使用Euler方法,并希望Numba的jit可以帮助我加快这个过程(现在脚本需要300秒才能运行,我需要它运行200次).

这是我简化的第一次尝试:

import numpy as np

from numba import jit

dt = 1e-5

T = 1

x0 = 1

noiter = int(T / dt)

res = np.zeros(noiter)

def fdot(x, t):

return -x + t / (x + 1) ** 2

def solve_my_ODE(res, fdot, x0, T, dt):

res[0] = x0

noiter = int(T / dt)

for i in range(noiter - 1):

res[i + 1] = res[i] + dt * fdot(res[i], i * dt)

if res[i + 1] >= 2:

res[i + 1] -= 2

return res

%timeit fdot(x0, T)

%timeit solve_my_ODE(res, fdot, x0, T, dt)

->The slowest run took 8.38 times longer than the fastest. This could mean that an intermediate result is being cached

->1000000 loops, best of 3: 465 ns per loop

->10 loops, best of 3: 122 ms per loop

@jit(nopython=True)

def fdot(x, t):

return -x + t / (x + 1) ** 2

%timeit fdot(x0, T)

%timeit solve_my_ODE(res, fdot, x0, T, dt)

->The slowest run took 106695.67 times longer than the fastest. This could mean that an intermediate result is being cached

->1000000 loops, best of 3: 240 ns per loop

->10 loops, best of 3: 99.3 ms per loop

@jit(nopython=True)

def solve_my_ODE(res, fdot, x0, T, dt):

res[0] = x0

noiter = int(T / dt)

for i in range(noiter - 1):

res[i + 1] = res[i] + dt * fdot(res[i], i * dt)

if res[i + 1] >= 2:

res[i + 1] -= 2

return res

%timeit fdot(x0, T)

%timeit solve_my_ODE(res, fdot, x0, T, dt)

->The slowest run took 10.21 times longer than the fastest. This could mean that an intermediate result is being cached

->1000000 loops, best of 3: 274 ns per loop

->TypingError Traceback (most recent call last)

ipython-input-10-27199e82c72c> in ()

1 get_ipython().magic('timeit fdot(x0, T)')

----> 2 get_ipython().magic('timeit solve_my_ODE(res, fdot, x0, T, dt)')

(...)

TypingError: Failed at nopython (nopython frontend)

Undeclared pyobject(float64, float64)

File "", line 6

我不明白为什么我得到这个错误.我怀疑numba无法识别输入字段fdot(这是一个已经用Numba编译的python函数).

由于我对Numba这么新,我有几个问题

>我能做些什么让Numba了解输入字段fdot是一个函数?

>在函数fdot上使用JIT“仅”导致减少50%.我应该期待更多吗?或者这是正常的吗?

>这个脚本看起来像是一种合理的方法来模拟具有离散跳跃的ODE吗?在数学上,这相当于用delta函数求解ODE.

Numba版本是0.17

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值