TExcelTAD
Home › Linear Algebra & Matrix Theory › Determinants and Row Operations

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 AA be square and let BB be obtained from AA by one row operation (Elementary Operations):

Row operation Effect on the determinant
swap two rows det⁡B=−det⁡A\det B = \boldsymbol{-\det A}
multiply a row by cc det⁡B=c⋅det⁡A\det B = \boldsymbol{c \cdot \det A}
add a multiple of one row to another det⁡B=det⁡A\det B = \boldsymbol{\det A} (unchanged)

Quick consequences:

  • A matrix with two equal rows has determinant 0 (swapping them changes nothing, yet flips the sign, so det⁡=−det⁡\det = -\det).
  • A matrix with a zero row has determinant 0 (scale that row by 0).
  • det⁡(cA)=cndet⁡A\det(cA) = c^n \det A for an n×nn \times n matrix, because every one of the nn rows is scaled by cc.

Since det⁡AT=det⁡A\det A^T = \det A (Properties of Determinants), the same three rules hold for column operations.

python · try it yourself
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 AA to an upper triangular UU using only row additions and swaps (no scaling needed). If you used ss swaps,

det⁡A=(−1)s⋅u11⋅u22⋅…⋅unn\det A = (-1)^s \cdot u_{11} \cdot u_{22} \cdot \ldots \cdot u_{nn}

Worked example:

A=[021132247]→swap R1,R2[132021247](s=1)A = \begin{bmatrix} 0 & 2 & 1 \\ 1 & 3 & 2 \\ 2 & 4 & 7 \end{bmatrix} \xrightarrow{\text{swap } R_1, R_2} \begin{bmatrix} 1 & 3 & 2 \\ 0 & 2 & 1 \\ 2 & 4 & 7 \end{bmatrix} \qquad (s = 1)
R3−2R1→[0,−2,3](det unchanged)R3+R2→[0,0,4](det unchanged)U=[132021004]det⁡A=(−1)1⋅1⋅2⋅4=−8\begin{aligned} R_3 - 2R_1 &\to [0, -2, 3] && \text{(det unchanged)} \\ R_3 + R_2 &\to [0, 0, 4] && \text{(det unchanged)} \\[1ex] U &= \begin{bmatrix} 1 & 3 & 2 \\ 0 & 2 & 1 \\ 0 & 0 & 4 \end{bmatrix} \qquad \det A = (-1)^1 \cdot 1 \cdot 2 \cdot 4 = -8 \end{aligned}

Here is the algorithm in code with exact fractions; it is O(n3)O(n^3):

python · try it yourself
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

Invertible  ⟺  det⁡≠0\text{Invertible} \iff \det \ne 0

Theorem. A square matrix AA is invertible if and only if det⁡A≠0\det A \ne 0.

Proof: row-reduce AA to its reduced echelon form RR. Each operation multiplies the determinant by a non-zero number (−1-1, c≠0c \ne 0, or 1), so det⁡A≠0\det A \ne 0 exactly when det⁡R≠0\det R \ne 0. RR is either II (det⁡1\det 1) when AA is invertible, or has a zero row (det⁡0\det 0) 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, det⁡A=0\det A = 0 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 cnc^n:

python · try it yourself
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

det⁡A=5\det A = 5 for a 3×33 \times 3 matrix AA. Find det of: (a) AA with rows 1 and 3 swapped, (b) AA with row 2 multiplied by −4-4, (c) AA with 7⋅(row 1)7 \cdot (\text{row } 1) added to row 2, (d) 2A2A.

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: 23⋅5=402^3 \cdot 5 = 40.

Exercise 2: Reduce and compute

Compute det⁡[246159314]\det \begin{bmatrix} 2 & 4 & 6 \\ 1 & 5 & 9 \\ 3 & 1 & 4 \end{bmatrix} by row reduction.

Answer

det⁡=30\det = 30.

Solution

Factor 2 out of row 1 first (det⁡=2⋅det⁡\det = 2 \cdot \det of the new matrix):

2⋅det⁡[123159314]R2−R1→[0,3,6];R3−3R1→[0,−5,−5]R3+53R2→[0,0,−5+10]=[0,0,5]det⁡=2⋅(1⋅3⋅5)=30\begin{aligned} &2 \cdot \det \begin{bmatrix} 1 & 2 & 3 \\ 1 & 5 & 9 \\ 3 & 1 & 4 \end{bmatrix} \\ &R_2 - R_1 \to [0, 3, 6]; \quad R_3 - 3R_1 \to [0, -5, -5] \\ &R_3 + \tfrac{5}{3}R_2 \to [0, 0, -5 + 10] = [0, 0, 5] \\ &\det = 2 \cdot (1 \cdot 3 \cdot 5) = 30 \end{aligned}

Check with code:

python · try it yourself
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 det⁡[12345678910111213141516]=0\det \begin{bmatrix} 1 & 2 & 3 & 4 \\ 5 & 6 & 7 & 8 \\ 9 & 10 & 11 & 12 \\ 13 & 14 & 15 & 16 \end{bmatrix} = 0.

Answer

The rows are dependent (R3−R2=R2−R1R_3 - R_2 = R_2 - R_1), so row operations produce a zero row.

Solution

R2−R1=[4,4,4,4]R_2 - R_1 = [4, 4, 4, 4] and R3−R2=[4,4,4,4]R_3 - R_2 = [4, 4, 4, 4]. Replacing R3R_3 by R3−R2R_3 - R_2 and R2R_2 by R2−R1R_2 - R_1 (det unchanged) gives two equal rows, and then R3−R2R_3 - R_2 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 [001010100]\begin{bmatrix} 0 & 0 & 1 \\ 0 & 1 & 0 \\ 1 & 0 & 0 \end{bmatrix}.

Answer

One swap, determinant −1.

Solution

Swapping rows 1 and 3 already gives II, so s=1s = 1 and det⁡=(−1)1=−1\det = (-1)^1 = -1.

python · try it yourself
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?

  1. Multiplies it by 5
  2. Leaves it unchanged
  3. Negates it
  4. Adds 5 to it

AA is 4×44 \times 4 with det⁡A=3\det A = 3. What is det⁡(−A)\det(-A)?

  1. 3
  2. −3
  3. −12
  4. 81

Why is checking abs(np.linalg.det(A)) < 1e-10 a bad singularity test?

  1. det is always exact
  2. It is too slow
  3. NumPy cannot compute determinants
  4. The determinant's size depends on scaling: 0.1⋅I1000.1 \cdot I_{100} has det 10−10010^{-100} but is perfectly invertible