hanqi
July 1, 2019, 9:32am
#1
a = np.array([[1,1],
[2,2],
[3,3]])
b = np.array([10,20])
a.shape
b.shape
[email protected]
Output:
(3, 2)
(2,)
array([30, 60, 90])
Something must be happening to b
in the backend? I expected @ to require 2D matrices for both operands
Bruno
July 1, 2019, 12:41pm
#2
For context, and because I myself didn’t know about the existence of the @ notation:
This was introduced in PEP 465 as a way to distinguish between regular multiplication and matrix multiplication, when there is ambiguity.
NumPy followed this convention and implemented it in the numpy.matmul
function . So [email protected]
is just numpy.matmul(a,b)
.
And straight from the documentation we read the following:
If the second argument is 1-D, it is promoted to a matrix by appending a 1 to its dimensions. After matrix multiplication the appended 1 is removed.
Which explains why b
doesn’t have to be a 2D-array and supports that something is indeed happening under the hood.
2 Likes