[SciPy-dev] Possible fix for scipy.sparse.lil_matrix column-slicing problem

Robert Cimrman cimrman3@ntc.zcu...
Mon Nov 30 04:32:08 CST 2009

Hi Tim,

Tim Victor wrote:
> On Thu, Nov 26, 2009 at 10:58 PM, Nathan Bell wrote:
>> On Thu, Nov 26, 2009 at 7:21 PM, Tim Victor wrote:
>>> I didn't know that I could do that! I've just created an account and
>>> added a comment there with a link to this email thread. Thanks for the
>>> reply.
>> Nice work!
>> Your patch has been merged into r6121 [1].  The previous
>> implementation was a bit of a nightmare so I'm glad someone finally
>> took the time to simplify it :)
>> Thanks for your contribution.  I hope it is the first of many!
>> [1] http://projects.scipy.org/scipy/changeset/6121
>> --
>> Nathan Bell wnbell@gmail.com
>> http://www.wnbell.com/
> Thanks for the really quick turnaround and the kind words, Nathan. I
> enjoyed working on it. It was kinda like helping someone else on their
> class project when I was a student. That was always more fun than
> doing my own work.
> I might see what other open tickets there are. It seems like a good
> way to learn something about the different parts of the system.
> Best regards,
> Tim Victor

Is there are reason why you removed the special case of x being a scalar, namely:

-        elif issequence(i) and issequence(j):
-            if np.isscalar(x):
-                for ii, jj in zip(i, j):
-                    self._insertat(ii, jj, x)
-            else:
-                for ii, jj, xx in zip(i, j, x):
-                    self._insertat(ii, jj, xx)

This removal broke a code of mine, which now takes forever, and behaves in a 
different way. Try this:

In [1]: import scipy.sparse as spp
In [2]: a = spp.lil_matrix((1000, 1000))
In [3]: a
<1000x1000 sparse matrix of type '<type 'numpy.float64'>'
         with 0 stored elements in LInked List format>
In [4]: import numpy as np
In [5]: ir = ic = np.arange(1000)
In [6]: a[ir, ic] = 1

The result is a matrix with all the entries set to 1 (= full!), not just the 
diagonal, which was the previous (IMHO good) behaviour. In the real code I do 
not set the diagonal, but some other elements given by two lists ir, ic, but 
the code above shows the symptoms.

I can fix easily my code by not using the LIL matrix:

In [15]: a = spp.coo_matrix((np.ones((ir.shape[0],)), (ir, ic)))
In [16]: a
<1000x1000 sparse matrix of type '<type 'numpy.float64'>'
         with 1000 stored elements in COOrdinate format>

but I wonder, if the above change in behaviour was intended...


More information about the SciPy-Dev mailing list