Solving with 2×2 Determinants
A determinant is a single number computed from a square grid of coefficients. For a system of two equations, three small determinants give the solution directly as two fractions, with no elimination steps to write out. The method is called Cramer's rule, and the same formula sits inside the line-intersection code of many 2D games.
The 2×2 determinant
A determinant is written between vertical bars and means "down-diagonal product minus up-diagonal product":
| Determinant | Calculation | Value |
|---|---|---|
| 2 | ||
| 11 | ||
| 0 |
(Here ; separates the rows: is the same as the barred grid above.) Watch the signs: subtracting a negative product adds it.
Cramer's rule for two equations
Write the system in standard form:
Build three determinants. uses the coefficients. is with the -column replaced by the constants; is with the -column replaced by the constants:
Example: and .
Where does the rule come from? Eliminate from the general system (multiply the first equation by , the second by , subtract) and you get , which is exactly . Cramer's rule is elimination done once, in general, and saved as a formula.
from fractions import Fraction
def det2(a, b, c, d):
return a * d - b * c
def cramer2(a1, b1, c1, a2, b2, c2):
D = det2(a1, b1, a2, b2)
if D == 0:
return None # no unique solution
Dx = det2(c1, b1, c2, b2)
Dy = det2(a1, c1, a2, c2)
return Fraction(Dx, D), Fraction(Dy, D)
print(cramer2(4, -3, 6, 2, 5, 16))
print(cramer2(3, 2, 1, 5, -1, 4))
print(cramer2(1, 2, 3, 2, 4, 5))(Fraction(3, 1), Fraction(2, 1))
(Fraction(9, 13), Fraction(-7, 13))
None
The second system has the fractional solution , ; Cramer's rule hands you the exact fractions without any messy intermediate steps.
When
If the two left-hand sides are multiples of each other, so the lines are parallel or identical (see Systems of Equations):
| , | Meaning | |
|---|---|---|
| anything | exactly one solution | |
| 0 | at least one | parallel lines, no solution |
| 0 | both 0 | the same line, infinitely many solutions |
In the third call above, and : while , so the lines are parallel.
Note: The determinant also has a picture. is the area of the parallelogram spanned by the column vectors and . means the parallelogram is flat: the two columns point the same way. The linear algebra course develops this in Determinants of 2×2 and 3×3 Matrices and proves the general rule in Cramer's Rule.
In software: where two lines cross
Game and graphics code stores a line as (the "implicit" form). Two such lines meet where both equations hold, so the meeting point is a Cramer's rule calculation, and is the "parallel, no hit" test:
def line_through(p, q):
"""Line a·x + b·y = c through points p and q."""
a = q[1] - p[1]
b = p[0] - q[0]
return a, b, a * p[0] + b * p[1]
def intersect(L1, L2, eps=1e-9):
a1, b1, c1 = L1
a2, b2, c2 = L2
D = a1 * b2 - a2 * b1
if abs(D) < eps: # floats: compare with a tolerance
return None
return ((c1 * b2 - c2 * b1) / D, (a1 * c2 - a2 * c1) / D)
wall = line_through((0, 4), (8, 0))
laser = line_through((0, 0), (6, 3))
print("wall :", wall, " laser:", laser)
print("hit :", intersect(wall, laser))
print("parallel:", intersect(laser, line_through((0, 1), (2, 2))))wall : (-4, -8, -32) laser: (3, -6, 0)
hit : (4.0, 2.0)
parallel: None
The laser from through hits the wall from to at . With floating-point coordinates, is rarely exactly 0, so real code compares with a small tolerance. To decide whether two segments (not infinite lines) cross, you also check that the hit point lies inside both segments; that version is in Solving Systems by Graphing.
Exercises
Exercise 1: Evaluate
Compute , and .
Answer
29, −10 and 0.
Solution
Exercise 2: Cramer by hand
Solve and with determinants.
Answer
, .
Solution
Exercise 3: A fraction answer
Solve and . Check with the cramer2 function from the lesson.
Answer
, .
Solution
from fractions import Fraction
def det2(a, b, c, d): return a * d - b * c
D, Dx, Dy = det2(5, 2, 3, 4), det2(3, 2, -1, 4), det2(5, 3, 3, -1)
print(D, Dx, Dy, Fraction(Dx, D), Fraction(Dy, D))14 14 -14 1 -1
Exercise 4: Find the parameter
For which value of does the system , have no unique solution? What happens then?
Answer
; the lines are then parallel and there is no solution.
Solution
Indeed, doubled is , not 5.
Test yourself
What is ?
- −7
- 23
- 8
- −23
In Cramer's rule for , , how is built?
- Replace the -column of by the constants
- Replace the -column by the constants
- Swap the two rows of
- Multiply by
Line-intersection code finds . What should it report?
- The intersection is at the origin
- The lines are parallel (or identical): no single intersection point
- The lines are perpendicular