Cramer's rule, visually
Cramer's rule writes the answer to a linear system as one determinant divided by another. A determinant is an area, so the answer is a ratio of areas. Drag the arrows and watch three parallelograms turn into the answer.
Short answer. Cramer's rule solves Ax = b with xᵢ = det(Aᵢ) ÷ det(A), where Aᵢ is A with its i-th column replaced by b. It only works when det(A) is not 0, and it is handy for 2×2 and 3×3 by hand. With many unknowns the work grows too fast, so people use Gaussian elimination.
left column = a₁, right column = a₂, right box = b
Drag the circles at the arrow tips or edit the numbers. The gray shape A is the parallelogram of the two columns a₁ and a₂. The green shape A₁ swaps the first column for b, so it is made by b and a₂ (it gives x₁). The blue shape A₂ swaps the second column for b, so it is made by a₁ and b (it gives x₂). The number in each label is the area, negative when the shape is flipped. The dashed arrows go x₁ times a₁, then x₂ times a₂, and land on b.
Definitions
The text below uses 2×2 and 3×3, but the definitions are the same for n×n. A is an n×n matrix (n ≥ 2), and aᵢⱼ is the entry of A in row i, column j.
- Cramer's rule
- If det A ≠ 0, the system Ax = b has exactly one solution, and its entries are given below. Aᵢ is A with column i replaced by b.
- minor
- Delete row i and column j of A. The determinant of the (n−1)×(n−1) matrix that is left is the minor of aᵢⱼ, written Mᵢⱼ. For example, in 3×3, M₁₂ is the one below.
- cofactor
- The minor Mᵢⱼ with the sign (−1)i+j attached is the cofactor of aᵢⱼ, written Cᵢⱼ.
- cofactor expansion
- Multiply each entry of one row (or one column) by its cofactor and add them all. Whichever row or column you pick, the value equals det A. The first line expands along row i, the second along column j.
- adjugate
- Put each cofactor Cᵢⱼ in spot (i, j) (the cofactor matrix), then transpose it. The result is the adjugate of A, written adj A. If det A ≠ 0, A has an inverse, given by the second line below.
1. What Cramer's rule is
The system 2x₁ − x₂ = 1, x₁ + x₂ = 5 is Ax = b with A = 2 −1 / 1 1 and b = (1, 5). Read A by its columns (matrix basics) and the question becomes: "How many of the first column a₁ = (2, 1) and how many of the second column a₂ = (−1, 1) add up to b?" Those two counts are x₁ and x₂.
Cramer's rule writes those counts with determinants.
For 2×2, this is what it looks like.
In equations, b = (b₁, b₂) is written standing up, as a column. It is the same vector.
A,b: the matrix and the right-hand numbers when the system is written asAx = b.xᵢ: how many of column i of A you take. This is what you solve for.det: the determinant. For 2×2 it is the area of the parallelogram made by the two columns.Aᵢ: A with column i replaced by b.aᵢⱼ: the entry of A in row i, column j (/separates the rows).bᵢ: entry i of b.
The determinant of a 2×2 matrix is the area of the parallelogram made by its two columns (determinant). So in the picture, x₁ = (area of b and a₂) ÷ (area of a₁ and a₂). The start values of the experiment above are this example: the areas are 3, 6 and 9, so x = (6 ÷ 3, 9 ÷ 3) = (2, 3).
Try this. ① At the start, det A = 3, det A₁ = 6 and det A₂ = 9, so x = (2, 3). ② Drag b onto the tip of a₁ at (2, 1) and x = (1, 0). The green A₁ now has the same area as the gray A (3), and the blue A₂ goes flat (0). ③ Move b to (3, 0): det A₂ = −3, so x = (1, −1). b is on the other side of a₁ from a₂, so you have to use a₂ backwards to reach it. ④ Press "Parallel columns": a₂ = (4, 2) is twice a₁, det A = 0, and the readout says there is no solution. Now move b onto the same line at (−2, −1). det A₁ and det A₂ also become 0 and the readout says there are infinitely many.
2. How the formula is built: x₁ = det(A₁) ÷ det(A)
We use two facts. Stretching one side by some number multiplies the area by that number. Sliding one side along the other (a shear) does not change the area, because the base and the height stay the same (shown on the determinant page). Put b = x₁·a₁ + x₂·a₂ in the first column and apply the two facts in turn. The numbers are the start values from the experiment above.
Press "Next" to go one step at a time. The formula on top grows by one piece (the orange background marks the new piece), the picture changes by that much, and the reason appears. The gray dashed line is the line a₂ lies on, the gray dashed shape is the one to compare with, and the orange dashed arrow is what moved in this step.
Written only as equations, the steps above look like this.
Step ③ is the key. b mixes an a₁ part (x₁·a₁) and an a₂ part (x₂·a₂). Inside a parallelogram with a₂, the a₂ part runs along a₂, so it adds no area. Only x₁ is left in the area. x₂ works the same way: put b in the second column, the a₁ part drops out, and det A₂ = x₂ × det A. For 3×3 and larger, the same two facts (scaling one column scales the determinant by the same number, and adding a multiple of another column leaves it unchanged) give the same formula.
3. Work it by hand
2×2: 2x₁ − x₂ = 1, x₁ + x₂ = 5
- det A.
2 −1 / 1 1→2×1 − (−1)×1 = 3. Not 0, so there is one solution. - det A₁. First column replaced by b:
1 −1 / 5 1→1×1 − (−1)×5 = 6. - det A₂. Second column replaced by b:
2 1 / 1 5→2×5 − 1×1 = 9. - Divide.
x₁ = 6 ÷ 3 = 2,x₂ = 9 ÷ 3 = 3. - Check.
2×2 − 3 = 1,2 + 3 = 5. Correct.
Written as equations, the same work looks like this.
3×3: 2x₁ + x₂ = 4, x₁ + 3x₂ + x₃ = 10, x₂ + 2x₃ = 8
A = 2 1 0 / 1 3 1 / 0 1 2, b = (4, 10, 8). Expand each 3×3 determinant along the first row. For each entry in the first row, multiply it by the 2×2 determinant left after you cross out its row and column, then add the three with signs + − +.
p q r / s t u / v w z: the nine entries of a 3×3 matrix./separates the rows, so the first row is p q r.det: the determinant.- Each bracket (
t×z − u×wand so on): the 2×2 determinant left after you cross out that first-row entry's row and column. + − +: the signs on the p, q and r terms, in order.
| Matrix | Expand along the first row | Value |
|---|---|---|
A = 2 1 0 / 1 3 1 / 0 1 2 | 2×(3×2 − 1×1) − 1×(1×2 − 1×0) + 0 | 8 |
A₁ = 4 1 0 / 10 3 1 / 8 1 2 | 4×(3×2 − 1×1) − 1×(10×2 − 1×8) + 0 | 8 |
A₂ = 2 4 0 / 1 10 1 / 0 8 2 | 2×(10×2 − 1×8) − 4×(1×2 − 1×0) + 0 | 16 |
A₃ = 2 1 4 / 1 3 10 / 0 1 8 | 2×(3×8 − 10×1) − 1×(1×8 − 10×0) + 4×(1×1 − 3×0) | 24 |
The first row of the table, written as an equation, looks like this. A₁, A₂ and A₃ follow the same pattern.
So x = (8 ÷ 8, 16 ÷ 8, 24 ÷ 8) = (1, 2, 3). Check: 2 + 2 = 4, 1 + 6 + 3 = 10, 2 + 6 = 8. In 3×3 the determinant is the volume of the box made by the three columns, and the idea is the same as in 2×2.
4. The inverse formula: A⁻¹ = adj(A) ÷ det(A)
The inverse matrix page shows the 2×2 recipe: swap a and d, flip the signs of b and c, divide by the determinant. Here we widen that recipe to any size with the adjugate.
- Cofactor. For entry (i, j), cross out its row and column, take the determinant of what is left, and attach the sign
(−1)^(i+j). That is the cofactorCᵢⱼ. The signs follow a checkerboard:+ − + / − + − / + − +. The numbers we multiplied by in the first-row expansion of section 3 are the first-row cofactors. - Adjugate. Put every cofactor in its own spot, then transpose (swap rows and columns). The result is
adj(A). - Inverse.
A⁻¹ = adj(A) ÷ det(A). If det(A) is 0 you cannot divide, and there is no inverse.
Check with 2×2. For a b / c d the cofactors are C₁₁ = d, C₁₂ = −c, C₂₁ = −b, C₂₂ = a. In place they read d −c / −b a, and transposed d −b / −c a. That is the recipe from the inverse page. Forget the transpose and −b and −c end up in the wrong spots.
3×3 example. For A = 2 1 0 / 1 3 1 / 0 1 2 from section 3, the cofactors in place are 5 −2 1 / −2 4 −2 / 1 −2 5. This A is symmetric (transposing it changes nothing), so adj(A) is the same. So A⁻¹ = (1 ÷ 8) × [5 −2 1 / −2 4 −2 / 1 −2 5]. To check, multiply the first row of A by the columns of adj(A): 2×5 + 1×(−2) + 0×1 = 8, 2×(−2) + 1×4 + 0×(−2) = 0, 2×1 + 1×(−2) + 0×5 = 0. Doing all of it gives A × adj(A) = 8 × I.
Same story as Cramer's rule. x = A⁻¹b = adj(A)b ÷ det(A). With the example, adj(A)b = (5×4 − 2×10 + 1×8, −2×4 + 4×10 − 2×8, 1×4 − 2×10 + 5×8) = (8, 16, 24). Those are exactly det A₁, det A₂ and det A₃ from section 3. Entry i of adj(A)b is the same sum as expanding det(Aᵢ) along column i, the column that now holds b. A determinant gives the same value whichever row or column you expand along.
5. Key properties
| Property | In plain words |
|---|---|
| Only when det(A) ≠ 0 | Use it only when there is exactly one solution. If det(A) = 0 you would divide by 0, so it stops, and it does not tell you whether there is no solution or infinitely many. |
| One unknown on its own | If you only need x₂, you only need det A and det A₂. |
| The sign tells the direction | If det Aᵢ and det A have opposite signs, xᵢ is negative. You have to use that column backwards to reach b (Try this ③). |
| The answer is a fraction of the coefficients | You can see in the formula how the answer moves when a coefficient changes. If an integer matrix has det A = 1 or −1, the answer for an integer b is also integers. |
| Big systems mean a lot of work | With n unknowns you need n + 1 determinants of size n × n (section 6). |
6. Why big systems use Gaussian elimination
If you expand a determinant by cofactors all the way down, as in section 3, you end up adding n! terms, each a product of n numbers. Cramer's rule needs n + 1 of those determinants.
| Unknowns n | Terms in one expanded determinant, n! | n³ (elimination work grows like this) |
|---|---|---|
| 2 | 2 | 8 |
| 3 | 6 | 27 |
| 5 | 120 | 125 |
| 10 | 3,628,800 | 1,000 |
Gaussian elimination subtracts rows from each other to clear the entries below the diagonal, and its work grows like n³. You can also get a determinant by elimination (eliminate, multiply the diagonal entries, and flip the sign once for every row swap), which brings it down to n³ too. But Cramer's rule still needs n + 1 of them, so it is still more work than eliminating once. Elimination also keeps going when det(A) = 0 and tells you whether there is no solution or infinitely many. That is why numerical tools such as NumPy's numpy.linalg.solve solve by elimination (LU decomposition).
7. When to use which
| Method | Good | Bad | Use it for |
|---|---|---|---|
| Cramer's rule | One formula gives the answer. You can get one unknown on its own. Coefficients written as letters turn straight into a formula for the answer | The work explodes as n grows. If det(A) = 0 it cannot tell no solution from infinitely many | 2×2 and 3×3 by hand. Writing the answer as a formula. Proofs |
| Gaussian elimination | Work grows like n³, so big systems are fine. It also reports no solution or infinitely many | By hand there are many steps, and an early slip carries through. It does all the work even if you want one unknown | Almost every system solved by a computer. Four or more unknowns |
Multiply by the inverse, x = A⁻¹b | Easy to handle in formulas. You get the inverse itself as a bonus | Finding the inverse is more work than solving directly. Does not work when det(A) = 0 | When you need A⁻¹ itself, for example to undo coordinates. Small matrices by hand (inverse matrix) |
Where it is used
- Small systems by hand. For 2×2 and 3×3 you just plug numbers into a fixed formula, which is easy to follow.
- When the answer must be a formula. The crossing point of two lines
a₁₁x + a₁₂y = b₁,a₂₁x + a₂₂y = b₂is written as a ratio of determinants, exactly the 2×2 Cramer's rule. The formula graphics code uses for where two lines meet has this form. - Proofs. Statements such as "an integer matrix with det A = ±1 gives integer answers" (section 5) read a property of the answer straight off the coefficients.
Frequently asked questions
When can I use Cramer's rule?
When there are as many equations as unknowns (A is square) and det(A) is not 0. Then there is exactly one solution, and xᵢ = det(Aᵢ) ÷ det(A) gives it directly.
What if det(A) is 0?
Cramer's rule does not work. There is either no solution or infinitely many, and Gaussian elimination tells you which. For 2×2, if A is not all zeros and det(A₁) and det(A₂) are both 0, there are infinitely many. From 3×3 on, all det(Aᵢ) can be 0 and there can still be no solution. For example, x + y + z = 1, x + y + z = 2, x + y + z = 3 has no solution.
Do computers solve systems with Cramer's rule?
Usually not. With n unknowns you need n + 1 determinants, which is a lot of work. Numerical libraries use Gaussian elimination (LU decomposition). Cramer's rule is for solving 2×2 and 3×3 by hand, or for writing the answer as a formula in letters.
Words
| Word used here | Textbook term |
|---|---|
| building b from multiples of the columns | linear combination. Reading A by columns is the column picture |
| A with column i replaced by b | Aᵢ. Some books use another letter, such as Bᵢ |
| determinant left after crossing out a row and a column | minor, Mᵢⱼ |
| minor with a sign attached | cofactor, Cᵢⱼ = (−1)^(i+j) Mᵢⱼ |
| expanding along the first row | cofactor expansion, Laplace expansion |
| swapping rows and columns | transpose, written Aᵀ |
| cofactors in place, then transposed | adjugate (classical adjoint), written adj(A) |
| solving by subtracting rows to clear entries | Gaussian elimination, LU decomposition |
| a matrix with det A = 0 | singular matrix |
Sources
- Wikipedia: Cramer's rule. The formula, the area picture, and the case det(A) = 0.
- Gilbert Strang, MIT 18.06 Linear Algebra, Lecture 20: Cramer's rule, inverse matrix, and volume. The cofactor formula for the inverse and how it relates to Cramer's rule.
- Wikipedia: Adjugate matrix. The transpose of the cofactor matrix, A × adj(A) = det(A) × I.
- NumPy docs: numpy.linalg.solve. Solves linear systems with LAPACK's gesv (LU decomposition).