Search code examples
numpymatrixmultidimensional-arraymatrix-multiplicationnumpy-ndarray

understand np.matmul on 1D vectors


a = [1, 2, 3]
b = [10, 10, 10]

np.matmul(a, b) gives 60 as the result.

How does numpy multiply (3,) and (3,) dimension and returns the dot product not outer product (3 * 3) or throw an error "dimension not matching"?


Solution

  • This is directly from the docs of numpy.matmul():

    • If the first argument is 1-D, it is promoted to a matrix by prepending a 1 to its dimensions. After matrix multiplication the prepended 1 is removed.
    • 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.

    Thus, the shape of the inputs a and b are converted to (1, 3) and (3,1) respectively, during the matrix multiplication.

    By the rules of matrix multiplication, we know that:

    1 x 33 x 1
       |    |
        --------   ===>   summed over.

    Thus, we get the result as a scalar.