7.4 Tests before use
Source: Ideas from Computational Physics (Landau, Páez, Bordeianu), §7.4. Rewritten in original wording. Continues 7.3b Matrices in Python labs. Labs: 7.4b Tests-before-use labs.
Trust, but verify
A library solve or a hand-written Gauss–Jordan inverse returns numbers. Before you publish a trajectory or an eigenvalue, ask three questions:
- Does look like ?
- Does reproduce ?
- Does for a claimed eigenpair?
Residuals near machine epsilon ( in double precision) mean the arithmetic succeeded. Large residuals mean a bug, a singular matrix, or an ill-conditioned problem — not “close enough for physics.”
A concrete test matrix
Take
An analytic inverse (useful as a gold standard) is
Gauss–Jordan on the augmented block reduces the left half to and leaves a numerical inverse on the right. Compare that inverse to the analytic form, then form and report . For this mild matrix you should see residuals at a few or better.
Solving for known right-hand sides (e.g. with exact ) is another check: print to many decimals and confirm is tiny. Prefer elimination on over forming when you only need one solve — a full inverse does extra work and can magnify round-off.
Eigenpairs and complex roots
For
one real eigenpair is with
Verify at machine noise. Algebraically, a rotation-like matrix can produce a complex conjugate pair with eigenvectors — check by multiplying; you do not need a numerical eigensolver to confirm the algebra. Degenerate eigenvalues (e.g. with a two-dimensional eigenspace) require two independent vectors that both satisfy ; one vector is not enough to span the subspace.
Ill-conditioned warm-up: Hilbert
The Hilbert matrix is famously poorly conditioned as grows. For , if is the first column of , the exact solution of is the first unit vector . Your solver should recover that — measure , not a flaky signed-zero printout of components.
Tests before use
1 / 4A numerical inverse is most convincingly checked by…
Pick an answer to see if you are right.
Next: 7.4b Tests-before-use labs.