TExcelTAD
Home › Algebra › Solving with 2×2 Determinants

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 2×22 \times 2 determinant is written between vertical bars and means "down-diagonal product minus up-diagonal product":

∣abcd∣=a⋅d−b⋅c\begin{vmatrix} a & b \\ c & d \end{vmatrix} = a \cdot d - b \cdot c
Determinant Calculation Value
det⁡ [3  1; 4  2]\det\,[3\ \ 1;\ 4\ \ 2] 3⋅2−1⋅43 \cdot 2 - 1 \cdot 4 2
det⁡ [5  −2; 3  1]\det\,[5\ \ {-2};\ 3\ \ 1] 5⋅1−(−2)⋅35 \cdot 1 - (-2) \cdot 3 11
det⁡ [2  6; 1  3]\det\,[2\ \ 6;\ 1\ \ 3] 2⋅3−6⋅12 \cdot 3 - 6 \cdot 1 0

(Here ; separates the rows: det⁡ [a  b; c  d]\det\,[a\ \ b;\ c\ \ d] 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:

{a1x+b1y=c1a2x+b2y=c2\begin{cases} a_1 x + b_1 y = c_1 \\ a_2 x + b_2 y = c_2 \end{cases}

Build three determinants. DD uses the coefficients. DxD_x is DD with the xx-column replaced by the constants; DyD_y is DD with the yy-column replaced by the constants:

D=∣a1b1a2b2∣Dx=∣c1b1c2b2∣Dy=∣a1c1a2c2∣x=DxDy=DyD(only when D≠0)\begin{gathered} D = \begin{vmatrix} a_1 & b_1 \\ a_2 & b_2 \end{vmatrix} \qquad D_x = \begin{vmatrix} c_1 & b_1 \\ c_2 & b_2 \end{vmatrix} \qquad D_y = \begin{vmatrix} a_1 & c_1 \\ a_2 & c_2 \end{vmatrix} \\[1ex] x = \frac{D_x}{D} \qquad y = \frac{D_y}{D} \qquad (\text{only when } D \ne 0) \end{gathered}

Example: 4x−3y=64x - 3y = 6 and 2x+5y=162x + 5y = 16.

D=4⋅5−(−3)⋅2=20+6=26Dx=6⋅5−(−3)⋅16=30+48=78x=7826=3Dy=4⋅16−6⋅2=64−12=52y=5226=2check: 12−6=6 ✓6+10=16 ✓\begin{aligned} D &= 4 \cdot 5 - (-3) \cdot 2 = 20 + 6 = 26 \\ D_x &= 6 \cdot 5 - (-3) \cdot 16 = 30 + 48 = 78 && x = \tfrac{78}{26} = 3 \\ D_y &= 4 \cdot 16 - 6 \cdot 2 = 64 - 12 = 52 && y = \tfrac{52}{26} = 2 \\ &\text{check: } 12 - 6 = 6\ \checkmark \quad 6 + 10 = 16\ \checkmark \end{aligned}

Where does the rule come from? Eliminate yy from the general system (multiply the first equation by b2b_2, the second by b1b_1, subtract) and you get (a1b2−a2b1)x=c1b2−c2b1(a_1 b_2 - a_2 b_1)x = c_1 b_2 - c_2 b_1, which is exactly D⋅x=DxD \cdot x = D_x. Cramer's rule is elimination done once, in general, and saved as a formula.

python · try it yourself
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 x=913x = \frac{9}{13}, y=−713y = -\frac{7}{13}; Cramer's rule hands you the exact fractions without any messy intermediate steps.

When D=0D = 0

If D=0D = 0 the two left-hand sides are multiples of each other, so the lines are parallel or identical (see Systems of Equations):

DD DxD_x, DyD_y Meaning
≠0\ne 0 anything exactly one solution
0 at least one ≠0\ne 0 parallel lines, no solution
0 both 0 the same line, infinitely many solutions

In the third call above, x+2y=3x + 2y = 3 and 2x+4y=52x + 4y = 5: D=4−4=0D = 4 - 4 = 0 while Dx=12−10=2≠0D_x = 12 - 10 = 2 \ne 0, so the lines are parallel.

Note: The determinant also has a picture. ∣D∣\lvert D \rvert is the area of the parallelogram spanned by the column vectors (a1,a2)(a_1, a_2) and (b1,b2)(b_1, b_2). D=0D = 0 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 a⋅x+b⋅y=ca \cdot x + b \cdot y = c (the "implicit" form). Two such lines meet where both equations hold, so the meeting point is a Cramer's rule calculation, and D=0D = 0 is the "parallel, no hit" test:

python · try it yourself
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 (0,0)(0, 0) through (6,3)(6, 3) hits the wall from (0,4)(0, 4) to (8,0)(8, 0) at (4,2)(4, 2). With floating-point coordinates, DD is rarely exactly 0, so real code compares ∣D∣\lvert D \rvert 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 ∣7325∣\begin{vmatrix} 7 & 3 \\ 2 & 5 \end{vmatrix}, ∣−143−2∣\begin{vmatrix} -1 & 4 \\ 3 & -2 \end{vmatrix} and ∣6−3−42∣\begin{vmatrix} 6 & -3 \\ -4 & 2 \end{vmatrix}.

Answer

29, −10 and 0.

Solution
7⋅5−3⋅2=35−6=29(−1)(−2)−4⋅3=2−12=−106⋅2−(−3)(−4)=12−12=0\begin{aligned} 7 \cdot 5 - 3 \cdot 2 &= 35 - 6 = 29 \\ (-1)(-2) - 4 \cdot 3 &= 2 - 12 = -10 \\ 6 \cdot 2 - (-3)(-4) &= 12 - 12 = 0 \end{aligned}

Exercise 2: Cramer by hand

Solve 2x+y=12x + y = 1 and 3x−4y=183x - 4y = 18 with determinants.

Answer

x=2x = 2, y=−3y = -3.

Solution
D=2⋅(−4)−1⋅3=−8−3=−11Dx=1⋅(−4)−1⋅18=−4−18=−22x=−22−11=2Dy=2⋅18−1⋅3=36−3=33y=33−11=−3check: 4−3=1 ✓6+12=18 ✓\begin{aligned} D &= 2 \cdot (-4) - 1 \cdot 3 = -8 - 3 = -11 \\ D_x &= 1 \cdot (-4) - 1 \cdot 18 = -4 - 18 = -22 && x = \tfrac{-22}{-11} = 2 \\ D_y &= 2 \cdot 18 - 1 \cdot 3 = 36 - 3 = 33 && y = \tfrac{33}{-11} = -3 \\ &\text{check: } 4 - 3 = 1\ \checkmark \quad 6 + 12 = 18\ \checkmark \end{aligned}

Exercise 3: A fraction answer

Solve 5x+2y=35x + 2y = 3 and 3x+4y=−13x + 4y = -1. Check with the cramer2 function from the lesson.

Answer

x=1x = 1, y=−1y = -1.

Solution
D=5⋅4−2⋅3=14Dx=3⋅4−2⋅(−1)=14x=1Dy=5⋅(−1)−3⋅3=−14y=−1\begin{aligned} D &= 5 \cdot 4 - 2 \cdot 3 = 14 \\ D_x &= 3 \cdot 4 - 2 \cdot (-1) = 14 && x = 1 \\ D_y &= 5 \cdot (-1) - 3 \cdot 3 = -14 && y = -1 \end{aligned}
python · try it yourself
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 kk does the system kx+3y=2kx + 3y = 2, 4x+6y=54x + 6y = 5 have no unique solution? What happens then?

Answer

k=2k = 2; the lines are then parallel and there is no solution.

Solution
D=6k−12=0→k=2with k=2:Dx=2⋅6−3⋅5=−3≠0→no solution (parallel lines)\begin{aligned} &D = 6k - 12 = 0 \to k = 2 \\ &\text{with } k = 2\text{:}\quad D_x = 2 \cdot 6 - 3 \cdot 5 = -3 \ne 0 \to \text{no solution (parallel lines)} \end{aligned}

Indeed, 2x+3y=22x + 3y = 2 doubled is 4x+6y=44x + 6y = 4, not 5.

Test yourself

What is ∣2−534∣\begin{vmatrix} 2 & -5 \\ 3 & 4 \end{vmatrix}?

  1. −7
  2. 23
  3. 8
  4. −23

In Cramer's rule for a1x+b1y=c1a_1 x + b_1 y = c_1, a2x+b2y=c2a_2 x + b_2 y = c_2, how is DyD_y built?

  1. Replace the yy-column (b1,b2)(b_1, b_2) of DD by the constants (c1,c2)(c_1, c_2)
  2. Replace the xx-column by the constants
  3. Swap the two rows of DD
  4. Multiply DD by yy

Line-intersection code finds D=0D = 0. What should it report?

  1. The intersection is at the origin
  2. The lines are parallel (or identical): no single intersection point
  3. x=y=1x = y = 1
  4. The lines are perpendicular