Home / Maths for AI / Symbols / Vectors and matrices

Matrix multiplication

Say it: "A times B"

What it means

Entry (i,j)(i, j) of ABAB is row ii of AA dotted with column jj of BB. An m×nm \times n times an n×pn \times p gives m×pm \times p; the inner sizes must match. In code it is @.

Example

(1234)(56)=(1739)\begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}\begin{pmatrix} 5 \\ 6 \end{pmatrix} = \begin{pmatrix} 17 \\ 39 \end{pmatrix}

1×5 + 2×6 = 17 and 3×5 + 4×6 = 39.

In code

import numpy as np
A = np.array([[1, 2], [3, 4]])
print(A @ np.array([5, 6]))

Output

[17 39]

Where it appears in machine learning

A neural network layer is XW+bXW + b: one matrix multiplication for a whole batch.

Related symbols

Learn it properly in the free lesson Matrices, with practice questions that check your answers.