In P1.B7 you met a one-line test for two vectors in the plane: means they point in different directions. That number is no random trick. It is the area of the parallelogram the two vectors make, and for a whole matrix it tells you how much the matrix stretches the plane. This lesson is about that number, the determinant: what it means, how to calculate it, which rules it obeys, and why a computer handles it with care.
What does a matrix do to areas?
In P1.B5 the two columns of a matrix told you where and land. So the unit square (the square with corners , , , , area 1) is carried to a parallelogram whose two sides are the columns. The matrix widget in P1.B5 printed the area of that parallelogram as . Now we look at it properly.
Take the matrix from P1.B7, with columns and :
Its parallelogram has area (the next sections show why). The unit square had area 1, so multiplies that area by 5. The determinant of a matrix is exactly this stretch factor for area, with a sign that we meet below. It is written , and for it is
For : . This is the test from P1.B7: with the two vectors as the columns, is the determinant. The determinant is only defined for square matrices (as many rows as columns).
The widget draws the parallelogram for any matrix. It starts with the matrix above.
Things to try:
- At the start the shape is green and the area is , as computed above.
- Press Swap the columns. The parallelogram is the same size, but the determinant is , the shape turns red, the arc between the columns runs the other way and the F is mirrored.
- Press Rotate 30° and Shear. Both have determinant : the F turns or leans, but its area does not change.
- Press Stretch (columns and ). The area is .
- Press Reflect. The determinant is : the area is kept, but the plane is flipped.
- Press Squash (columns and , the dependent pair from P1.B7). The shape is flat and the determinant is . Now use the slider for to change to . The determinant is , not , and the status says “nearly squashed”.
- Tick Show a grid of small squares. Every small square turns into a parallelogram of the same shape and the same area.
Intuition
Think of a photocopier. Copying at 200% doubles every length and multiplies every area by , which is the determinant of . A matrix is a more general copier: it can stretch one direction and squash another, and the determinant is the net factor for area.
Turning a transparent sheet over gives a mirror image: that is a negative determinant. Pressing the sheet flat onto a single line leaves nothing to measure: that is a determinant of .
The analogy breaks in two places. A copier stretches every direction equally, a matrix does not (a stretch and a shear can have the same determinant). And the determinant tells you only the area factor, not the shape: a rotation and a shear both have determinant , yet they move things very differently. Also, the factor is the same everywhere only because the map is linear; a curved map stretches differently at different points (P1.C7).
Math
For a square matrix , is a single number. For it is . You will also see it written , but that is easy to confuse with an absolute value, so this course writes and writes for the area factor.
- is the factor by which multiplies areas (volumes in 3-D, and so on).
- The sign of says whether the space is flipped.
- means the space is squashed flat.
The rules below say how it behaves when you multiply, transpose or invert.
Code
import numpy as np
A = np.array([[2, -1], [1, 2]])
print(np.linalg.det(A)) # about 5.0 (NumPy may print 5.000000000000001)np.linalg.det works for any square array. The tiny rounding noise in the last digit is explained in the NumPy section below.
Why ?
Here is a short argument for the area, using the same . The area of a parallelogram is base times height, so we reshape it into one where base and height are easy to read, without changing the area.
Step 1: slide one side. Add a multiple of column 1 to column 2 (the multiple can be negative) so that column 2 lands on the vertical axis. Here we add of column 1: . Adding a multiple of one side to the other side slides the far edge of the parallelogram along its own direction, like pushing the top of a deck of cards sideways. The base and the height do not change, so the area does not change (this is the shear of P1.B5, which keeps area).
Step 2: read base and height. Now the sides are and . Take the second as the base: it lies on the vertical axis and has length . The height is the horizontal distance from that axis to the opposite edge, which is the -entry of the first column, . Area .
In general, with columns and and :
If the columns are and : the first lies on the vertical axis, the base is , the height is , and the area is again .
So the size of is the area. What the sign tells you comes next.
The sign: did the plane flip?
Swap the two columns of and you get
Its determinant is .
Both matrices make the same parallelogram, so the area is for both. The difference is orientation. In the identity the second column lies a quarter turn counterclockwise from the first column . If a matrix keeps this order (the second column is counterclockwise from the first by less than a half turn, as in the widget’s green case) the determinant is positive. If it reverses the order (the second column is clockwise from the first) the determinant is negative, and the whole plane has been flipped over, like a mirror image. You can see it in the F: a positive determinant keeps it reading the right way round, a negative one mirrors it.
- A rotation keeps the order of the columns: .
- A reflection, such as , reverses it: .
- Swapping two columns (or two rows) always changes the sign.
The area itself is never negative. The area factor is , and the sign is extra information: “and the plane is flipped”. The sign is worth keeping because it makes rules like the product rule below work without special cases. In 3-D the sign tells whether the three columns, in order, follow the right-hand rule (positive) or the left-hand rule (negative).
Zero: the plane gets squashed
What if the two columns lie on one line? Take and from P1.B7. The parallelogram is flat: it has no width, so its area is . Every point of the plane lands on the line through , so the whole plane is squashed onto a line. The same happens with the projection of P1.B5, : . If one column is , or both are, the parallelogram is flat too (a line segment, or a single point), so the determinant is .
All of these are the same fact said in different ways:
“Squashed” explains the last part: after a squash, different inputs have the same output (P1.B5), so nothing can recover the input. P1.B9 shows how to undo a matrix that is not squashed. For a matrix the inverse has a preview worth seeing now:
For our this gives . If you would divide by zero, which is the formula’s way of saying there is no inverse. (P1.B9 explains it.)
In P1.B7, “rank below ” and “dependent columns” were stated for any matrix. For a square matrix there is now a one-number test: full rank is the same as . In 2-D this is the flat-parallelogram picture; in 3-D it is the flat box, as the widget in P1.B7 showed.
Every area scales, not only the unit square
The determinant is defined with the unit square, but it applies to every shape. Tick Show a grid of small squares in the widget. Each small square has side , and each one lands on a parallelogram with the same two sides, and (a linear map sends a square to a shifted copy of the same parallelogram, wherever the square sits). So every small square has its area multiplied by the same : from to .
Any region is made of many tiny squares, so any region has its area multiplied by . A triangle of area becomes one of area . The unit circle (area ) becomes an ellipse of area ; for our that is . The factor is the same everywhere because the map is linear: grid lines stay straight and evenly spaced. For a curved map the factor changes from point to point, and recording that is the job of the Jacobian determinant (P1.C7).
Determinants of matrices you already know
| Matrix | Why | |
|---|---|---|
| Identity | nothing moves | |
| Diagonal , for example the scale of P1.B5, | ( for the example) | the square becomes a rectangle with sides and |
| Triangular | : the product of the diagonal | |
| Rotation by an angle | areas are kept, the F only turns | |
| Reflection or the swap | areas are kept, the plane is flipped | |
| Shear | a slide keeps the area | |
| Projection | the plane is squashed onto a line | |
| Scaling every coordinate by in dimensions | is diagonal with entries equal to |
For example, multiplies volume by , and multiplies it by . In general a diagonal or triangular matrix has the product of its diagonal entries as determinant. Rotations and reflections are the orthogonal matrices of P1.B10: they keep lengths, and their determinant is or .
Note that does not mean “nothing changes”: rotations and shears have determinant and move everything. It only means the area is kept.
Rules: products, transposes, inverses
Products. Apply first and second. multiplies areas by , and then multiplies the new areas by . P1.B6 promised the result:
Flips combine too: two flips cancel, because a negative times a negative is positive. Example with above and (columns and , ):
The widget below shows it: the unit square after has area , and after as well it has area .
Things to try:
- Leave the defaults. The areas are and then , and the two bottom numbers are equal.
- Choose Mirror in the y-axis for and for . Each flips the plane, the two flips cancel and .
- Choose Mirror in the y-axis for only one of them: the result is negative.
- Choose Project onto the x-axis for either map: the product is flat, with , whatever the other map is.
- Press Swap A and B. The matrix changes (P1.B6: the order matters) but its determinant does not, because . So even when . Applying one matrix times gives .
Transpose. . Swapping the rows and columns does not change the determinant, so the rows make a parallelogram of the same area as the columns. Check: has . (For it is clear from , because and swap places in a product. For larger matrices it is a standard result.)
Inverse. . The inverse undoes , so it must divide the area factor. Algebraically, and the product rule give . For our , . This also shows why cannot have an inverse: no number times equals .
Scaling the whole matrix. For an matrix, , not . Every one of the columns is multiplied by , and each one multiplies the volume by . Check: has .
Sums. There is no rule for , and it is not . With : , but . With and : both determinants are , yet has determinant .
Three dimensions: the volume of a box
In 3-D a matrix with columns , , turns the unit cube into a slanted box (a parallelepiped) with those three edges. Its volume is , and the sign again says whether the space is flipped. This is the box in the three-vector widget of P1.B7. There, with , and , the box had volume ; and with it went flat, volume .
The triple product. The cross product of two 3-D vectors is another 3-D vector:
It points perpendicular to both, and its length is the area of the parallelogram that and make. Dotting with gives (base area) times (height along the perpendicular), the signed volume:
For the example, and . The volume is , as P1.B7 said, and the sign is negative: in this order the three columns are left-handed, a flipped copy of the standard axes. With the triple product is .
Expanding along a row. For a matrix, take each entry of the first row, multiply it by the determinant of the matrix left when you cross out its row and column, and add them with signs (a checkerboard of signs that continues for bigger matrices). Take
(The triple product is this same expansion for the matrix with rows , and the transpose has the same determinant.) You can cross out any row or column, as long as you keep the checkerboard signs.
Sarrus’s rule is a shortcut for only: write the first two columns again to the right of the matrix, add the three products along the diagonals going down to the right and subtract the three going up to the right. For : .
Warning: Sarrus’s rule does not work for or larger. A determinant has terms (that is ), and a pattern of diagonals gives only . For example, swapping two rows of gives determinant , but extending the diagonal pattern to gives . For larger matrices use expansion (slow) or elimination (next).
Row operations and how computers do it
Three row operations change the determinant in simple ways. They are the same for columns, because , and each has a picture:
| Operation | Effect on | Picture |
|---|---|---|
| Swap two rows | multiplied by | the orientation flips |
| Multiply one row by | multiplied by | one side of the box is times longer |
| Add a multiple of one row to another | unchanged | a shear: area and volume are kept |
Try it on (determinant ): swapping rows 1 and 2 gives ; multiplying row 1 by gives ; adding times row 1 to row 3 leaves .
The third operation is the useful one, because it changes nothing. Elimination uses only this operation to make everything below the diagonal zero:
(first and , then ). A triangular matrix has the product of its diagonal as determinant (expand along the first column: only one entry is non-zero, and the same happens again in the smaller matrix). So , the same as before. If a pivot (a diagonal entry you need to divide by) is , swap in a row below that has a non-zero entry (if there is none, the determinant is ): each swap multiplies the determinant by , so you keep count. In general (the product of the pivots).
Cost. Expanding along a row of an matrix repeats the same trick on smaller matrices, so the number of terms grows like (the full expansion has exactly products). Elimination takes about steps, up to a constant factor.
| terms | about steps | |
|---|---|---|
| 3 | 6 | 27 |
| 10 | 3,628,800 | 1,000 |
| 20 | about | 8,000 |
At a billion terms per second, terms take about 77 years, while steps take a few microseconds. This is why software never expands. NumPy’s np.linalg.det computes an LU factorization (an organised form of elimination, with row swaps for safety) and multiplies the diagonal.
A small determinant is not “nearly singular”
It is tempting to read as “almost squashed, so dangerous”. That is wrong, because the determinant depends on scale. The matrix shrinks all coordinates by a factor of , so its determinant is , almost zero. Yet it is a perfectly good matrix: multiply by and you undo it.
Next, take and . Both have determinant . But just zooms out equally in both directions, while crushes the -direction to almost nothing and leaves the -direction alone, so it is very close to squashing the plane onto a line.
The measure that tells them apart is the condition number: the ratio of the largest stretch of the matrix to the smallest. It is for (all directions treated alike) and for . A huge condition number means that tiny errors in the input are blown up in the output and that rank decisions are fragile. (The stretches are the singular values of P1.B13; here only the idea is needed.) The reverse also happens: has determinant exactly , yet a condition number of about . So a determinant that is neither tiny nor huge does not make a matrix safe either.
To decide whether a matrix is “numerically singular”, use the rank with a tolerance (P1.B7), or the condition number, not the determinant.
The determinant in NumPy
np.linalg.det(A) returns the determinant of a square array. Run the first cell and compare each line with the numbers in this lesson.
Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).
What the output shows:
- , and is the same .
- The transpose keeps , the inverse gives , and gives .
- , which is not .
- The three row operations on give , and , as predicted.
- and : the same number.
The r helper rounds because NumPy prints, for example, 5.000000000000001 for . Floating-point arithmetic (P1.B7 met it with the rank tolerance) works with tiny rounding errors, and elimination divides and subtracts many times.
That matters most for singular matrices. In the second cell, the third row of the integer matrix is row 1 plus row 2, so on paper exactly.
Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).
What to expect:
- A singular matrix can give a tiny non-zero determinant. When this lesson was checked,
det Sprinted2.664535259100386e-15in the in-browser runner and3.552713678800511e-15in a desktop NumPy, not0. The entries are whole numbers, so the problem is not the input: the LU steps divide and subtract in floating point, and the last bits do not cancel perfectly. The two builds disagree, so yours may print yet another tiny number, or exactly0.0. The rank is , andabs(det) < 1e-9isTrue. So never testdet == 0. Use the rank (or compare with a tolerance that fits the size of the entries). slogdetreturns two numbers, the sign of the determinant and , so that . For the true determinant is , far smaller than a 64-bit float can hold (the smallest normal float is about ), sodetunderflows to0.0, which would wrongly say “singular”.slogdetreturns , which is . The opposite problem, overflow toinf, happens for very large determinants, andslogdetavoids that too. Because the log of a product is a sum of logs, it stays at a comfortable size. It returns sign and only when elimination meets an exact zero, as for . For above, which is singular on paper, rounding leaves a tiny pivot andslogdetgives sign and about , so use the rank or a tolerance there too.- and both have determinant but condition numbers and .
Where you will meet it next
Densities. A density is probability per unit area. Suppose is uniform on the unit square (density ) and , a map with . The points fill a square of area uniformly, so their density is . In symbols, . A map that stretches area by spreads the same probability over times the area. For a curved map the factor changes from point to point and is , the determinant of the Jacobian (P1.C7). A normalizing flow (P8.6) chains many invertible maps from the data to a simple base distribution, and the log-density of a data point is the base log-density plus the sum of of every step on that way. That is why flow layers are often designed so that is triangular, where the determinant is just the product of the diagonal (other designs keep it cheap in other ways).
The Gaussian. The density of a -dimensional Gaussian with covariance has a factor in front (P1.D7). For , and the height of the peak is : a wider spread, a lower peak. Its logarithm contains , which software computes with slogdet or a Cholesky factor (see the box below).
Go deeper What the determinant really is: three properties that fix everything
Think of as a function of the columns of a matrix. It has three properties:
- (the unit square has area 1).
- It is linear in each column separately: and , and the same for the second column.
- It is when two columns are equal (a flat parallelogram).
Exactly one function has these properties for each size , and it is the determinant ( when ). Everything else follows. From 3 and 2: , so swapping the columns flips the sign. And , so adding a multiple of one column to another changes nothing.
The product rule also follows. Fix and look at . The columns of are applied to the columns of , so is linear in each column of and is when two columns of are equal. Properties 2 and 3 therefore force .
Property 2 is about one column at a time, not the whole matrix. When you add two matrices you change both columns, and expands into four terms, so there is no simple rule for .
Go deeper The determinant and the eigenvalues (a preview)
P1.B12 develops this. An eigenvalue of is a number by which stretches some direction, and the determinant is the product of the eigenvalues. For the eigenvalues are and and . This makes sense: the matrix stretches one direction by and leaves another alone, so areas are multiplied by . The eigenvalues are also found from the condition : must squash some direction to .
Go deeper The log-determinant of a covariance matrix
A covariance matrix can be factored as with lower triangular (a Cholesky factorization). Then , because the determinant of a triangular matrix is the product of its diagonal and the transpose has the same determinant. Taking logs, . For , and , and both ways agree. This is a standard way to compute the log-determinant that appears in Gaussian log-densities.
Common mistakes
Check your understanding
What is ?
The determinant is . Adding the cross terms (14) or only multiplying the diagonal (12) is wrong, and swapping the order of the subtraction would give .
A matrix has determinant . It is applied to a triangle of area 2. What happens?
Areas are multiplied by , so . The minus sign means the plane is flipped over, so the triangle is mirrored. An area itself is never negative.
is a matrix with . What is ?
All three columns are doubled, and each doubling multiplies the volume by 2, so the factor is . Then . (4 is the mistake , and 8 forgets the original determinant.)
and . What is ?
The product rule gives : areas are multiplied by 4 and by 0.5, and exactly one of the two maps flips the plane. The determinant of a product is never found by adding or dividing the two determinants.
A square matrix has determinant 0. Which statement must be true?
Determinant 0 means the space is squashed flat: the columns are dependent, the rank is below and no inverse exists. The matrix does not have to be the zero matrix, a flip needs a negative determinant, and a squash sends every area to 0 rather than keeping it.
A matrix has determinant 5. After which single operation is the determinant of the new matrix still 5?
Adding a multiple of one row to another is a shear and keeps the determinant. Swapping two rows gives , and multiplying one row by 3 gives 15.
The matrix has determinant . Is it invertible?
The matrix shrinks every coordinate by a factor of 10, and undoes that. Only an exact determinant of 0 rules out an inverse, and the size of the determinant alone says nothing about conditioning: this matrix has condition number 1.
Exercises
- Compute . What is the area of the parallelogram with columns and ?
Show answer
. The area is , and the sign is positive, so the plane is not flipped. - For which are the columns and dependent?
Show answer
The determinant of the matrix with these columns is . It is for : then . - Find the determinant of the rotation by , , and of the swap . What does each sign tell you?
Show answer
for the rotation (areas kept, no flip), and for the swap (areas kept, the plane is flipped: it is a mirror in the line ). - Give a matrix that doubles every area and flips the plane.
Show answer
Any matrix with , for example (stretch by 2, mirror ): . Another is (). - Find .
Show answer
The matrix is triangular, so the determinant is the product of the diagonal: . - Compute by expanding along the first row, and then by elimination.
Show answer
Expansion: . Elimination: , , give the triangular matrix with diagonal , so . - A matrix has . Find , , and .
Show answer
. . (two columns are each multiplied by ). . - How does change volume? Is it invertible?
Show answer
: volumes shrink by that factor. It is invertible, because (the inverse is , with ). A small determinant does not make a matrix singular. - Show, using the columns, that .
Show answer
Column 3 is (column 2) (column 1): . So the weights on the three columns give : the columns are dependent, the rank is 2 and . - A map has . By what factor does applying it twice change areas, and what is the determinant of the matrix that does both steps?
Show answer
Areas are multiplied by and then by again: a factor of . The matrix of both steps is , and . - Code: predict the sign and approximate size of
np.linalg.det(np.array([[1, 2], [3, 4]])), then check.Show answer
, so about (NumPy may show ). The negative sign means the matrix flips the plane.