I am trying to understand why the numpy function dotbehaves like this:
M = np.ones((9, 9))
V1 = np.ones((9,))
V2 = np.ones((9, 5))
V3 = np.ones((2, 9, 5))
V4 = np.ones((3, 2, 9, 5))
Now np.dot(M, V1)and np.dot(M, V2)behave as expected. But for V3and the V4result is unexpectedly I:
>>> np.dot(M, V3).shape
(9, 2, 5)
>>> np.dot(M, V4).shape
(9, 3, 2, 5)
I expected (2, 9, 5)and (3, 2, 9, 5)accordingly. On the other hand, np.matmul
it does what I expect: the matrix is multiplied by the first N - 2 dimensions of the second argument and the result has the same form:
>>> np.matmul(M, V3).shape
(2, 9, 5)
>>> np.matmul(M, V4).shape
(3, 2, 9, 5)
So my question is this: what is the basis for
np.dotbehaving as it is? Does it have a specific purpose, or is it the result of applying some general rule?