Settings

Theme
Lite Mode (saves mobile data)

In Lite Mode, animations and videos don't play until you tap them, and their size is shown first. Interactive widgets still work.

Text size

Back up or move your progress →

Part 1: Math FoundationsLinear Algebra

The determinant

P1.B840 min●●○ IntermediateReviewed Nov 30, 2026

Why this matters for machine learning

The determinant is the factor by which a square matrix stretches area or volume, with a sign for flipping and a zero for squashing, so it appears wherever a formula must account for stretched space.

  • The multivariate Gaussian density divides by (2π)ddet⁡Σ\sqrt{(2\pi)^d\det\boldsymbol{\Sigma}}: a wider covariance spreads the same probability over more volume. (P1.D7: Covariance and the multivariate Gaussian)
  • Changing variables in a density or an integral multiplies by ∣det⁡∣|\det| of the Jacobian, the local area-scaling factor of a curved map. (P1.C7: Vector functions and the Jacobian)
  • Normalizing flows add log⁡∣det⁡J∣\log|\det\mathbf{J}| for every layer to a log-density, which is why they are built from layers whose determinant is cheap to compute. (P8.6: Autoregressive image models and flows, coming soon)
  • det⁡=0\det=0 means redundant columns and no inverse, for example a feature that is the sum of two others. (P1.B9: Inverse and linear systems)
  • Linear regression has no single best weight vector when a feature is redundant, which is a zero determinant for X⊤X\mathbf{X}^\top\mathbf{X}. (P3.A2: Linear regression, coming soon)

Before this: Matrices as data and as transformations, Matrix multiplication, Span, independence, basis and rank

After this lesson you can

  • Read the determinant of a 2×22\times2 matrix as the signed factor by which the matrix scales areas, and compute it with ad−bcad-bc
  • Say what a negative determinant (a flip) and a zero determinant (a squash) mean, and link det⁡=0\det=0 to dependent columns, rank below nn and no inverse
  • Use det⁡(AB)=det⁡Adet⁡B\det(\mathbf{A}\mathbf{B})=\det\mathbf{A}\det\mathbf{B}, the rules for the transpose, the inverse and cAc\mathbf{A}, and the row-operation rules, and compute a 3×33\times3 determinant by the box volume, by expansion and by elimination
  • Use np.linalg.det and np.linalg.slogdet, explain why a singular matrix can give a tiny non-zero value, and say why a small determinant does not by itself mean “nearly singular”

In P1.B7 you met a one-line test for two vectors in the plane: a1b2−a2b1≠0a_1b_2-a_2b_1\ne0 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 2×22\times2 matrix told you where (1,0)(1,0) and (0,1)(0,1) land. So the unit square (the square with corners (0,0)(0,0), (1,0)(1,0), (1,1)(1,1), (0,1)(0,1), 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 ∣det⁡∣|\det|. Now we look at it properly.

Take the matrix from P1.B7, with columns (2,1)(2,1) and (−1,2)(-1,2):

A=(2−112)\mathbf{A}=\begin{pmatrix}2&-1\\1&2\end{pmatrix}

Its parallelogram has area 55 (the next sections show why). The unit square had area 1, so A\mathbf{A} multiplies that area by 5. The determinant of a 2×22\times2 matrix is exactly this stretch factor for area, with a sign that we meet below. It is written det⁡A\det\mathbf{A}, and for (abcd)\begin{pmatrix}a&b\\c&d\end{pmatrix} it is

det⁡(abcd)=ad−bc\det\begin{pmatrix}a&b\\c&d\end{pmatrix}=ad-bc

For A\mathbf{A}: 2⋅2−(−1)⋅1=4+1=52\cdot2-(-1)\cdot1=4+1=5. This is the test from P1.B7: with the two vectors as the columns, a1b2−a2b1a_1b_2-a_2b_1 is the determinant. The determinant is only defined for square matrices (as many rows as columns).

The widget draws the parallelogram for any 2×22\times2 matrix. It starts with the matrix above.

The determinant is the area of the warped unit square

−3−2−1123−3−2−1123col 1col 2
A = 2−1 12 col 1 = (2, 1), col 2 = (−1, 2)
det = A₁₁×A₂₂ − A₁₂×A₂₁ = 2×2 − (−1)×1 = 5
area of the warped unit square = |det| = 5 (the unit square had area 1)

Areas are multiplied by 5 (they grow), and the orientation is kept (det is positive): the F is not mirrored.

Grey outline: the original unit square and “F”. Filled shape: what the matrix turns them into, green when det is positive, red when it is negative, grey when it is 0. The arc runs from col 1 to col 2: counterclockwise when det is positive, clockwise when it is negative. Drag the blue and pink tips to edit the matrix. Entries are shown rounded to 3 decimals.

Determinant warp. Edit the four entries with the sliders, or drag the tips of column 1 (blue) and column 2 (pink). The unit square and the F become the filled shape. Green means the determinant is positive, red means it is negative, grey means it is 0. The readout shows ad − bc with the numbers filled in and the area |det|. Tick the box to warp a grid of 64 small squares too. The status line says whether areas grow or shrink, whether the plane is flipped, and whether it is squashed.

Things to try:

  1. At the start the shape is green and the area is 55, as computed above.
  2. Press Swap the columns. The parallelogram is the same size, but the determinant is −5-5, the shape turns red, the arc between the columns runs the other way and the F is mirrored.
  3. Press Rotate 30° and Shear. Both have determinant 11: the F turns or leans, but its area does not change.
  4. Press Stretch (columns (2,0)(2,0) and (0,1.5)(0,1.5)). The area is 2⋅1.5=32\cdot1.5=3.
  5. Press Reflect. The determinant is −1-1: the area is kept, but the plane is flipped.
  6. Press Squash (columns (1,2)(1,2) and (2,4)(2,4), the dependent pair from P1.B7). The shape is flat and the determinant is 00. Now use the slider for A11A_{11} to change 11 to 1.011.01. The determinant is 0.040.04, not 00, and the status says “nearly squashed”.
  7. 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 44, which is the determinant of (2002)\begin{pmatrix}2&0\\0&2\end{pmatrix}. 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 00.

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 11, 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 A\mathbf{A}, det⁡A\det\mathbf{A} is a single number. For 2×22\times2 it is ad−bcad-bc. You will also see it written ∣A∣|\mathbf{A}|, but that is easy to confuse with an absolute value, so this course writes det⁡A\det\mathbf{A} and writes ∣det⁡A∣|\det\mathbf{A}| for the area factor.

  • ∣det⁡A∣|\det\mathbf{A}| is the factor by which A\mathbf{A} multiplies areas (volumes in 3-D, and so on).
  • The sign of det⁡A\det\mathbf{A} says whether the space is flipped.
  • det⁡A=0\det\mathbf{A}=0 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 ad−bcad-bc?

Here is a short argument for the area, using the same A\mathbf{A}. 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 12\tfrac12 of column 1: (−1,2)+12(2,1)=(0, 2.5)(-1,2)+\tfrac12(2,1)=(0,\,2.5). 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 (2,1)(2,1) and (0,2.5)(0,2.5). Take the second as the base: it lies on the vertical axis and has length 2.52.5. The height is the horizontal distance from that axis to the opposite edge, which is the xx-entry of the first column, 22. Area =2.5×2=5=2.5\times2=5.

In general, with columns (a,c)(a,c) and (b,d)(b,d) and a≠0a\ne0:

(b,d)−ba(a,c)=(0, d−bca)area=∣a∣⋅∣d−bca∣=∣ad−bc∣\begin{aligned} (b,d)-\tfrac{b}{a}(a,c)&=\bigl(0,\ d-\tfrac{bc}{a}\bigr)\\ \text{area}&=|a|\cdot\bigl|d-\tfrac{bc}{a}\bigr|\\ &=|ad-bc| \end{aligned}

If a=0a=0 the columns are (0,c)(0,c) and (b,d)(b,d): the first lies on the vertical axis, the base is ∣c∣|c|, the height is ∣b∣|b|, and the area ∣bc∣|bc| is again ∣ad−bc∣|ad-bc|.

So the size of ad−bcad-bc is the area. What the sign tells you comes next.

The sign: did the plane flip?

Swap the two columns of A\mathbf{A} and you get

(−1221)\begin{pmatrix}-1&2\\2&1\end{pmatrix}

Its determinant is (−1)⋅1−2⋅2=−5(-1)\cdot1-2\cdot2=-5.

Both matrices make the same parallelogram, so the area is 55 for both. The difference is orientation. In the identity the second column (0,1)(0,1) lies a quarter turn counterclockwise from the first column (1,0)(1,0). 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: det⁡=+1\det=+1.
  • A reflection, such as (−1001)\begin{pmatrix}-1&0\\0&1\end{pmatrix}, reverses it: det⁡=−1\det=-1.
  • Swapping two columns (or two rows) always changes the sign.

The area itself is never negative. The area factor is ∣det⁡∣|\det|, 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 (1,2)(1,2) and (2,4)(2,4) from P1.B7. The parallelogram is flat: it has no width, so its area is 1⋅4−2⋅2=01\cdot4-2\cdot2=0. Every point of the plane lands on the line through (1,2)(1,2), so the whole plane is squashed onto a line. The same happens with the projection of P1.B5, (0.50.50.50.5)\begin{pmatrix}0.5&0.5\\0.5&0.5\end{pmatrix}: det⁡=0.25−0.25=0\det=0.25-0.25=0. If one column is (0,0)(0,0), or both are, the parallelogram is flat too (a line segment, or a single point), so the determinant is 00.

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 2×22\times2 matrix the inverse has a preview worth seeing now:

(abcd)−1=1ad−bc(d−b−ca)\begin{pmatrix}a&b\\c&d\end{pmatrix}^{-1}=\frac{1}{ad-bc}\begin{pmatrix}d&-b\\-c&a\end{pmatrix}

For our A\mathbf{A} this gives 15(21−12)\tfrac15\begin{pmatrix}2&1\\-1&2\end{pmatrix}. If ad−bc=0ad-bc=0 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 nn” 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 det⁡≠0\det\ne0. 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 0.50.5, and each one lands on a parallelogram with the same two sides, A(0.50)\mathbf{A}\binom{0.5}{0} and A(00.5)\mathbf{A}\binom{0}{0.5} (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 ∣det⁡∣|\det|: from 0.250.25 to 0.25⋅∣det⁡∣0.25\cdot|\det|.

Any region is made of many tiny squares, so any region has its area multiplied by ∣det⁡∣|\det|. A triangle of area 22 becomes one of area 2 ∣det⁡∣2\,|\det|. The unit circle (area π\pi) becomes an ellipse of area π ∣det⁡A∣\pi\,|\det\mathbf{A}|; for our A\mathbf{A} that is 5π≈15.715\pi\approx15.71. 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

Matrixdet⁡\detWhy
Identity11nothing moves
Diagonal (d100d2)\begin{pmatrix}d_1&0\\0&d_2\end{pmatrix}, for example the scale of P1.B5, (2000.5)\begin{pmatrix}2&0\\0&0.5\end{pmatrix}d1d2d_1d_2 (=1=1 for the example)the square becomes a rectangle with sides d1d_1 and d2d_2
Triangular (2103)\begin{pmatrix}2&1\\0&3\end{pmatrix}2⋅3=62\cdot3=6: the product of the diagonalad−b⋅0ad-b\cdot0
Rotation by an angle θ\thetacos⁡2θ+sin⁡2θ=1\cos^2\theta+\sin^2\theta=1areas are kept, the F only turns
Reflection (−1001)\begin{pmatrix}-1&0\\0&1\end{pmatrix} or the swap (0110)\begin{pmatrix}0&1\\1&0\end{pmatrix}−1-1areas are kept, the plane is flipped
Shear (1101)\begin{pmatrix}1&1\\0&1\end{pmatrix}11a slide keeps the area
Projection (0.50.50.50.5)\begin{pmatrix}0.5&0.5\\0.5&0.5\end{pmatrix}00the plane is squashed onto a line
Scaling every coordinate by cc in nn dimensionscnc^nc Ic\,\mathbf{I} is diagonal with nn entries equal to cc

For example, 2 I32\,\mathbf{I}_3 multiplies volume by 23=82^3=8, and 0.5 I100.5\,\mathbf{I}_{10} multiplies it by 2−10=1/10242^{-10}=1/1024. 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 +1+1 or −1-1.

Note that det⁡=1\det=1 does not mean “nothing changes”: rotations and shears have determinant 11 and move everything. It only means the area is kept.

Rules: products, transposes, inverses

Products. Apply B\mathbf{B} first and A\mathbf{A} second. B\mathbf{B} multiplies areas by ∣det⁡B∣|\det\mathbf{B}|, and then A\mathbf{A} multiplies the new areas by ∣det⁡A∣|\det\mathbf{A}|. P1.B6 promised the result:

det⁡(AB)=det⁡A⋅det⁡B\det(\mathbf{A}\mathbf{B})=\det\mathbf{A}\cdot\det\mathbf{B}

Flips combine too: two flips cancel, because a negative times a negative is positive. Example with A\mathbf{A} above and B=(2013)\mathbf{B}=\begin{pmatrix}2&0\\1&3\end{pmatrix} (columns (2,1)(2,1) and (0,3)(0,3), det⁡B=6\det\mathbf{B}=6):

AB=(3−346)det⁡(AB)=3⋅6−(−3)⋅4=30=5⋅6\begin{aligned} \mathbf{A}\mathbf{B}&=\begin{pmatrix}3&-3\\4&6\end{pmatrix} \\ \det(\mathbf{A}\mathbf{B})&=3\cdot6-(-3)\cdot4 \\ &=30=5\cdot6 \end{aligned}

The widget below shows it: the unit square after B\mathbf{B} has area 66, and after A\mathbf{A} as well it has area 5⋅6=305\cdot6=30.

Two maps in a row: how do the areas combine?

−10−5510−10−5510after ABafter B
B = 2013
A = 2−112
AB = 3−346
det B = 6: area after B = 6
det A = 5
det A × det B = 5 × 6 = 30
det(AB) from the entries of AB = 30: area after AB = 30

B multiplies areas by 6 and then A multiplies them by 5, so AB multiplies them by 5 × 6 = 30. Neither map flips the plane, so AB does not flip it either (det positive).

The grey outline is the starting unit square and “F”. The pink shape is where B alone puts them (halfway), the blue shape is where B and then A put them. The last two numbers are always equal. Entries are shown rounded to 3 decimals.

Determinant of a product. B is applied first (pink shape, halfway), then A (blue shape). The readouts show det B, det A, their product, and the determinant computed directly from the entries of AB: the last two always agree. The status line says how the areas multiply and how the flips combine, including when one of the maps squashes the plane.

Things to try:

  1. Leave the defaults. The areas are 66 and then 3030, and the two bottom numbers are equal.
  2. Choose Mirror in the y-axis for B\mathbf{B} and for A\mathbf{A}. Each flips the plane, the two flips cancel and det⁡(AB)=+1\det(\mathbf{A}\mathbf{B})=+1.
  3. Choose Mirror in the y-axis for only one of them: the result is negative.
  4. Choose Project onto the x-axis for either map: the product is flat, with det⁡=0\det=0, whatever the other map is.
  5. Press Swap A and B. The matrix AB\mathbf{A}\mathbf{B} changes (P1.B6: the order matters) but its determinant does not, because det⁡A⋅det⁡B=det⁡B⋅det⁡A\det\mathbf{A}\cdot\det\mathbf{B}=\det\mathbf{B}\cdot\det\mathbf{A}. So det⁡(AB)=det⁡(BA)\det(\mathbf{A}\mathbf{B})=\det(\mathbf{B}\mathbf{A}) even when AB≠BA\mathbf{A}\mathbf{B}\ne\mathbf{B}\mathbf{A}. Applying one matrix kk times gives det⁡(Ak)=(det⁡A)k\det(\mathbf{A}^k)=(\det\mathbf{A})^k.

Transpose. det⁡A⊤=det⁡A\det\mathbf{A}^\top=\det\mathbf{A}. Swapping the rows and columns does not change the determinant, so the rows make a parallelogram of the same area as the columns. Check: A⊤=(21−12)\mathbf{A}^\top=\begin{pmatrix}2&1\\-1&2\end{pmatrix} has det⁡=4−(−1)=5\det=4-(-1)=5. (For 2×22\times2 it is clear from ad−bcad-bc, because bb and cc swap places in a product. For larger matrices it is a standard result.)

Inverse. det⁡A−1=1/det⁡A\det\mathbf{A}^{-1}=1/\det\mathbf{A}. The inverse undoes A\mathbf{A}, so it must divide the area factor. Algebraically, AA−1=I\mathbf{A}\mathbf{A}^{-1}=\mathbf{I} and the product rule give det⁡A⋅det⁡A−1=det⁡I=1\det\mathbf{A}\cdot\det\mathbf{A}^{-1}=\det\mathbf{I}=1. For our A\mathbf{A}, det⁡A−1=1/5\det\mathbf{A}^{-1}=1/5. This also shows why det⁡=0\det=0 cannot have an inverse: no number times 00 equals 11.

Scaling the whole matrix. For an n×nn\times n matrix, det⁡(c A)=cndet⁡A\det(c\,\mathbf{A})=c^n\det\mathbf{A}, not cdet⁡Ac\det\mathbf{A}. Every one of the nn columns is multiplied by cc, and each one multiplies the volume by cc. Check: 3A=(6−336)3\mathbf{A}=\begin{pmatrix}6&-3\\3&6\end{pmatrix} has det⁡=36+9=45=32⋅5\det=36+9=45=3^2\cdot5.

Sums. There is no rule for det⁡(A+B)\det(\mathbf{A}+\mathbf{B}), and it is not det⁡A+det⁡B\det\mathbf{A}+\det\mathbf{B}. With A=B=I\mathbf{A}=\mathbf{B}=\mathbf{I}: det⁡(I+I)=det⁡(2I)=4\det(\mathbf{I}+\mathbf{I})=\det(2\mathbf{I})=4, but 1+1=21+1=2. With A=(1000)\mathbf{A}=\begin{pmatrix}1&0\\0&0\end{pmatrix} and B=(0001)\mathbf{B}=\begin{pmatrix}0&0\\0&1\end{pmatrix}: both determinants are 00, yet A+B=I\mathbf{A}+\mathbf{B}=\mathbf{I} has determinant 11.

Three dimensions: the volume of a box

In 3-D a matrix with columns a\mathbf{a}, b\mathbf{b}, c\mathbf{c} turns the unit cube into a slanted box (a parallelepiped) with those three edges. Its volume is ∣det⁡∣|\det|, and the sign again says whether the space is flipped. This is the box in the three-vector widget of P1.B7. There, with a=(1,0,1)\mathbf{a}=(1,0,1), b=(0,1,1)\mathbf{b}=(0,1,1) and c=(1,1,1)\mathbf{c}=(1,1,1), the box had volume 11; and with c=(1,1,2)=a+b\mathbf{c}=(1,1,2)=\mathbf{a}+\mathbf{b} it went flat, volume 00.

The triple product. The cross product of two 3-D vectors is another 3-D vector:

b×c=(b2c3−b3c2,b3c1−b1c3,b1c2−b2c1)\begin{aligned} \mathbf{b}\times\mathbf{c}=\bigl(&b_2c_3-b_3c_2,\\ &b_3c_1-b_1c_3,\\ &b_1c_2-b_2c_1\bigr) \end{aligned}

It points perpendicular to both, and its length is the area of the parallelogram that b\mathbf{b} and c\mathbf{c} make. Dotting with a\mathbf{a} gives (base area) times (height along the perpendicular), the signed volume:

det⁡(abc)=a⋅(b×c)\det\begin{pmatrix}\mathbf{a}&\mathbf{b}&\mathbf{c}\end{pmatrix}=\mathbf{a}\cdot(\mathbf{b}\times\mathbf{c})

For the example, b×c=(1⋅1−1⋅1, 1⋅1−0⋅1, 0⋅1−1⋅1)=(0,1,−1)\mathbf{b}\times\mathbf{c}=(1\cdot1-1\cdot1,\ 1\cdot1-0\cdot1,\ 0\cdot1-1\cdot1)=(0,1,-1) and a⋅(0,1,−1)=−1\mathbf{a}\cdot(0,1,-1)=-1. The volume is ∣−1∣=1|-1|=1, 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 c=(1,1,2)\mathbf{c}=(1,1,2) the triple product is 00.

Expanding along a row. For a 3×33\times3 matrix, take each entry of the first row, multiply it by the determinant of the 2×22\times2 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

M=(211433879)\mathbf{M}=\begin{pmatrix}2&1&1\\4&3&3\\8&7&9\end{pmatrix} det⁡M=2 (3⋅9−3⋅7)−1 (4⋅9−3⋅8)+1 (4⋅7−3⋅8)=2⋅6−1⋅12+1⋅4=4\begin{aligned} \det\mathbf{M}&=2\,(3\cdot9-3\cdot7)\\ &\quad-1\,(4\cdot9-3\cdot8)\\ &\quad+1\,(4\cdot7-3\cdot8)\\ &=2\cdot6-1\cdot12+1\cdot4=4 \end{aligned}

(The triple product is this same expansion for the matrix with rows a,b,c\mathbf{a},\mathbf{b},\mathbf{c}, 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 3×33\times3 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 M\mathbf{M}: (2⋅3⋅9+1⋅3⋅8+1⋅4⋅7)−(1⋅3⋅8+2⋅3⋅7+1⋅4⋅9)=106−102=4(2\cdot3\cdot9+1\cdot3\cdot8+1\cdot4\cdot7)-(1\cdot3\cdot8+2\cdot3\cdot7+1\cdot4\cdot9)=106-102=4.

Warning: Sarrus’s rule does not work for 4×44\times4 or larger. A 4×44\times4 determinant has 2424 terms (that is 4!4!), and a pattern of diagonals gives only 88. For example, swapping two rows of I4\mathbf{I}_4 gives determinant −1-1, but extending the diagonal pattern to 4×44\times4 gives 00. 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 det⁡A⊤=det⁡A\det\mathbf{A}^\top=\det\mathbf{A}, and each has a picture:

OperationEffect on det⁡\detPicture
Swap two rowsmultiplied by −1-1the orientation flips
Multiply one row by kkmultiplied by kkone side of the box is kk times longer
Add a multiple of one row to anotherunchangeda shear: area and volume are kept

Try it on M\mathbf{M} (determinant 44): swapping rows 1 and 2 gives −4-4; multiplying row 1 by 33 gives 1212; adding 55 times row 1 to row 3 leaves 44.

The third operation is the useful one, because it changes nothing. Elimination uses only this operation to make everything below the diagonal zero:

(211433879)→(211011035)→(211011002)\begin{aligned} \begin{pmatrix}2&1&1\\4&3&3\\8&7&9\end{pmatrix}&\to \begin{pmatrix}2&1&1\\0&1&1\\0&3&5\end{pmatrix}\\ &\to\begin{pmatrix}2&1&1\\0&1&1\\0&0&2\end{pmatrix} \end{aligned}

(first R2−2R1R_2-2R_1 and R3−4R1R_3-4R_1, then R3−3R2R_3-3R_2). 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 det⁡M=2⋅1⋅2=4\det\mathbf{M}=2\cdot1\cdot2=4, the same as before. If a pivot (a diagonal entry you need to divide by) is 00, swap in a row below that has a non-zero entry (if there is none, the determinant is 00): each swap multiplies the determinant by −1-1, so you keep count. In general det⁡=±\det=\pm (the product of the pivots).

Cost. Expanding along a row of an n×nn\times n matrix repeats the same trick on nn smaller matrices, so the number of terms grows like n!n! (the full expansion has exactly n!n! products). Elimination takes about n3n^3 steps, up to a constant factor.

nnn!n! termsabout n3n^3 steps
3627
103,628,8001,000
20about 2.4×10182.4\times10^{18}8,000

At a billion terms per second, 20!20! terms take about 77 years, while 20320^3 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 det⁡≈0\det\approx0 as “almost squashed, so dangerous”. That is wrong, because the determinant depends on scale. The matrix 0.1 I1000.1\,\mathbf{I}_{100} shrinks all 100100 coordinates by a factor of 1010, so its determinant is 0.1100=10−1000.1^{100}=10^{-100}, almost zero. Yet it is a perfectly good matrix: multiply by 1010 and you undo it.

Next, take X=(10010−10)\mathbf{X}=\begin{pmatrix}1&0\\0&10^{-10}\end{pmatrix} and Y=(10−50010−5)\mathbf{Y}=\begin{pmatrix}10^{-5}&0\\0&10^{-5}\end{pmatrix}. Both have determinant 10−1010^{-10}. But Y\mathbf{Y} just zooms out equally in both directions, while X\mathbf{X} crushes the yy-direction to almost nothing and leaves the xx-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 11 for Y\mathbf{Y} (all directions treated alike) and 101010^{10} for X\mathbf{X}. 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: (110601)\begin{pmatrix}1&10^6\\0&1\end{pmatrix} has determinant exactly 11, yet a condition number of about 101210^{12}. 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.

Try it: the rules of the determinant

Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).

What the output shows:

  • det⁡(AB)=30=5⋅6\det(\mathbf{A}\mathbf{B})=30=5\cdot6, and det⁡(BA)\det(\mathbf{B}\mathbf{A}) is the same 3030.
  • The transpose keeps 55, the inverse gives 0.2=1/50.2=1/5, and 3A3\mathbf{A} gives 45=32⋅545=3^2\cdot5.
  • det⁡(A+B)=22\det(\mathbf{A}+\mathbf{B})=22, which is not 5+6=115+6=11.
  • The three row operations on M\mathbf{M} give −4-4, 1212 and 44, as predicted.
  • a⋅(b×c)=−1\mathbf{a}\cdot(\mathbf{b}\times\mathbf{c})=-1 and det⁡C=−1\det\mathbf{C}=-1: the same number.

The r helper rounds because NumPy prints, for example, 5.000000000000001 for det⁡A\det\mathbf{A}. 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 S\mathbf{S} is row 1 plus row 2, so on paper det⁡S=0\det\mathbf{S}=0 exactly.

Try it: singular matrices, slogdet and conditioning

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 S printed 2.664535259100386e-15 in the in-browser runner and 3.552713678800511e-15 in a desktop NumPy, not 0. 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 exactly 0.0. The rank is 22, and abs(det) < 1e-9 is True. So never test det == 0. Use the rank (or compare with a tolerance that fits the size of the entries).
  • slogdet returns two numbers, the sign of the determinant and log⁡∣det⁡∣\log|\det|, so that det⁡=sign⋅elogabsdet\det=\text{sign}\cdot e^{\text{logabsdet}}. For 0.1 I10000.1\,\mathbf{I}_{1000} the true determinant is 10−100010^{-1000}, far smaller than a 64-bit float can hold (the smallest normal float is about 2×10−3082\times10^{-308}), so det underflows to 0.0, which would wrongly say “singular”. slogdet returns −2302.585-2302.585, which is 1000ln⁡0.11000\ln0.1. The opposite problem, overflow to inf, happens for very large determinants, and slogdet avoids that too. Because the log of a product is a sum of logs, it stays at a comfortable size. It returns sign 00 and log⁡∣det⁡∣=−∞\log|\det|=-\infty only when elimination meets an exact zero, as for (1224)\begin{pmatrix}1&2\\2&4\end{pmatrix}. For S\mathbf{S} above, which is singular on paper, rounding leaves a tiny pivot and slogdet gives sign 11 and about −33-33, so use the rank or a tolerance there too.
  • X\mathbf{X} and Y\mathbf{Y} both have determinant 10−1010^{-10} but condition numbers 101010^{10} and 11.

Where you will meet it next

Densities. A density is probability per unit area. Suppose x\mathbf{x} is uniform on the unit square (density 11) and z=2x\mathbf{z}=2\mathbf{x}, a map with det⁡=4\det=4. The points z\mathbf{z} fill a square of area 44 uniformly, so their density is 14\tfrac14. In symbols, pX(x)=pZ(Ax) ∣det⁡A∣=14⋅4=1p_X(\mathbf{x})=p_Z(\mathbf{A}\mathbf{x})\,|\det\mathbf{A}|=\tfrac14\cdot4=1. A map that stretches area by kk spreads the same probability over kk times the area. For a curved map the factor changes from point to point and is ∣det⁡J∣|\det\mathbf{J}|, 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 log⁡∣det⁡J∣\log|\det\mathbf{J}| of every step on that way. That is why flow layers are often designed so that J\mathbf{J} 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 dd-dimensional Gaussian with covariance Σ\boldsymbol{\Sigma} has a factor 1/(2π)ddet⁡Σ1/\sqrt{(2\pi)^d\det\boldsymbol{\Sigma}} in front (P1.D7). For Σ=(4001)\boldsymbol{\Sigma}=\begin{pmatrix}4&0\\0&1\end{pmatrix}, det⁡Σ=4\det\boldsymbol{\Sigma}=4 and the height of the peak is 1/(2π4)=1/(4π)≈0.07961/(2\pi\sqrt4)=1/(4\pi)\approx0.0796: a wider spread, a lower peak. Its logarithm contains −12log⁡det⁡Σ-\tfrac12\log\det\boldsymbol{\Sigma}, 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 det⁡\det as a function of the columns u,v\mathbf{u},\mathbf{v} of a 2×22\times2 matrix. It has three properties:

  1. det⁡I=1\det\mathbf{I}=1 (the unit square has area 1).
  2. It is linear in each column separately: det⁡(ku,v)=kdet⁡(u,v)\det(k\mathbf{u},\mathbf{v})=k\det(\mathbf{u},\mathbf{v}) and det⁡(u+u′,v)=det⁡(u,v)+det⁡(u′,v)\det(\mathbf{u}+\mathbf{u}',\mathbf{v})=\det(\mathbf{u},\mathbf{v})+\det(\mathbf{u}',\mathbf{v}), and the same for the second column.
  3. It is 00 when two columns are equal (a flat parallelogram).

Exactly one function has these properties for each size nn, and it is the determinant (ad−bcad-bc when n=2n=2). Everything else follows. From 3 and 2: 0=det⁡(u+v,u+v)=det⁡(u,v)+det⁡(v,u)0=\det(\mathbf{u}+\mathbf{v},\mathbf{u}+\mathbf{v})=\det(\mathbf{u},\mathbf{v})+\det(\mathbf{v},\mathbf{u}), so swapping the columns flips the sign. And det⁡(u,v+ku)=det⁡(u,v)+kdet⁡(u,u)=det⁡(u,v)\det(\mathbf{u},\mathbf{v}+k\mathbf{u})=\det(\mathbf{u},\mathbf{v})+k\det(\mathbf{u},\mathbf{u})=\det(\mathbf{u},\mathbf{v}), so adding a multiple of one column to another changes nothing.

The product rule also follows. Fix A\mathbf{A} and look at f(B)=det⁡(AB)f(\mathbf{B})=\det(\mathbf{A}\mathbf{B}). The columns of AB\mathbf{A}\mathbf{B} are A\mathbf{A} applied to the columns of B\mathbf{B}, so ff is linear in each column of B\mathbf{B} and is 00 when two columns of B\mathbf{B} are equal. Properties 2 and 3 therefore force f(B)=f(I)det⁡B=det⁡A⋅det⁡Bf(\mathbf{B})=f(\mathbf{I})\det\mathbf{B}=\det\mathbf{A}\cdot\det\mathbf{B}.

Property 2 is about one column at a time, not the whole matrix. When you add two matrices you change both columns, and det⁡(u+u′,v+v′)\det(\mathbf{u}+\mathbf{u}',\mathbf{v}+\mathbf{v}') expands into four terms, so there is no simple rule for det⁡(A+B)\det(\mathbf{A}+\mathbf{B}).

Go deeper The determinant and the eigenvalues (a preview)

P1.B12 develops this. An eigenvalue of A\mathbf{A} is a number λ\lambda by which A\mathbf{A} stretches some direction, and the determinant is the product of the eigenvalues. For (2112)\begin{pmatrix}2&1\\1&2\end{pmatrix} the eigenvalues are 33 and 11 and det⁡=4−1=3=3⋅1\det=4-1=3=3\cdot1. This makes sense: the matrix stretches one direction by 33 and leaves another alone, so areas are multiplied by 3⋅13\cdot1. The eigenvalues are also found from the condition det⁡(A−λI)=0\det(\mathbf{A}-\lambda\mathbf{I})=0: A−λI\mathbf{A}-\lambda\mathbf{I} must squash some direction to 0\mathbf{0}.

Go deeper The log-determinant of a covariance matrix

A covariance matrix Σ\boldsymbol{\Sigma} can be factored as Σ=LL⊤\boldsymbol{\Sigma}=\mathbf{L}\mathbf{L}^\top with L\mathbf{L} lower triangular (a Cholesky factorization). Then det⁡Σ=det⁡L⋅det⁡L⊤=(∏iLii)2\det\boldsymbol{\Sigma}=\det\mathbf{L}\cdot\det\mathbf{L}^\top=(\prod_iL_{ii})^2, because the determinant of a triangular matrix is the product of its diagonal and the transpose has the same determinant. Taking logs, log⁡det⁡Σ=2∑ilog⁡Lii\log\det\boldsymbol{\Sigma}=2\sum_i\log L_{ii}. For Σ=(4223)\boldsymbol{\Sigma}=\begin{pmatrix}4&2\\2&3\end{pmatrix}, det⁡=8\det=8 and ln⁡8≈2.0794\ln8\approx2.0794, 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

  1. What is det⁡(3124)\det\begin{pmatrix}3&1\\2&4\end{pmatrix}?

  2. A 2×22\times2 matrix has determinant −3-3. It is applied to a triangle of area 2. What happens?

  3. A\mathbf{A} is a 3×33\times3 matrix with det⁡A=2\det\mathbf{A}=2. What is det⁡(2A)\det(2\mathbf{A})?

  4. det⁡A=4\det\mathbf{A}=4 and det⁡B=−0.5\det\mathbf{B}=-0.5. What is det⁡(AB)\det(\mathbf{A}\mathbf{B})?

  5. A square matrix has determinant 0. Which statement must be true?

  6. A matrix has determinant 5. After which single operation is the determinant of the new matrix still 5?

  7. The 100×100100\times100 matrix 0.1 I0.1\,\mathbf{I} has determinant 10−10010^{-100}. Is it invertible?

Exercises

  1. Compute det⁡(4−235)\det\begin{pmatrix}4&-2\\3&5\end{pmatrix}. What is the area of the parallelogram with columns (4,3)(4,3) and (−2,5)(-2,5)?
    Show answerdet⁡=4⋅5−(−2)⋅3=20+6=26\det=4\cdot5-(-2)\cdot3=20+6=26. The area is ∣26∣=26|26|=26, and the sign is positive, so the plane is not flipped.
  2. For which kk are the columns (1,2)(1,2) and (3,k)(3,k) dependent?
    Show answerThe determinant of the matrix with these columns is 1⋅k−3⋅2=k−61\cdot k-3\cdot2=k-6. It is 00 for k=6k=6: then (3,6)=3 (1,2)(3,6)=3\,(1,2).
  3. Find the determinant of the rotation by 90∘90^\circ, (0−110)\begin{pmatrix}0&-1\\1&0\end{pmatrix}, and of the swap (0110)\begin{pmatrix}0&1\\1&0\end{pmatrix}. What does each sign tell you?
    Show answer0⋅0−(−1)⋅1=10\cdot0-(-1)\cdot1=1 for the rotation (areas kept, no flip), and 0⋅0−1⋅1=−10\cdot0-1\cdot1=-1 for the swap (areas kept, the plane is flipped: it is a mirror in the line y=xy=x).
  4. Give a 2×22\times2 matrix that doubles every area and flips the plane.
    Show answerAny matrix with det⁡=−2\det=-2, for example (200−1)\begin{pmatrix}2&0\\0&-1\end{pmatrix} (stretch xx by 2, mirror yy): det⁡=2⋅(−1)−0=−2\det=2\cdot(-1)-0=-2. Another is (0210)\begin{pmatrix}0&2\\1&0\end{pmatrix} (det⁡=0⋅0−2⋅1=−2\det=0\cdot0-2\cdot1=-2).
  5. Find det⁡(25103700−4)\det\begin{pmatrix}2&5&1\\0&3&7\\0&0&-4\end{pmatrix}.
    Show answerThe matrix is triangular, so the determinant is the product of the diagonal: 2⋅3⋅(−4)=−242\cdot3\cdot(-4)=-24.
  6. Compute det⁡(211433879)\det\begin{pmatrix}2&1&1\\4&3&3\\8&7&9\end{pmatrix} by expanding along the first row, and then by elimination.
    Show answerExpansion: 2(27−21)−1(36−24)+1(28−24)=12−12+4=42(27-21)-1(36-24)+1(28-24)=12-12+4=4. Elimination: R2−2R1R_2-2R_1, R3−4R1R_3-4R_1, R3−3R2R_3-3R_2 give the triangular matrix with diagonal 2,1,22,1,2, so det⁡=4\det=4.
  7. A 2×22\times2 matrix A\mathbf{A} has det⁡A=5\det\mathbf{A}=5. Find det⁡(A3)\det(\mathbf{A}^3), det⁡(A−1)\det(\mathbf{A}^{-1}), det⁡(−A)\det(-\mathbf{A}) and det⁡(A⊤A)\det(\mathbf{A}^\top\mathbf{A}).
    Show answerdet⁡(A3)=53=125\det(\mathbf{A}^3)=5^3=125. det⁡(A−1)=1/5=0.2\det(\mathbf{A}^{-1})=1/5=0.2. det⁡(−A)=(−1)2⋅5=5\det(-\mathbf{A})=(-1)^2\cdot5=5 (two columns are each multiplied by −1-1). det⁡(A⊤A)=5⋅5=25\det(\mathbf{A}^\top\mathbf{A})=5\cdot5=25.
  8. How does 0.5 I100.5\,\mathbf{I}_{10} change volume? Is it invertible?
    Show answerdet⁡=0.510=1/1024≈0.00098\det=0.5^{10}=1/1024\approx0.00098: volumes shrink by that factor. It is invertible, because det⁡≠0\det\ne0 (the inverse is 2 I102\,\mathbf{I}_{10}, with det⁡=1024\det=1024). A small determinant does not make a matrix singular.
  9. Show, using the columns, that det⁡(123456789)=0\det\begin{pmatrix}1&2&3\\4&5&6\\7&8&9\end{pmatrix}=0.
    Show answerColumn 3 is 2⋅2\cdot(column 2) −- (column 1): (2⋅2−1, 2⋅5−4, 2⋅8−7)=(3,6,9)(2\cdot2-1,\ 2\cdot5-4,\ 2\cdot8-7)=(3,6,9). So the weights −1,2,−1-1,2,-1 on the three columns give 0\mathbf{0}: the columns are dependent, the rank is 2 and det⁡=0\det=0.
  10. A map has det⁡=3\det=3. By what factor does applying it twice change areas, and what is the determinant of the matrix that does both steps?
    Show answerAreas are multiplied by 33 and then by 33 again: a factor of 99. The matrix of both steps is A2\mathbf{A}^2, and det⁡(A2)=(det⁡A)2=9\det(\mathbf{A}^2)=(\det\mathbf{A})^2=9.
  11. Code: predict the sign and approximate size of np.linalg.det(np.array([[1, 2], [3, 4]])), then check.
    Show answer1⋅4−2⋅3=−21\cdot4-2\cdot3=-2, so about −2.0-2.0 (NumPy may show −2.0000000000000004-2.0000000000000004). The negative sign means the matrix flips the plane.

Key takeaways

  • The determinant of a square matrix is the factor by which it scales area (in 2-D) or volume (in 3-D and beyond), with a sign. For (abcd)\begin{pmatrix}a&b\\c&d\end{pmatrix} it is ad−bcad-bc, the signed area of the parallelogram made by the columns.
  • A negative determinant means the matrix flips the space over, like a mirror, so an F comes out mirrored. The area scale factor is ∣det⁡∣|\det|, which is never negative.
  • det⁡=0\det=0 means the space is squashed flat: the columns are dependent, the rank is below nn, many inputs share one output and the matrix has no inverse. For a square matrix, full rank is the same as det⁡≠0\det\ne0.
  • Every region, not only the unit square, has its area multiplied by ∣det⁡∣|\det|. The product rule det⁡(AB)=det⁡A⋅det⁡B\det(\mathbf{A}\mathbf{B})=\det\mathbf{A}\cdot\det\mathbf{B} says areas scale one after the other. Also det⁡A⊤=det⁡A\det\mathbf{A}^\top=\det\mathbf{A} and det⁡A−1=1/det⁡A\det\mathbf{A}^{-1}=1/\det\mathbf{A}.
  • Traps: det⁡(cA)=cndet⁡A\det(c\mathbf{A})=c^n\det\mathbf{A} for an n×nn\times n matrix (not cdet⁡Ac\det\mathbf{A}), and det⁡(A+B)\det(\mathbf{A}+\mathbf{B}) is not det⁡A+det⁡B\det\mathbf{A}+\det\mathbf{B}. Triangular and diagonal matrices have the product of the diagonal as determinant.
  • Swapping two rows flips the sign, scaling a row by kk multiplies the determinant by kk, and adding a multiple of one row to another changes nothing. Computers eliminate to a triangular matrix (about n3n^3 steps) instead of expanding (about n!n! terms).
  • A small determinant does not mean a matrix is nearly singular (scale matters), and a large one does not make it safe: the condition number measures that. np.linalg.slogdet returns the sign and log⁡∣det⁡∣\log|\det|, which avoids underflow and overflow.
Found a mistake? Tell us