[Scipy-tickets] [SciPy] #1859: Sparse matrix subtraction with another matrix seems to regard the other matrix as a scalar

SciPy Trac scipy-tickets@scipy....
Fri Mar 1 21:43:44 CST 2013


#1859: Sparse matrix subtraction with another matrix seems to regard the other
matrix as a scalar
------------------------------------+---------------------------------------
 Reporter:  photon.engine           |       Owner:  jakevdp    
     Type:  defect                  |      Status:  new        
 Priority:  normal                  |   Milestone:  Unscheduled
Component:  scipy.sparse            |     Version:  0.10.1     
 Keywords:  sparse scalar subtract  |  
------------------------------------+---------------------------------------

Comment(by warren.weckesser):

 When I use a CSR matrix in numpy.dot, I get the exception "ValueError:
 setting an array element with a sequence" in scipy version 0.11 (and in
 the current development version), but not in version 0.10.1.  So something
 changed since 0.10.1 that causes the exception.

 While the message isn't helpful, the crash is "good", because the computed
 array is not useful.  Notice the types of the results of numpy.dot in your
 output: they are 'object' arrays. The elements of the arrays are
 themselves instances of sparse matrices, when they should be the scalars
 that make up the dot product.  You get the error about scalars because the
 sparse matrices do not allow a nonzero scalar to be added to them--that
 destroys the sparseness, and it is almost always not what you intend to
 do.

 For example:
 {{{
 In [28]: d = dia_matrix([[1,0],[2,3]])

 In [29]: d - 1
 ---------------------------------------------------------------------------
 NotImplementedError                       Traceback (most recent call
 last)
 <ipython-input-29-79e2276bf8fe> in <module>()
 ----> 1 d - 1

 /home/warren/anaconda/lib/python2.7/site-packages/scipy/sparse/base.pyc in
 __sub__(self, other)
     222     def __sub__(self, other):   # self - other
     223         #note: this can't be replaced by self + (-other) for
 unsigned types
 --> 224         return self.tocsr().__sub__(other)
     225
     226     def __rsub__(self, other):  # other - self

 /home/warren/anaconda/lib/python2.7/site-
 packages/scipy/sparse/compressed.pyc in __sub__(self, other)
     195                 return self.copy()
     196             else: # Now we would add this scalar to every element.
 --> 197                 raise NotImplementedError('adding a nonzero scalar
 to a '
     198                                           'sparse matrix is not
 supported')
     199         elif isspmatrix(other):

 NotImplementedError: adding a nonzero scalar to a sparse matrix is not
 supported
 }}}

 In your code, you have an array whose elements are sparse matrices, so
 when you try to subtract rhs from it, you are attempting to subtracts
 elements of rhs from the sparse matrices that were (incorrectly) creating
 by numpy.dot, and you get the 'NotImplementedError' exception.

-- 
Ticket URL: <http://projects.scipy.org/scipy/ticket/1859#comment:3>
SciPy <http://www.scipy.org>
SciPy is open-source software for mathematics, science, and engineering.


More information about the Scipy-tickets mailing list