Solving Linear Systems (Ax = b), Gaussian Elimination & Inverses
Row vs. Column geometry, the 3 outcomes of Ax = b, step-by-step Gaussian elimination, matrix inverses, and LU/Cholesky solvers
Solving Ax = b is how Linear Regression, Ridge, Kalman Filters, and Newton's Method find exact optimal parameters!
Never write `np.linalg.inv(A) @ b` in production! Understanding LU/Cholesky elimination explains why `np.linalg.solve(A, b)` is 3× faster and vastly more numerically accurate.
Knowing when Ax = b has 0, 1, or ∞ solutions immediately diagnoses why your regression model threw `LinAlgError: Singular matrix` on real tabular data.
A top ML systems question: 'Why shouldn't you explicitly invert (XᵀX) to solve OLS regression, and what should you use instead?' (Answer: `np.linalg.solve` / QR / SVD!).
You need to solve Ax = b in Python/NumPy for a 2,000 × 2,000 matrix A. Which line of code is BOTH faster AND more numerically accurate?
- Translate any system of linear equations into matrix form Ax = b and visualize both the Row Picture and Column Picture.
- Classify any system immediately into its 3 possible outcomes: Unique solution, No solution (inconsistent), or Infinitely many solutions.
- Solve linear systems by hand using Augmented Matrices [A | b], Elementary Row Operations, Gaussian Elimination, and Back-Substitution.
- Compute 2×2 matrix inverses by hand and understand why production ML uses LU/Cholesky `np.linalg.solve` instead of explicit `np.linalg.inv`.
Solving is the central computational problem of Linear Algebra. Whether you are finding the exact intersection of two lines in high school algebra or solving a 10,000-variable Ridge regression system in production, the exact same geometry and elimination mechanics apply!
1. Two Geometric Pictures of Ax = b & Its 3 Possible Outcomes
Suppose you buy 1 coffee + 1 muffin for $4 (x + y = 4), and your friend tells you coffee costs the same as a muffin (x − y = 0). Instantly you know both cost $2! Each equation is a straight line, and solving Ax = b is literally asking: where do the lines cross?
Switch between the 3 fates of ANY linear system below!
Consider the tiny 2-equation system:
There are two powerful ways to picture :
- The Row Picture (Intersecting Lines/Planes): Each row is one linear equation — a line in 2D (or a plane in 3D). The solution is the point where all lines intersect!
- The Column Picture (Linear Combination): Rewrite as: We are asking: "Which weights combine the columns of to hit target ?"
Every Linear System Has Exactly ONE of 3 Outcomes
No matter if you have 2 variables or 2 million variables, can NEVER have 2 or 3 distinct solutions — only:
- Exactly 1 Unique Solution: Lines cross at a single point (, full rank).
- Zero Solutions (Inconsistent): Parallel lines that never touch (e.g., and ).
- Infinitely Many Solutions: Redundant equations describing the same line/plane (at least one free variable).
Each equation gives genuinely NEW information — they meet cleanly at (x=2, y=2)!
Without solving completely, classify the system: (1) x + 2y = 4 and (2) 2x + 4y = 10. Does it have 1, 0, or infinitely many solutions?
2. Gaussian Elimination & Row Echelon Form (Step-by-Step)
How does a computer solve 1,000 equations with 1,000 unknowns without guessing? Gaussian Elimination — the exact high-school trick of subtracting multiples of Equation 1 from Equation 2 to knock out variables one by one until the system looks like a neat staircase (upper triangular) that unravels from the bottom up!
To solve systematically for any size , pack and into an Augmented Matrix and apply the 3 Legal Elementary Row Operations (which never change the solution!):
- Swap two rows ().
- Scale a row by any non-zero constant ().
- Add a multiple of one row to another ().
Worked Example 1 — Solving a 3×3 System by Hand
Solve:
- Step 1 (Eliminate Column 1 below pivot ):
- :
- :
- Step 2 (Eliminate Column 2 below pivot ):
- :
- Step 3 (Back-Substitution from bottom row up):
- Row 3:
- Row 2:
- Row 1:
- Check: , , . Exact!
(In computer science, recording the elimination multipliers produces LU Factorization , costing once — and only for every new right-hand side !)
Solving a 2×2 System by Hand via Elimination (3 Easy Steps)
Solve: (1) 2x + y = 5, and (2) 4x + 3y = 11.
Subtract from Eq (2) so the cancels cleanly:
Use Gaussian elimination to solve: 2x + y = 5 and 4x + 5y = 13.
3. Matrix Inverses (A⁻¹) & Why Production Code Uses Solve Instead
Think of a matrix inverse A⁻¹ as Ctrl+Z (Undo)! If A rotates an image 30° clockwise, A⁻¹ rotates it 30° counter-clockwise. If A multiplies a price by 2, A⁻¹ divides by 2. When does Ctrl+Z FAIL? When A destroys information (like multiplying by 0 or squashing a 3D photo flat into 2D — you can't un-squash a pancake!).
For a square matrix , its inverse is the unique matrix that completely undoes :
When exists (called invertible or non-singular, which happens if and only if ), multiplying both sides of on the left by gives .
The 2×2 Inverse Shortcut (Memorize This!)
(Swap the main diagonal , flip the signs of off-diagonals , and divide by !)
Worked Example 2 — Computing a 2×2 Inverse & Checking
Let :
- Determinant: (invertible!).
- Swap & negate: .
- Verify: !
Inverse Product Rule
Just like transpose, taking the inverse of a product reverses order: .
Swap the main diagonal, flip signs on the off-diagonal, and divide by det(A) = ad − bc!
If ad − bc = 0, you'd be dividing by ZERO — proving A is Singular and has NO inverse!
Pause & Check: Why can a linear system Ax = b NEVER have 'exactly 2 solutions'?
Find the inverse of A = [[3, 1], [5, 2]] by hand, and use it to solve Ax = [4, 7]ᵀ.
The exact honest questions almost every beginner wonders — answered in plain English without hiding behind jargon.
YES — what an awesome catch!! Real ML tables are tall and skinny (100,000 × 10), so no straight line passes through all 100,000 noisy data points exactly (0 solutions). That's why in Chapter 7 (Least Squares) we project y onto the closest reachable point by solving the square 10×10 Normal Equations (XᵀX)w = Xᵀy!
Ridge Regression & Kernel Ridge Solvers
Solving (XᵀX + λI)w = Xᵀy uses Cholesky decomposition (A = LLᵀ, 2× faster than LU) because XᵀX + λI is symmetric positive definite!
Gaussian Processes (Cholesky Solve)
GP posterior inference computes (K + σ²I)⁻¹y via Cholesky triangular solves instead of ever inverting the N×N covariance kernel matrix K.
Newton's Method & Second-Order Optimizers (L-BFGS)
Each Newton optimization step solves the linear system H Δθ = −∇L for the update direction Δθ using Hessian curvature H.
Overdetermined Systems (N ≫ D) → Least Squares
Real ML datasets have far more rows (equations) than features (unknowns), so Ax = b has no exact solution — transitioning directly into Least Squares projection!
import numpy as np
A = np.array([[2.0, 1.0],
[4.0, 5.0]])
b = np.array([5.0, 13.0])
# ✓ BEST PRACTICE: Direct LU solver — faster & far more accurate than inv(A) @ b
x = np.linalg.solve(A, b) # array([2., 1.])
# ✓ For Symmetric Positive Definite matrices (e.g. Ridge XᵀX + λI):
# Cholesky factorization A = L @ L.T is 2× faster than general LU!
M = A.T @ A + 0.1 * np.eye(2)
L = np.linalg.cholesky(M) # lower-triangular factor
y = np.linalg.solve(L, A.T @ b) # forward substitution
w = np.linalg.solve(L.T, y) # back substitution
# ✓ Overdetermined / singular systems (never crashes with LinAlgError):
x_ls, residuals, rank, s = np.linalg.lstsq(A, b, rcond=None)