Determinants and inverses

The determinant

For a 2×2 matrix [[a, b], [c, d]] the determinant is

det = a·d − b·c

It has a picture: the matrix stretches the unit square into a parallelogram, and |det| is that parallelogram's area. A negative determinant means the shape was also flipped over, like a mirror image.

When det is 0

Determinant 0 means the plane is squashed flat onto a line (or a point). Information is lost: different points land on the same place, so the transformation cannot be undone.

The inverse

The inverse A⁻¹ undoes A: A·A⁻¹ = I. It exists exactly when det is not 0. For a 2×2 matrix:

A⁻¹ = (1 ÷ det) × [[d, −b], [−c, a]]

Swap a and d, change the signs of b and c, and divide by the determinant.

from fractions import Fraction

def det2(m):
    return m[0][0] * m[1][1] - m[0][1] * m[1][0]

def inverse2(m):
    d = det2(m)
    if d == 0:
        return None
    return [[Fraction(m[1][1], d), Fraction(-m[0][1], d)],
            [Fraction(-m[1][0], d), Fraction(m[0][0], d)]]

A = [[4, 7], [2, 6]]
print("det A =", det2(A))
for row in inverse2(A):
    print("inverse row:", " ".join(str(x) for x in row))
flat = [[1, 2], [2, 4]]
print("det of [[1, 2], [2, 4]] =", det2(flat), "so its inverse is", inverse2(flat))
Open in compiler

Bigger matrices

For larger matrices, run Gaussian elimination: the determinant is the product of the pivots, with a sign change for every row swap. Solving A·x = b this way is also faster and more accurate than computing A⁻¹ first.

Common mistakes

  • Computing a·d + b·c. The determinant subtracts: a·d − b·c.
  • Expecting every matrix to have an inverse. [[1, 2], [2, 4]] has det 4 − 4 = 0: its second row is twice the first.

Quick check

1. det of [[3, 1], [2, 4]]?
2. A matrix has an inverse exactly when its determinant is…
3. A transformation with determinant −2…

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.