Chapter 21 — Teaching Notes

One-line purpose. Cash the book's biggest promise — "the numerical libraries are Fortran" — by getting students to call dgesv correctly on the first try, and to internalize that the expert move is to call the library, not reimplement it.

Key ideas to emphasize

  • State the problem, call the library, check the answer. This is the whole chapter as a slogan. The deliverable is not "I can write Gaussian elimination"; it is "I can hand a system to LAPACK and verify the result." Say it, and return to it after every worked call.
  • dgesv overwrites BOTH a and b. The single most common beginner surprise. Make them recite it: a → LU factors, b → solution. Every residual check in the chapter exists to force a copy of the originals; do not let this slide.
  • Leading dimension is the declared first dimension. The one genuinely non-obvious argument. Draw the filing-cabinet picture (columns a fixed stride apart); show a 5×5 solve inside a 100×100 array and make them tell you lda is 100, not 5. This is where careful students separate from copy-paste students.
  • The naming scheme is a language, not a lookup. Spend five minutes decoding dgesv, dsyev, dgesvd, sgetrf, zheev live. Once they can read a name, LAPACK stops being intimidating.
  • Check info, always. Tie it to Case Study 1: an unchecked failure returns NaN/garbage that looks like an answer and poisons everything downstream.
  • BLAS levels and arithmetic intensity. The "why you can't beat the library" idea. Level 3 does $O(n^3)$ work on $O(n^2)$ data → cache reuse → near-peak. This is the seed of Chapter 29.

Misconceptions to preempt

  • "I should write my own solver to really understand it." (Write it once; ship the library. The textbook algorithm omits pivoting — Case Study 1.)
  • "dgesv leaves my matrix alone." (It destroys a and b. Copy first.)
  • "lda is just n." (Only for a full, tightly-declared matrix; for a submatrix it is the big array's first dimension.)
  • "undefined reference to dgesv_ means my code won't compile." (It compiled — that is a link error; add -llapack -lblas.)
  • "reshape([1,2,3,4],[2,2]) gives the matrix I typed." (Column-major → the transpose; use order=[2,1].)
  • "info == 0 means the answer is accurate." (It means the routine finished. Accuracy is conditioning — Chapter 20.)
  • "The normal equations are the way to do least squares." (They square the condition number; prefer dgels/SVD.)

A live demonstration (5–10 minutes)

Type example-02-dgesv.f90 live and compile it without the libraries first, so the class sees the undefined reference to 'dgesv_' link error — then add -llapack -lblas and watch it resolve. This single demo teaches the link-vs-compile distinction better than any slide. If time, deliberately swap two arguments (ipiv and b) to show it still compiles (no interface, -Wall silent) and then produces garbage — the argument-order hazard made visceral. Finish by printing the residual to show it is exactly zero.

Class-time budget (~75 min for this advanced chapter)

  • 8 min: matrices as rank-2 arrays; column-major, the reshape/order=[2,1] trap; matmul/dot_product (§21.1).
  • 8 min: write my_matmul, then BLAS levels and why it loses — arithmetic intensity (§21.2).
  • 22 min: the coredgesv, the naming scheme, lda, ipiv, info, the worked solve + residual, the overwrite warning, the silent-argument hazard (§21.3). This is the chapter; give it the most time.
  • 12 min: dsyev + the workspace query; reading dgesvd's page (§21.4).
  • 8 min: linking, OpenBLAS/MKL, the link-error demo (§21.5).
  • 7 min: sparse matrices and dgtsv briefly (§21.6).
  • 10 min: the implicit-step Project Checkpoint (optional path; contrast explicit/CFL, forward to Ch. 24).

Prerequisites to review

Chapters 5 (column-major, matmul/transpose, loop order — the spaced review targets this), 16 (LAPACK/BLAS named; fpm — the other spaced-review target), 20 (conditioning, catastrophic cancellation — both case studies lean on it), and 6 (intent/assumed-shape, for the procedure signatures). Confirm students still remember why reshape fills column-major and what matmul vs * do — both reappear in the first five minutes.

Connections

Back: Ch. 1 (LAPACK-under-NumPy, now cashed), Ch. 5 (column-major/matmul — the foreshadow paid off), Ch. 16 (LAPACK named), Ch. 20 (conditioning). Forward: Ch. 22 (numerical calculus), Ch. 24 (the implicit path becomes a real option beside the explicit default; the tridiagonal Laplacian), Ch. 29 (tuned BLAS beats your loop, measured). This chapter is the payoff of the "call the library" arc and one of the highest-value chapters for the working-scientist reader.