Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 62 ยท Linear Systems

Cramer's Rule and the Adjugate

Learn Cramer's rule and the adjugate (adjoint) formula for the inverse, work through 2x2 and 3x3 examples in Python, and see why elimination beats them in practice.

There are two ways to solve A*x = b. One is to eliminate: row-reduce, substitute back, and let the algorithm discover the answer step by step. The other is to write down the answer directly as a formula in terms of determinants. That second route is Cramer's rule, and its companion, the adjugate formula, gives the inverse matrix the same way. Both are beautiful, both are exact, and both are a poor choice for large computations. Understanding why is the real lesson here.

This lesson derives the cofactor machinery they rely on, works through a 2-by-2 and a 3-by-3 example, implements both formulas in Python, and then measures how expensive they become. It builds on determinants and inverse matrices, and it compares unfavorably with Gaussian elimination and LU decomposition, which is the point.

Cramer's rule

Let A be an n-by-n matrix with det(A) != 0. Then the unique solution of A*x = b is

x_i = det(A_i) / det(A)

where A_i is the matrix A with its i-th column replaced by b.

Why does this work? Replacing a column by b = A*x = x_1*(column 1) + ... + x_n*(column n) gives a matrix whose i-th column is a combination of all the columns. A determinant is unchanged by adding multiples of other columns to a column, so every term except x_i * (column i) cancels out: det(A_i) = x_i * det(A). Dividing gives the formula. Geometrically, det(A) is the volume of the box spanned by the columns, and x_i is the ratio of two volumes.

For a 2-by-2 system a*x + b*y = e, c*x + d*y = f:

x = (e*d - b*f) / (a*d - b*c), y = (a*f - e*c) / (a*d - b*c).

Example: 2x + y = 5, x + 3y = 10. Then det(A) = 2*3 - 1*1 = 5, x = (5*3 - 1*10)/5 = 1 and y = (2*10 - 5*1)/5 = 3.

Cramer's rule needs det(A) != 0. If the determinant is zero, the system has either no solution or infinitely many, and the rule cannot say which.

Python Cramer's rule for a 3x3 system

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.

Minors, cofactors and the adjugate

To get an inverse by formula you need two more definitions.

  • The minor M_ij of an entry is the determinant of the matrix left after deleting row i and column j.
  • The cofactor C_ij = (-1)**(i+j) * M_ij. The signs follow a checkerboard pattern: + - + in the first row, - + - in the second, and so on.
  • The cofactor matrix C collects all C_ij. The adjugate (also called the classical adjoint) is its transpose: adj(A) = C.T.

The remarkable identity is

A * adj(A) = det(A) * I, so A**(-1) = adj(A) / det(A).

Each entry of A * adj(A) is a cofactor expansion. On the diagonal it is a genuine expansion of det(A) along a row; off the diagonal it is the expansion of a matrix with two identical rows, which is zero. For a 2-by-2 matrix the adjugate swaps the diagonal entries and flips the signs of the off-diagonal ones:

adj([[a, b], [c, d]]) = [[d, -b], [-c, a]]

and the inverse formula you may already know falls out: (1/(a*d - b*c)) * [[d, -b], [-c, a]].

flowchart LR
    A["A"] --> B["minors M_ij"]
    B --> C["cofactors C_ij = (-1)^(i+j) M_ij"]
    C --> D["adj(A) = C transposed"]
    D --> E["A^-1 = adj(A) / det(A)"]

The adjugate is built from signed minors, transposed.

Python Build the adjugate from cofactors

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.

Useful consequences of adj(A):

  • det(adj(A)) = det(A)**(n-1).
  • If A is singular, A * adj(A) = 0, and for rank n - 1 the adjugate is non-zero and has rank 1, which can be used to find null-space vectors.
  • Formula-based inverses let you study how the inverse depends on entries, since each entry of A**(-1) is a ratio of polynomials in the entries of A.

Why elimination wins

Cramer's rule needs n + 1 determinants. Computing a determinant by cofactor expansion costs about n! operations, which is astronomically slow: for n = 20, 20! is about 2.4 times ten to the eighteenth. Even with the smart way to compute determinants (via LU, about n**3/3 operations), Cramer needs n + 1 of them, so its cost is on the order of n**4, while a single LU solve costs n**3. For n = 100 that is a slowdown on the order of n, roughly 50 to 100 times. The adjugate approach costs even more: n**2 determinants of size n - 1.

Numerics make it worse. A determinant is a product of many numbers and is wildly sensitive to scaling; for ill-conditioned matrices the ratio of two nearly equal determinants amplifies rounding error. The elimination-based np.linalg.solve uses partial pivoting to avoid this.

Python Counting the cost of cofactor expansion, and timing Cramer against solve

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.

When the formulas are still useful

  • Small systems by hand. For two or three unknowns, Cramer's rule is quick and gives symbolic answers, handy in physics and geometry (intersection of lines, areas of triangles).
  • Theory. It proves that the solution depends continuously on the entries of A and b, and that integer systems with det = +/-1 have integer solutions.
  • Symbolic work. With SymPy, formulas for the inverse of a matrix with symbols come straight from adjugate().
  • Geometry. The same determinants give areas and volumes, and the adjugate appears in computer graphics for transforming normals.

For anything numerical, use np.linalg.solve (or LU) instead.

Common mistakes

  • Forgetting the checkerboard sign (-1)**(i+j) in cofactors.
  • Forgetting the transpose when going from the cofactor matrix to the adjugate.
  • Replacing a row instead of a column of A by b in Cramer's rule.
  • Using Cramer's rule when det(A) = 0.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In Cramer's rule, how is A_i formed?

    A_i is A with its i-th column replaced by b; then x_i = det(A_i) / det(A).
  2. 2. What is the adjugate of [[a, b], [c, d]]?

    Swap the diagonal entries and flip the signs of the off-diagonal entries.
  3. 3. Which identity defines the adjugate's role in finding inverses?

    Dividing A adj(A) = det(A) I by det(A) gives A^-1 = adj(A) / det(A).
  4. 4. Why is Cramer's rule rarely used for large numerical systems?

    Each determinant costs about as much as a full elimination, and rounding errors can be amplified in the ratio of determinants.

Summary

  • Cramer's rule: x_i = det(A_i) / det(A), where A_i has column i replaced by b; it requires det(A) != 0.
  • Cofactors C_ij = (-1)**(i+j) * M_ij; the adjugate is the transposed cofactor matrix, and A**(-1) = adj(A) / det(A).
  • For 2-by-2: adj = [[d, -b], [-c, a]].
  • The formulas cost far more than elimination (n + 1 determinants) and can be numerically unstable; use solve or LU for real work.
  • They remain handy for small symbolic problems and for proofs.

Next: Cayley-Hamilton Theorem, another elegant identity that gives inverses from polynomials.