Minors and Cofactors
Cofactor expansion defines the determinant of any matrix in terms of determinants of matrices. It is the natural recursive definition, it lets you exploit rows full of zeros, and it is hopelessly slow for large matrices, which is a lesson in itself.
Minors and cofactors
For an matrix :
- the minor is the determinant of the matrix obtained by deleting row and column ;
- the cofactor is .
The sign follows a checkerboard pattern starting with in the top-left corner:
Example with : deleting row 1 and column 2 leaves , so and .
Cofactor (Laplace) expansion
Theorem. For any fixed row ,
and for any fixed column ,
Every row and every column gives the same answer. For and , expanding along row 1 reproduces the formulas of the previous lesson. For we can take this as the definition of .
Strategy: expand along the row or column with the most zeros, because a zero entry kills its whole term.
from sympy import Matrix
A = Matrix([[3, 0, 2, -1],
[1, 2, 0, -2],
[4, 0, 6, -3],
[5, 0, 2, 0]])
# column 2 (index 1) has three zeros: only the entry a22 = 2 contributes
M22 = A.minor_submatrix(1, 1)
print("minor submatrix M22:", M22.tolist())
print("M22 =", M22.det(), " C22 =", A.cofactor(1, 1))
print("det via column 2:", A[1, 1] * A.cofactor(1, 1))
# expanding along any row or column gives the same number
rows = [sum(A[i, j] * A.cofactor(i, j) for j in range(4)) for i in range(4)]
cols = [sum(A[i, j] * A.cofactor(i, j) for i in range(4)) for j in range(4)]
print("all rows:", rows, " all columns:", cols, " det:", A.det())minor submatrix M22: [[3, 2, -1], [4, 6, -3], [5, 2, 0]]
M22 = 10 C22 = 10
det via column 2: 20
all rows: [20, 20, 20, 20] all columns: [20, 20, 20, 20] det: 20
A recursive implementation and its cost
Cofactor expansion is a natural recursive function: an determinant calls determinants of size , each of which calls of size , and so on, down to 1×1. That is about terms.
from math import factorial
calls = 0
def det(M):
global calls
calls += 1
n = len(M)
if n == 1:
return M[0][0]
total = 0
for j in range(n):
minor = [row[:j] + row[j + 1:] for row in M[1:]] # delete row 0 and column j
total += (-1) ** j * M[0][j] * det(minor)
return total
for n in range(2, 9):
calls = 0
M = [[(i * 7 + j * 3) % 5 + (i == j) for j in range(n)] for i in range(n)]
d = det(M)
print(f"n = {n}: det = {d:4d}, recursive calls = {calls:6d}, n! = {factorial(n)}")n = 2: det = -5, recursive calls = 3, n! = 2
n = 3: det = 25, recursive calls = 10, n! = 6
n = 4: det = -104, recursive calls = 41, n! = 24
n = 5: det = 451, recursive calls = 206, n! = 120
n = 6: det = 1006, recursive calls = 1237, n! = 720
n = 7: det = 2245, recursive calls = 8660, n! = 5040
n = 8: det = 5005, recursive calls = 69281, n! = 40320
The number of calls grows like . For that is about calls: centuries of computing. Row reduction does the same job in about operations, 2 700 for (Determinants and Row Operations). Application: Cramer's Rule times the difference.
Why expansion along any row works (sketch)
The determinant can be characterised by three properties: it is linear in each row, it changes sign when two rows are swapped, and . Expansion along row 1 satisfies all three, and moving row to the top takes swaps, which produces exactly the sign that turns row-1 expansion into row- expansion. Columns follow from (Properties of Determinants).
In software
- Symbolic math uses cofactor-style expansion for small matrices with symbolic entries (where division would create messy fractions); for numbers, every serious library uses LU factorisation.
- Fixed-size 2×2, 3×3, 4×4 code in game math libraries writes out the cofactor expansion explicitly. With that is a few dozen multiplications, faster than a general LU routine, and it reuses the 2×2 minors to compute the inverse too (Matrix Inverse).
- Algorithm analysis: the recursion above is a textbook example of factorial complexity, alongside brute-force traveling salesman.
Exercises
Exercise 1: A minor and a cofactor
For , find and .
Answer
, .
Solution
Exercise 2: Expand along the best row
Compute by expanding along column 2.
Answer
.
Solution
Only is non-zero in column 2, with sign :
from sympy import Matrix
print(Matrix([[2, 0, 1], [3, 0, -4], [1, 5, 2]]).det())55
Exercise 3: A 4×4 by hand
Compute .
Answer
.
Solution
Expand along row 2 (only , sign ), then along the last row of the 3×3 minor:
Exercise 4: Count the work
Modify the recursive det so that it skips the recursive call whenever M[0][j] is 0. How many calls does it make on the 4×4 matrix of Exercise 3, compared with 41 without the shortcut?
Answer
8 calls instead of 41: zeros in the first row prune most of the recursion tree.
Solution
calls = 0
def det(M):
global calls
calls += 1
if len(M) == 1:
return M[0][0]
total = 0
for j, a in enumerate(M[0]):
if a == 0:
continue # zero entry: the whole term vanishes
minor = [row[:j] + row[j + 1:] for row in M[1:]]
total += (-1) ** j * a * det(minor)
return total
A = [[1, 0, 0, 2], [0, 3, 0, 0], [4, 0, 1, 0], [0, 0, 0, 5]]
print("det =", det(A), " calls =", calls)det = 15 calls = 8
Test yourself
What is the sign of the cofactor ?
- It depends on the entry
- 0
Which row or column should you expand along to compute a determinant by hand?
- The one with the most zeros
- Always the first row
- The one with the largest numbers
- The main diagonal
How does the work of plain cofactor expansion grow with ?
- Like
- Like
- Like
- Like