Why we can calculate the matrix and vectors with different number of rows and columns.
import numpy as np
x = np.array([[1,2],[3,4]])
y = np.array([5,6])
print(x@y)
print(y@x)
I couldn't understand why both xy and yx works in this code?
Why we can calculate the matrix and vectors with different number of rows and columns.
import numpy as np
x = np.array([[1,2],[3,4]])
y = np.array([5,6])
print(x@y)
print(y@x)
I couldn't understand why both xy and yx works in this code?
On
The key facts are:
The matrix multiplication A @ B requires the number of columns in A to be the same as the number of rows in B.
numpy matrix multiplication will "promote" a 1-D array to a matrix (documentation). If the 1-D array is the first arg of the multiplication, "it is promoted to a matrix by prepending a 1 to its dimensions": i.e. it becomes a (1,N) matrix. If the 1-D array is the second arg, "it is promoted to a matrix by appending a 1 to its dimensions": i.e. it becomes an (N,1) matrix.
Applying these facts to your examples shows how both of them are valid:
x @ y: y is promoted to (2, 1). x has 2 columns, promoted y has 2 rows, so we're good.
y @ x: y is promoted to (1, 2). Promoted y has 2 columns, x has 2 rows, so again that's fine.
yis 1-dimensional, and so NumPy broadcasts the arrays and does whatever is compatible. If you look aty.shape, you'll get a single-element tuple(2,). If you change the definition ofytoy = np.array([[5, 6]]),y.shapeis instead(1, 2), i.e., 1 row and 2 cols. In that case, the first matrix multiply will not work.