A1335 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 cos⁡πN+1 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 10−8 residual against N. Jacobi grows as N2, CG as N.
  • At N = 50, Jacobi takes thousands of sweeps and CG 25 steps.

Predict

A float32 factorization of a matrix with κ = 104 is used with float64 iterative refinement. After three refinement steps, about how accurate is the solution?

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, ljj=ajj−∑k<jljk2 and lij=aij−∑k<jlikljkljj.

Test against the definitions: max⁡|PA−LU| should be at rounding level, and all |lij|≤1 with pivoting.

Refinement in mixed precision

Factor or solve in float32 (A.astype(np.float32)), compute residuals r=b−Ax in float64, solve for the correction in float32, and add it in float64.

CG in a few lines

x=0, r=p=b; repeat: α=rTrpTAp, x+=αp, rnew=r−αAp, p=rnew+rnewTrnewrTrp.

Worked example

Refine a float32 solution of a 50 × 50 system with κ = 104 and x = (1, …, 1).

Show the solution
  1. The float32 solve. Its worst error is 3.1×10−5.
  2. Refine. After one refinement step it's 3.1×10−10, and after two 4.5×10−13. Further steps hover around 3×10−13 to 5×10−13, the float64 floor for this κ.
  3. The rate. The ratio from step to step, about 10−5, is close to κu32.
  4. The limit. If κ were 108, κu32 would exceed 1 and refinement would diverge: mixed precision works only for matrices that float32 can factor meaningfully.

Answer the worst error falls from 3.1×10−5 to 3.1×10−10 and then 4.5×10−13, 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 10−8.

Show the solution
  1. Jacobi. It takes 9653 sweeps, at a rate per sweep of cos⁡π51=0.9981
  2. CG. It takes 25 steps.
  3. 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.
  4. 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 106?

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 A[P]=LU, using partial pivoting. Test it on np.random.default_rng(0).standard_normal((6, 6)): set resid to max⁡|A[P]−LU| and lmax to max⁡|lij|.

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 M=BBT+5I 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 10−4 (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 ‖b−Tx‖≤10−8‖b‖, 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?

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.