Why python can calculate dot product with different number of rows

53 Views Asked by At

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?

2

There are 2 best solutions below

0
otaithleigh On

y is 1-dimensional, and so NumPy broadcasts the arrays and does whatever is compatible. If you look at y.shape, you'll get a single-element tuple (2,). If you change the definition of y to y = np.array([[5, 6]]), y.shape is instead (1, 2), i.e., 1 row and 2 cols. In that case, the first matrix multiply will not work.

0
slothrop On

The key facts are:

  1. The matrix multiplication A @ B requires the number of columns in A to be the same as the number of rows in B.

  2. 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.