← Safis

Math

How a Matrix Transforms Space

How four numbers can stretch, turn, shear, or flip every point of a plane at once, and what the determinant measures.

  • A 2×2 matrix is four numbers that move every point of the plane at once, keeping lines straight, parallel, and evenly spaced.
  • Its two columns are simply where the two unit arrows land. Everything else follows from those two arrows.
  • The determinant is the factor every area gets multiplied by; when it's negative the plane is flipped, and when it's zero the matrix can't be undone.
  • Eigenvectors are the directions a matrix only stretches. In their own axes, many matrices are nothing but a stretch.
  • Any matrix is a turn, a stretch along two perpendicular directions, and another turn. That's what graphics, data analysis, and compression put to work.

Turn a photo on your phone, swing the camera around in a video game, or squeeze a picture to fit a frame, and somewhere a computer is moving every point of an image with a small square of numbers called a matrix. For a flat picture the matrix has just four numbers, in two rows of two. Those four numbers decide where every point of the plane goes, all at once.

Here is a square grid with a letter F drawn on it, and in the corner the matrix that's about to act on it. Drag the slider to apply the matrix, and watch what happens to the grid:

Every point moved, but the grid survived. Lines that were straight are still straight, lines that were parallel are still parallel, and they're still evenly spaced, even though the squares between them have become slanted parallelograms. The point where the two dark axes cross, the origin, doesn't move at all. Try the other matrices under the figure: one shears, one squashes, one turns the F a long way round, and one flips it over. All of them keep these rules.

A change of the plane that keeps the rules is called a linear transformation.“Linear” because straight lines stay straight. The precise version: moving the sum of two arrows gives the sum of the moved arrows, and moving a stretched arrow gives the stretched moved arrow. Every 2×2 matrix makes one, and every linear transformation of the plane can be written as a 2×2 matrix. I've had each point slide along a straight line from its start to its end so you can follow it; the matrix itself only says where each point ends up.

Where the arrows land

A whole grid is a lot to keep track of, but two arrows are enough. Take the arrow one step to the right, from the origin to (1, 0), and the arrow one step up, to (0, 1). They're usually called î and ĵ, said “i-hat” and “j-hat”, and every line of the grid is made of copies of one of them laid end to end. Drag the tips of the two arrows and watch the rest of the plane follow:

The grid is built from the two arrows, so it has no choice but to follow them. Lines that ran across now run along the orange arrow, and lines that ran up now run along the blue one. Once you know where the two arrows land, you know where everything lands.

That's what the matrix records. Its first column is where î lands and its second column is where ĵ lands. At the start the orange tip sits at (, ) and the blue tip at (, ). Drag any of those four numbers sideways and the matching tip moves. Written the usual way, with the columns colored like the arrows:

A = \begin{pmatrix} \orange{a} & \blue{b} \\ \orange{c} & \blue{d} \end{pmatrix} = \begin{pmatrix} \orange{1.1} & \blue{-0.5} \\ \orange{0.4} & \blue{0.9} \end{pmatrix}

Following one point

The columns are enough to follow any single point. The point (2, 1) is two steps along î and one step along ĵ. The matrix moves both arrows, but the recipe stays the same: after the move, the point is two of the new orange arrow plus one of the new blue arrow. Pick a point in the small square under the figure, or drag the dot itself, and follow its recipe out from the origin:

The orange leg is x copies of the orange arrow, the blue leg is y copies of the blue arrow, and the dot where they end is where the point lands. The ring on the faint grid is where it started. Written with the matrix's entries, that recipe is the rule for multiplying a matrix by a point:

\begin{pmatrix} \orange{a} & \blue{b} \\ \orange{c} & \blue{d} \end{pmatrix} \begin{pmatrix} x \\ y \end{pmatrix} = x \begin{pmatrix} \orange{a} \\ \orange{c} \end{pmatrix} + y \begin{pmatrix} \blue{b} \\ \blue{d} \end{pmatrix} = \begin{pmatrix} \orange{a}x + \blue{b}y \\ \orange{c}x + \blue{d}y \end{pmatrix}

The new first coordinate is a times x plus b times y, and the new second coordinate is c times x plus d times y. That's all a matrix ever does to a point. Because the recipe only scales the arrows and adds them, it can't bend a line, and it can't move the origin: zero of one arrow plus zero of the other is still zero.

Four multiplications and two additions per point is very little work, which is why matrices are everywhere in computing. A phone photo has about 12 million pixels. Turning it means running that small recipe 12 million times, and a phone's graphics chip does it in a few thousandths of a second, because every point gets exactly the same treatment and they can all be done side by side.

Stretch, turn, shear, flip

A few matrices come up again and again. Here are four of them. The slider grows each one from doing nothing, which is the matrix with 1s on its diagonal and 0s elsewhere, called the identity, to its full effect:

A stretch leaves the arrows on their axes and changes their lengths: 2 across and 0.5 up makes the F twice as wide and half as tall. A turn by 90° sends î up to (0, 1) and ĵ over to (−1, 0). For a turn by any angle the columns are a point on the unit circle and that point turned another quarter:

R_\theta = \begin{pmatrix} \orange{\cos\theta} & \blue{-\sin\theta} \\ \orange{\sin\theta} & \blue{\cos\theta} \end{pmatrix}

A shear leaves î alone and tips ĵ sideways, so each row of the grid slides over by an amount that grows with its height, like a pushed deck of cards. The flip reverses î, and the F comes out mirror-written. Set the slider to and look at the flip: before the F can face the other way, it's squeezed flat, like a card seen edge-on as it turns over. That flat moment comes back below.

Area, and flipping over

A matrix turns every square of the grid into the same parallelogram, only in a different place. So once we know how the area of one square changes, we know how the area of any shape changes, since any shape can be filled, as closely as you like, with small squares. Drag the arrows and compare the green parallelogram, which started as the 1 × 1 square between î and ĵ, with the F:

The F started with an area of 1.2 squares. Whatever you do with the arrows, its new area is 1.2 times the area of the green parallelogram. That one number, the factor every area gets multiplied by, is called the determinant of the matrix, and for a 2×2 matrix it is:

\det A = \orange{a}\,\blue{d} - \blue{b}\,\orange{c}

Reset the figure with and turn on Show where ad − bc comes from. The dashed box around the parallelogram is a + b wide and c + d tall. Besides the parallelogram it holds two orange triangles that together make an a by c rectangle, two blue triangles that make a b by d rectangle, and two hatched rectangles that are b by c. Take them all away from the box, (a + b)(c + d) − ac − bd − 2bc, and ad − bc is what's left.

Now . The blue arrow glides onto the line through the orange one, the parallelogram has no area left, and the whole plane is crushed onto one line. The determinant is 0.

The formula ad − bc can also come out negative, which no area can. To see what that means, I'll hold the orange arrow still and let a slider swing the blue arrow around. Swing it down past the orange arrow and watch the F and the plot:

As the blue arrow closes in on the orange one, the parallelogram gets thinner and the determinant drops, reaching 0 at 15°, where the two arrows line up and the plane is flat. Swing further and the determinant goes negative, and the F reads backward. The size of the determinant is still the factor for areas; its sign says whether the plane has been flipped over. The small curved arrow shows the same thing: going the short way round from î to ĵ turns counterclockwise before the flip and clockwise after it.

The determinant changes smoothly as the arrows move, so there's no way to get from unflipped to flipped without passing through 0. That's why the flip in the gallery had to squash the F flat on its way over.

One after another

Apply one matrix and then another, and the grid is still made of straight, parallel, evenly spaced lines, so the two steps together are a single linear transformation, with a single matrix. Before you run the figure, a guess:

Turn the F a quarter turn and then shear it, or shear it first and then turn it. Do you end up in the same place?

The two orders give different pictures and different matrices. The shear acts along the horizontal, and turning first changes what is horizontal.

Here the same two moves are applied in both orders. Drag the slider back to the start, then forward again to watch the two steps:

Same two moves, different results. Turning and then shearing lays the F's spine flat, with its arms slanting up to the right; shearing and then turning tips the spine up to the left and points the arms straight up. Order matters here, which it never does when multiplying ordinary numbers.

The single matrix for both steps is called the product of the two. Its columns come from following the arrows: the first column is where î ends up after both moves, which is the second matrix applied to the first matrix's first column, and likewise for ĵ. This is exactly the rule for multiplying matrices that Arthur Cayley wrote down in 1858, when he first treated matrices as things you could add and multiply .

Areas follow the same chain: the determinant of a product is the product of the determinants. A turn and a shear each leave areas alone, with determinant 1, so in both orders the F still covers 1.2 squares.

Undoing a matrix

Most matrices can be undone. If a matrix A moves the F somewhere, another matrix moves it back, and that matrix is called the inverse, written A^{-1}. Its columns answer the reverse question: which arrows would A send to (1, 0) and (0, 1)? Run the slider through both halves, and then use the second slider to lean the blue arrow toward the orange one:

The first half moves the F with A, the second half applies the inverse and every point lands exactly where it started. On the left of the readout is A; on the right, its inverse. For a 2×2 matrix the inverse has a short formula: swap a and d, change the signs of b and c, and divide everything by the determinant:

A^{-1} = \frac{1}{\orange{a}\blue{d} - \blue{b}\orange{c}} \begin{pmatrix} \blue{d} & -\blue{b} \\ -\orange{c} & \orange{a} \end{pmatrix}

Now lean the blue arrow with the second slider, to , and watch the way back. The determinant is down to a tenth of what it was, so dividing by it makes the inverse's numbers ten times bigger, and the return trip swings the grid far out before it lands. At a lean of 100% the arrows would sit on one line, the determinant would be 0, and the formula would divide by zero. There's no way back from a flattened plane: two points that landed on the same spot can't be told apart again.

Why the inverse looks like that 2 min

Multiply A by the matrix in the formula, before dividing by the determinant:

\begin{pmatrix} a & b \\ c & d \end{pmatrix} \begin{pmatrix} d & -b \\ -c & a \end{pmatrix} = \begin{pmatrix} ad - bc & 0 \\ 0 & ad - bc \end{pmatrix}

The off-diagonal entries cancel exactly, and both diagonal entries are the determinant. So the swapped-and-negated matrix undoes A except for a uniform stretch by ad − bc, and dividing by that number removes the stretch. When the determinant is 0 there's nothing to divide by, which is the algebra's way of saying the plane was flattened.

Some matrices flatten on purpose. A projection drops every point straight onto a line, the way a shadow falls on the ground when the sun is overhead. Turn the knob to tilt the line, and drag the dot to see where it lands:

Every point of the F drops at right angles onto the brass line, and the whole letter collapses to a dark segment. Each drop is the shortest way from a point to the line, which is what makes projections useful: fitting a straight line to noisy measurements, the method of least squares, comes down to projecting the measurements onto the set of answers a straight line could give. The projection's matrix has determinant 0 at every angle, as it must: it flattens the plane, so it has no inverse. It has another property no turn or stretch has. Project the shadow again and nothing moves, because points already on the line stay put. In symbols, P^2 = P.

Directions that only stretch

Look again at the stretch from the gallery. Arrows along the horizontal axis came out longer but still horizontal; arrows along the vertical came out shorter but still vertical. Every other arrow got turned. Most matrices have a few directions like that, which they stretch or shrink but don't turn. First, a guess about the shear:

A shear slides each row of the grid sideways by an amount that grows with its height. Which directions come out pointing the same way they went in?

The horizontal row through the origin doesn't slide at all, so horizontal arrows stay put. Every other arrow, the vertical one included, gets tipped sideways.

Here's a way to find such directions for any matrix. The purple arrow has length 1 and can point any way; the green arrow is where the matrix sends it. Turn the knob (or drag the purple tip) all the way round, and watch the plot of the angle between the two:

For most directions the green arrow points somewhere else: the plot sits above or below zero. But four times on the way round the plot crosses zero, and there the two arrows line up. Set the knob to and the green arrow points exactly along the purple one, 1.8 times as long. Set it to and it lines up again, now only 0.7 times as long. The other two crossings are the same two lines, pointing the other way.

A direction a matrix only stretches is called an eigenvector, and the factor it's stretched by is its eigenvalue, usually written λ (lambda).“Eigen” is German for “own”: these are the matrix's own directions. The word came into English from David Hilbert's work on integral equations around 1904. This matrix has eigenvalues 1.8 and 0.7, along the two dashed purple lines:

A\,\purple{\vec v} = \lambda\,\purple{\vec v}

Now try the presets. The has its eigenvectors on the axes, as we saw. The has only one line, the horizontal, and the plot just touches zero there instead of crossing. The lines up the horizontal with eigenvalue 1, and reverses the vertical, so the plot reaches ±180° there: an eigenvalue of −1. And the never lines anything up. Its plot is a flat line at 37°, because a turn turns every direction by the same angle. A turn has no real eigenvectors at all.

Finding eigenvalues without a figure 3 min

If A\vec v = \lambda \vec v for some nonzero \vec v, then the matrix A - \lambda I sends \vec v to zero. It crushes a direction, so its determinant is 0:

\det\begin{pmatrix} a - \lambda & b \\ c & d - \lambda \end{pmatrix} = \lambda^2 - (a + d)\,\lambda + (ad - bc) = 0

That's a quadratic in λ, called the characteristic equation. The sum of the two roots is a + d, the trace, and their product is the determinant. For our matrix, 1.8 + 0.7 = 1.6 + 0.9 and 1.8 × 0.7 = 1.26 = 1.6 × 0.9 − 0.6 × 0.3. When the quadratic has no real roots, as for a turn, the eigenvalues are complex numbers, and the matrix turns every direction. For the turn by 37° they come out as 0.8 ± 0.6i, the cosine and sine of the angle.

The eigenvectors show up in the whole picture too. Here's the grid again, with dots placed around a circle. As the slider applies the matrix, every dot moves in a straight line, but only the dots on the purple lines stay on their own ray from the origin. Drag the arrow tips to change the matrix and watch the purple lines follow:

Each purple dot moves along its own ray: out to 1.8 times as far on one line, in to 0.7 times as far on the other. Every other dot leaves its ray behind. Try the preset, whose top-right and bottom-left numbers match: its two purple lines come out exactly perpendicular, which is always true of symmetric matrices. We'll need that fact at the end.

Eigenvectors also say what happens when a matrix is applied over and over, which is how populations grow year after year, how probabilities spread through a network, and how search engines once ranked web pages.Google's original PageRank was the eigenvector, with eigenvalue 1, of a huge matrix describing how a random surfer moves between pages. A guess first:

Start with an arrow pointing any way and apply the same matrix to it ten times. Where does it end up pointing?

Each application multiplies the arrow's part along one eigenvector by 1.8 and its part along the other by 0.7. After ten rounds the first part is 1.8¹⁰ ≈ 357 times bigger and the second 0.7¹⁰ ≈ 0.03 times, so the arrow lines up with the first.

Step through the directions with the stepper. Whatever starting direction you drag the purple arrow to, a few applications pull it onto the stronger eigenvector line, because the part of the arrow along that line grows by 1.8 each time while the other part shrinks by 0.7. Only an arrow that starts exactly on the weaker line stays there. Switch to the , whose eigenvalues are complex, and the arrow never settles: it goes round by 25° every step.

The same thing hides in the Fibonacci numbers, 1, 1, 2, 3, 5, 8, 13, where each number is the sum of the two before it. Going from one pair to the next, (a, b) to (a + b, a), is a matrix with rows (1, 1) and (1, 0). Its larger eigenvalue is 1.618…, the golden ratio, and its other is −0.618…. Apply it again and again and the pair lines up with the first eigenvector, which is why the ratio of one Fibonacci number to the one before it creeps toward 1.618: 13 / 8 is 1.625, 21 / 13 is 1.615, and 89 / 55 is 1.618.

Seeing a matrix in its own axes

The grid we've been drawing on, made of î and ĵ, is only one choice of axes. Draw a grid along the two eigenvector lines instead, and the matrix looks completely different. Run the slider through the three stages:

In the first stage the purple grid is straightened out until its lines run along the ordinary axes. In the middle stage, the only thing that happens is a stretch: 1.8 along one axis and 0.7 along the other. In the last stage the purple grid is put back where it was. The F ends exactly where the matrix would have sent it in one go. Written as matrices, read from right to left as always:

A = V\,D\,V^{-1}, \qquad D = \begin{pmatrix} \purple{1.8} & 0 \\ 0 & \purple{0.7} \end{pmatrix}

The columns of V are the eigenvectors, and D has the eigenvalues on its diagonal and zeros elsewhere. Splitting a matrix like this is called diagonalizing it. It makes repeated application easy: applying A ten times is V D^{10} V^{-1}, and raising D to the tenth power only means raising 1.8 and 0.7 to the tenth power. That's the calculation behind the arrows that lined up a moment ago.

Not every matrix can be diagonalized with real numbers. The turn has no real eigenvectors to make axes from, and the shear has only one line, too few to make a grid. For those the next idea does better: it works for every matrix, with no exceptions.

Circles become ellipses

Draw a circle and apply a matrix to it. Every straight line stays straight, but a circle is not a line, and it comes out as an ellipse. Drag the arrow tips and watch the ellipse's two axes:

Every matrix turns the unit circle into an ellipse, perhaps a squashed flat one. Its longest radius is called the first singular value, σ₁ (sigma one), and its shortest the second, σ₂. Their product is the factor areas change by, the absolute value of the determinant, which makes sense: the circle's area π becomes the ellipse's area π σ₁ σ₂.

Look at the dashed arrows on the circle. They're the two directions that land on the ellipse's axes, and they're always at right angles to each other, before and after. That holds for every matrix, even the , which had only one eigenvector: its circle becomes an ellipse with radii 1.618 and 0.618, the golden ratio and its reciprocal. For the the ellipse is the circle itself, both singular values 1. For a matrix, σ₂ is close to 0, and the ellipse is almost a line segment.

Those right angles give a way to take any matrix apart. Here's the same idea as three stages:

First a turn brings the two special directions onto the axes. Then a stretch along the axes, by σ₁ and σ₂, turns the circle into an ellipse lying flat. Then a second turn swings the ellipse into place. Every matrix, without exception, can be written this way, as a turn U, a stretch Σ, and a turn VT:

A = U\,\Sigma\,V^{\mathrm{T}}

This is the singular value decomposition, or SVD. With the example, the stretch has a negative factor, which is where the mirror image comes from. (Mathematicians usually keep σ positive and let one of the turns be a reflection instead; it's the same picture.) The SVD is one of the most used tools in numerical computing : it says how close a matrix is to being flat, which directions it amplifies most, and how to approximate a big matrix by a simpler one by keeping only its largest σ's.That last use is behind some image compression: a photo is a big matrix of brightness values, and keeping only its largest few dozen singular values keeps most of what the eye sees.

The ratio σ₁ / σ₂, called the condition number, measures how lopsided the ellipse is. It matters whenever a computer solves equations with a matrix, which means undoing it. A small error in the input along the short axis gets divided by the small σ₂ on the way back, so it grows by up to σ₁ / σ₂ compared with an error along the long axis. For the nearly flat matrix in the figure above, that factor is about 50: three correct digits going in can leave only one or two coming out.

Three dimensions

Nothing here depends on the plane being flat. In space there are three unit arrows, and a 3×3 matrix has three columns, one for where each arrow lands. The unit square becomes a unit cube, and the matrix turns the cube into a slanted box called a parallelepiped. Drag to turn the view, use the first slider to apply the matrix, and use the second to lean the purple arrow toward the other two:

The volume of the green box is the determinant of the 3×3 matrix, 1.54 at the start, and every little cube of space grows by the same factor. Lean the purple arrow toward the plane of the other two and the box flattens, its volume sinking toward 0. It's the same collapse as in the plane, one dimension up: at a lean of 100% the box would be flat and the matrix couldn't be undone. Everything else carries over too. A 3×3 matrix has up to three eigenvector directions, turns a sphere into an ellipsoid with three singular values, and splits into turn, stretch, turn.

Matrices at work

Back to the phone and the video game. A drawing program keeps every shape as a list of points in the shape's own coordinates, and a matrix places it on the page. Here's a small house; drag its corners in the left panel to build your own shape, and use the controls to turn, stretch, and shear it:

The shape on the right is drawn by applying one matrix to each corner of the shape on the left. Change the shape and the matrix doesn't change; change the matrix and the shape doesn't. That separation is what lets a game store a character once and draw it in a thousand poses: each pose is a handful of matrices, and every frame, every corner of every model is multiplied by them, many millions of times a second.

Sliding with a matrix: one more dimension 2 min

A 2×2 matrix can't slide a shape sideways, because it never moves the origin. Graphics systems get around this with a trick: write each point (x, y) as (x, y, 1) and use a 3×3 matrix whose last column carries the slide:

\begin{pmatrix} a & b & \green{t_x} \\ c & d & \green{t_y} \\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} x \\ y \\ 1 \end{pmatrix} = \begin{pmatrix} ax + by + \green{t_x} \\ cx + dy + \green{t_y} \\ 1 \end{pmatrix}

The top-left 2×2 block turns and stretches, the extra column slides, and products of these 3×3 matrices chain turns, stretches, and slides together. 3D graphics does the same with 4×4 matrices, which can also apply the perspective that makes far things look smaller .

Matrices also find structure in data. Here are measurements of two quantities, say height and arm span for a few dozen people, one dot each. Drag any dot, or pick another cloud:

The ellipse and the two purple arrows come from a matrix built from the data: the covariance matrix, whose diagonal holds how spread out each quantity is and whose off-diagonal entries say how much the two rise and fall together. It's symmetric, so, as with the symmetric matrix earlier, its eigenvectors are perpendicular. The longer arrow points along the direction in which the data varies most, and its eigenvalue measures how much. Finding these directions is called principal component analysis, and it goes back to Karl Pearson in 1901 and Harold Hotelling in 1933 .

For the cloud, almost all of the spread lies along one direction, so two numbers per dot can be replaced by one with very little lost. With hundreds of quantities instead of two, the same idea picks out the few directions that matter, and it's still the eigenvectors of one symmetric matrix.

That's the whole toolkit. A matrix is a list of where the arrows go. It keeps lines straight, multiplies areas by its determinant, and has its own directions, where it only stretches. And any matrix at all is a turn, a stretch, and a turn. The rest of linear algebra, in any number of dimensions, is built on these pictures .

References

  1. Gilbert Strang, Introduction to Linear Algebra, 5th edition, Wellesley-Cambridge Press, 2016. Built around the columns picture used on this page.
  2. Sheldon Axler, Linear Algebra Done Right, 3rd edition, Springer, 2015. Linear maps first, matrices second; eigenvectors without determinants.
  3. Lloyd N. Trefethen and David Bau III, Numerical Linear Algebra, SIAM, 1997. Starts with the SVD as a circle becoming an ellipse.
  4. Arthur Cayley, “A memoir on the theory of matrices”, Philosophical Transactions of the Royal Society of London 148 (1858), 17–37.
  5. Karl Pearson, “On lines and planes of closest fit to systems of points in space”, Philosophical Magazine, series 6, 2 (1901), 559–572.
  6. Harold Hotelling, “Analysis of a complex of statistical variables into principal components”, Journal of Educational Psychology 24 (1933), 417–441.
  7. John F. Hughes, Andries van Dam, Morgan McGuire, David F. Sklar, James D. Foley, Steven K. Feiner, and Kurt Akeley, Computer Graphics: Principles and Practice, 3rd edition, Addison-Wesley, 2013.