mathlinear algebra
matrices
a grid of numbers that moves every point by one rule: turns, stretches and shears.
a 2 × 2 matrix moves every point of the plane by one rule. its first column is where i = (1, 0) lands and its second is where j = (0, 1) lands, and every other point follows, since (x, y) is x of i plus y of j. straight lines stay straight, the origin stays put.
- the columns
- Ai, Aj where i and j land. read a matrix by its columns, not its rows
- A(x, y)
- x·Ai + y·Aj x times the first column plus y times the second
- AB
- B first, then A AB and BA are different moves
- det A
- ad − bc the factor every area is scaled by. negative: flipped over. 0: squashed flat
- inverse
- [d −b; −c a] / det swap a and d, negate b and c, divide by det. none when det = 0
- turn by θ
- cos θ, sin θ the columns are i and j turned by θ: (cos θ, sin θ) and (−sin θ, cos θ)
- turning, stretching and leaning shapes in 2d, and in 3d with one row and column more
- the arrows of the vectors page, all moved at once by the same rule
- solving linear systems and finding eigenvectors, the next two pages
on this page
the idea
a matrix is a rule for moving every point of the plane at once. a 2 × 2 matrix is four numbers in a grid, and the picture to keep is this: its two columns are where the two unit arrows land. goes to the first column and to the second. every other point is some amount of plus some amount of , and it goes where that recipe says.
so a matrix moves the whole grid in one piece: grid lines stay straight, parallel lines stay parallel and evenly spaced, and the origin never moves. what changes is where and point. the movie moves the plane one matrix at a time and follows the point , which is .
an animation in five chapters on a grid from −3 to 3, with the unit square filled in, an arrow to where i lands in cyan and one to where j lands in amber, the matrix in brackets with its determinant, and the point (2, 1) followed. first the shear [1 1; 0 1]: i stays at (1, 0), j leans to (1, 1), the grid leans with it, the square becomes a parallelogram of area 1, and (2, 1) lands on (3, 1). second, a quarter turn on top of it: everything turns 90 degrees counter-clockwise, (3, 1) goes to (−1, 3), and the one matrix for both moves is [0 −1; 1 1]. third, both are undone in the reverse order: the turn back first, then the lean, and the grid is at rest again. fourth, the mirror [1 0; 0 −1]: j slides down to (0, −1), the square flips over, the determinant goes from 1 through 0 to −1, and (2, 1) lands on (2, −1). fifth, the squash [1 0; 0 0]: j slides onto the origin, the whole plane lands on the x axis, the determinant is 0, and (2, 1) and (2, −1) both land on (2, 0).
explore
set the matrix by hand. the tip of the cyan arrow is the first column and the tip of the amber one is the second, so dragging them is writing the matrix.
drag the tip of i or j, or tab to one and use the arrow keys: 0.5 a step
- i lands on
- (2, 0)
- j lands on
- (1, 1)
- A(x, y)
- x·(2, 0) + y·(1, 1)
- det = a·d − b·c
- 2·1 − 1·0 = 2
- the square
- area 2, the same way round
a grid from −3 to 3 with the unit square filled in, an arrow to where i lands in cyan and one to where j lands in amber, and the matrix beside it with its first column in cyan and its second in amber. drag either tip, or tab to it and use the arrow keys, half a unit a step, and the grid and the square follow. it opens with i at (2, 0) and j at (1, 1): the matrix [2 1; 0 1], a stretch and a lean at once, and the square is a parallelogram of area 2. the readout gives the two columns, the rule A(x, y) = x·(2, 0) + y·(1, 1), the determinant worked out, 2·1 − 1·0 = 2, and what it says about the square: its area, and whether it is flipped over or flat. buttons set a preset: rest, a stretch by 2 along x, a turn of 90 degrees, a shear, the mirror that swaps i and j with determinant −1, and a squash with columns (1, 0.5) and (2, 1) that sends every point onto one line, determinant 0.
try it
A is the matrix below. where does it send (2, 1)?
pick one.
a practice exercise with a matrix in brackets and a small grid under it: where the matrix sends a point, which move the matrix is (a stretch, a turn, a shear, a mirror or a squash onto a line), its determinant, or whether it has an inverse. after an answer the grid shows where i and j land, the image of the unit square, and for a point question where the point went.
the rules
the columns
the first column is where lands and the second is where lands. that is the whole matrix: a point is of and of , and a matrix keeps sums and multiples, so its image is of the first column and of the second. the derivation is below, in the key idea up close. to read a matrix, read its columns. sends to and to , so it sends to .
the moves to know
each one is its columns. a stretch is diagonal: goes to and to , so each axis is scaled on its own, and the same number twice scales everything evenly. a turn by sends to the point at angle on the unit circle, , and to the point a quarter turn further, : the circle picture from the trig page, once for each column. with it’s , and is a stretch by both ways. a shear leaves alone and leans to : every point slides sideways by times its height, so horizontal lines stay where they are and vertical ones tilt. a mirror flips the plane over: this one negates , across the axis. mirrors across the axis, and swaps and , a mirror across the line . a squash, a projection, drops a dimension: this one sends to the origin, so every point lands on the axis, at its own . any matrix whose two columns point along one line squashes the plane onto that line.
one move after another
is the one matrix that does first and then , read from the right, the way runs first. its columns come one at a time: the first column of is where ends up after both moves, which is applied to the first column of , and the second column is applied to the second column of . the formula is that, written out: each column of is fed through with the rule above. entry by entry, the number in row and column of is row of times column of , matching parts multiplied and added, which is the dot product of the vectors page.
the order matters. the shear and the quarter turn from the movie:
lean then turn sends to . turn then lean sends it to . two different pictures, so in general. they agree for some pairs, like two turns, or two stretches, and the cg fundamentals page has a figure of the same thing with a turn and a move.
the determinant
the determinant is the factor every area is multiplied by. the unit square, with corners at , , and , lands on the parallelogram with sides and , and that parallelogram has area : the derivation is below. since the grid is moved evenly, every shape’s area is scaled by the same factor, not only the square’s. the sign tells you which way round the shape came out. positive, and the corners still run counter-clockwise. negative, and the plane has been flipped over, as by a mirror. zero, and the square has been squashed flat: the two columns point along one line, or one of them is , and all of the plane lands on a line or a point. a turn and a shear have determinant 1, a stretch has , a mirror . doing two moves multiplies the factors: .
the inverse and the identity
the identity moves nothing: lands on and on . the inverse of undoes it: whatever did to a point, sends it back, so one after the other is . for a the recipe is: swap and , change the signs of and , and divide by the determinant. multiply it out and the off-diagonal entries cancel to while the diagonal ones come to , which the division turns into . the division is why a determinant of means there is no inverse, and the picture says the same: a squash sends two different points to one place, and no rule can send that one place back to both. undoing two moves goes in the reverse order, the last move first: . a turn’s inverse is the turn back, to , which is the same matrix with its rows and columns swapped. a mirror is its own inverse.
worked examples
where a point goes. and the point . times the first column plus times the second: . the formula agrees: .
a quarter turn. the turn by has and , so it is : goes to , straight up, and to . the point goes to . for the entries are and , so lands on , a point on the unit circle.
two moves as one. the shear and the quarter turn from the movie. column by column: applied to ‘s first column, , is . applied to the second, , is . so . check with the point : the shear sends it to and the turn sends that to , and sends it to in one step.
the determinant. : a shape of area 1 comes out with area 5. for any turn, , so a turn never changes an area. : the columns and point the same way, and every point lands on that line.
the inverse. has . swap the diagonal, negate the rest, divide by 5: . check: has first column and second column : the identity. and , with determinant 0, has none: the formula would divide by 0.
undoing two moves. above has , so . the other way to get it: is the turn back, , and is the shear back, . , turn back then unlean, is : the same. it takes back to .
the key idea up close
why the columns are where i and j land
a matrix is a rule with two properties: it keeps sums and it keeps multiples. move two arrows and add them, or add them and move the sum, and you land in the same place; stretch an arrow by and move it, or move it and stretch, the same.
that is what it means for the grid to move as one piece, with the lines kept straight and evenly spaced. now write any point as its recipe in and and move it:
so the whole move is pinned down by two arrows, and : where lands and where lands. write those two arrows side by side as columns and you have the matrix. the formula is this sum with the columns filled in. the same reasoning in three dimensions gives three columns, where , and land, and the and matrices of the cg fundamentals page read the same way.
why the area factor is ad − bc
the unit square lands on the parallelogram with sides and . take the case where both columns point into the first quadrant with the first one lower, as in the explore figure’s opening matrix. box the parallelogram in the rectangle from the origin to , area , and cut away what isn’t parallelogram: two right triangles, two right triangles, and two rectangles.
in the other arrangements the same bookkeeping gives or its negative, and the sign records which way round the corners run: negative when the second column sits clockwise of the first, which is the flip. every other shape is scaled by the same factor, because a shape is a sum of small squares of the grid and each one lands on a copy of the same parallelogram.
pitfalls
- isn’t . the matrix on the right acts first. lean then turn and turn then lean are different pictures, and in general swapping the order changes every entry. write the product in the order the moves happen, right to left, and check it on : the first column of is applied to the first column of .
- row times column: the first index is the row. the entry in row , column of is row of against column of . a matrix written by rows, as on paper, has its columns split across the lines, so where lands is , the first number of each row, not . code often stores the numbers a column at a time instead: css’s
matrix(a, b, c, d, tx, ty)and canvas’stransform(a, b, c, d, e, f)send to , so there and are where lands, and and where lands.1 the cg fundamentals page has the same note for glsl and three.js. - a determinant of 0 means no inverse. the formula divides by it, and the picture says why: the plane has been squashed onto a line, two points share an image, and no rule can send one point back to two. a determinant that’s merely tiny isn’t 0, but the inverse’s entries are huge, and in floating point that’s nearly as bad.
- on a screen, y points down. the turn by above is counter-clockwise with pointing up, the way math draws it and the way gl’s window coordinates run. a canvas, the dom and css count downward from the top edge, and the same matrix, with the same and in the same places, then shows up as a clockwise turn: css’s
rotate(90deg)turns an element a quarter turn clockwise, and its matrix is the one above.2,3 the matrix didn’t change, the picture is a mirror image of the math one. so a turn that goes the wrong way is often a sign that is flipped, not that the matrix is. - is . the moves are undone in the reverse order, the last one first, like shoes before socks. undoes , not , and the two differ unless and happen to commute.
- a matrix can’t move the origin. for every matrix, so sliding everything over by a fixed amount, a translation, isn’t a matrix. graphics gets it by adding a coordinate: the matrices and the of the cg fundamentals page, where the translation sits in the last column.
practice
-
. where does it send , and where ?
answer
times the first column plus times the second. . : a point on the axis lands on a multiple of the second column.
-
which move is , and which is ? give each determinant.
answer
the first sends to and to : they trade places, which is the mirror across the line . , the flip. the second is diagonal with the same number twice: a stretch by 3 in every direction. , since a square 3 times as wide and 3 times as tall has 9 times the area.
-
stretches by 2 and is the shear. find and , and say what each does to .
answer
is applied to the columns of : and , so . is applied to the columns of : and , so . lean first and then stretch, and the lean is stretched too: lands on . stretch first and then lean, and lands on , as under the shear alone, since the stretch left where it was.
-
find the determinant and the inverse of , and check the inverse on .
answer
. swap the diagonal, negate the rest, divide by 10: . check: the matrix sends to , and the inverse sends to , which is again.
-
for which does have no inverse? what does the matrix do to the plane then?
answer
no inverse means : , so . then the columns are and , and the second is twice the first: both point along the same line, and every point lands on it. the plane is squashed onto the line through , and two different points land in one place, so there is nothing to invert.
-
a triangle has area 5. what is the area of its image under , and under the quarter turn ? is either image flipped over?
answer
every area is multiplied by the determinant’s size. , so the image has area . the quarter turn has , so its image still has area 5. both determinants are positive, so neither image is flipped: the corners run the same way round as before. a mirror would give and a flipped triangle of the same size.
sources
-
mdn’s CanvasRenderingContext2D.transform() (opens in a new tab) page:
transform(a, b, c, d, e, f)sends a point to . with and at 0, and scale; with and at 1, and skew; and translate. ↩ -
css transforms module level 1 (opens in a new tab), section 3, the transform rendering model: the axis increases vertically downwards. section 9.1, on
rotate():rotate(90deg)makes an element appear turned a quarter turn clockwise. ↩ -
mdn’s rotate() (opens in a new tab) page: in a left-to-right page a positive angle is a clockwise turn, and the function’s matrix is written out with and in the same places as the turn above. ↩