Matrix multiplication

5.4. Matrix multiplication#

NumPy provides an operator @ to calculate the dot product. For instance, to calculate the dot product between two vectors:

\[\begin{split} \begin{bmatrix} 1 & 2 & 3 \end{bmatrix} \begin{bmatrix} 2 \\ 4 \\ 6 \end{bmatrix} = (1 * 2) + (2 * 4) + (3 * 6) = 28 \end{split}\]

we would write:

import numpy as np

a = np.array([1.0, 2.0, 3.0])
b = np.array([2.0, 4.0, 6.0])

a @ b
28.0

Similarly, to calculate the dot product between two matrices:

\[\begin{split} \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix} \begin{bmatrix} 1 \\ 2 \\ 3 \end{bmatrix} \end{split}\]

which is:

\[\begin{split} \begin{bmatrix} (1 * 1) + (2 * 2) + (3 * 3)\\ (4 * 1) + (5 * 2) + (6 * 3)\\ (7 * 1) + (8 * 2) + (9 * 3) \end{bmatrix} = \begin{bmatrix} 14 \\ 32 \\ 50 \end{bmatrix} \end{split}\]

we write:

a = np.array(
    [
        [1.0, 2.0, 3.0],
        [4.0, 5.0, 6.0],
        [7.0, 8.0, 9.0],
    ]
)

b = np.array([1.0, 2.0, 3.0])

a @ b
array([14., 32., 50.])