[Scipy-tickets] [SciPy] #1042: sparse matrix failed with element-wise multiplication using numpy.multiply()

SciPy Trac scipy-tickets@scipy....
Thu Apr 18 14:06:39 CDT 2013


#1042: sparse matrix failed with element-wise multiplication using
numpy.multiply()
--------------------------+-------------------------------------------------
 Reporter:  dingle        |       Owner:  wnbell
     Type:  defect        |      Status:  new   
 Priority:  normal        |   Milestone:        
Component:  scipy.sparse  |     Version:  0.7.0 
 Keywords:  multiply      |  
--------------------------+-------------------------------------------------

Comment(by izzycecil):

 I think they must have meant asp.multiply(bsp).

 I was tinkering around with this bug last night, and found a number of odd
 behaviors.
 With the following environment...
 {{{
 >>> import numpy as np
 >>> from scipy import sparse
 >>> a = np.array([1,2,3])
 >>> b = np.array([1,0,2])
 >>> asp = sparse.lil_matrix(a)
 >>> bsp = sparse.lil_matrix(b)
 >>> c = np.matrix([1,2,3])
 >>> d = np.matrix([1,0,2])
 }}}

 We have this known fail case
 {{{
 >>> np.multiply(asp,bsp)
 Traceback (most recent call last):
   File "<input>", line 1, in <module>
   File "/home/izzy/.virtualenvs/scipy2.7/lib/python2.7/site-
 packages/scipy/sparse/base.py", line 262, in __mul__
     raise ValueError('dimension mismatch')
 ValueError: dimension mismatch
 }}}

 But then, I looked at multiplying a sparse matrix by an array...
 {{{
 >>> np.multiply(asp,b)
 array([ <1x3 sparse matrix of type '<type 'numpy.int64'>'
         with 3 stored elements in LInked List format>,
        <1x3 sparse matrix of type '<type 'numpy.int64'>'
         with 0 stored elements in LInked List format>,
        <1x3 sparse matrix of type '<type 'numpy.int64'>'
         with 3 stored elements in LInked List format>], dtype=object)
 }}}
 Where if a normal matrix was multiplied by an array,
 {{{
 >>> np.multiply(c,a)
 matrix([[1, 4, 9]])
 }}}
 I would think we would want sparse matrices to simply work like a normal
 matrix. Or was this indeed the desired behavior? np.multiply(asp,d) will
 actually return the same result, with type matrix instead of array.

 Then I looked at spmatrix.multiply...
 {{{
 >>> asp.multiply(b)
 Traceback (most recent call last):
   File "<input>", line 1, in <module>
   File "/home/izzy/.virtualenvs/scipy2.7/lib/python2.7/site-
 packages/scipy/sparse/base.py", line 215, in multiply
     return self.tocsr().multiply(other)
   File "/home/izzy/.virtualenvs/scipy2.7/lib/python2.7/site-
 packages/scipy/sparse/compressed.py", line 245, in multiply
     raise ValueError('inconsistent shapes')
 ValueError: inconsistent shapes
 >>> asp.multiply(bsp)
 <1x3 sparse matrix of type '<type 'numpy.int64'>'
         with 2 stored elements in Compressed Sparse Row format>
 >>> asp.multiply(d)
 matrix([[1, 0, 6]])
 }}}
 It's not playing nice with Array's. This is because of the way it looks at
 the shape of "other" --- an easy fix.

 Looking through the code, I have a pretty good understanding of why all of
 these are happening, but I'm confused on what the desired behaviors should
 be. We want spmatrix to essentially act as a matrix, yes? It also seems
 like there should be more separation between point-wise and matrix
 multiplication. Am I being nieve?

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


More information about the Scipy-tickets mailing list