[SciPy-user] dimension mismatch error

0 views
Skip to first unread message

Dinesh B Vadhia

unread,
Jan 11, 2009, 7:15:03 AM1/11/09
to scipy...@scipy.org
I want to do a vector-matrix multiplication as follows:
 
z = y * A
 
... where y is a (1 x J) vector, A is a (I x J) Scipy (csr) Sparse matrix, and the resulting z a (1 x J) vector.
 
The calculation results in this dimension mismatch error:
 
Traceback (most recent call last):
  File " ... .py", line 260, in <module>
    ...
  File "C:\Python25\Lib\site-packages\scipy\sparse\base.py", line 350, in __rmul__
    return (self.transpose() * tr).transpose()
  File "C:\Python25\Lib\site-packages\scipy\sparse\base.py", line 299, in __mul__
    raise ValueError('dimension mismatch')
ValueError: dimension mismatch
 
Any ideas?
 
Dinesh
 
 

David Cournapeau

unread,
Jan 11, 2009, 7:26:27 AM1/11/09
to SciPy Users List
Dinesh B Vadhia wrote:
> I want to do a vector-matrix multiplication as follows:
>
> z = y * A
>
> ... where y is a (1 x J) vector, A is a (I x J) Scipy (csr) Sparse
> matrix, and the resulting z a (1 x J) vector.

Is y dimension (1xJ) a type or the actual dimension ? In that later
case, a ValueError is expected for matrix product :)

cheers,

David
_______________________________________________
SciPy-user mailing list
SciPy...@scipy.org
http://projects.scipy.org/mailman/listinfo/scipy-user

josef...@gmail.com

unread,
Jan 11, 2009, 7:55:01 AM1/11/09
to SciPy Users List

I guess this is the problem
It looks like, which multiplication is used, is defined by the order,
__rmult__, __mult__

>>> A = sparse.lil_matrix([[0.0,0,5,3],]).tocsr()
>>> A
<1x4 sparse matrix of type '<type 'numpy.float64'>'
with 2 stored elements in Compressed Sparse Row format>
>>> np.ones(4)*A


Traceback (most recent call last):

File "<pyshell#126>", line 1, in <module>
np.ones(4)*A
File "\Programs\Python25\Lib\site-packages\scipy\sparse\base.py",


line 350, in __rmul__
return (self.transpose() * tr).transpose()

File "\Programs\Python25\Lib\site-packages\scipy\sparse\base.py",


line 299, in __mul__
raise ValueError('dimension mismatch')
ValueError: dimension mismatch

>>> A*np.ones(4)
array([ 8.])

Josef

Reply all
Reply to author
Forward
0 new messages