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.

x =

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.

xi=detAidetA(i=1,2,…,n)

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.

M12=|a21a23a31a33|

cofactor
The minor Mᵢⱼ with the sign (−1)i+j attached is the cofactor of aᵢⱼ, written Cᵢⱼ.

Cij=(−1)i+jMij

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.

detA=ai1Ci1+ai2Ci2+⋯+ainCin

detA=a1jC1j+a2jC2j+⋯+anjCnj

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.

adjA=[C11C12⋯C1nC21C22⋯C2n⋮⋮⋱⋮Cn1Cn2⋯Cnn]𝖳

A−1=1detAadjA

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.

xi=detAidetA

For 2×2, this is what it looks like.

A=[a11a12a21a22],𝐛=[b1b2]

x1=|b1a12b2a22||a11a12a21a22|=b1a22−a12b2a11a22−a12a21

x2=|a11b1a21b2||a11a12a21a22|=a11b2−b1a21a11a22−a12a21

In equations, b = (b₁, b₂) is written standing up, as a column. It is the same vector.

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.

det[𝐚1𝐚2]=detA

det[x1𝐚1𝐚2]=x1detA

detA1=det[x1𝐚1+x2𝐚2𝐚2]=det[x1𝐚1𝐚2]=x1detA

x1=detA1detA(detA≠0)

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

  1. det A. 2 −1 / 1 1 → 2×1 − (−1)×1 = 3. Not 0, so there is one solution.
  2. det A₁. First column replaced by b: 1 −1 / 5 1 → 1×1 − (−1)×5 = 6.
  3. det A₂. Second column replaced by b: 2 1 / 1 5 → 2×5 − 1×1 = 9.
  4. Divide. x₁ = 6 ÷ 3 = 2, x₂ = 9 ÷ 3 = 3.
  5. Check. 2×2 − 3 = 1, 2 + 3 = 5. Correct.

Written as equations, the same work looks like this.

[2−111][x1x2]=[15]

detA=|2−111|=2·1−(−1)·1=3

detA1=|1−151|=1·1−(−1)·5=6,detA2=|2115|=2·5−1·1=9

x1=detA1detA=63=2,x2=detA2detA=93=3

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 + − +.

|pqrstuvwz|=p|tuwz|−q|suvz|+r|stvw|

=p(tz−uw)−q(sz−uv)+r(sw−tv)

MatrixExpand along the first rowValue
A = 2 1 0 / 1 3 1 / 0 1 22×(3×2 − 1×1) − 1×(1×2 − 1×0) + 08
A₁ = 4 1 0 / 10 3 1 / 8 1 24×(3×2 − 1×1) − 1×(10×2 − 1×8) + 08
A₂ = 2 4 0 / 1 10 1 / 0 8 22×(10×2 − 1×8) − 4×(1×2 − 1×0) + 016
A₃ = 2 1 4 / 1 3 10 / 0 1 82×(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.

detA=|210131012|=2|3112|−1|1102|+0|1301|=2·5−1·2+0=8

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.

  1. 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 cofactor Cᵢⱼ. The signs follow a checkerboard: + − + / − + − / + − +. The numbers we multiplied by in the first-row expansion of section 3 are the first-row cofactors.
  2. Adjugate. Put every cofactor in its own spot, then transpose (swap rows and columns). The result is adj(A).
  3. 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

PropertyIn plain words
Only when det(A) ≠ 0Use 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 ownIf you only need x₂, you only need det A and det A₂.
The sign tells the directionIf 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 coefficientsYou 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 workWith 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 nTerms in one expanded determinant, n!n³ (elimination work grows like this)
228
3627
5120125
103,628,8001,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

MethodGoodBadUse it for
Cramer's ruleOne formula gives the answer. You can get one unknown on its own. Coefficients written as letters turn straight into a formula for the answerThe work explodes as n grows. If det(A) = 0 it cannot tell no solution from infinitely many2×2 and 3×3 by hand. Writing the answer as a formula. Proofs
Gaussian eliminationWork grows like n³, so big systems are fine. It also reports no solution or infinitely manyBy hand there are many steps, and an early slip carries through. It does all the work even if you want one unknownAlmost every system solved by a computer. Four or more unknowns
Multiply by the inverse, x = A⁻¹bEasy to handle in formulas. You get the inverse itself as a bonusFinding the inverse is more work than solving directly. Does not work when det(A) = 0When you need A⁻¹ itself, for example to undo coordinates. Small matrices by hand (inverse matrix)

Where it is used

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 hereTextbook term
building b from multiples of the columnslinear combination. Reading A by columns is the column picture
A with column i replaced by bAᵢ. Some books use another letter, such as Bᵢ
determinant left after crossing out a row and a columnminor, Mᵢⱼ
minor with a sign attachedcofactor, Cᵢⱼ = (−1)^(i+j) Mᵢⱼ
expanding along the first rowcofactor expansion, Laplace expansion
swapping rows and columnstranspose, written Aᵀ
cofactors in place, then transposedadjugate (classical adjoint), written adj(A)
solving by subtracting rows to clear entriesGaussian elimination, LU decomposition
a matrix with det A = 0singular matrix

Sources

← DeterminantVectors →