[SciPy-dev] Complex sparse matrices

Nils Wagner nwagner@iam.uni-stuttgart...
Fri Feb 15 16:03:40 CST 2008

Hi all,

How do I assign complex values to a sparse lil matrix

The following code doesn't work

from scipy import sparse
from scipy.splinalg import spsolve, use_solver
from numpy import linalg, ones
from numpy.random import rand
A = sparse.lil_matrix((3, 3),complex)
A = A.tocsr()
print A
print A.real()
print A.imag()

The imaginary part is missing

/usr/bin/python -i test_complex.py
   (0, 0)        1.0
   (1, 1)        1.0
   (2, 2)        1.0

and I get

Traceback (most recent call last):
   File "test_complex.py", line 9, in ?
     print A.real()
line 99, in __getattr__
     return _cs_matrix.__getattr__(self, attr)
line 310, in __getattr__
     return self._real()
line 30, in _real
NameError: global name 'numpy' is not defined

I am using
>>> import scipy
>>> scipy.__version__
>>> import numpy
>>> numpy.__version__

More information about the Scipy-dev mailing list