Gaussian Elimination
March 21, 2026 · View on GitHub
Overview & Motivation
Gaussian elimination is the standard direct method for solving a system of linear equations . It transforms the coefficient matrix into upper-triangular form through systematic row operations, then solves via back-substitution.
It serves as the foundational linear solver in this library — used inside the normal equation solver for Linear Regression, inside the DARE iteration for LQR, and anywhere else a small dense linear system arises at runtime.
The implementation uses partial pivoting (selecting the largest-magnitude entry in each column as pivot) to improve numerical stability.
Mathematical Theory
Problem Statement
Solve where is non-singular, .
Forward Elimination with Partial Pivoting
For each column :
- Pivot selection: Find and swap rows and .
- Elimination: For each row :
After all columns are processed, is upper-triangular: .
Back-Substitution
Multi-Column Extension
For where has columns, each column is solved independently.
Complexity Analysis
| Phase | Time | Space | Notes |
|---|---|---|---|
| Forward elimination | In-place on copies of and | ||
| Back-substitution | |||
| Multi-column solve | back-substitutions |
Why : The elimination has stages; stage performs multiplications. Summing: .
Step-by-Step Walkthrough
System:
Step 1 — Column 0: pivot selection
, (largest), . Swap rows 0 and 1:
Step 2 — Eliminate below pivot
- Row 1: . Row 1 += × Row 0 →
- Row 2: . Row 2 -= × Row 0 →
Step 3 — Column 1: pivot selection
. Swap rows 1 and 2.
Step 4 — Eliminate below:
. Row 2 -= × Row 1 → . Upper-triangular form reached.
Step 5 — Back-substitution:
Verification: $2(2) + 1(3) + (-1)(-1) = 8;-3(2) - 1(3) + 2(-1) = -11;-2(2) + 1(3) + 2(-1) = -3$ ✓.
Pitfalls & Edge Cases
- Singular matrices. If a zero pivot is encountered after pivoting, the matrix is singular (or near-singular). The solver asserts non-zero pivots in debug builds.
- Ill-conditioning. Even with pivoting, matrices with condition number yield inaccurate solutions. Monitor or use iterative refinement.
- Fixed-point overflow. The multiplier and the elimination update involve divisions and multiply-accumulate operations that can overflow Q15/Q31 ranges. Scale the system if necessary.
- Near-zero pivots without pivoting. Without partial pivoting, small pivots amplify round-off errors. Always use pivoting.
- Symmetric positive-definite systems. Gaussian elimination works but is not optimal — Cholesky factorization is twice as fast and maintains symmetry.
Variants & Generalizations
| Variant | Key Difference |
|---|---|
| Full pivoting | Pivots on both rows and columns; more stable but rarely needed in practice |
| LU factorization | Stores the and factors for reuse; solves multiple right-hand sides efficiently |
| Cholesky factorization | Specialized for symmetric positive-definite matrices; instead of |
| Gauss-Jordan elimination | Reduces to reduced row echelon form (identity matrix); used for matrix inversion |
| Iterative refinement | Solves once, then iteratively corrects the residual to improve accuracy |
Applications
- Normal equation solver — Used by Linear Regression to solve .
- DARE sub-problem — Each iteration of the DARE solver requires solving a linear system.
- LQR gain computation — The optimal gain involves a linear solve.
- Filter design — Solving Vandermonde or interpolation systems for filter coefficient calculation.
Connections to Other Algorithms
graph LR
GE["Gaussian Elimination"]
LR["Linear Regression"]
DARE["DARE Solver"]
LQR["LQR Controller"]
LD["Levinson-Durbin"]
LR --> GE
DARE --> GE
LD -.->|"Toeplitz-specialized alternative"| GE
| Algorithm | Relationship |
|---|---|
| Linear Regression | Uses Gaussian elimination to solve the normal equation |
| DARE Solver | Calls Gaussian elimination at each Riccati iteration |
| Levinson-Durbin | Specialized solver for Toeplitz systems; Gaussian elimination is the fallback |
References & Further Reading
- Golub, G.H. and Van Loan, C.F., Matrix Computations, 4th ed., Johns Hopkins University Press, 2013 — Chapter 3.
- Trefethen, L.N. and Bau, D., Numerical Linear Algebra, SIAM, 1997 — Lectures 20–23.
- Higham, N.J., Accuracy and Stability of Numerical Algorithms, 2nd ed., SIAM, 2002.