linearly

Lecture 02

Matrices as Transformations

A matrix is a machine that moves every point of space at once. Its columns say everything about it, and some machines destroy information.

47 slides / MIT 18.06, Lectures 1–3 / Strang §1.3–1.4, §8.1

Slide 1 of 47
1 / 47

The idea in one sentence

A matrix is a machine: a vector goes in, a moved vector comes out, and the columns of the matrix tell you everything the machine will ever do.

One multiplication, two readings

Start with the numbers:

A=[123456],x=[78].A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \\ 5 & 6 \end{bmatrix}, \qquad x = \begin{bmatrix} 7 \\ 8 \end{bmatrix}.

There are two ways to compute AxAx, and they teach two different things.

Read AA by columns. Then AxAx is the move from Lecture 1, a linear combination, with the entries of xx as the scalars:

Ax  =  7[135]+8[246]  =  [235383].Ax \;=\; 7 \begin{bmatrix} 1 \\ 3 \\ 5 \end{bmatrix} + 8 \begin{bmatrix} 2 \\ 4 \\ 6 \end{bmatrix} \;=\; \begin{bmatrix} 23 \\ 53 \\ 83 \end{bmatrix}.

Read AA by rows. Then each entry of the answer is a dot product: the first entry is 17+28=231 \cdot 7 + 2 \cdot 8 = 23, and so on down.

×7×812345678=23538312345678=2353837 × column 1 + 8 × column 2row 1 dot x = 1×7 + 2×8 = 23
Fig. 1 

One product, two readings. The orange columns get mixed. The green row gets dotted with xx. Both land on the same blue answer, always.

Both readings matter, for different reasons. The row reading is how you compute by hand. The column reading answers the deeper question: since AxAx is always a mix of the columns, the outputs a matrix can produce are exactly the span of its columns. Reachability, the question Lecture 1 ended on, is a question about columns.

A machine that moves every point at once

Now change the point of view. Fix AA and let xx vary. The matrix becomes a function: feed it any vector, it returns a moved vector. To see what the machine does, you do not need to test every input. Two are enough. Watch e1=(1,0)e_1 = (1, 0) and e2=(0,1)e_2 = (0, 1):

Ae1=column 1,Ae2=column 2.A e_1 = \text{column 1}, \qquad A e_2 = \text{column 2}.

The columns of a matrix are the landing spots of the basis vectors. Every other input follows along, because x=x1e1+x2e2x = x_1 e_1 + x_2 e_2 forces Ax=x1(column 1)+x2(column 2)Ax = x_1(\text{column 1}) + x_2(\text{column 2}). Grid lines stay straight, parallel lines stay parallel, and the origin stays put. That is what the word linear means, drawn.

where it landsA e₁ = (2, 0)A e₂ = (1, 1)unit square
Fig. 2 

The machine AA with columns (2,0)(2, 0) and (1,1)(1, 1). The orange dashed unit square lands on the green parallelogram, and the blue arrows are where the two basis vectors go. Watch the corners: they are the landing spots.

Every 2 by 2 matrix is one of these machines. Rotations, shears, reflections, scalings: each is four numbers, and the four numbers are just the two landing spots written side by side. Try it:

Try it. Four numbers move the whole plane
(a, c)(b, d)

det A = ad − bc = 2.0

The orange arrow is where e₁ lands, the green arrow is where e₂ lands, and the yellow patch is where the unit square lands. Press Collapse and watch the plane flatten to a line: that is a singular matrix.

The machine view is what AI runs on. A language model stores words as vectors, and in word2vec those vectors did combination arithmetic: king minus man plus woman landed near queen. Subtract one vector, add another, and you have walked from one word to another.

manwomankingqueenking − man + womanthe miss− man+ womanwoman − manthe same steproyalfemale
Fig. 3 

Four words placed by hand in two invented dimensions, royal and female, so the arithmetic can be drawn. Start at king, subtract man in orange, add woman in green, and land on the blue dot. Queen sits one short dashed miss away. The dashed arrow from man to woman is the same step, which is the whole reason the walk works. Real embeddings have hundreds of dimensions and never close exactly, and that gap is why the result is always stated as lands near queen.

The operations inside every neural network layer are the two moves of this lecture, a matrix multiplication and an addition, repeated at scale.

The machine that destroys information

Here is a machine with a story. Three cities sit on a ring road, with altitudes x1,x2,x3x_1, x_2, x_3. The machine reports the climbs between them, one number for each stretch of road:

C=[101110011],Cx=[x1x3x2x1x3x2].C = \begin{bmatrix} 1 & 0 & -1 \\ -1 & 1 & 0 \\ 0 & -1 & 1 \end{bmatrix}, \qquad Cx = \begin{bmatrix} x_1 - x_3 \\ x_2 - x_1 \\ x_3 - x_2 \end{bmatrix}.
x₁x₂x₃b₁ = x₁ − x₃b₂ = x₂ − x₁b₃ = x₃ − x₂b₁ + b₂ + b₃ = 0
Fig. 4 

The cyclic difference machine. The blue arrows are the three climbs it reports. Walk the ring and every climb you gain you must give back: the three outputs always cancel.

Now raise all three cities by one meter. The climbs do not change: CC applied to (1,1,1)(1, 1, 1) gives (0,0,0)(0, 0, 0). Two different inputs, one output. The machine cannot see the direction (1,1,1)(1, 1, 1), and what a machine cannot see, no one can recover from its output. CC destroys information, and a machine that destroys information cannot be undone. Matrices like this are called singular.

Singularity also limits the outputs. Add the three rows of CxCx and everything cancels, so any output bb this machine can produce must satisfy b1+b2+b3=0b_1 + b_2 + b_3 = 0. That one equation sorts every right-hand side into one of two endings:

  • b=(1,1,2)b = (1, 1, -2) sums to zero. Solvable, with altitudes (1,2,0)(1, 2, 0), and once you have one answer, adding (1,1,1)(1, 1, 1) gives another. Infinitely many solutions.
  • b=(1,2,3)b = (1, 2, 3) sums to six. No altitudes on earth produce these climbs. No solution.

Walking the ring shows why.

b = (1, 1, −2)b = (1, 2, 3)+1+1−2+1+2+3city 3city 1city 2city 3city 3city 1city 2city 3the climbs cancel: altitudes (1, 2, 0)the walk ends 6 meters too high6 m
Fig. 5 

Walk the ring and add the climbs as you go. On the left they cancel, so you arrive back at the altitude you started from. On the right they add to six, so the walk ends six meters above its own starting point. The pink bar is the contradiction, and no set of altitudes can close it.

A singular machine never gives the clean ending: exactly one answer. The machines that do, for every bb, are called invertible, and telling the two kinds apart quickly is where this course goes next.

python
import numpy as np

A = np.array([[1., 2.], [3., 4.], [5., 6.]])
x = np.array([7., 8.])
print(A @ x)                        # [23. 53. 83.]
print(7 * A[:, 0] + 8 * A[:, 1])    # the same: Ax mixes the columns

C = np.array([[1., 0., -1.], [-1., 1., 0.], [0., -1., 1.]])
print(C @ np.array([1., 1., 1.]))   # [0. 0. 0.]  the invisible direction
print(np.linalg.det(C))             # 0.0  singular

b = np.array([1., 2., 3.])          # climbs that don't cancel
best = np.linalg.pinv(C) @ b        # the best possible attempt
print(C @ best - b)                 # [-2. -2. -2.]  misses: no solution

b = np.array([1., 1., -2.])         # climbs that cancel
print(C @ (np.linalg.pinv(C) @ b))  # [ 1.  1. -2.]  reached exactly

Where this is going

You now know what a matrix does: it mixes its columns, it moves the whole space, and sometimes it flattens the space and cannot be undone. What you do not have yet is a procedure. Given a big system Ax=bAx = b, how do you decide which ending you are in, and find xx fast? The answer is elimination, the algorithm inside every solver, and it is the next lecture.