Assume we want to compute the dot product of a matrix and a column vector:
So in Numpy/Python here we go:
a=numpy.asarray([[1,2,3], [4,5,6], [7,8,9]])
b=numpy.asarray([[2],[1],[3]])
a.dot(b)
Results in:
array([[13], [31], [49]])
So far, so good, however why is this also working?
b=numpy.asarray([2,1,3])
a.dot(b)
Results in:
array([13, 31, 49])
I would expect that [2,1,3] is a row vector (which requires a transpose to apply the dot product), but Numpy seems to see arrays by default as column vectors (in case of matrix multiplication)?
How does this work?
EDIT:
And why is:
b=numpy.asarray([2,1,3])
b.transpose()==b
So the matrix dot vector array does work (so then it sees it as a column vector), however other operations (transpose) does not work. This is not really consistent design isn't it?
array([2, 1, 3])
isn't a row vector or a column vector. It's just a vector. – user2357112 supports Monica[n x 1]
or[1 x n]
object. But as I see it, the point is exactly that a 1dndarray
has a single dimension, so I'd say that it's not a vector, but an array. (And sure, there are special nd arrays which can be thought of as vectors or matrices, namely withn==2
:) – Andras Deak