Solving Systems by Elimination#
A linear system asks for a vector whose linear combination of the columns of equals the right-hand side . Gaussian elimination is the algorithm that turns this into an equivalent triangular system you can solve by back-substitution. The idea is simple: use each pivot to clear every entry below it, one column at a time, until only an upper triangular matrix remains.
Worked example. Solve with
Write the augmented matrix :
Column 1. The first pivot is . Multipliers for the rows below are
Replace row 2 by and row 3 by :
Column 2. The second pivot is now . The multiplier for row 3 is . Replace row 3 by :
The coefficient matrix is now upper triangular:
Back-substitution. Solve from the bottom up:
So
Check: . The pivots of are , , and — the successive diagonal entries used to divide when eliminating below. If any pivot is zero, this process stalls; we return to that case in the last section.
Elimination did not change the solution set: each row operation is reversible (add back the multiple you subtracted), so and have exactly the same .
Elimination as Matrix Multiplication#
Every elimination step is left-multiplication by an elementary matrix — one matrix per row operation. Week 2 treated matrix multiplication as composition of linear maps; the product of the step matrices encodes the whole elimination.
To subtract times row from row , multiply on the left by the matrix that looks like the identity except for a single nonzero off-diagonal entry in position . For our example:
In general, when the current pivot is (after previous steps) and the entry below it is , the multiplier is
and has in position . Applying all three steps to yields
Write for the product of all elementary matrices. Then the full elimination is the single matrix equation
The same acts on : , the right-hand side of the eliminated system from the worked example. Each step adds a multiple of an earlier row to a later one, so is lower triangular with 's on the diagonal; each is invertible — put back where was — and so is their product .
Small check. For , the only multiplier is . Then
That multiplier is the quantity the exercise below asks for.
Elimination on A = [[2,1],[4,3]] uses the multiplier ℓ = a21/a11 to clear the entry below the first pivot. What is ℓ?
The LU Factorization#
From we can recover by inverting :
Define . Then
This is the LULower-Upper (matrix factorization) factorization of (when no row exchanges are needed). Because is unit lower triangular, so is : it has 's on the diagonal and the elimination multipliers below the diagonal (with positive signs). For the example,
The subdiagonal entries of are exactly , , — the multipliers we computed while eliminating. You never need to form and invert it by hand: record the multipliers as you go and place them under the diagonal of ; the upper triangular result is .
Verify . The entry of is , matching ; the rest of the product recovers column by column the same way.
Why factor once? Solving for many different right-hand sides reuses the same and :
- Forward substitution: solve for (cheap, because is triangular with unit diagonal).
- Back-substitution: solve for .
Factoring costs : about entries lie below the diagonal, and clearing each one costs an row update. Each subsequent solve costs only — forward and back substitution each perform about multiply–adds. In robotics, simulation, and least-squares pipelines where is fixed and changes often, this amortization is the reason is a workhorse factorization. Week 4 will also use the pivots on the diagonal of : when no row exchanges occur, equals the product of those pivots.
Row Exchanges and PA=LU#
Plain elimination fails when a pivot position becomes zero (or is so small that dividing by it is numerically unsafe) — no multiplier exists when . The fix is a row exchange: swap the current row with a lower row that has a nonzero entry in the pivot column, then continue.
A row exchange is left-multiplication by a permutation matrix — the identity with two rows swapped (or a product of such swaps). For example, swapping rows 1 and 2 of a system uses
After the necessary permutations, elimination proceeds without division by zero and produces and for the permuted matrix. The general factorization is
where is a permutation matrix, is unit lower triangular, and is upper triangular. Equivalently, (since for a permutation matrix).
When do you need ? Whenever a pivot is zero (or tiny) while a nonzero candidate sits below it. If an entire pivot column from the diagonal downward is zero, the matrix is singular: no sequence of row exchanges produces a full set of nonzero pivots, and either has no solution or infinitely many. Week 5 will connect that failure mode to rank and linear dependence.
Practical note. Production libraries often use partial pivoting (swap in the largest available entry below the diagonal) and return , , with . That can reorder rows even when every pivot is already nonzero, so the printed and may differ from a hand calculation without row swaps — both factorizations are valid. The browser lab below builds and with plain elimination (no pivoting) in NumPy so the factors match the worked example above; for this , and still holds.
Knowledge check#
In A = LU, what type of matrix is L?
Browser lab: LU factorization#
Compute an factorization with NumPy-only elimination (same as the worked example) and verify with .
import numpy as np
A = np.array([[2., 1., 1.],
[4., 3., 3.],
[8., 7., 9.]])
n = A.shape[0]
U = A.copy()
L = np.eye(n)
P = np.eye(n) # no row exchanges needed for this A
for k in range(n - 1):
for i in range(k + 1, n):
L[i, k] = U[i, k] / U[k, k]
U[i, k:] -= L[i, k] * U[k, k:]
print("L =\n", L)
print("U =\n", U)
print("P (identity) =\n", P)
print("L @ U =\n", L @ U)
print("PA ≈ LU:", np.allclose(P @ A, L @ U))
print("LU ≈ A:", np.allclose(L @ U, A))
print("Notice: PA ≈ LU and LU ≈ A both print True — the NumPy elimination reproduces A to floating point.")
Done when#
You can run Gaussian elimination to an upper-triangular , read the multipliers into , and confirm (or ) by multiplying back. Check it against the browser lab: both np.allclose checks print True, and the output matches and .
Further Reading#
- Gilbert Strang, Introduction to Linear Algebra, 6th ed., Ch. 2 — the classic elimination and treatment used in MIT 18.06.
- Lloyd N. Trefethen and David Bau III, Numerical Linear Algebra, Ch. 20–21 — computational perspective on with pivoting and stability.
- Gene H. Golub and Charles F. Van Loan, Matrix Computations, 4th ed., Ch. 3 — authoritative reference on Gaussian elimination and pivoting strategies.
- Ian Goodfellow, Yoshua Bengio, and Aaron Courville, Deep Learning, Ch. 2 — linear algebra review directed at modern machine learning practitioners.