A13 · Lesson 35 · Lab: linear algebra in code
LU with pivoting, Cholesky, iterative refinement, Jacobi against CG, and the SVD
Lessons 23 to 34 covered direct solvers, iterative solvers, eigenvalues and the SVD. In this lab you'll write the two classic factorizations yourself and check them against NumPy, recover float64 accuracy from a float32 factorization, race Jacobi against conjugate gradients on the model problem, and confirm that the truncated SVD's error is exactly the first singular value left out.
- On the 1D model problem with N unknowns, Jacobi's error shrinks by per sweep, and CG finishes in at most N steps (here N/2, by symmetry of the right-hand side).
- The curves show each method's iteration count for a residual against N. Jacobi grows as , CG as N.
- At N = 50, Jacobi takes thousands of sweeps and CG 25 steps.
Predict
A float32 factorization of a matrix with κ = is used with float64 iterative refinement. After three refinement steps, about how accurate is the solution?
About to : float64's own limit, . The first solve is good to about , and each refinement step multiplies the error by about until it reaches that floor.
Name it
Writing the factorizations
- LU with partial pivoting: for each column k, find the largest entry at or below the diagonal, swap that row up (record it in a permutation and swap the finished part of L too), then eliminate below. NumPy row swaps:
A[[k, p]] = A[[p, k]]. - Cholesky: column by column, and .
Test against the definitions: should be at rounding level, and all with pivoting.
Refinement in mixed precision
Factor or solve in float32 (A.astype(np.float32)), compute residuals in float64, solve for the correction in float32, and add it in float64.
CG in a few lines
, ; repeat: , , , .
Worked example
Refine a float32 solution of a 50 × 50 system with κ = and x = (1, …, 1).
Show the solution
- The float32 solve. Its worst error is .
- Refine. After one refinement step it's , and after two . Further steps hover around to , the float64 floor for this κ.
- The rate. The ratio from step to step, about , is close to .
- The limit. If κ were , would exceed 1 and refinement would diverge: mixed precision works only for matrices that float32 can factor meaningfully.
Answer the worst error falls from to and then , the float64 floor, in two steps.
Worked example
Solve the 1D model problem with N = 50 and b = 1 by Jacobi and by CG, to a relative residual of .
Show the solution
- Jacobi. It takes 9653 sweeps, at a rate per sweep of
- CG. It takes 25 steps.
- Why so few. In exact arithmetic CG finishes in at most as many steps as the matrix has distinct eigenvalues that the right-hand side touches; b = 1 is symmetric about the middle, so only the 25 symmetric modes are involved.
- The work. Each CG step costs a matrix–vector product and a few vector operations, about twice a Jacobi sweep, so CG wins here by a factor of almost 200.
Answer 9653 Jacobi sweeps against 25 CG steps, almost 200 times less work for CG.
Check
About how many Jacobi sweeps reduce the error of the 1D model problem with N = 50 unknowns by ?
Check
A float32 solve of a system with κ = 100 is refined 1 times in float64. About how small is the error, before reaching float64's floor?
Check
Write lu_pp(A) returning (P, L, U) with , using partial pivoting. Test it on np.random.default_rng(0).standard_normal((6, 6)): set resid to and lmax to .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write chol(A) returning the lower-triangular Cholesky factor. Test it on with B = np.random.default_rng(1).standard_normal((5, 5)): set diff to the largest difference from np.linalg.cholesky(M).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Build a 50 × 50 symmetric matrix with singular values from 1 down to (Q, _ = np.linalg.qr(rng.standard_normal((50, 50))) with rng = np.random.default_rng(2), then A = (Q * np.logspace(0, -4, 50)) @ Q.T) and b = A·ones. Solve in float32, refine four times in float64, and set errs to the five worst errors.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
On the 1D model matrix T (2 on the diagonal, −1 beside it) with N = 50 and b = 1, count Jacobi sweeps jac and CG steps cg until , starting from zero.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Take the SVD of M = np.random.default_rng(3).standard_normal((40, 30)), form the rank-5 truncation, and set err to its 2-norm error and s6 to the sixth singular value.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why must partial pivoting also swap the already-computed part of L when it swaps rows?
The multipliers in L record which multiple of each pivot row was subtracted from each other row. When a later column's pivot search swaps two rows, those rows' identities change from that step on, but the multipliers already computed for them belong to the rows, not to the positions. Swapping the finished columns of L along with the rows keeps each stored multiplier attached to the row it was computed for, so that at the end L times U reproduces the rows of A in the final permuted order. Forgetting the swap gives an L and U whose product is a different matrix, with a residual far above rounding.
Practice
Keep practicing
4 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.