[SciPy-user] dimension mismatch error

josef.pktd at gmail.com josef.pktd at gmail.com
Sun Jan 11 07:55:01 EST 2009


On Sun, Jan 11, 2009 at 7:26 AM, David Cournapeau
<david at ar.media.kyoto-u.ac.jp> wrote:
> 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-user at scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>

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



More information about the SciPy-User mailing list