Sub topic 12
Extension: Cramer's rule and determinants
This chapter shows that the elimination method you already know can be run once on general letters. The result is a pair of formulas that solve any 2×2 system directly. Those formulas are called Cramer's rule. It adds no new power, but it gives you a fast, mechanical solver and a one-number test for the three solution types.
Prerequisites
You should already be comfortable with these skills:
- Solving a system by elimination, including multiplying equations by constants first.
- Rewriting a linear equation in the form ax + by = c.
- Recognizing the three outcomes: one solution, no solution, infinitely many solutions.
- Arithmetic with negative numbers and fractions.
Standard form with named coefficients
Cramer's rule needs every equation in standard form, with the variable terms on the left and the constant on the right. Name the coefficients with letters:
a1*x + b1*y = c1
a2*x + b2*y = c2The subscript says which equation a number belongs to. For example, 2x + 3y = 13 gives a1 = 2, b1 = 3, c1 = 13. An equation such as y = 2x - 1 must first become -2x + y = -1, so a = -2, b = 1, c = -1.
A missing variable has coefficient 0. The equation x = 4 is 1x + 0y = 4.
Deriving the formulas by elimination
To find x, eliminate y. Multiply the first equation by b2 and the second by b1, so both y terms become b1·b2·y:
a1*b2*x + b1*b2*y = c1*b2
a2*b1*x + b1*b2*y = c2*b1Subtract the second from the first. The y terms cancel:
(a1*b2 - a2*b1) * x = c1*b2 - c2*b1To find y, eliminate x. Multiply the first equation by a2 and the second by a1, then subtract the first result from the second:
(a1*b2 - a2*b1) * y = a1*c2 - a2*c1Both results contain the same expression, a1·b2 − a2·b1, multiplying the unknown. That expression is the determinant.
The determinant
The determinant of a system is the number
D = a1*b2 - a2*b1You get it from the coefficient grid by multiplying along the main diagonal and subtracting the product along the other diagonal. Textbooks write the grid between vertical bars:
| a1 b1 |
| a2 b2 | = a1*b2 - a2*b1Two more determinants come from the same grid. You build each one by replacing a column of coefficients with the constants c1 and c2:
| Name | Replace this column | Formula |
|---|---|---|
| D | none | a1·b2 − a2·b1 |
| Dx | the x column (a1, a2) | c1·b2 − c2·b1 |
| Dy | the y column (b1, b2) | a1·c2 − a2·c1 |
The two derived equations now read D·x = Dx and D·y = Dy. When D is not zero, you can divide:
x = Dx / D
y = Dy / DThis is Cramer's rule for two equations.
A worked example
Solve the system from the elimination chapter:
2x + 3y = 13
5x - 2y = 4Here a1 = 2, b1 = 3, c1 = 13, a2 = 5, b2 = −2, c2 = 4.
- D = 2·(−2) − 5·3 = −4 − 15 = −19.
- Dx = 13·(−2) − 4·3 = −26 − 12 = −38.
- Dy = 2·4 − 5·13 = 8 − 65 = −57.
- x = −38 / −19 = 2 and y = −57 / −19 = 3.
The solution is (2, 3), the same answer elimination gave. As always, check it in both original equations: 2(2) + 3(3) = 13 and 5(2) − 2(3) = 4.
Now a system with a fractional answer and a zero numerator:
x + 2y = 1
3x + 4y = 2D = 1·4 − 3·2 = −2. Dx = 1·4 − 2·2 = 0. Dy = 1·2 − 3·1 = −1. So x = 0 / −2 = 0 and y = −1 / −2 = 1/2. Check: 0 + 2(1/2) = 1 and 3(0) + 4(1/2) = 2. A numerator of 0 is a valid result. It only means that x equals 0.
What the determinant tells you about the lines
The derived equation D·x = Dx has a different meaning depending on D.
- If D ≠ 0, you can divide by D, so there is exactly one solution. The lines cross at a single point.
- If D = 0, the left side D·x is 0 for every x. The equation becomes 0 = Dx, and there is no division to perform.
The condition D = 0 also has a slope interpretation. In slope-intercept form the slopes are −a1/b1 and −a2/b2. These are equal exactly when a1·b2 = a2·b1, which is the same as D = 0. So D = 0 means the lines are parallel or identical, and D ≠ 0 means the slopes differ.
To split the D = 0 case into its two outcomes, look at Dx and Dy:
| D | Dx and Dy | Conclusion |
|---|---|---|
| not 0 | any values | exactly one solution |
| 0 | at least one is not 0 | no solution (parallel lines) |
| 0 | both are 0 | usually infinitely many solutions (same line) |
Two examples from the earlier solution-types chapter:
y = 2x + 1 becomes -2x + y = 1
y = 2x - 3 becomes -2x + y = -3Here a1 = −2, b1 = 1, c1 = 1, a2 = −2, b2 = 1, c2 = −3. D = (−2)(1) − (−2)(1) = 0. Dx = (1)(1) − (−3)(1) = 4, which is not 0. So there is no solution.
x + y = 3
2x + 2y = 6D = 1·2 − 2·1 = 0. Dx = 3·2 − 6·1 = 0. Dy = 1·6 − 2·3 = 0. All three are 0, so the system has infinitely many solutions.
The last row of the table says "usually" because of one edge case. An equation with both coefficients 0, such as 0x + 0y = 5, says 0 = 5, which is false. Pair it with x + y = 1, or with another 0x + 0y = 0, and D, Dx and Dy all come out 0 even though no solution exists. Rewriting by hand rarely produces such an equation, but a program that accepts arbitrary input must check for it.
Implementing the rule in Python
The code below uses Fraction so that answers stay exact, as they do in hand work. The first function returns the unique solution or None. The second function classifies any system, including the degenerate case above.
from fractions import Fraction
def solve_2x2(a1, b1, c1, a2, b2, c2):
"""Solve a1*x + b1*y = c1 and a2*x + b2*y = c2 using Cramer's rule."""
det = a1 * b2 - a2 * b1
if det == 0:
return None # no unique solution: parallel or identical lines
x = Fraction(c1 * b2 - c2 * b1, det)
y = Fraction(a1 * c2 - a2 * c1, det)
return x, y
def classify_2x2(a1, b1, c1, a2, b2, c2):
"""Return 'one solution', 'no solution' or 'infinitely many solutions'."""
det = a1 * b2 - a2 * b1
if det != 0:
return "one solution"
dx = c1 * b2 - c2 * b1
dy = a1 * c2 - a2 * c1
if dx != 0 or dy != 0:
return "no solution"
for a, b, c in ((a1, b1, c1), (a2, b2, c2)):
if a == 0 and b == 0 and c != 0:
return "no solution" # an equation like 0 = 5
return "infinitely many solutions"
print(solve_2x2(2, 3, 13, 5, -2, 4)) # (Fraction(2, 1), Fraction(3, 1))
print(solve_2x2(1, 2, 1, 3, 4, 2)) # (Fraction(0, 1), Fraction(1, 2))
print(solve_2x2(1, 1, 3, 2, 2, 6)) # None
print(classify_2x2(-2, 1, 1, -2, 1, -3)) # no solution
print(classify_2x2(1, 1, 3, 2, 2, 6)) # infinitely many solutions
print(classify_2x2(0, 0, 5, 1, 1, 1)) # no solutionYou can use solve_2x2 to check answers you worked by hand. If your hand answer differs from the program, check your standard-form coefficients first, because that is where most mistakes start.
Common errors
- Using a system that is not in standard form. For
y = 3x + 2you must move the x term:-3x + y = 2. Reading a = 3 and b = 1 from the original form gives a wrong answer. - Losing a sign. Negative coefficients are the main source of arithmetic errors. Write
(-2)in parentheses when you substitute into a formula. - Mixing up the subtraction order. Each determinant is the main diagonal minus the other diagonal. Reversing one of them changes its sign, so x and y come out with the wrong signs. Keep all three in the same pattern: first product minus second product.
- Replacing the wrong column. Dx replaces the x column, Dy replaces the y column. If you swap them, you get x and y exchanged.
- Dividing when D = 0. The formulas do not apply in that case. Switch to the Dx and Dy test, or use another method to decide between no solution and infinitely many.
- Skipping the check. The rule is exact, but a copying error in c1 or c2 is not caught by it. Substitute your answer into both original equations.
Cramer's rule compared with the other methods
Cramer's rule is substitution and elimination in disguise. It is the elimination steps done once, symbolically, with the results saved as formulas.
| Situation | Better choice |
|---|---|
| Coefficients are ugly numbers and you have a calculator or program | Cramer's rule |
| One variable is already isolated | Substitution |
| Coefficients are small and some cancel quickly | Elimination |
| You need to see the lines, or estimate | Graphing |
| You only need to know how many solutions exist | The determinant D |
| Many equations and many unknowns | Gaussian elimination, not Cramer's rule |
The last row matters. Cramer's rule extends to 3×3 and larger systems, but the number of calculations grows very quickly with the size of the system. For large systems, computers use row reduction instead. For two equations, the rule is short and reliable.
Summary
For a system in standard form, D = a1·b2 − a2·b1 measures whether the two lines have different slopes. If D is nonzero, x = Dx/D and y = Dy/D give the unique solution. If D is zero, the values of Dx and Dy separate parallel lines from identical lines, apart from the degenerate-equation edge case. Always verify the result in both original equations.
Exercise
Use Cramer's rule to solve 3x - 2y = 12 and x + 4y = -2. Show D, Dx and Dy.
Show answer
a1 = 3, b1 = -2, c1 = 12, a2 = 1, b2 = 4, c2 = -2. D = 3·4 - 1·(-2) = 12 + 2 = 14. Dx = 12·4 - (-2)(-2) = 48 - 4 = 44. Dy = 3·(-2) - 1·12 = -6 - 12 = -18. So x = 44/14 = 22/7 and y = -18/14 = -9/7. Check in the first equation: 3(22/7) - 2(-9/7) = 66/7 + 18/7 = 84/7 = 12. Solution: (22/7, -9/7).
Exercise
Without solving, decide whether 4x - 6y = 5 and 6x - 9y = 7 has one solution, no solution, or infinitely many. Use D, Dx and Dy.
Show answer
a1 = 4, b1 = -6, c1 = 5, a2 = 6, b2 = -9, c2 = 7. D = 4·(-9) - 6·(-6) = -36 + 36 = 0. Dx = 5·(-9) - 7·(-6) = -45 + 42 = -3, which is not 0. Because D = 0 and Dx is nonzero, there is no solution. The lines have the same slope, 2/3, but different intercepts, so they are parallel.
Exercise
For which value of k does the system x + 2y = 4 and 3x + ky = 5 fail to have exactly one solution? What happens at that value?
Show answer
The determinant is D = 1·k - 3·2 = k - 6. It is 0 when k = 6. For k = 6 the system is x + 2y = 4 and 3x + 6y = 5. Then Dx = 4·6 - 5·2 = 14, which is not 0, so there is no solution. The second equation would need to be 3x + 6y = 12 to match the first, which is not the case. For every other value of k, there is exactly one solution.