如何在 numba 中使用 numpy 函数

How to use numpy functions in numba

我必须求逆矩阵。但是我收到一个错误。如何解决?

@njit
def inv():
    x = [[1,2],[9,0]]
    inverted = np.linalg.inv(x)
    return x
inv()

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<function inv at 0x7f1ce02abb90>) found for signature:

inv(list(list(int64)<iv=None>)<iv=None>)

There are 2 candidate implementations:
- Of which 2 did not match due to:
Overload in function 'inv_impl': File: numba/np/linalg.py: Line 833.
With argument(s): '(list(list(int64)<iv=None>)<iv=None>)':
Rejected as the implementation raised a specific error:
TypingError: np.linalg.inv() only supported for array types
raised from /opt/conda/envs/rapids/lib/python3.7/site-packages/numba/np/linalg.py:767

During: resolving callee type: Function(<function inv at 0x7f1ce02abb90>)
During: typing of call at (4)


File "", line 4:
def inv():


x = [[1,2],[9,0]]
inverted = np.linalg.inv(x)

您需要提供 floatcomplex dtype 的 np.array,而不是 int:

list
from numba import jit
import numpy as np  

@jit(nopython=True)
def inv():
    x = np.array([[1,2],[9,0]], dtype=np.float64) # explicitly specify float64 dtype
    # x = np.array([[1.,2.],[9.,0.]]) # or just add floating points
    inverted = np.linalg.inv(x)
    return x
inv()

查看 numba 中的 linear algebra supported numpy features