Matrix multiplication

Multiplying matrices is doing one transformation after another — which is why the order matters.

What you'll learn
  • Know when two matrices can be multiplied, and the shape of the result
  • See a product AB as "apply B, then A"
  • Tell a pure rotation from one that also stretches

When can you multiply?

An \(m \times k\) matrix times a \(k \times n\) matrix gives an \(m \times n\) matrix. The inner sizes must match; the outer sizes are the result. Each entry of the product is a dot product of a row of the left matrix with a column of the right one:

\[(AB)_{ij} = \sum_{k} A_{ik} B_{kj}\]

The notebook also builds the product a different way — as a sum of layers, each one an outer product of a column of \(A\) with a row of \(B\):

A = np.random.randn(4, 6)
B = np.random.randn(6, 4)

C1 = np.zeros((4, 4))
for i in range(6):
    C1 += np.outer(A[:, i], B[i, :])   # one rank-1 layer at a time

C2 = A @ B                             # identical

Both views compute the same numbers. The layer view is the one that pays off later, in the SVD.

A product is a composition

From the first lesson, a matrix is a transformation. So what is a product of two matrices? It is one transformation followed by another. In \(AB\mathbf{x}\), the vector meets \(B\) first, then \(A\) — read right to left.

Space is transformed by the first matrix, then the second. Switch between AB and BA: with a rotation and a shear the grid ends up in two different places, which is exactly why matrix multiplication is not commutative.

That picture explains the rule you are told to memorise: \(AB \neq BA\) in general. Rotating then shearing is not the same as shearing then rotating. The pairs that do commute are the exception — two rotations about the origin, for instance: set A to rotate 90° and B to rotate 45° and both orders land in the same place.

The same reading explains the order of operations the notebook checks:

\[(LIVE)^{\mathsf{T}} = E^{\mathsf{T}} V^{\mathsf{T}} I^{\mathsf{T}} L^{\mathsf{T}}\]

Undoing or transposing a chain reverses its order — taking off your shoes after your socks.

Rotation, pure and impure

A pure 2D rotation by \(\theta\) is

\[R(\theta) = \begin{bmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{bmatrix}\]

It turns every vector without changing its length. The notebook’s challenge compares it with an “impure” rotation that has a 2*cos(th) in the corner — a small change that quietly adds stretching:

v = np.array([2, 0])
for th in np.linspace(0, 2*np.pi, 100):
    A1 = np.array([[2*np.cos(th), -np.sin(th)],    # impure
                   [  np.sin(th),  np.cos(th)]])
    A2 = np.array([[  np.cos(th), -np.sin(th)],    # pure
                   [  np.sin(th),  np.cos(th)]])
    mag_impure = np.linalg.norm(A1 @ v)
    mag_pure   = np.linalg.norm(A2 @ v)
The pure rotation keeps |Av| at exactly 2 for every angle. The impure one swings between 2 and 4: it rotates and stretches at once.

A transformation that preserves length is called orthogonal, and for those the inverse is just the transpose — a fact the SVD lesson leans on heavily.

Matrix times vector

Multiplying a matrix by a vector is the special case where the second matrix has one column. Pre- and post-multiplying by a vector give different shapes: \(A\mathbf{w}\) is a column, \(\mathbf{w}^{\mathsf{T}}A\) is a row — and for a symmetric matrix (\(A = A^{\mathsf{T}}\)) the two contain the same numbers.

Try it yourself. In the animation, confirm the two rotations commute, then explain geometrically why a rotation and a stretch cannot. In the notebook, confirm A @ B and B @ A differ for random 3×3 matrices, and check that \((AB)^{\mathsf{T}} = B^{\mathsf{T}}A^{\mathsf{T}}\) holds to within floating-point error using np.allclose.

View source on GitHub →