lenskit / csr

Compressed sparse matrices
https://csr.lenskit.org
MIT License
15 stars 2 forks source link

Multiplying a complex matrix with a complex vector raises an error #19

Open christian-cahig opened 2 years ago

christian-cahig commented 2 years ago

Hi, I was playing with CSR as a prospect tool for using sparse matrices in Numba. In my use case, multiplication of a complex matrix and a complex vector is common, e.g.,

import numpy as np, scipy as sp
import csr

# Complex matrix
A_ = sp.sparse.random(5, 5, density=0.1, format='csr') + 1j*sp.sparse.random(5, 5, density=0.1, format='csr')
A = csr.create(A_.shape[0], A_.shape[1], A_.nnz, A_.indptr, A_.indices, A_.data)

# Complex vector
b = np.random.random(5) + 1j*np.random.random(5)

# Expected output is a complex vector
A.mult_vec(b)

However, the last line in the above snippet raises a TypingError, i.e.,

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<built-in function setitem>) found for signature:

 >>> setitem(array(float64, 1d, C), int64, complex128)

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(float64, 1d, C), int64, complex128)':
       No match.

During: typing of setitem at absolute\path\to\my\conda-env\lib\site-packages\csr\kernels\numba\__init__.py (62)

File "relative\path\to\my\conda-env\lib\site-packages\csr\kernels\numba\__init__.py", line 62:
def mult_vec(h: CSR, v):
    <source elided>
        col = h.colinds[i]
        res[row] += v[col] * h._e_value(i)
        ^
mdekstrand commented 2 years ago

Sorry for the delay in getting to this, somehow didn't notice it. Thank you so much for this report. Right now CSR hasn't been tested with complex data types (my use cases are all real-valued) - that sounds like a useful feature to add. I don't expect there would be fundamental problems assuming Numba supports all the required operations (although it may restrict things to the Numba kernel), we just need to add tests & shake out the relevant problems.

christian-cahig commented 2 years ago

@mdekstrand anyway, regarding the use case that prompted my opening of this issue, I managed to reformulate some stuff by breaking up real and imaginary parts and some algebra. It's slightly more complex (ha unexpected pun) but at least it will allow me to use CSR for some Numba acceleration.

mdekstrand commented 2 years ago

@christian-cahig Good news, thanks! If you encounter any other problems let me know :).

I've started adding tests for complex CSR in a branch on my fork (mdekstrand/csr).