[SciPy-dev] Trouble with sparse complex matrices

Nils Wagner nwagner at iam.uni-stuttgart.de
Mon Oct 9 06:00:24 CDT 2006


Robert Cimrman wrote:
> Nils Wagner wrote:
>   
>>  Hi all,
>>
>> I have constructed a 2x2 lil_matrix and added some complex values to it.
>> The imaginary is neglected for some reason.
>>
>> from scipy import *
>> A = sparse.lil_matrix((2,2))
>> A[:,1] = random.rand(2)+1j*random.rand(2)
>> A[1,:] = random.rand(2)+1j*random.rand(2)
>> print A.real
>> print A.imag
>> print A
>> A = A.todense()
>> print 'The imiginary part of A is missing'
>> print A
>>
>>     
>
> In [1]:A = sp.lil_matrix((2,2))
> In [2]:A
> Out[2]:
> <2x2 sparse matrix of type '<type 'numpy.float64'>'
>          with 0 stored elements in LInked List format>
>
> In [3]:A[:,1] = rand(2)+1j*rand(2)
> In [4]:A[1,:] = rand(2)+1j*rand(2)
> In [8]:A
> Out[8]:
> <2x2 sparse matrix of type '<type 'numpy.float64'>'
>          with 3 stored elements in LInked List format>
>
> --------------------
> - so here the type of the matrix is still real! I think you have found 
> another bug... In the meantime, you can use the code below:
> --------------------
>
> In [15]:A = sp.lil_matrix((2,2), dtype = nm.complex128)
>
> In [16]:A
> Out[16]:
> <2x2 sparse matrix of type '<type 'numpy.complex128'>'
>          with 0 stored elements in LInked List format>
>
> In [17]:A[:,1] = rand(2)+1j*rand(2)
>
> In [18]:A[1,:] = rand(2)+1j*rand(2)
>
> In [19]:A
> Out[19]:
> <2x2 sparse matrix of type '<type 'numpy.complex128'>'
>          with 3 stored elements in LInked List format>
>
> In [20]:print A
>    (0, 1)        (0.958469152451+0.477931946516j)
>    (1, 0)        (0.945332109928+0.92267626524j)
>    (1, 1)        (0.734181404114+0.921519994736j)
>
> In [21]:A.real
> Out[21]:
> <2x2 sparse matrix of type '<type 'numpy.float64'>'
>          with 3 stored elements (space for 3)
>          in Compressed Sparse Column format>
>
> In [22]:print A.real
>    (1, 0)        0.945332109928
>    (0, 1)        0.958469152451
>    (1, 1)        0.734181404114
>
> In [23]:print A.imag
>    (1, 0)        0.92267626524
>    (0, 1)        0.477931946516
>    (1, 1)        0.921519994736
>
> In [24]:A = A.todense()
>
> In [25]:print A
> [[  0.00000000e+00+0.j           9.58469152e-01+0.47793195j]
>   [  9.45332110e-01+0.92267627j   7.34181404e-01+0.92151999j]]
> _______________________________________________
> Scipy-dev mailing list
> Scipy-dev at scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-dev
>   
Hi Robert,

Thank you very much for your explanation and workaround !

Cheers,
                  Nils



More information about the Scipy-dev mailing list