fX Engineers Explore the community

Mathematics for Engineers

Matrix multiplication, visually

The row-times-column rule is how you compute it. It is not what it means. What it means is: do one transformation, then another.

· 6 min read

What is it?

A matrix is a linear transformation of space written down. Multiplying two matrices is composing two transformations: apply BB, then apply AA.

(AB)x=A(Bx)(AB)\mathbf{x} = A(B\mathbf{x})

That is the definition worth carrying around. The arithmetic rule - take the dot product of each row of AA with each column of BB - is a consequence, not the meaning.

Why does it exist?

Start with what a matrix does to a vector. The key fact: the columns of a matrix are where the basis vectors land.

Take

A=[2103]A = \begin{bmatrix} 2 & 1 \\ 0 & 3 \end{bmatrix}

Column one says ı^=(1,0)\hat{\imath} = (1,0) lands on (2,0)(2,0). Column two says ȷ^=(0,1)\hat{\jmath} = (0,1) lands on (1,3)(1,3). Because the transformation is linear, that is all you need - every other vector goes where its combination of basis vectors goes:

A[xy]=x[20]+y[13]A\begin{bmatrix} x \\ y \end{bmatrix} = x\begin{bmatrix} 2 \\ 0 \end{bmatrix} + y\begin{bmatrix} 1 \\ 3 \end{bmatrix}

Matrix-vector multiplication is a weighted sum of the columns. Once you see it that way, the product ABAB is forced: to find where ABAB sends the basis vectors, take the columns of BB (where BB sends them) and push each one through AA.

import numpy as np

A = np.array([[2, 1], [0, 3]])
B = np.array([[0, -1], [1, 0]])      # rotate 90° counter-clockwise

# columns of AB are A applied to each column of B
print(A @ B)
print(np.column_stack([A @ B[:, 0], A @ B[:, 1]]))   # identical

How does it work?

Three properties stop being facts to memorise and become obvious:

Order matters. AB≠BAAB \neq BA, because “rotate then stretch” is not “stretch then rotate.”

print(A @ B)     # [[ 1 -2]
                 #  [ 3  0]]
print(B @ A)     # [[ 0 -3]
                 #  [ 2  1]]

Dimensions must line up. An m×nm\times n times an n×pn \times p works because the second transformation must output vectors that live in the space the first one takes as input. The inner dimensions match or the composition is meaningless.

The determinant multiplies. det⁡(AB)=det⁡(A)det⁡(B)\det(AB) = \det(A)\det(B), because the determinant is the factor by which area (or volume) is scaled, and doing two transformations scales area twice.

print(np.linalg.det(A @ B), np.linalg.det(A) * np.linalg.det(B))   # 6.0 6.0

A determinant of zero means the transformation squashes space into a lower dimension - and squashing loses information, which is exactly why such a matrix has no inverse.

Where is it actually used?

  • Neural networks. A layer is a matrix multiply plus a non-linearity. Stack two linear layers with nothing between them and you get W2W1W_2W_1 - a single linear transformation. That product identity is the reason activation functions have to exist.
  • Graphics and robotics. Chains of rotations, translations and projections, composed by multiplying matrices in the order the operations happen.
  • Portfolio mathematics. σp2=w⊤Σw\sigma_p^2 = \mathbf{w}^\top \Sigma \mathbf{w} - a quadratic form, which is a transformation applied and then projected back onto the weights.
  • Everything about performance. Matrix multiply is O(n3)O(n^3) naively and is what GPUs are physically built to do. Understanding it as composition is also what tells you when you can reassociate a product to make it cheaper: computing (AB)C(AB)C versus A(BC)A(BC) gives the same answer and can differ by orders of magnitude in cost.

Going deeper

  • Watch 3Blue1Brown episodes 3 and 4 with a pen. They are the best explanation of this that exists.
  • Then write the transformation by hand: for a 2×22\times2 matrix, plot where a grid of points goes. Seeing the grid deform is the moment the row-column rule stops feeling arbitrary.

Sources

  • 3Blue1Brown - Essence of Linear Algebra, episodes 3-4
  • Mathematics for Machine Learning - Deisenroth, Faisal, Ong, ch. 2
Found an error in this?

Use the contact and involvement page to find the current ways to share a correction. Corrections are credited on the page - they are the most welcome messages I get.

More in Mathematics for Engineers

Or see the whole Mathematics for Engineers track.

Previous note

Expected value does not mean what I thought it meant

Not what you should expect, but an average across parallel worlds. If your process is multiplicative, your own path can go broke while the average soars.