Gaussian elimination is the standard direct method for solving a system of linear equations
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.
Solve
For each column
-
Pivot selection: Find
$p = \arg\max_{i \geq k} |A_{ik}|$ and swap rows$k$ and$p$ . -
Elimination: For each row
$i > k$ :
After all columns are processed,
For
| Phase | Time | Space | Notes |
|---|---|---|---|
| Forward elimination | In-place on copies of |
||
| Back-substitution | |||
| Multi-column solve |
|
Why
System:
Step 1 — Column 0: pivot selection
Step 2 — Eliminate below pivot
- Row 1:
$\ell = 2/(-3) = -2/3$ . Row 1 +=$(2/3)$ × Row 0 →$[0,; 1/3,; 1/3 \mid 2/3]$ - Row 2:
$\ell = -2/(-3) = 2/3$ . Row 2 -=$(2/3)$ × Row 0 →$[0,; 5/3,; 2/3 \mid 13/3]$
Step 3 — Column 1: pivot selection
Step 4 — Eliminate below:
Step 5 — Back-substitution:
Verification:
- 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
$\kappa(A) \gg 1$ yield inaccurate solutions. Monitor$\kappa(A)$ or use iterative refinement. -
Fixed-point overflow. The multiplier
$\ell_{ik}$ 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.
| Variant | Key Difference |
|---|---|
| Full pivoting | Pivots on both rows and columns; more stable but rarely needed in practice |
| LU factorization | Stores the |
| Cholesky factorization | Specialized for symmetric positive-definite matrices; |
| 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 |
-
Normal equation solver — Used by Linear Regression to solve
$(\mathbf{X}^T\mathbf{X})\boldsymbol{\beta} = \mathbf{X}^T\mathbf{y}$ . - DARE sub-problem — Each iteration of the DARE solver requires solving a linear system.
-
LQR gain computation — The optimal gain
$K = (R + B^T P B)^{-1} B^T P A$ involves a linear solve. - Filter design — Solving Vandermonde or interpolation systems for filter coefficient calculation.
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 |
- 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.