TExcelTAD
Home › Algebra › Solving with 3×3 Determinants

Solving with 3×3 Determinants

Cramer's rule works for three equations as well: the solution is three fractions of 3×33 \times 3 determinants. This lesson shows two ways to evaluate a 3×33 \times 3 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 3×33 \times 3 determinant is reduced to three 2×22 \times 2 determinants (see Solving with 2×2 Determinants). Each entry of the first row is multiplied by the 2×22 \times 2 determinant left over when you cross out its row and column, with signs + − ++\ -\ +:

∣abcdefghi∣=a⋅∣efhi∣−b⋅∣dfgi∣+c⋅∣degh∣=a(ei−fh)−b(di−fg)+c(dh−eg)\begin{aligned} \begin{vmatrix} a & b & c \\ d & e & f \\ g & h & i \end{vmatrix} &= a \cdot \begin{vmatrix} e & f \\ h & i \end{vmatrix} - b \cdot \begin{vmatrix} d & f \\ g & i \end{vmatrix} + c \cdot \begin{vmatrix} d & e \\ g & h \end{vmatrix} \\[1ex] &= a(ei - fh) - b(di - fg) + c(dh - eg) \end{aligned}

Example:

∣12121−11−12∣=1⋅(1⋅2−(−1)(−1))−2⋅(2⋅2−(−1)⋅1)+1⋅(2⋅(−1)−1⋅1)=1⋅(2−1)−2⋅(4+1)+1⋅(−2−1)=1−10−3=−12\begin{aligned} \begin{vmatrix} 1 & 2 & 1 \\ 2 & 1 & -1 \\ 1 & -1 & 2 \end{vmatrix} &= 1 \cdot (1 \cdot 2 - (-1)(-1)) - 2 \cdot (2 \cdot 2 - (-1) \cdot 1) + 1 \cdot (2 \cdot (-1) - 1 \cdot 1) \\ &= 1 \cdot (2 - 1) - 2 \cdot (4 + 1) + 1 \cdot (-2 - 1) \\ &= 1 - 10 - 3 = -12 \end{aligned}

The middle minus sign is the step people forget most often.

The rule of Sarrus

For 3×33 \times 3 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:

∣abcdefghi∣abdeghdet⁡=aei+bfg+cdh−ceg−afh−bdi\begin{vmatrix} a & b & c \\ d & e & f \\ g & h & i \end{vmatrix} \begin{matrix} a & b \\ d & e \\ g & h \end{matrix} \qquad \det = aei + bfg + cdh - ceg - afh - bdi

For the matrix above: 1⋅1⋅2+2⋅(−1)⋅1+1⋅2⋅(−1)−1⋅1⋅1−1⋅(−1)⋅(−1)−2⋅2⋅2=2−2−2−1−1−8=−121 \cdot 1 \cdot 2 + 2 \cdot (-1) \cdot 1 + 1 \cdot 2 \cdot (-1) - 1 \cdot 1 \cdot 1 - 1 \cdot (-1) \cdot (-1) - 2 \cdot 2 \cdot 2 = 2 - 2 - 2 - 1 - 1 - 8 = -12. Same value.

Note: Sarrus' diagonals do not work for 4×44 \times 4 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 a1x+b1y+c1z=d1a_1 x + b_1 y + c_1 z = d_1 (and rows 2, 3 alike), let DD be the determinant of the coefficients, and build DxD_x, DyD_y, DzD_z by replacing the xx-, yy- or zz-column with the constants d1d_1, d2d_2, d3d_3:

x=DxDy=DyDz=DzD(D≠0)x = \frac{D_x}{D} \qquad y = \frac{D_y}{D} \qquad z = \frac{D_z}{D} \qquad (D \ne 0)

Take the system from The Matrix Method: x+2y+z=0x + 2y + z = 0, 2x+y−z=32x + y - z = 3, x−y+2z=7x - y + 2z = 7. We already found D=−12D = -12.

Dx=∣02131−17−12∣=0⋅(…)−2⋅(3⋅2−(−1)⋅7)+1⋅(3⋅(−1)−1⋅7)=−26−10=−36Dy=∣10123−1172∣=1⋅(3⋅2−(−1)⋅7)−0+1⋅(2⋅7−3⋅1)=13+11=24Dz=∣1202131−17∣=1⋅(1⋅7−3⋅(−1))−2⋅(2⋅7−3⋅1)+0=10−22=−12\begin{aligned} D_x &= \begin{vmatrix} 0 & 2 & 1 \\ 3 & 1 & -1 \\ 7 & -1 & 2 \end{vmatrix} = 0 \cdot (\ldots) - 2 \cdot (3 \cdot 2 - (-1) \cdot 7) + 1 \cdot (3 \cdot (-1) - 1 \cdot 7) = -26 - 10 = -36 \\[1ex] D_y &= \begin{vmatrix} 1 & 0 & 1 \\ 2 & 3 & -1 \\ 1 & 7 & 2 \end{vmatrix} = 1 \cdot (3 \cdot 2 - (-1) \cdot 7) - 0 + 1 \cdot (2 \cdot 7 - 3 \cdot 1) = 13 + 11 = 24 \\[1ex] D_z &= \begin{vmatrix} 1 & 2 & 0 \\ 2 & 1 & 3 \\ 1 & -1 & 7 \end{vmatrix} = 1 \cdot (1 \cdot 7 - 3 \cdot (-1)) - 2 \cdot (2 \cdot 7 - 3 \cdot 1) + 0 = 10 - 22 = -12 \end{aligned}
x=−36−12=3y=24−12=−2z=−12−12=1x = \frac{-36}{-12} = 3 \qquad y = \frac{24}{-12} = -2 \qquad z = \frac{-12}{-12} = 1

That matches the row-reduction answer (3,−2,1)(3, -2, 1). The code version replaces each column in turn:

python · try it yourself
from 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 2×22 \times 2 case, D=0D = 0 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 nn unknowns needs n+1n + 1 determinants, and computing each by expansion takes about n!n! 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 3×33 \times 3 determinant with a column of ones:

orient=∣x1y11x2y21x3y31∣=twice the signed area of triangle P1P2P3\text{orient} = \begin{vmatrix} x_1 & y_1 & 1 \\ x_2 & y_2 & 1 \\ x_3 & y_3 & 1 \end{vmatrix} = \text{twice the signed area of triangle } P_1P_2P_3

Positive means P1→P2→P3P_1 \to P_2 \to P_3 turns counter-clockwise (P3P_3 is left of the line P1P2P_1P_2), 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 yourself
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 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 (0,0)(0, 0), (4,0)(4, 0), (1,3)(1, 3) has base 4 and height 3, so its area is 6, as the determinant says. Watch out on screens where yy points down: the signs of "clockwise" and "counter-clockwise" swap (see Coordinate Systems).

Exercises

Exercise 1: Evaluate two ways

Find the determinant of [201132014]\begin{bmatrix} 2 & 0 & 1 \\ 1 & 3 & 2 \\ 0 & 1 & 4 \end{bmatrix} by row expansion and by Sarrus.

Answer
Solution
expansion:2⋅(3⋅4−2⋅1)−0⋅(…)+1⋅(1⋅1−3⋅0)=2⋅10+1=21Sarrus:(2⋅3⋅4+0⋅2⋅0+1⋅1⋅1)−(1⋅3⋅0+2⋅2⋅1+0⋅1⋅4)=25−4=21\begin{aligned} \text{expansion:} &\quad 2 \cdot (3 \cdot 4 - 2 \cdot 1) - 0 \cdot (\ldots) + 1 \cdot (1 \cdot 1 - 3 \cdot 0) = 2 \cdot 10 + 1 = 21 \\ \text{Sarrus:} &\quad (2 \cdot 3 \cdot 4 + 0 \cdot 2 \cdot 0 + 1 \cdot 1 \cdot 1) - (1 \cdot 3 \cdot 0 + 2 \cdot 2 \cdot 1 + 0 \cdot 1 \cdot 4) = 25 - 4 = 21 \end{aligned}

Exercise 2: Cramer's rule

Solve 2x+y+z=32x + y + z = 3, x−y+2z=−3x - y + 2z = -3, x+3y−z=8x + 3y - z = 8 with determinants.

Answer

x=1x = 1, y=2y = 2, z=−1z = -1 (D=−3D = -3, Dx=−3D_x = -3, Dy=−6D_y = -6, Dz=3D_z = 3).

Solution
D=2⋅(1−6)−1⋅(−1−2)+1⋅(3+1)=−10+3+4=−3Dx=∣311−3−1283−1∣=3⋅(1−6)−1⋅(3−16)+1⋅(−9+8)=−15+13−1=−3Dy=∣2311−3218−1∣=2⋅(3−16)−3⋅(−1−2)+1⋅(8+3)=−26+9+11=−6Dz=∣2131−1−3138∣=2⋅(−8+9)−1⋅(8+3)+3⋅(3+1)=2−11+12=3x=−3−3=1,y=−6−3=2,z=3−3=−1\begin{aligned} D &= 2 \cdot (1 - 6) - 1 \cdot (-1 - 2) + 1 \cdot (3 + 1) = -10 + 3 + 4 = -3 \\ D_x &= \begin{vmatrix} 3 & 1 & 1 \\ -3 & -1 & 2 \\ 8 & 3 & -1 \end{vmatrix} = 3 \cdot (1 - 6) - 1 \cdot (3 - 16) + 1 \cdot (-9 + 8) = -15 + 13 - 1 = -3 \\ D_y &= \begin{vmatrix} 2 & 3 & 1 \\ 1 & -3 & 2 \\ 1 & 8 & -1 \end{vmatrix} = 2 \cdot (3 - 16) - 3 \cdot (-1 - 2) + 1 \cdot (8 + 3) = -26 + 9 + 11 = -6 \\ D_z &= \begin{vmatrix} 2 & 1 & 3 \\ 1 & -1 & -3 \\ 1 & 3 & 8 \end{vmatrix} = 2 \cdot (-8 + 9) - 1 \cdot (8 + 3) + 3 \cdot (3 + 1) = 2 - 11 + 12 = 3 \\ x &= \tfrac{-3}{-3} = 1,\quad y = \tfrac{-6}{-3} = 2,\quad z = \tfrac{3}{-3} = -1 \end{aligned}
python · try it yourself
from 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 (2,5)(2, 5) to the left or the right of the directed line from (0,0)(0, 0) to (4,2)(4, 2)? What is the area of the triangle the three points form?

Answer

Left (the determinant is 16>016 > 0); the area is 8.

Solution
∣001421251∣=0−0+1⋅(4⋅5−2⋅2)=16>0→counter-clockwise, leftarea=162=8\begin{aligned} &\begin{vmatrix} 0 & 0 & 1 \\ 4 & 2 & 1 \\ 2 & 5 & 1 \end{vmatrix} = 0 - 0 + 1 \cdot (4 \cdot 5 - 2 \cdot 2) = 16 > 0 \to \text{counter-clockwise, left} \\ &\text{area} = \frac{16}{2} = 8 \end{aligned}

Exercise 4: When is D zero?

For which kk is the determinant of [1230k1111]\begin{bmatrix} 1 & 2 & 3 \\ 0 & k & 1 \\ 1 & 1 & 1 \end{bmatrix} equal to 0?

Answer

k=12k = \frac{1}{2}.

Solution
det⁡=1⋅(k⋅1−1⋅1)−2⋅(0⋅1−1⋅1)+3⋅(0⋅1−k⋅1)=k−1+2−3k=1−2k1−2k=0→k=12\begin{aligned} \det &= 1 \cdot (k \cdot 1 - 1 \cdot 1) - 2 \cdot (0 \cdot 1 - 1 \cdot 1) + 3 \cdot (0 \cdot 1 - k \cdot 1) \\ &= k - 1 + 2 - 3k = 1 - 2k \\ 1 - 2k &= 0 \to k = \tfrac{1}{2} \end{aligned}
python · try it yourself
from 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 3×33 \times 3 determinant along the first row, what are the signs of the three terms?

  1. + + ++\ +\ +
  2. + − ++\ -\ +
  3. − + −-\ +\ -
  4. + + −+\ +\ -

What is the determinant of [100020003]\begin{bmatrix} 1 & 0 & 0 \\ 0 & 2 & 0 \\ 0 & 0 & 3 \end{bmatrix}?

  1. 0
  2. 1
  3. 6
  4. 5

The orientation determinant of three points is 0. What does that tell you?

  1. The points form a right angle
  2. The points lie on one straight line
  3. The triangle is clockwise
  4. The points are all at the origin