[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.setdiag(ones(3)+1j*ones(3))
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()
   File 
"/usr/lib/python2.4/site-packages/scipy/sparse/csr.py", 
line 99, in __getattr__
     return _cs_matrix.__getattr__(self, attr)
   File 
"/usr/lib/python2.4/site-packages/scipy/sparse/base.py", 
line 310, in __getattr__
     return self._real()
   File 
"/usr/lib/python2.4/site-packages/scipy/sparse/data.py", 
line 30, in _real
     return 
self._with_data(numpy.real(self.data),copy=False)
NameError: global name 'numpy' is not defined

I am using
>>> import scipy
>>> scipy.__version__
'0.7.0.dev3942'
>>> import numpy
>>> numpy.__version__
'1.0.5.dev4807'


More information about the Scipy-dev mailing list