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: .
This time you don't know the weights. You only have two timed deliveries:
| Delivery | Distance (km) | Traffic level | Minutes |
|---|---|---|---|
| A | 2 | 0 | 8 |
| B | 5 | 1 | 26 |
Put the features in a matrix, one delivery per row. Then the predictions are a matrix times the weight vector:
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:
Multiply it by a few points and see where they go. For example, . In the same way, , and .
Every point in the plane gets sent somewhere new. So you can think of 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.
Start: the identity, nothing moves 1001
Area of the shaded square: 1
Whatever the matrix, three things stay true:
- grid lines stay straight
- lines that were parallel stay parallel, and evenly spaced
- the origin 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 , 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. , which is the first column of . , the second column.
That's always true. The arrows and 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:
A linear transformation keeps that mix. So lands at 1 × (first column) + 2 × (second column).
Worked example
Where does A send (1, 2)?
Use the columns: .
So there are two ways to read . Row by row, each entry is a dot product. Column by column, is a mix of the columns, with the entries of 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.
A = 2101
det A = 2 × 1 − 1 × 0 = 2
Every area is multiplied by 2.
The matrix that leaves both basis vectors where they are has columns and :
This is the identity matrix, written . It moves nothing: for every . 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 to , with area 1. The matrix turns it into a parallelogram with the two columns as its sides.
For , the parallelogram has a base of 2 (along the first column) and a height of 1. So its area is . 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 .
For a 2×2 matrix there's a short formula:
For our : . That matches the area.
Read it back: only the whole of is the area scale. Don't read and as two separate areas. For the quarter-turn rotation, and . So : 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 A | What happens to the plane |
|---|---|
| bigger than 1 | areas grow |
| exactly 1 | areas stay the same (a shear, a rotation) |
| between 0 and 1 | areas shrink |
| negative | flipped; the size tells you the area scale |
| 0 | squashed flat onto a line or a point |
The Flip tab in the first figure has . 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 . The second multiplies every area by . So together, areas get multiplied by .
With numbers: a shear has and a quarter-turn rotation has . So both orders of doing them have , even though they give different matrices. A stretch with followed by a flip with gives : three times the area, flipped.
This also explains why a squash can't be undone. If , then . Nothing you do afterwards can get back to .
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 is a shear and is a quarter turn:
"Shear, then rotate" means: take , shear it to get , then rotate that to get . Grouping it as gives the same answer. So the single matrix is .
Doing one transformation after another is called composition. Its matrix is the matrix product, read right to left: in , happens first.
Predict first
Is 'shear, then rotate' the same as 'rotate, then shear'?
Press play.
So far: 1001
Worked example
Multiply out both orders
Shear, then rotate: .
Undoing a transformation: the inverse
If a matrix moves the plane, can another matrix move it back? Often, yes. The inverse of , written , is the matrix that undoes :
Read it as: do , then , and nothing has moved. The other order works too: .
For a 2×2 matrix there's a formula. Swap and , flip the signs of and , and divide by the determinant:
Worked example
Undo A = [[2, 1], [0, 1]]
. Swap and negate: . Divide by 2: .
Notice the division by . 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?
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).
The squash matrix is . Its columns and point the same way, so the unit square collapses to a line segment. Its determinant is . It sends to , and to 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 . 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 , the weights and the minutes :
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 .
Now the transformation picture gives the plan. sends the weights to the minutes. To get the weights back, undo :
Worked example
Solve for the delivery weights
. It isn't 0, so has an inverse and there's exactly one answer.
By hand, you'd probably skip the inverse. Delivery A says , so . Then delivery B says , so . Computers use the same idea, called elimination: find one unknown, substitute it, find the next.
Elimination is less work than building 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 with . Delivery B allows any with . 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?
A: 2 w₁ + 0 w₂ = 8 · B: 5 w₁ + 1 w₂ = 26
det = 2 × 1 − 0 × 5 = 2 → one answer: w = (4, 6)
For this matrix, . So the system has one answer exactly when delivery B has some traffic. So a singular 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 and , which means and . They're parallel and apart, so no weights fit both.
Tick "B ran 1 minute late" in the figure to see it.
So when , there's either no answer or infinitely many, never exactly one.
With 100 deliveries, you fit instead of solve
Real deliveries don't follow 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. , so acts first. Read right to left, like .
"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 , the inverse is , not entry-by-entry reciprocals. And 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 and 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.
- means do , then . Order matters.
- The inverse undoes . It exists only when .
- Solving runs the transformation backwards. Elimination does this without building the inverse.
Section 2 shows how to compute these in PyTorch.