Maths toolkit4 / 17

Lesson 4 · Linear algebra

Matrices as transformations: determinants and inverses

The problem

Back at the pizza shop, you predict a delivery's time from two features: distance in km and traffic level. The prediction is a weighted sum: w1×distance+w2×trafficw_1 \times \text{distance} + w_2 \times \text{traffic}.

This time you don't know the weights. You only have two timed deliveries:

DeliveryDistance (km)Traffic levelMinutes
A208
B5126

Put the features in a matrix, one delivery per row. Then the predictions are a matrix times the weight vector:

[2051][w1w2]=[826]\begin{bmatrix} 2 & 0 \\ 5 & 1 \end{bmatrix} \begin{bmatrix} w_1 \\ w_2 \end{bmatrix} = \begin{bmatrix} 8 \\ 26 \end{bmatrix}

In the last chapter you went forwards: weights in, minutes out. Now you have the minutes and want the weights. You need to run the matrix backwards.

To see how, we'll look at a matrix in a new way: as something that moves the plane around.

A matrix moves the plane

Take this matrix:

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

Multiply it by a few points and see where they go. For example, A(1,0)=(2×1+1×0, 0×1+1×0)=(2,0)A(1, 0) = (2 \times 1 + 1 \times 0,\ 0 \times 1 + 1 \times 0) = (2, 0). In the same way, A(0,1)=(1,1)A(0, 1) = (1, 1), A(1,1)=(3,1)A(1, 1) = (3, 1) and A(0,0)=(0,0)A(0, 0) = (0, 0).

Every point in the plane gets sent somewhere new. So you can think of AA as a rule that moves the whole plane at once. Moving the plane like this is called a transformation.

The figure shows what happens to the grid. Press play to slide from "nothing moves" to the full matrix. The A tab is the matrix above. Then try the others.

-4-4-2-22244col 1col 2
Frame 0 of 12

Start: the identity, nothing moves 1001

Area of the shaded square: 1

The grey grid is where points start; the blue grid is where they end up. The shaded square starts as the square from (0, 0) to (1, 1). The F inside it reads backwards when the plane is flipped. Each frame is the blend (1 − t) I + t A, from t = 0 (I moves nothing) to t = 1 (the full matrix).

Whatever the matrix, three things stay true:

  • grid lines stay straight
  • lines that were parallel stay parallel, and evenly spaced
  • the origin (0,0)(0, 0) doesn't move

A transformation with these three properties is a linear transformation. Every matrix gives one. In the other direction, every linear transformation of the plane can be written as a 2×2 matrix.

The in-between frames are only for the animation. The matrix itself jumps straight to the end.

Rotate looks odd halfway

In the Rotate tab, the grid shrinks halfway through and then grows back. That's the blend, not the rotation. Halfway between "nothing" and "a quarter turn", the blend is [0.5−0.50.50.5]\begin{bmatrix} 0.5 & -0.5 \\ 0.5 & 0.5 \end{bmatrix}, which turns by 45° and shrinks. A real rotation would keep every length the same.

The columns tell you everything

Look at the first two points again. A(1,0)=(2,0)A(1, 0) = (2, 0), which is the first column of AA. A(0,1)=(1,1)A(0, 1) = (1, 1), the second column.

That's always true. The arrows (1,0)(1, 0) and (0,1)(0, 1) are called the basis vectors. A matrix's columns are where the basis vectors land.

Why is that enough to know where everything lands? Every vector is a mix of the two basis vectors. For example:

(1,2)=1⋅(1,0)+2⋅(0,1)(1, 2) = 1 \cdot (1, 0) + 2 \cdot (0, 1)

A linear transformation keeps that mix. So (1,2)(1, 2) lands at 1 × (first column) + 2 × (second column).

Worked example

Where does A send (1, 2)?

  1. Use the columns: 1⋅(2,0)+2⋅(1,1)=(2,0)+(2,2)=(4,2)1 \cdot (2, 0) + 2 \cdot (1, 1) = (2, 0) + (2, 2) = (4, 2).

So there are two ways to read AvA\mathbf{v}. Row by row, each entry is a dot product. Column by column, AvA\mathbf{v} is a mix of the columns, with the entries of v\mathbf{v} as the amounts. The column view is the one that explains the picture.

Drag the two arrow tips below. They are the matrix's columns, and the whole grid follows them.

-4-4-2-22244col 1col 2

A = 2101

det A = 2 × 1 − 1 × 0 = 2

Every area is multiplied by 2.

Drag where (1, 0) and (0, 1) land. The matrix's columns are those two points, and they fix the whole grid.

The matrix that leaves both basis vectors where they are has columns (1,0)(1, 0) and (0,1)(0, 1):

I=[1001]I = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}

This is the identity matrix, written II. It moves nothing: Iv=vI\mathbf{v} = \mathbf{v} for every v\mathbf{v}. It plays the same role for matrices as the number 1 does for numbers. Larger identity matrices follow the same pattern, with 1s down the diagonal and 0s everywhere else.

The determinant: how much area changes

Watch the shaded square in the first figure's A tab. It starts as the square from (0,0)(0, 0) to (1,1)(1, 1), with area 1. The matrix turns it into a parallelogram with the two columns as its sides.

For A=[2101]A = \begin{bmatrix} 2 & 1 \\ 0 & 1 \end{bmatrix}, the parallelogram has a base of 2 (along the first column) and a height of 1. So its area is 2×1=22 \times 1 = 2. The matrix doubled the area.

Every square of the grid becomes a copy of that same parallelogram. So every shape's area gets multiplied by the same number, here 2. That number is the determinant, written det⁡A\det A.

For a 2×2 matrix there's a short formula:

det⁡[abcd]=ad−bc\det \begin{bmatrix} a & b \\ c & d \end{bmatrix} = ad - bc

For our AA: 2×1−1×0=22 \times 1 - 1 \times 0 = 2. That matches the area.

Read it back: only the whole of ad−bcad - bc is the area scale. Don't read adad and bcbc as two separate areas. For the quarter-turn rotation, ad=0×0=0ad = 0 \times 0 = 0 and bc=(−1)×1=−1bc = (-1) \times 1 = -1. So det⁡=0−(−1)=1\det = 0 - (-1) = 1: a rotation keeps areas the same.

Predict first

Swap the two columns of A, so the first column is (1, 1) and the second is (2, 0). What is the determinant now?

So the determinant carries two pieces of information:

det AWhat happens to the plane
bigger than 1areas grow
exactly 1areas stay the same (a shear, a rotation)
between 0 and 1areas shrink
negativeflipped; the size tells you the area scale
0squashed flat onto a line or a point

The Flip tab in the first figure has det⁡=−1\det = -1. Notice that the blend passes through a flat frame halfway. To get from unflipped to flipped smoothly, the determinant has to pass through 0.

DeeperWhy det(AB) = det(A) det(B)Area scalings multiply. One picture shows why. · 2 min

Do one matrix, then another. The first multiplies every area by det⁡B\det B. The second multiplies every area by det⁡A\det A. So together, areas get multiplied by det⁡A×det⁡B\det A \times \det B.

With numbers: a shear has det⁡=1\det = 1 and a quarter-turn rotation has det⁡=1\det = 1. So both orders of doing them have det⁡=1\det = 1, even though they give different matrices. A stretch with det⁡=3\det = 3 followed by a flip with det⁡=−1\det = -1 gives det⁡=−3\det = -3: three times the area, flipped.

This also explains why a squash can't be undone. If det⁡A=0\det A = 0, then det⁡(anything×A)=0\det(\text{anything} \times A) = 0. Nothing you do afterwards can get back to det⁡I=1\det I = 1.

One transformation after another

You can do one transformation and then another. The result is a single transformation too, and its matrix is the product of the two.

Say SS is a shear and RR is a quarter turn:

S=[1101],R=[0−110]S = \begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix}, \qquad R = \begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix}

"Shear, then rotate" means: take v\mathbf{v}, shear it to get SvS\mathbf{v}, then rotate that to get R(Sv)R(S\mathbf{v}). Grouping it as (RS)v(RS)\mathbf{v} gives the same answer. So the single matrix is RSRS.

Doing one transformation after another is called composition. Its matrix is the matrix product, read right to left: in RSRS, SS happens first.

Predict first

Is 'shear, then rotate' the same as 'rotate, then shear'?

-2-222col 1col 2
Frame 0 of 16

Press play.

So far: 1001

The first half of the animation does one matrix; the second half does the other on top of it. Compare where the columns end up in each tab.

Worked example

Multiply out both orders

  1. Shear, then rotate: RS=[0⋅1+(−1)⋅00⋅1+(−1)⋅11⋅1+0⋅01⋅1+0⋅1]=[0−111]RS = \begin{bmatrix} 0 \cdot 1 + (-1) \cdot 0 & 0 \cdot 1 + (-1) \cdot 1 \\ 1 \cdot 1 + 0 \cdot 0 & 1 \cdot 1 + 0 \cdot 1 \end{bmatrix} = \begin{bmatrix} 0 & -1 \\ 1 & 1 \end{bmatrix}.

Undoing a transformation: the inverse

If a matrix moves the plane, can another matrix move it back? Often, yes. The inverse of AA, written A−1A^{-1}, is the matrix that undoes AA:

A−1A=IA^{-1} A = I

Read it as: do AA, then A−1A^{-1}, and nothing has moved. The other order works too: AA−1=IA A^{-1} = I.

For a 2×2 matrix there's a formula. Swap aa and dd, flip the signs of bb and cc, and divide by the determinant:

[abcd]−1=1ad−bc[d−b−ca]\begin{bmatrix} a & b \\ c & d \end{bmatrix}^{-1} = \frac{1}{ad - bc} \begin{bmatrix} d & -b \\ -c & a \end{bmatrix}

Worked example

Undo A = [[2, 1], [0, 1]]

  1. det⁡A=2\det A = 2. Swap and negate: [1−102]\begin{bmatrix} 1 & -1 \\ 0 & 2 \end{bmatrix}. Divide by 2: A−1=[0.5−0.501]A^{-1} = \begin{bmatrix} 0.5 & -0.5 \\ 0 & 1 \end{bmatrix}.

Notice the division by det⁡A\det A. When the determinant is 0, the formula breaks. The picture says why.

Predict first

A matrix squashes the whole plane onto a line. Can two different points end up at the same place?

Predict first

Two different points have landed on the same spot. Can any matrix undo the squash?

-4-4-2-22244pq

A = 2101 A⁻¹ = 0.5−0.501

Done so far: nothing

p = (2, 0) is now at (2, 0), q = (0, 1) is now at (0, 1).

Apply A, then press Apply A⁻¹ to send the grid back. In the Squash tab, the points p and q land on the same spot, and there is no inverse.

The squash matrix is [120.51]\begin{bmatrix} 1 & 2 \\ 0.5 & 1 \end{bmatrix}. Its columns (1,0.5)(1, 0.5) and (2,1)(2, 1) point the same way, so the unit square collapses to a line segment. Its determinant is 1×1−2×0.5=01 \times 1 - 2 \times 0.5 = 0. It sends p=(2,0)p = (2, 0) to (2,1)(2, 1), and q=(0,1)q = (0, 1) to (2,1)(2, 1) as well.

A matrix like this is a singular matrix. Its determinant is 0, it squashes space flat, and it has no inverse. A matrix with a non-zero determinant can always be undone.

DeeperRank: how many dimensions surviveSquashing a plane onto a line is not the same as squashing it onto a point. · 2 min

A singular 2×2 matrix can squash the plane in two ways.

The squash matrix above sends everything onto a line. Its two columns point the same way, so only one direction survives. We say it has rank 1.

The all-zeros matrix sends everything to the single point (0,0)(0, 0). No direction survives, so its rank is 0. A matrix that keeps the plane a plane, like every non-singular 2×2 matrix, has rank 2.

So the rank is the number of dimensions left after the matrix acts. It's the number of truly different directions among the columns. Bigger matrices work the same way. A 3×3 matrix can squash space onto a plane (rank 2), a line (rank 1) or a point (rank 0).

The rank matters for the delivery problem below. If your deliveries don't vary in enough different ways, some weights can't be pinned down.

Solving for the weights

Back to the two deliveries. Call the feature matrix XX, the weights w\mathbf{w} and the minutes t\mathbf{t}:

Xw=t,X=[2051],t=[826]X\mathbf{w} = \mathbf{t}, \qquad X = \begin{bmatrix} 2 & 0 \\ 5 & 1 \end{bmatrix}, \quad \mathbf{t} = \begin{bmatrix} 8 \\ 26 \end{bmatrix}

A set of equations like this, where the unknowns are only multiplied by numbers and added, is a linear system. In matrix form it's Xw=tX\mathbf{w} = \mathbf{t}.

Now the transformation picture gives the plan. XX sends the weights to the minutes. To get the weights back, undo XX:

w=X−1t\mathbf{w} = X^{-1} \mathbf{t}

Worked example

Solve for the delivery weights

  1. det⁡X=2×1−0×5=2\det X = 2 \times 1 - 0 \times 5 = 2. It isn't 0, so XX has an inverse and there's exactly one answer.

By hand, you'd probably skip the inverse. Delivery A says 2w1=82w_1 = 8, so w1=4w_1 = 4. Then delivery B says 5×4+w2=265 \times 4 + w_2 = 26, so w2=6w_2 = 6. Computers use the same idea, called elimination: find one unknown, substitute it, find the next.

Elimination is less work than building X−1X^{-1} and then multiplying by it.

There's a second picture of the same system. Each delivery on its own allows a whole line of possible weights. Delivery A allows any (w1,w2)(w_1, w_2) with 2w1=82w_1 = 8. Delivery B allows any with 5w1+w2=265w_1 + w_2 = 26. The answer is where the two lines cross.

Predict first

Suppose delivery B had been 4 km with no traffic (level 0), taking 16 minutes. How many weight vectors fit both deliveries?

02468100246810w₁: minutes per kmw₂: minutes per traffic levelAB(4, 6)

A: 2 w₁ + 0 w₂ = 8 · B: 5 w₁ + 1 w₂ = 26

det = 2 × 1 − 0 × 5 = 2 → one answer: w = (4, 6)

Each line holds every weight vector that fits one delivery. One crossing means one answer. Set B's traffic to 0 and the lines become one. Then tick 'B ran 1 minute late'.

For this matrix, det⁡X=2×(B’s traffic)\det X = 2 \times \text{(B's traffic)}. So the system has one answer exactly when delivery B has some traffic. So a singular XX means your data can't separate the two features.

A singular system can also have no answer. Real times are a little noisy. Suppose that 4 km, no-traffic delivery took 17 minutes instead of 16. Then the lines are 2w1=82w_1 = 8 and 4w1=174w_1 = 17, which means w1=4w_1 = 4 and w1=4.25w_1 = 4.25. They're parallel and apart, so no weights fit both.

Tick "B ran 1 minute late" in the figure to see it.

So when det⁡X=0\det X = 0, there's either no answer or infinitely many, never exactly one.

With 100 deliveries, you fit instead of solve

Real deliveries don't follow (4,6)(4, 6) exactly. Traffic lights and slow customers add noise. With 100 deliveries you'd have 100 lines that don't all cross at one point, and no weights fit every one. Instead, you look for the weights that come closest overall. That's linear regression, which you'll solve with gradient descent in the ML fundamentals course.

Common misconceptions

"AB means do A first." No. ABv=A(Bv)AB\mathbf{v} = A(B\mathbf{v}), so BB acts first. Read right to left, like f(g(x))f(g(x)).

"A determinant of 0 means the matrix is all zeros." No. The squash matrix has no zero entries, but its columns point the same way. A determinant of 0 means the columns don't cover the plane.

"The inverse means 1 over each entry." No. For A=[2101]A = \begin{bmatrix} 2 & 1 \\ 0 & 1 \end{bmatrix}, the inverse is [0.5−0.501]\begin{bmatrix} 0.5 & -0.5 \\ 0 & 1 \end{bmatrix}, not entry-by-entry reciprocals. And 1/01/0 wouldn't make sense anyway.

"A negative determinant means the area is negative." Area is never negative. The size of the determinant is the area scale, and the sign only tells you whether the plane was flipped.

Recap

  • A matrix is a linear transformation. It moves the plane, keeping grid lines straight, parallel and evenly spaced.
  • Its columns are where the basis vectors (1,0)(1, 0) and (0,1)(0, 1) land, and they fix everything else.
  • The determinant is the area scale factor. A negative one means a flip, and 0 means the plane is squashed flat.
  • ABAB means do BB, then AA. Order matters.
  • The inverse A−1A^{-1} undoes AA. It exists only when det⁡A≠0\det A \neq 0.
  • Solving Xw=tX\mathbf{w} = \mathbf{t} runs the transformation backwards. Elimination does this without building the inverse.

Section 2 shows how to compute these in PyTorch.