Writing

Interactive essay

· 8 min read

What Does a Matrix Actually Do?

Four numbers are enough to rotate, stretch, shear, flip or flatten a whole plane.

A grid turned about its origin, with the point (1, 1) carried to (0, 1.42).
Plate 4 A 45° rotation moves the point (1, 1) to about (0, 1.42), straight above the origin.

These are my visual notes from studying linear algebra. I built this playground because matrices didn't make sense to me until I could drag them around.

Drag the amber and ink handles, or click a preset.

Nothing moved. This is the identity matrix, which leaves every point where it is.

A =
Fig. 1Grid transformer

The whole grid moved at once, and the only thing changing was the four numbers in the box under it. A matrix is a function: put a point in and you get a new point out.

In my linear algebra class, I memorized how to multiply rows by columns long before I knew what the answer meant. So this essay starts with what you can see, and the arithmetic comes after.

The columns are where the arrows land

I used to see a matrix as a grid of numbers, something you plug values into to crank out an answer. The amber arrow in the grid above is what changed that for me.

Before any transformation, the amber arrow sits at [1, 0], pointing straight right. It's called the first basis vector. Now compare it with the first column of the matrix: the numbers match. The first column answers one question, which is where the amber arrow ended up.

The ink arrow starts at [0, 1], and the second column tells you where it ended up.

So you don't have to take in the whole matrix at once. To build one, answer two questions: where does [1, 0] go, and where does [0, 1] go? Write the two answers as columns and that's your matrix.

Drag the amber arrow in the grid below and the first column updates as you move it. The column holds the arrow's x and y coordinates, top and bottom, so the two always move together.

Once this clicked, the rest of linear algebra got easier for me. When I look at a matrix now, I read it as the places where [1, 0] and [0, 1] land, and that's enough to tell me where the whole grid goes.

A =
Fig. 2Column mapping

Put the amber arrow at [0, 1] and the ink arrow at [-1, 0]. The grid turns a quarter turn counterclockwise, and the matrix now matches the Rotate 90° preset from the grid at the top.

Try this

Find a matrix that rotates the grid exactly 180°. Hint: after a half turn, which way does each arrow point?

How every point moves

Knowing where those two arrows land is enough to work out where every other point goes.

Take the point [3, 2]. It's 3 copies of [1, 0] plus 2 copies of [0, 1]. After the transformation, [1, 0] became column 1 and [0, 1] became column 2. So [3, 2] becomes 3 times column 1 plus 2 times column 2.

[abcd][xy]=x[ac]+y[bd]=[ax+bycx+dy]\begin{bmatrix} a & b \\ c & d \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} = x \begin{bmatrix} a \\ c \end{bmatrix} + y \begin{bmatrix} b \\ d \end{bmatrix} = \begin{bmatrix} ax + by \\ cx + dy \end{bmatrix}

Here it is with real numbers. A 45° rotation sends the amber arrow to roughly [0.71, 0.71] and the ink arrow to roughly [-0.71, 0.71]. To set it up, click Rotate 90° in the grid at the top and drag each arrow halfway back. Now take the point (1, 1):

1×[0.710.71]+1×[−0.710.71]=[01.42]1 \times \begin{bmatrix} 0.71 \\ 0.71 \end{bmatrix} + 1 \times \begin{bmatrix} -0.71 \\ 0.71 \end{bmatrix} = \begin{bmatrix} 0 \\ 1.42 \end{bmatrix}

The point (1, 1) ends up straight above the origin, at about (0, 1.42). Click (1, 1) on the grid below to check.

Click any point on the grid to see this calculation with your current matrix.

A =
Fig. 3Point transformer

Whichever point you click, the recipe is the same: its x times column 1 plus its y times column 2. That's the rows-times-columns rule from class, read one column at a time.

Rotation, scaling and shearing

Rotation, scaling and shearing are each a particular choice of four numbers. What differs is which entries change, and how.

Rotation uses cos and sin to keep the arrows the same length while spinning them. Scaling puts numbers on the diagonal to stretch each axis on its own. Shearing nudges one off-diagonal entry, which tilts the grid without changing the axis it acts along.

Move the sliders below and watch which entries change. On the Rotation tab all four change together. On Scaling only the diagonal moves, and on Shearing only the top-right entry does.

det = 1

θ = 0°

R(0°)=[1.000.000.001.00]R(0°) = \begin{bmatrix} 1.00 & 0.00 \\ 0.00 & 1.00 \end{bmatrix}
Fig. 4Rotation, scaling and shearing

The determinant tracks area

Keep an eye on the det readout above the grid while you move the sliders. When you rotate, it stays at 1: the grid spins, but every shape keeps its area. Shearing also keeps det at 1, whatever k is. The grid looks very different, but the area of any region hasn't changed. Scaling is where the determinant moves. Scale x by 2 and y by 3 and det = 6, because the grid now covers 6 times its original area.

When the determinant hits zero, the grid collapses to a line. The matrix has squashed a 2D plane into 1D, and points that used to be different now land on the same spot. There's no way to tell them apart again, so there's no way to undo it. That's why a matrix with det = 0 has no inverse.

When the determinant goes negative, the grid flips over the way a mirror flips your reflection, and clockwise becomes counterclockwise. Set s_y to -1 on the Scaling tab to see it.

Try this

Find a matrix with det = 0 where neither column is the zero vector. What does the grid look like? Hint: what happens when both columns point the same way?

Where these matrices show up

Arthur Cayley formalized matrix algebra in 1858. The 2×2 grid you've been dragging is a small version of something I run into in three places: CSS, neural networks and 3D graphics.

CSS transforms

When a page element rotates or skews, your browser is doing a matrix multiply. The CSS transform property accepts matrix(a, b, c, d, tx, ty). The first four arguments are a 2×2 matrix written column by column, so a and b are where the amber arrow lands. The last two slide the element (translation), which a 2×2 matrix can't do, so under the hood CSS uses a 3×3 matrix. One difference: the CSS y axis points down, so a matrix that turns the grid counterclockwise turns the card clockwise.

Change the values below and the card moves. They're laid out like the matrices in the grids above, and the browser does the drawing.

<div>

CSS transform: matrix()

transform: matrix(1, 0, 0, 1, 0, 0)

Fig. 5CSS transform: matrix()

Neural networks

A neural network layer takes an input vector, multiplies it by a weight matrix, adds a bias and applies a nonlinear activation function like ReLU. That first step is the transformation you've been watching: the grid warps and every data point moves to a new position.

Training a network means searching for the matrix entries that send inputs to the right outputs. The 2×2 grid above is the multiply step of a layer with 2 inputs and 2 outputs. Add a bias and a ReLU, which zeroes out negative values after the multiply, and you'd have the whole layer.

Large language models stack many of these layers, and their matrices have thousands of rows and columns instead of two. Most of the work in each layer is still multiplying by a matrix.

Computer graphics

A 3D game moves every object through a chain of matrices each frame: model space to world space to camera space to your screen. Each link in that chain is a matrix multiply. A single character model might go through 5 to 10 matrix transformations before a single pixel gets drawn.

Graphics cards were built to do this fast: millions of vertices, 60 times a second, each one multiplied by a matrix.

An open playground

The det readout above the grid shows how much area grows or shrinks, goes negative when you flip the plane and hits zero when you collapse it to a line. The presets here add Reflect X and Rotate 45°.

det = 1

Nothing moved. This is the identity matrix, which leaves every point where it is.

A =
Fig. 6Matrix laboratory

Try this

Try to make the grid look like a letter. Which mix of shear, scale and rotation gets you closest?

The same idea works in 3D, and there's one more grid below.

A taste of 3D

In 3D the matrix is 3×3, and its three columns are where the three basis vectors land. Each outlined parallelogram is the plane spanned by one pair of them. Drag the dot at the end of a column to move it, and drag anywhere else to orbit the view.

Fig. 73D grid transformer

Click Flatten Y and det drops to 0. Every point's y coordinate goes to zero and the whole space squashes onto the xz plane, the 3D version of the grid collapsing to a line.

I leaned on the det readout all through this essay without saying where its number comes from. The next essay does that: The Determinant: How Much of the Plane Survived?