【发布时间】:2022-09-23 12:26:28
【问题描述】:
我想用 numba 制作 RK4 以加快速度。 我是使用 numba 的初学者。 为什么 numba 不能理解我的代码?
简单的代码如下
在swing.py中
@numba.jit(nopython=True) def RK4(func, t_end, X0, dt): t = np.arange(0,t_end, dt, dtype=np.float64) X = np.zeros((t.shape[0], X0.shape[0])) X[0] = X0 hdt = dt*.5 for i in range(t.shape[0]-1): t1 = t[i] x1 = X[i] k1 = func(t[i], X[i]) t2 = t[i] + hdt x2 = X[i] + hdt * k1 k2 = func(t2, x2) t3 = t[i] + hdt x3 = X[i] + hdt * k2 k3 = func(t3, x3) t4 = t[i] + dt x4 = X[i] + dt * k3 k4 = func(t4, x4) X[i+1] = X[i] + dt / 6. * (k1 + 2. * k2 + 2. * k3 + k4) return X # dyummy function for test @numba.jit(nopython=True) def fff(t, X): t = 1 X = 3 res = [0] res.append(t*X) return res运行的主要代码。
import numpy as np import numba swing.RK4(swing.fff, 10, np.array([0,1]), 0.1)错误信息如下: 但我无法理解这个简单代码中有什么不正确的地方。
--------------------------------------------------------------------------- TypingError Traceback (most recent call last) Input In [2], in <cell line: 1>() ----> 1 swing.RK4(swing.fff, 10, np.array([0,1]), 0.1) File ~/miniconda3/lib/python3.9/site-packages/numba/core/dispatcher.py:468, in _DispatcherBase._compile_for_args(self, *args, **kws) 464 msg = (f\"{str(e).rstrip()} \\n\\nThis error may have been caused \" 465 f\"by the following argument(s):\\n{args_str}\\n\") 466 e.patch_message(msg) --> 468 error_rewrite(e, \'typing\') 469 except errors.UnsupportedError as e: 470 # Something unsupported is present in the user code, add help info 471 error_rewrite(e, \'unsupported_error\') File ~/miniconda3/lib/python3.9/site-packages/numba/core/dispatcher.py:409, in _DispatcherBase._compile_for_args.<locals>.error_rewrite(e, issue_type) 407 raise e 408 else: --> 409 raise e.with_traceback(None) TypingError: Failed in nopython mode pipeline (step: nopython frontend) No implementation of function Function(<built-in function mul>) found for signature: >>> mul(float64, list(int64)<iv=[0]>) There are 14 candidate implementations: - Of which 12 did not match due to: Overload of function \'mul\': File: <numerous>: Line N/A. With argument(s): \'(float64, list(int64)<iv=None>)\': No match. - Of which 2 did not match due to: Operator Overload in function \'mul\': File: unknown: Line unknown. With argument(s): \'(float64, list(int64)<iv=None>)\': No match for registered cases: * (int64, int64) -> int64 * (int64, uint64) -> int64 * (uint64, int64) -> int64 * (uint64, uint64) -> uint64 * (float32, float32) -> float32 * (float64, float64) -> float64 * (complex64, complex64) -> complex64 * (complex128, complex128) -> complex128 During: typing of intrinsic-call at /disk/disk2/youngjin/workspace/workspace/DS/Inference/MCMC/Swing/swing.py (36) File \"swing.py\", line 36: def RK4(func, t_end, X0, dt): <source elided> t2 = t[i] + hdt x2 = X[i] + hdt * k1 ^找到原因和解决方法了吗
-
您的 fff 函数返回一个列表,并且该列表的大小错误。在 RK4 步骤中,您期望具有与 x 状态向量相同维度的向量算术的类型。因此,即使没有 numba,这些也无法组合在一起。首先尝试在没有 numba 的情况下运行所有内容,错误消息会更加严格。 (请注意,您必须从头到尾阅读错误消息。)
-
谢谢你发现我的愚蠢的东西!我正在用确切的功能测试我的代码!
-
@LutzLehmann 我的原始代码在课堂上存在 JIT 问题。所以,我可以理解为什么不能在带有 numba 的类中使用函数。谢谢你的帮助!! XD
-
另请参阅this previous discussion,了解如何加速 RK4 或其他集成器。
标签: python numba runge-kutta