# [SciPy-dev] Feature request : Comparison of sparse matrices is not implemented.

Nils Wagner nwagner at mecha.uni-stuttgart.de
Thu Dec 2 05:55:11 CST 2004

```Robert Kern wrote:

> Nils Wagner wrote:
>
>> Hi all,
>>
>> I tried to visualize the structure of large and sparse matrices using
>>
>> from matplotlib.colors import LinearSegmentedColormap
>> from matplotlib.matlab import *
>> from scipy import *
>> import IPython
>>
>> def spy2(Z):
>>    """
>>    SPY(Z) plots the sparsity pattern of the matrix S as an image
>>    """
>>
>>    #binary colormap min white, max black
>>    cmapdata = {
>>         'red'  :  ((0., 1., 1.), (1., 0., 0.)),
>>         'green':  ((0., 1., 1.), (1., 0., 0.)),
>>         'blue' :  ((0., 1., 1.), (1., 0., 0.))
>>         }
>>    binary =  LinearSegmentedColormap('binary',  cmapdata, 2)
>>
>>    Z = where(Z>0,1.,0.)
>>    imshow(transpose(Z), interpolation='nearest', cmap=binary)
>
>
> Since imshow() doesn't deal with sparse matrices (to my knowledge),
> but only dense matrices, you need to use Z.todense() regardless.
>
This might be a memory problem for such large matrices arising in  my
applications...
Just, to receive an impression the number of rows, cols and entries are
67986, 67986 and 4222171, respectively.

Nils

>   Z = Z.transp().todense() > 0
>   imshow(Z, ...)
>
> If you really need the array to be Float, you can explicitly cast it.
> Otherwise, where(<condition>, 1.0, 0.0) is extraneous.
>
> I'm sure sparse matrix comparisons are already "on the list," as it were.
>

```