Jaakko Luttinen | 17 Feb 12:04 2012

Dot product of a matrix and an array


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. Now numpy.dot seems to
give errors in such cases.

Best regards,