Solving with 3×3 Determinants
Cramer's rule works for three equations as well: the solution is three fractions of determinants. This lesson shows two ways to evaluate a determinant, expansion along a row and the diagonal rule of Sarrus, then solves a system with them and ends with the orientation test that geometry code uses every day.
Expanding along the first row
A determinant is reduced to three determinants (see Solving with 2×2 Determinants). Each entry of the first row is multiplied by the determinant left over when you cross out its row and column, with signs :
Example:
The middle minus sign is the step people forget most often.
The rule of Sarrus
For only, there is a shortcut. Copy the first two columns to the right, add the three products going down to the right, and subtract the three products going up to the right:
For the matrix above: . Same value.
Note: Sarrus' diagonals do not work for or larger. Row expansion does work for any size, but its cost explodes; computers use elimination instead (see Determinants and Row Operations).
Cramer's rule for three equations
For (and rows 2, 3 alike), let be the determinant of the coefficients, and build , , by replacing the -, - or -column with the constants , , :
Take the system from The Matrix Method: , , . We already found .
That matches the row-reduction answer . The code version replaces each column in turn:
python · try it yourselffrom fractions import Fraction
def det3(m):
(a, b, c), (d, e, f), (g, h, i) = m
return a * (e * i - f * h) - b * (d * i - f * g) + c * (d * h - e * g)
def cramer3(A, rhs):
D = det3(A)
if D == 0:
return None
result = []
for col in range(3):
M = [row[:] for row in A]
for r in range(3):
M[r][col] = rhs[r] # replace one column by the constants
result.append(Fraction(det3(M), D))
return D, result
A = [[1, 2, 1], [2, 1, -1], [1, -1, 2]]
D, xyz = cramer3(A, [0, 3, 7])
print("D =", D, " x, y, z =", [str(v) for v in xyz])
D = -12 x, y, z = ['3', '-2', '1']
As in the case, means no unique solution: the three planes do not meet in a single point.
When to use determinants
| Method |
Best for |
| Cramer's rule |
small systems, especially with letters (symbolic coefficients) where you want a formula |
| elimination / matrix method |
numeric systems of any size; this is what software does |
Cramer's rule for unknowns needs determinants, and computing each by expansion takes about steps. For 10 unknowns that is tens of millions of operations, against a few hundred for elimination. The linear algebra course times the difference in Cramer's Rule, and explains row expansion in general in Minors and Cofactors.
In software: the orientation test
Put three 2D points into a determinant with a column of ones:
Positive means turns counter-clockwise ( is left of the line ), negative means clockwise, and 0 means the points are on one line. This single test drives convex-hull algorithms, polygon triangulation, "is the player left or right of the wall" checks and segment-intersection code.
python · try it yourselfdef det3(m):
(a, b, c), (d, e, f), (g, h, i) = m
return a * (e * i - f * h) - b * (d * i - f * g) + c * (d * h - e * g)
def orient(p1, p2, p3):
return det3([[p1[0], p1[1], 1], [p2[0], p2[1], 1], [p3[0], p3[1], 1]])
A, B = (0, 0), (4, 0)
for C in [(1, 3), (1, -3), (8, 0)]:
o = orient(A, B, C)
side = "left (ccw)" if o > 0 else "right (cw)" if o < 0 else "collinear"
print(C, "det =", o, "area =", abs(o) / 2, side)
(1, 3) det = 12 area = 6.0 left (ccw)
(1, -3) det = -12 area = 6.0 right (cw)
(8, 0) det = 0 area = 0.0 collinear
The triangle , , has base 4 and height 3, so its area is 6, as the determinant says. Watch out on screens where points down: the signs of "clockwise" and "counter-clockwise" swap (see Coordinate Systems).
Exercises
Exercise 1: Evaluate two ways
Find the determinant of by row expansion and by Sarrus.
Answer
Solution
Exercise 2: Cramer's rule
Solve , , with determinants.
Answer
, , (, , , ).
Solution
python · try it yourselffrom sympy import Matrix
A = Matrix([[2, 1, 1], [1, -1, 2], [1, 3, -1]])
b = Matrix([3, -3, 8])
dets = []
for col in range(3):
M = A.copy()
M[:, col] = b
dets.append(M.det())
print(A.det(), dets, [d / A.det() for d in dets])
-3 [-3, -6, 3] [1, 2, -1]
Exercise 3: Left or right?
Is the point to the left or the right of the directed line from to ? What is the area of the triangle the three points form?
Answer
Left (the determinant is ); the area is 8.
Solution
Exercise 4: When is D zero?
For which is the determinant of equal to 0?
Answer
.
Solution
python · try it yourselffrom sympy import Matrix, symbols, solve
k = symbols("k")
d = Matrix([[1, 2, 3], [0, k, 1], [1, 1, 1]]).det()
print(d, solve(d, k))
1 - 2*k [1/2]
Test yourself
When expanding a determinant along the first row, what are the signs of the three terms?
What is the determinant of ?
- 0
- 1
- 6
- 5
The orientation determinant of three points is 0. What does that tell you?
- The points form a right angle
- The points lie on one straight line
- The triangle is clockwise
- The points are all at the origin