Determinants and Row Operations
Each elementary row operation changes the determinant in a simple, predictable way. Combined with the fact that triangular determinants are products of diagonals, this gives the fast way to compute determinants: row-reduce, keep a tally, multiply the diagonal. It also proves that a matrix is invertible exactly when its determinant is non-zero.
The three rules
Let be square and let be obtained from by one row operation (Elementary Operations):
| Row operation | Effect on the determinant |
|---|---|
| swap two rows | |
| multiply a row by | |
| add a multiple of one row to another | (unchanged) |
Quick consequences:
- A matrix with two equal rows has determinant 0 (swapping them changes nothing, yet flips the sign, so ).
- A matrix with a zero row has determinant 0 (scale that row by 0).
- for an matrix, because every one of the rows is scaled by .
Since (Properties of Determinants), the same three rules hold for column operations.
from sympy import Matrix
A = Matrix([[1, 2, 3], [0, 4, 5], [2, 1, 1]])
print("det A:", A.det())
B = A.copy(); B.row_swap(0, 2)
print("swap R1, R3 :", B.det())
C = A.copy(); C[1, :] = 3 * C[1, :]
print("3 * R2 :", C.det())
D = A.copy(); D[2, :] = D[2, :] - 2 * D[0, :]
print("R3 - 2 R1 :", D.det())
print("det(2A) = 2^3 det A:", (2 * A).det(), 2**3 * A.det())det A: -5
swap R1, R3 : 5
3 * R2 : -15
R3 - 2 R1 : -5
det(2A) = 2^3 det A: -40 -40
Computing a determinant by row reduction
Reduce to an upper triangular using only row additions and swaps (no scaling needed). If you used swaps,
Worked example:
Here is the algorithm in code with exact fractions; it is :
from fractions import Fraction
from sympy import Matrix
def det_by_elimination(rows):
M = [[Fraction(x) for x in r] for r in rows]
n, sign = len(M), 1
for col in range(n):
piv = next((r for r in range(col, n) if M[r][col] != 0), None)
if piv is None:
return Fraction(0) # no pivot: singular
if piv != col:
M[col], M[piv] = M[piv], M[col]
sign = -sign # a swap flips the sign
for r in range(col + 1, n):
f = M[r][col] / M[col][col]
M[r] = [a - f * b for a, b in zip(M[r], M[col])] # det unchanged
prod = Fraction(sign)
for i in range(n):
prod *= M[i][i]
return prod
A = [[0, 2, 1], [1, 3, 2], [2, 4, 7]]
print(det_by_elimination(A), Matrix(A).det())
B = [[2, -1, 0, 3], [4, 1, 5, -2], [-2, 3, 1, 1], [6, 0, 2, 4]]
print(det_by_elimination(B), Matrix(B).det())-8 -8
126 126
Theorem. A square matrix is invertible if and only if .
Proof: row-reduce to its reduced echelon form . Each operation multiplies the determinant by a non-zero number (, , or 1), so exactly when . is either () when is invertible, or has a zero row () when it is not. ∎
This adds another item to the invertible matrix theorem (Matrix Inverses).
In software: det is a poor singularity test
In exact arithmetic, means singular. In floating point, the size of the determinant says almost nothing about how close to singular a matrix is, because det scales like :
import numpy as np
n = 100
A = 0.1 * np.eye(n) # perfectly well-conditioned, inverse is 10 I
print("det(0.1 I):", float(np.linalg.det(A)), " cond:", float(np.linalg.cond(A)))
B = np.array([[1.0, 1.0], [1.0, 1.0 + 1e-12]]) # nearly singular
print(f"det(B): {np.linalg.det(B):.1e} cond: {np.linalg.cond(B):.1e}")det(0.1 I): 1.00000000000033e-100 cond: 1.0
det(B): 1.0e-12 cond: 4.0e+12
The first matrix has a tiny determinant and is perfectly safe to invert; the second has a larger determinant but is on the edge of singularity. Use the condition number (np.linalg.cond) or np.linalg.matrix_rank (which uses singular values with a tolerance) to judge singularity numerically (Numerical Linear Algebra). np.linalg.det itself is computed by LU factorisation, exactly the elimination above with partial pivoting.
Exercises
Exercise 1: Predict the change
for a matrix . Find det of: (a) with rows 1 and 3 swapped, (b) with row 2 multiplied by , (c) with added to row 2, (d) .
Answer
(a) −5, (b) −20, (c) 5, (d) 40.
Solution
(a) a swap negates: −5. (b) scaling one row by −4: −20. (c) row addition: unchanged, 5. (d) all three rows scaled by 2: .
Exercise 2: Reduce and compute
Compute by row reduction.
Answer
.
Solution
Factor 2 out of row 1 first ( of the new matrix):
Check with code:
from sympy import Matrix
print(Matrix([[2, 4, 6], [1, 5, 9], [3, 1, 4]]).det())30
Exercise 3: Spot the zero
Without computing, explain why .
Answer
The rows are dependent (), so row operations produce a zero row.
Solution
and . Replacing by and by (det unchanged) gives two equal rows, and then gives a zero row. A matrix with a zero row has determinant 0.
Exercise 4: Count swaps
Using det_by_elimination as a model, write a version that also returns how many row swaps it made, and run it on .
Answer
One swap, determinant −1.
Solution
Swapping rows 1 and 3 already gives , so and .
from fractions import Fraction
def det_swaps(rows):
M = [[Fraction(x) for x in r] for r in rows]; n = len(M); swaps = 0
for c in range(n):
p = next((r for r in range(c, n) if M[r][c] != 0), None)
if p is None:
return 0, swaps
if p != c:
M[c], M[p] = M[p], M[c]; swaps += 1
for r in range(c + 1, n):
f = M[r][c] / M[c][c]
M[r] = [a - f * b for a, b in zip(M[r], M[c])]
d = (-1) ** swaps
for i in range(n):
d *= M[i][i]
return d, swaps
print(det_swaps([[0, 0, 1], [0, 1, 0], [1, 0, 0]]))(Fraction(-1, 1), 1)
Test yourself
Adding 5 times row 1 to row 3 changes the determinant how?
- Multiplies it by 5
- Leaves it unchanged
- Negates it
- Adds 5 to it
is with . What is ?
- 3
- −3
- −12
- 81
Why is checking abs(np.linalg.det(A)) < 1e-10 a bad singularity test?
- det is always exact
- It is too slow
- NumPy cannot compute determinants
- The determinant's size depends on scaling: has det but is perfectly invertible