Nurislom Rakhmatullaev
Nurislom Rakhmatullaev

Reputation: 315

How to use numpy functions in numba

I have to inverse my matrix. But I am getting an error. How to solve it?

@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)

Upvotes: 2

Views: 4285

Answers (1)

yacola
yacola

Reputation: 3013

You need to provide a np.array of float or complex dtype, not some list with int:

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()

see the linear algebra supported numpy features in numba.

Upvotes: 4

Related Questions