[SciPy-User] numpy.diff of sparse matrix

Jonathan Stickel jjstickel@vcn....
Tue Sep 7 13:49:20 CDT 2010

I'd like to use numpy.diff on a sparse matrix, but I get the following:

In [76]: np.diff(np.identity(3),axis=0)
array([[-1.,  1.,  0.],
        [ 0., -1.,  1.]])

In [77]: np.diff(sparse.identity(3),axis=0)
IndexError                                Traceback (most recent call last)

/Users/jstickel/temp/<ipython console> in <module>()

in diff(a, n, axis)
     961     slice1 = [slice(None)]*nd
     962     slice2 = [slice(None)]*nd
--> 963     slice1[axis] = slice(1, None)
     964     slice2[axis] = slice(None, -1)
     965     slice1 = tuple(slice1)

IndexError: list assignment index out of range

I suppose numpy.diff doesn't know how to handle a sparse matrix object. 
  Converting to dense would work, but that would defeat the purpose of 
using sparse matrices.  Any quick solutions?  I don't have time now, but 
if I were to implement a sparse diff, should it go within numpy.diff or 
be a separate function (maybe scipy.sparse.spdiff)?


More information about the SciPy-User mailing list