I want to make RK4 with the numba for speed-up. I'm a beginner using the numba. Why can't the numba understand my code?
The simple code is following
in 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
The main code for running.
import numpy as np
import numba
swing.RK4(swing.fff, 10, np.array([0,1]), 0.1)
The error message following: But I can not understand what isn't correct in this simple code.
---------------------------------------------------------------------------
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
^
Do you find the reason and solution