Home > other >  Why is numba library in python not recognizing numpy 2D array
Why is numba library in python not recognizing numpy 2D array

Time:01-25

I just started learning about numba and have an exercise here to determine the time taken to solve a simple matrix problem. My goal is to achieve parallel execution of this program using python numba library The program has a function create_matrix(row: int, col: int) that takes two inputs and creates a 2D matrix. I then create two matrices, find sum of their primary diagonals and calculate their totals. The problem is numba seem not to understand and the numpy 2D array. Any help will be highly appreciated. Thanks

#imports
from numba import njit
import numpy as np

#create a 2D matrix by consecutive numbers - starting 1
def create_matrix(row, col):
    arr = np.array([[j   (col * i) for j in range(1, col   1)] for i in range(row)])

    return np.matrix(arr)

# calculate the sum of primary diagonals of matrix1
jitted_function = njit()(create_matrix)
m1 = jitted_function(1, 1)
print(f"Matrix 1 : {m1}")
print(f"Matrix 1 diagonal: {np.diagonal(m1)}")
print(f"Matrix 1 sum of primary diagonal is : {np.trace(m1)}")
mat1_sum = np.trace(m1, dtype='i')


# calculate the sum of primary diagonals of matrix2
m2 = create_matrix(4, 4)
print(f"Matrix 2 : {m2}")
print(f"Matrix 2 diagonal : {np.diagonal(m2)}")
print(f"Matrix 2 Sum of diagonal is : {np.trace(m2)}")
mat2_sum = np.trace(m2, dtype='i')

sum_of_two_diagonals = mat1_sum   mat2_sum
print(f"THE SUM IS :  {sum_of_two_diagonals}")

The error is

Traceback (most recent call last):
  File "E:\Users\SoniTech\PycharmProjects\computer_hardware\practise.py", line 21, in <module>
    m1 = jitted_function(1, 1)
  File "E:\Users\SoniTech\PycharmProjects\computer_hardware\venv\lib\site-packages\numba\core\dispatcher.py", line 468, in _compile_for_args
    error_rewrite(e, 'typing')
  File "E:\Users\SoniTech\PycharmProjects\computer_hardware\venv\lib\site-packages\numba\core\dispatcher.py", line 409, in error_rewrite
    raise e.with_traceback(None)
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<built-in function setitem>) found for signature:
 
 >>> setitem(array(undefined, 1d, C), int64, array(int64, 1d, C))
 
There are 16 candidate implementations:
   - Of which 16 did not match due to:
   Overload of function 'setitem': File: <numerous>: Line N/A.
     With argument(s): '(array(undefined, 1d, C), int64, array(int64, 1d, C))':
    No match.

During: typing of setitem at E:\Users\SoniTech\PycharmProjects\computer_hardware\practise.py (10)

File "practise.py", line 10:
def create_matrix(row, col):
    <source elided>
    """
    arr = np.array([[j   (col * i) for j in range(1, col   1)] for i in range(row)])  # create a matrix starting 1
    ^

CodePudding user response:

Some of your functions such as np.matrix() are not supported in njit mode. You can instead rewrite the create_matrix() function such that numba can delegate to its own functions.

#imports
from numba import njit
import numpy as np

#create a 2D matrix by consecutive numbers - starting 1
def create_matrix(row, col):
    arr = np.zeros((row, col))
    for i in range(row):
        for j in range(1, col   1):
            arr[i,j-1] = j   (col * i)
    return arr

# calculate the sum of primary diagonals of matrix1
jitted_function = njit()(create_matrix)
  •  Tags:  
  • Related