Sparse Matrix in Numba
If all you have to do is iterate over the values of a CSR matrix, you can pass the attributes data, indptr, and indices to a function instead of the CSR matrix object.
from scipy import sparsefrom numba import njit@njitdef print_csr(A, iA, jA): for row in range(len(iA)-1): for i in range(iA[row], iA[row+1]): print(row, jA[i], A[i])A = sparse.csr_matrix([[1, 2, 0], [0, 0, 3], [4, 0, 5]])print_csr(A.data, A.indptr, A.indices)
You can access the data of your sparse matrix as pure numpy or python. For example
M=sparse.csr_matrix([[1,0,0],[1,0,1],[1,1,1]])ML = M.tolil()for d,r in enumerate(zip(ML.data,ML.rows)) # d,r are lists dr = np.array([d,r]) print dr
produces:
[[1] [0]][[1 1] [0 2]][[1 1 1] [0 1 2]]
Surely numba can handle code that uses these arrays, provided, of course, that it does not expect each row to have the same size of array.
The lil
format stores values 2 object dtype arrays, with data and indices stored lists, by row.