Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 62 · Determinants and Orthogonality

Determinants

Understand the determinant as an area and volume scaling factor, learn its sign and properties, compute it by hand and with NumPy, and avoid underflow with slogdet.

Every square matrix carries a single number that summarizes how its transformation changes space: the determinant. If a matrix maps the plane to the plane, its determinant tells you by what factor areas grow or shrink. In three dimensions it does the same for volumes. A determinant of zero is a warning sign, because it means the transformation squashes space into something flatter and the matrix has no inverse.

This lesson starts with the geometric picture, then covers the sign, the algebraic rules that let you compute determinants quickly, and the numerical traps that come with np.linalg.det.

What the determinant tells you

Size How much area or volume is scaled.
Sign Negative means orientation flips.
Zero Space collapses, no inverse.
1 / 2

The area picture

Take the unit square with corners (0,0), (1,0), (1,1), (0,1). Its area is 1. Push the four corners through a matrix A. The result is a parallelogram whose sides are the two columns of A, and its area is |det A|.

For a 2 by 2 matrix the determinant is

TEXT
det [[a, b],
     [c, d]] = a·d − b·c

We can check this against a completely independent area computation, the shoelace formula, which adds up cross terms of consecutive corners.

Python The determinant is the area scale factor

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The scaling matrix multiplies the area by 6, the shear keeps it at 1 (it slides rows sideways without changing the base or height), and the general matrix gives 5. The last matrix has dependent columns, so the square collapses onto a line and the area is 0.

The sign: orientation

The determinant can be negative. The size |det A| is still the scale factor, and the sign records whether the transformation preserves or reverses orientation. A reflection swaps left and right, like a mirror, and has determinant −1. A rotation has determinant +1.

Python Rotation, reflection and a collapse

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In 3D the same story holds with volume: |det| is the volume of the parallelepiped spanned by the columns, and a negative sign means the right-handed system became left-handed. See The Cross Product for the 3D volume-from-three-vectors connection.

Computing determinants

2 by 2: ad − bc.

3 by 3: expand along the first row. Each entry is multiplied by the determinant of the smaller matrix left after deleting its row and column (its minor), with alternating signs + − +:

TEXT
det A = a11·M11 − a12·M12 + a13·M13

The same rule works for any size and gives a short recursive function. It is great for understanding and terrible for speed: the work grows like n!.

Python Cofactor expansion from scratch

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For anything bigger, use elimination. Gaussian elimination turns the matrix into an upper triangular one, and the determinant of a triangular matrix is the product of its diagonal. Each row swap flips the sign; adding a multiple of one row to another changes nothing. That is exactly what np.linalg.det does internally through an LU decomposition, in about n³ steps.

Shortcuts: Sarrus' rule, any-row expansion and row operations

The first-row expansion above is only one choice. Three shortcuts make hand computation much quicker, and they are worth knowing even though NumPy does the real work.

Sarrus' rule for 3 by 3

For a 3 × 3 matrix only, there is a diagram trick. Copy the first two columns to the right of the matrix, add the products of the three diagonals that run down to the right, and subtract the products of the three that run down to the left:

TEXT
a11 a12 a13 | a11 a12
a21 a22 a23 | a21 a22
a31 a32 a33 | a31 a32

det = a11·a22·a33 + a12·a23·a31 + a13·a21·a32
    − a13·a22·a31 − a11·a23·a32 − a12·a21·a33

It is the same number as the cofactor expansion, just written out. Sarrus' rule does not extend to 4 × 4 matrices or larger; for those, use expansion or elimination.

Expand along any row or column

Cofactor (Laplace) expansion works along any row or column, not just the first. The sign of the term in position (i, j) follows a checkerboard pattern, (-1)^(i+j):

TEXT
+ − + −
− + − +
+ − + −

The practical trick: expand along the row or column with the most zeros, since each zero entry kills a whole sub-determinant. For a matrix with a row (0, 0, 5, 0), a 4 × 4 determinant collapses to a single 3 × 3.

Triangular matrices and row operations

The determinant of a triangular (or diagonal) matrix is the product of its diagonal entries. That is the key to the fast method: use elimination to make the matrix triangular, track how each row operation changes the determinant, and multiply the diagonal.

Row operation Effect on the determinant
Add a multiple of one row to another no change
Swap two rows multiplies by -1
Multiply a row by c multiplies by c

So with only "add a multiple" operations and k row swaps, det A = (-1)^k × (product of the pivots). This takes about n³ steps instead of the n! of cofactor expansion, and it is how libraries compute determinants (via LU).

Python Sarrus, row-by-row expansion and elimination agree

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Sarrus, expansion along different rows and elimination all agree. Notice that the elimination version never needs more than a double loop, while the recursive expansion is only practical when most entries are zero.

Rules worth knowing

These properties follow from the geometric picture, and they make hand calculations and proofs much easier:

  • det(I) = 1.
  • det(AB) = det(A) · det(B): scaling factors multiply when you compose transformations.
  • det(Aᵀ) = det(A).
  • det(A⁻¹) = 1 / det(A).
  • Swapping two rows flips the sign.
  • Multiplying one row by k multiplies the determinant by k. Hence det(kA) = kⁿ det(A) for an n × n matrix.
  • Adding a multiple of one row to another changes nothing.
  • A is invertible exactly when det(A) ≠ 0.
Python Checking the rules numerically

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Do not use det(A) != 0 to decide whether a matrix is invertible in practice. A determinant can be tiny because the entries are small, not because the matrix is almost singular, and rounding makes exact comparisons unreliable. Use rank or the condition number instead.

Numerical pitfalls and slogdet

Determinants are products, and products of many small or large numbers leave the range of floating-point numbers. A 400 by 400 matrix 0.1·I has a perfectly sensible determinant of 10⁻⁴⁰⁰, but the smallest double is about 10⁻³⁰⁸, so NumPy returns 0.0. That zero looks like "singular" even though the matrix is perfectly invertible.

The fix is to work with logarithms. np.linalg.slogdet returns the sign and the natural log of the absolute value.

Python Underflow and slogdet

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Log-determinants appear all the time in statistics, for example in the Gaussian density, where the determinant of a covariance matrix would otherwise underflow.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. A 2 by 2 matrix has determinant -4. What happens to the area of a shape it transforms?

    The absolute value 4 is the area scale factor; the minus sign means the transformation flips orientation like a mirror. Areas themselves are never negative.
  2. 2. If det(A) = 3 and det(B) = 5, what is det(AB)?

    The determinant of a product is the product of the determinants: scaling factors multiply when transformations are composed.
  3. 3. A 3 by 3 matrix A has det(A) = 2. What is det(2A)?

    Each of the 3 rows is multiplied by 2, so the determinant is multiplied by 2 three times: 2^3 * 2 = 16.
  4. 4. Why prefer np.linalg.slogdet over np.linalg.det for a huge matrix with small entries?

    The determinant is a long product that can fall outside the floating-point range. Its logarithm is a sum, which stays in range.

Summary

  • |det A| is the factor by which A scales area (2D) or volume (3D); the sign records orientation.
  • det A = 0 means space collapses and A is not invertible.
  • For 2 by 2: ad − bc. For larger matrices use elimination: product of the pivots, flipping the sign for each row swap.
  • Key rules: det(AB) = det(A)det(B), det(Aᵀ) = det(A), det(A⁻¹) = 1/det(A), det(kA) = kⁿ det(A).
  • Use np.linalg.slogdet for large matrices, and do not test invertibility by comparing a determinant to zero.

Next up: Orthogonality and Projections, where perpendicular directions make many problems simple.