Skip to main content

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:

  1. Does AA−1AA^{-1} look like II?
  2. Does AxA\mathbf x reproduce b\mathbf b?
  3. Does Av≈λvA\mathbf v\approx\lambda\mathbf v for a claimed eigenpair?

Residuals near machine epsilon (∼10−16\sim 10^{-16} 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 3×33\times 3 test matrix​

Take

A=(4−2136−4218).A=\begin{pmatrix}4&-2&1\\3&6&-4\\2&1&8\end{pmatrix}.

An analytic inverse (useful as a gold standard) is

A−1=1263(52172−323019−9−830).A^{-1}=\frac{1}{263}\begin{pmatrix}52&17&2\\-32&30&19\\-9&-8&30\end{pmatrix}.

Gauss–Jordan on the augmented block [A∣I][A\mid I] reduces the left half to II and leaves a numerical inverse on the right. Compare that inverse to the analytic form, then form AA−1−IAA^{-1}-I and report max⁡∣(⋅)ij∣\max|(\cdot)_{ij}|. For this mild matrix you should see residuals at a few ×10−16\times 10^{-16} or better.

Solving Ax=bA\mathbf x=\mathbf b for known right-hand sides (e.g. b=(12,−25,32)T\mathbf b=(12,-25,32)^{\mathsf T} with exact x=(1,−2,4)T\mathbf x=(1,-2,4)^{\mathsf T}) is another check: print x\mathbf x to many decimals and confirm Ax−bA\mathbf x-\mathbf b is tiny. Prefer elimination on [A∣b][A\mid\mathbf b] over forming A−1bA^{-1}\mathbf b when you only need one solve — a full inverse does extra work and can magnify round-off.

Eigenpairs and complex roots​

For

A3=(−22−321−6−1−20),A_3=\begin{pmatrix}-2&2&-3\\2&1&-6\\-1&-2&0\end{pmatrix},

one real eigenpair is λ=5\lambda=5 with

x1=16(−1,−2,1)T.\mathbf x_1=\frac{1}{\sqrt{6}}(-1,-2,1)^{\mathsf T}.

Verify ∥A3x1−5x1∥\|A_3\mathbf x_1-5\mathbf x_1\| at machine noise. Algebraically, a 2×22\times 2 rotation-like matrix can produce a complex conjugate pair α±iβ\alpha\pm i\beta with eigenvectors [1,∓i]T[1,\mp i]^{\mathsf T} — check by multiplying; you do not need a numerical eigensolver to confirm the algebra. Degenerate eigenvalues (e.g. λ=−3\lambda=-3 with a two-dimensional eigenspace) require two independent vectors that both satisfy (A+λI)v=0(A+\lambda I)\mathbf v=\mathbf 0; one vector is not enough to span the subspace.

Ill-conditioned warm-up: Hilbert​

The Hilbert matrix Hij=1/(i+j+1)H_{ij}=1/(i+j+1) is famously poorly conditioned as NN grows. For N=5N=5, if b\mathbf b is the first column of HH, the exact solution of Hx=bH\mathbf x=\mathbf b is the first unit vector e1\mathbf e_1. Your solver should recover that — measure max⁡∥x−e1∥\max\|\mathbf x-\mathbf e_1\|, not a flaky signed-zero printout of components.

Quiz

Tests before use

1 / 4

A numerical inverse is most convincingly checked by…

Pick an answer to see if you are right.

Next: 7.4b Tests-before-use labs.