[SciPy-User] Dot product of a matrix and an array

Jaakko Luttinen jaakko.luttinen@aalto...
Fri Feb 17 05:00:18 CST 2012


The dot product of a matrix and an array seems to return a matrix.
However, this resulting matrix seems to have inconsistent shape. For
simplicity, let I be an identity matrix (matrix object) and x a vector
(1-d array object). Then np.dot gives wrong dimensions for I*x which
causes that one can not compute I*(I*x).
See the code below:
>>> import numpy as np
>>> x = np.arange(5)
>>> I = np.asmatrix(np.identity(5))
>>> np.dot(I,x)
matrix([[ 0.,  1.,  2.,  3.,  4.]])
>>> np.dot(I, np.dot(I,x))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: matrices are not aligned

I think np.dot(I,x) should return either 1-d vector (array object) or
2-d column vector (array or matrix object), but NOT 2-d row vector
because that, in my opinion, is incorrect interpretation of the dot product.

Also, I think numpy.dot should return an array object when given an
array and a matrix, because the given array might have more than two
dimensions (which is okay by the definition of numpy.dot) so the
resulting object should be able to handle that.

Best regards,

More information about the SciPy-User mailing list