Numerical Methods

All books

M03 — Linear systems and conditioning

Almost every solver in this repository ends in Ax=bAx = b: each Newton step of M02 is one, and so is every least-squares fit. Elimination is the oldest algorithm in the subject, and the interesting part is not how it works but how much of the answer survives it. This notebook separates the two owners of the error: the algorithm, which pivoting and orthogonal factorisations keep honest, and the problem, whose condition number no algorithm can change.

Book pages: Linear system and One matrix, two errors in books/numerical-methods.

The question. A solver hands back x^\hat x and reports a residual ∥b−Ax^∥\|b - A\hat x\| at the level of rounding. How many digits of x^\hat x can you trust, and who decides?

By the end you can:

  • write Gaussian elimination as A=LUA = LU and say why it costs n3/3n^3/3 multiply-adds
  • show a tiny pivot destroying an answer, and say what partial pivoting bounds
  • derive ∥x^−x∥/∥x∥≤κ(A) ∥r∥/∥b∥\|\hat x - x\|/\|x\| \le \kappa(A)\,\|r\|/\|b\| and read log⁡10κ\log_{10}\kappa as digits lost
  • separate backward error (the algorithm's) from forward error (the problem's too)
  • explain why the normal equations square κ\kappa and QR does not
  • read rank from singular values, and say how otkd uses it to pick its solver

Assumes: M01 (relative error, machine epsilon, conditioning), M02 (Newton's linear solve), and matrix norms.

How to use it: predict before you reveal, commit before you check, and move the controls until the equation's behaviour stops surprising you.

Predict first. Try first: np.linalg.solve on the 12×1212 \times 12 Hilbert matrix Hij=1/(i+j−1)H_{ij} = 1/(i + j - 1) leaves a relative residual ∥r∥/∥b∥\|r\|/\|b\| near 10−1610^{-16}. The relative error of x^\hat x is about

Choose a prediction

Check answer (without scripting)

Answer: between 1e-3 and 1: almost every digit is gone. κ(H12)≈1.6×1016\kappa(H_{12}) \approx 1.6 \times 10^{16}, so a backward error of 10−1610^{-16} allows a forward error of order 1. The solver is not at fault.

Warm-up: remember (closed book)

1. Newton's errors read 0.04, 0.02, 0.01, 0.005 on a function whose derivative you have checked against a central difference. The likeliest cause is

Question 1
How sure?

2. You have fhf_h, fh/2f_{h/2}, fh/4f_{h/4} but no exact answer. What can you still estimate?

Question 2
How sure?

3. Why is the secant method's order about 1.618 rather than Newton's 2?

Question 3
How sure?

Show answers and explanations

1. the root is a double root. At a root of multiplicity mm, ek+1≈m−1meke_{k+1} \approx \frac{m-1}{m} e_k; a ratio of 12\tfrac12 means m=2m = 2. With mm known, the step mf/f′m f/f' restores order 2.

2. The observed order, from the ratio of successive differences. fh−fh/2f_h - f_{h/2} behaves like E(h)E(h), so log⁡2\log_2 of the ratio of successive differences estimates pp with no exact answer.

3. Its new error is proportional to the product of the last two errors. ek+1≈Cekek−1e_{k+1} \approx C e_k e_{k-1}. Trying ek+1∼ekpe_{k+1} \sim e_k^p gives p=1+1/pp = 1 + 1/p, so p=(1+5)/2p = (1 + \sqrt5)/2.

1. Gaussian elimination is LU

Setting: the problem, its sets and its symbols

Problem. Given a nonsingular A∈Rn×nA \in \mathbb{R}^{n \times n} whose pivots are all nonzero (no row swaps) and b∈Rnb \in \mathbb{R}^{n}, find a unit lower-triangular LL and an upper-triangular UU with A=LUA = LU, then x∈Rnx \in \mathbb{R}^{n} with Ax=bAx = b by solving Ly=bLy = b and Ux=yUx = y; count the multiply-adds as a function of nn.

Symbol In Meaning
nn {2,3,…,400}\{2, 3, \dots, 400\} the number of unknowns, set by the slider
AA Rn×n\mathbb{R}^{n \times n} the matrix, nonsingular; the example is (4113)\begin{pmatrix} 4 & 1 \\ 1 & 3 \end{pmatrix}
b, xb,\ x Rn\mathbb{R}^{n} the right-hand side (data) and the solution (unknown); the example has b=(1,2)b = (1, 2)
LL Rn×n\mathbb{R}^{n \times n} unit lower-triangular: ones on the diagonal, the multipliers ℓik\ell_{ik} below it
UU Rn×n\mathbb{R}^{n \times n} upper-triangular: the rows left after elimination; ukku_{kk} is the kk-th pivot
ℓik\ell_{ik} R\mathbb{R} the multiplier that clears entry (i,k)(i, k) at step kk, for i>ki > k: that entry over the pivot ukku_{kk}
kk {1,…,n−1}\{1, \dots, n - 1\} the elimination step: column kk is cleared below the diagonal
yy Rn\mathbb{R}^{n} the intermediate vector of the forward sweep, y=L−1by = L^{-1} b
How these symbols connect

The data are a nonsingular matrix A∈Rn×nA \in \mathbb{R}^{n \times n} and right-hand side b∈Rnb \in \mathbb{R}^{n}; the unknown is x∈Rnx \in \mathbb{R}^{n} satisfying Ax=bAx = b. Elimination at steps k=1,…,n−1k = 1, \dots, n - 1 clears below-diagonal entries using multipliers ℓik\ell_{ik} to factorise A=LUA = LU into a unit lower-triangular LL and an upper-triangular UU. That factorisation turns the linear solve into two triangular sweeps: forward substitution solves Ly=bLy = b for the intermediate vector y∈Rny \in \mathbb{R}^{n}, and back substitution solves Ux=yUx = y for xx. All quantities in the example are dimensionless; when units are present, LL is dimensionless, UU shares the units of AA, and yy shares those of bb.

Symbol-by-symbol notes
$L$ and $U$ — triangular factors

LL is unit lower-triangular: every diagonal entry is 11, every entry above the diagonal is 00, and the subdiagonal entry (i,k)(i, k) holds the multiplier ℓik\ell_{ik}. UU is upper-triangular, with zeros below the diagonal and the pivots ukku_{kk} on it; the no-swap assumption here requires every ukk≠0u_{kk} \ne 0.

$\ell_{ik}$ — elimination multiplier

Defined only for i>ki > k at step kk: it is the current entry in row ii, column kk divided by the pivot ukku_{kk} of that step. Do not confuse ℓik\ell_{ik} with the original matrix entry aika_{ik}, because earlier steps 1,…,k−11, \dots, k - 1 have already updated row ii.

$y$ — intermediate vector, not a coordinate

In the two-step solve Ly=bLy = b then Ux=yUx = y, y=L−1b∈Rny = L^{-1}b \in \mathbb{R}^{n} is the right-hand side after forward elimination. In the 2×22 \times 2 scalar equations written below, the two components of xx are also called xx and yy, which is distinct from this vector yy.

$A$ nonsingular and zero-free pivots

Nonsingularity guarantees a unique solution x=A−1bx = A^{-1}b, but A=LUA = LU without row swaps is a stronger requirement: every leading principal submatrix must be nonsingular so no pivot ukku_{kk} vanishes. Section 2 removes that restriction with row permutations.

Take the book's system 4x+y=14x + y = 1, x+3y=2x + 3y = 2. Subtract 14\tfrac14 of row 1 from row 2: the second row becomes 114y=74\tfrac{11}{4}y = \tfrac74. Keep the multiplier ℓ21=14\ell_{21} = \tfrac14 and the reduced rows, and you have

(4113)=(100.251)⏟L(4102.75)⏟U.\begin{pmatrix} 4 & 1 \\ 1 & 3 \end{pmatrix} = \underbrace{\begin{pmatrix} 1 & 0 \\ 0.25 & 1 \end{pmatrix}}_{L} \underbrace{\begin{pmatrix} 4 & 1 \\ 0 & 2.75 \end{pmatrix}}_{U}.

Elimination step kk is left-multiplication by a unit lower-triangular matrix holding the multipliers −ℓik-\ell_{ik}; its inverse flips their signs, and the product of the inverses is LL. So elimination is the factorisation A=LUA = LU, and solving is two triangular sweeps: Ly=bLy = b forward, then Ux=yUx = y backward. Here x=(0.090909,0.636364)=(1/11, 7/11).x = (0.090909, 0.636364) = (1/11,\ 7/11).

Cost. Step kk updates the trailing (n−k)×(n−k)(n-k) \times (n-k) block, one multiply-add per entry, so the total is

∑k=1n−1(n−k)2=(n−1) n (2n−1)6≈n33\sum_{k=1}^{n-1} (n-k)^2 = \frac{(n-1)\,n\,(2n-1)}{6} \approx \frac{n^3}{3}

multiply-adds (23n3\tfrac23 n^3 flops). Each triangular solve is about n2/2n^2/2. The factorisation is the expensive part, and once it exists every further right-hand side is cheap.

nn multiply-adds in elimination n3/3n^3/3 ratio
50 40,425 41,667 0.970

Predict first. Doubling nn multiplies the work of the factorisation by about

Choose a prediction

Check answer (without scripting)

Answer: 8. The work is about n3/3n^3/3, so the factor is 232^3. On log–log axes the slope is 3.

Reveal result (lock in your prediction above first)
10¹10²10³10⁰10²10⁴10⁶10⁸countedn³/3nmultiply-adds

At your n=50n = 50: 40,425 multiply-adds; at 2n=1002n = 100: 328,350, a factor of 8.12.

The two lines merge: the lower-order terms vanish by n≈100n \approx 100. This is the bill M02's Newton method pays every step with a dense Jacobian: one factorisation of JJ, then two triangular solves for J δ=−FJ\,\delta = -F. np.linalg.solve is LAPACK's gesv, LU with partial pivoting; scipy.linalg.lu_factor and lu_solve split the two parts so a factor can be reused.

When LU factorisation breaks
  • Assumes: every pivot ukku_{kk} met on the way is non-zero, which holds for a non-singular matrix once rows may be swapped
  • When it fails: a zero pivot stops elimination without row swaps even when AA is invertible (try (0110)\begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix}), and a dense factor of a large sparse matrix fills in and runs out of memory
  • Check: the factor reports its pivots; scipy.linalg.lu_factor warns on an exactly zero one

2. Partial pivoting

Setting: the problem, its sets and its symbols

Problem. Given δ∈(0,1)\delta \in (0, 1), the matrix A(δ)A(\delta) and bb below, find x∈R2x \in \mathbb{R}^{2} with A(δ)x=bA(\delta)x = b in double precision, by elimination without a row swap and by elimination with partial pivoting (PA=LUPA = LU); compare each computed x^\hat x with xx.

Symbol In Meaning
δ\delta {10−20,…,10−1}\{10^{-20}, \dots, 10^{-1}\} the (1, 1) entry, the first pivot without a swap; the slider sets δ=10k\delta = 10^{k}
A(δ)A(\delta) R2×2\mathbb{R}^{2 \times 2} (δ111)\begin{pmatrix} \delta & 1 \\ 1 & 1 \end{pmatrix}, nonsingular for δ≠1\delta \ne 1
bb R2\mathbb{R}^{2} the data, (1,2)(1, 2)
x, x^x,\ \hat x R2\mathbb{R}^{2} the exact solution and the computed one
PP {0,1}2×2\{0, 1\}^{2 \times 2} a permutation matrix: the row swaps partial pivoting chooses
L, UL,\ U R2×2\mathbb{R}^{2 \times 2} the triangular factors, as in §1; u22u_{22} is the second pivot
y2y_2 R\mathbb{R} the second entry of the forward sweep's yy
κ∞(A)\kappa_\infty(A) [1,∞)[1, \infty) ∥A∥∞∥A−1∥∞\lVert A \rVert_\infty \lVert A^{-1} \rVert_\infty, with ∥⋅∥∞\lVert \cdot \rVert_\infty the largest absolute row sum
ρ\rho R>0\mathbb{R}_{>0} the growth factor max⁡ij∣uij∣/max⁡ij∣aij∣\max_{ij} \lvert u_{ij} \rvert / \max_{ij} \lvert a_{ij} \rvert
ϵ\epsilon R>0\mathbb{R}_{>0} machine epsilon for doubles, 2−52≈2.2×10−162^{-52} \approx 2.2 \times 10^{-16}
How these symbols connect

Choosing δ∈(0,1)\delta \in (0, 1) via the slider fixes the dimensionless matrix A(δ)∈R2×2A(\delta) \in \mathbb{R}^{2 \times 2}, which together with b=(1,2)∈R2b = (1, 2) \in \mathbb{R}^{2} defines the exact solution x∈R2x \in \mathbb{R}^{2} of A(δ)x=bA(\delta)x = b. Elimination without swaps and elimination with partial pivoting (PA=LUPA = LU, where PP records row swaps) each produce triangular factors L,UL, U, a forward-sweep entry y2y_2, and a floating-point solution x^∈R2\hat x \in \mathbb{R}^{2} computed in double precision (spacing scale ϵ\epsilon). Comparing x^\hat x against xx alongside κ∞(A)\kappa_\infty(A) and the growth factor ρ\rho separates how sensitive the linear system is from how much intermediate numbers grow during factorisation.

Symbol-by-symbol notes
$P$ — permutation matrix

A matrix in {0,1}2×2\{0, 1\}^{2 \times 2} with exactly one 11 in each row and column, formed by reordering the rows of the identity matrix. Multiplying on the left (PA=LUPA = LU) reorders the equations before elimination, so the same permutation must be applied to the right-hand side (Ly=PbLy = Pb); PT=P−1P^{\mathsf T} = P^{-1}.

$\kappa_\infty(A)$ — infinity-norm condition number

Defined as ∥A∥∞∥A−1∥∞≥1\lVert A \rVert_\infty \lVert A^{-1} \rVert_\infty \ge 1, where the matrix ∞\infty-norm is the maximum absolute row sum max⁡i∑j∣aij∣\max_i \sum_j \lvert a_{ij} \rvert. It is a property of A(δ)A(\delta) alone, independent of whether rows are swapped, and is dimensionless.

$\rho$ — growth factor

The dimensionless ratio max⁡ij∣uij∣/max⁡ij∣aij∣\max_{ij} \lvert u_{ij} \rvert / \max_{ij} \lvert a_{ij} \rvert comparing the largest entry created in UU with the largest entry of AA. Unlike κ∞(A)\kappa_\infty(A), it depends on the pivoting strategy and measures element amplification inside elimination.

$\delta$ vs $\epsilon$ — two small parameters

δ=10k\delta = 10^{k} is the configurable (1,1)(1, 1) entry of A(δ)A(\delta), whereas ϵ=2−52≈2.2×10−16\epsilon = 2^{-52} \approx 2.2 \times 10^{-16} is the fixed double-precision unit spacing at 11. The slider lets δ\delta cross ϵ\epsilon so you can compare behaviour on both sides of floating-point precision.

Solve

(δ111)x=(12),x=(11−δ, 1−2δ1−δ)≈(1,1).\begin{pmatrix} \delta & 1 \\ 1 & 1 \end{pmatrix} x = \begin{pmatrix} 1 \\ 2 \end{pmatrix}, \qquad x = \Big(\tfrac{1}{1-\delta},\ \tfrac{1-2\delta}{1-\delta}\Big) \approx (1, 1).

The matrix is as well conditioned as matrices get: κ∞=4\kappa_\infty = 4 for every small δ\delta. Eliminate without swapping rows. The multiplier is 1/δ1/\delta, so

u22=1−1δ,y2=2−1δ,x2=y2/u22,x1=(1−x2)/δ.u_{22} = 1 - \tfrac{1}{\delta}, \qquad y_2 = 2 - \tfrac{1}{\delta}, \qquad x_2 = y_2 / u_{22}, \qquad x_1 = (1 - x_2)/\delta .

Once 1/δ1/\delta exceeds 1/ϵ1/\epsilon, both 1−1/δ1 - 1/\delta and 2−1/δ2 - 1/\delta round to −1/δ-1/\delta: the second row's own entries have been swamped. Before that point, 1−x21 - x_2 is M01's cancellation (two numbers near 1) divided by a tiny δ\delta, the same shape as the difference quotient's round-off floor.

Partial pivoting swaps rows so the pivot is the largest entry in its column. Here the multiplier becomes δ\delta, u22=1−δu_{22} = 1 - \delta, and nothing is swamped. Every multiplier is at most 1 in size, which keeps the growth factor ρ=max⁡∣uij∣/max⁡∣aij∣\rho = \max|u_{ij}| / \max|a_{ij}| small, and the error bound of elimination is a modest multiple of ρ ϵ\rho\,\epsilon.

δ\delta κ∞(A)\kappa_\infty(A) method x^\hat x relative error growth ρ\rho
10−410^{-4} 4.000 no swap (1.0001000100, 0.9998999900) 2.81e-13 1.0e+04
partial pivoting (1.0001000100, 0.9998999900) 2.22e-16 1.0e+00

Predict first. At δ=10−20\delta = 10^{-20}, elimination without a row swap returns x1=x_1 =

Choose a prediction

Check answer (without scripting)

Answer: 0, although the true value is about 1. x2x_2 comes out as exactly 1, then x1=(1−1)/δ=0x_1 = (1 - 1)/\delta = 0. A perfectly conditioned problem, and a 100 per cent error: the algorithm's fault.

Reveal result (lock in your prediction above first)
10⁻²⁰10⁻¹⁶10⁻¹²10⁻⁸10⁻⁴10⁻¹⁸10⁻¹⁴10⁻¹⁰10⁻⁶10⁻²no swappartial pivotingpivot δrelative error

Without the swap the error grows like ϵ/δ\epsilon/\delta and reaches 100 per cent once δ<ϵ\delta \lt \epsilon; with it, the error stays at rounding level (points on the floor are exact). The matrix never changed its condition number. This is the one failure in this notebook that a better algorithm removes.

When partial pivoting breaks
  • Assumes: the growth factor ρ=max⁡∣uij∣/max⁡∣aij∣\rho = \max|u_{ij}| / \max|a_{ij}| stays modest
  • When it fails: it can reach 2n−12^{n-1} (Wilkinson's example: 1 on the diagonal and in the last column, −1-1 below the diagonal), though it is rare in practice
  • Check: compute ρ\rho from scipy.linalg.lu; a large one says the backward error may be large too

3. The condition number: a small residual is not a small error

Setting: the problem, its sets and its symbols

Problem. Given a nonsingular A∈Rn×nA \in \mathbb{R}^{n \times n}, data b∈Rn∖{0}b \in \mathbb{R}^{n} \setminus \{0\} and a computed x^∈Rn\hat x \in \mathbb{R}^{n}, bound the relative error ∥x^−x∥/∥x∥\lVert \hat x - x \rVert / \lVert x \rVert of x^\hat x against x=A−1bx = A^{-1}b by a quantity computable from x^\hat x; then measure both on the Hilbert matrices HnH_n.

Symbol In Meaning
nn {2,3,…,12}\{2, 3, \dots, 12\} the order, set by the slider
AA Rn×n\mathbb{R}^{n \times n} the matrix, nonsingular
b, xb,\ x Rn\mathbb{R}^{n} the data and the exact solution x=A−1bx = A^{-1}b, not computable
x^\hat x Rn\mathbb{R}^{n} the solution the solver returned
rr Rn\mathbb{R}^{n} the residual b−Ax^b - A\hat x, computable
∥⋅∥\lVert \cdot \rVert Rn→[0,∞)\mathbb{R}^{n} \to [0, \infty) a vector norm, and on matrices the norm it induces, ∥A∥=max⁡v≠0∥Av∥/∥v∥\lVert A \rVert = \max_{v \ne 0} \lVert Av \rVert / \lVert v \rVert; the table uses the 2-norm
κ(A)\kappa(A) [1,∞)[1, \infty) the condition number ∥A∥ ∥A−1∥\lVert A \rVert\,\lVert A^{-1} \rVert; κ2\kappa_2 in the 2-norm
σmax⁡, σmin⁡\sigma_{\max},\ \sigma_{\min} (0,∞)(0, \infty) the largest and smallest singular values of AA
HnH_n Rn×n\mathbb{R}^{n \times n} the Hilbert matrix, (Hn)ij=1/(i+j−1)(H_n)_{ij} = 1/(i + j - 1) for i,j∈{1,…,n}i, j \in \{1, \dots, n\}; symmetric positive definite
1\mathbf 1 Rn\mathbb{R}^{n} the vector of ones; the test takes b=Hn1b = H_n \mathbf 1, rounded to doubles
ϵ\epsilon R>0\mathbb{R}_{>0} machine epsilon for doubles, 2−52≈2.2×10−162^{-52} \approx 2.2 \times 10^{-16}
How these symbols connect

Given a nonsingular matrix A∈Rn×nA \in \mathbb{R}^{n \times n} and nonzero data b∈Rnb \in \mathbb{R}^{n}, a solver returns x^∈Rn\hat x \in \mathbb{R}^{n} in place of the unknown exact solution x=A−1bx = A^{-1}b. From x^\hat x alone you can compute the residual r=b−Ax^∈Rnr = b - A\hat x \in \mathbb{R}^{n}, and an induced matrix norm ∥⋅∥\lVert \cdot \rVert together with κ(A)=∥A∥∥A−1∥\kappa(A) = \lVert A \rVert \lVert A^{-1} \rVert links the computable relative residual ∥r∥/∥b∥\lVert r \rVert / \lVert b \rVert to the uncomputable relative error ∥x^−x∥/∥x∥\lVert \hat x - x \rVert / \lVert x \rVert. The experiment evaluates both ratios on the dimensionless Hilbert family HnH_n with b=Hn1b = H_n \mathbf 1 as the order nn varies from 22 to 1212.

Symbol-by-symbol notes
$r$ vs $x - \hat x$ — residual against error

The residual r=b−Ax^r = b - A\hat x lives in the data space (same units as bb) and can always be computed once x^\hat x is known. The error x−x^x - \hat x lives in the solution space (same units as xx) and cannot be computed without knowing xx; they are linked by r=A(x−x^)r = A(x - \hat x).

$\lVert \cdot \rVert$ — vector norm and induced matrix norm

The same notation denotes a norm on Rn\mathbb{R}^{n} and its operator norm ∥A∥=max⁡v≠0∥Av∥/∥v∥\lVert A \rVert = \max_{v \ne 0} \lVert Av \rVert / \lVert v \rVert on Rn×n\mathbb{R}^{n \times n}, ensuring submultiplicativity ∥Av∥≤∥A∥∥v∥\lVert Av \rVert \le \lVert A \rVert \lVert v \rVert. In the 2-norm, ∥A∥2=σmax⁡\lVert A \rVert_2 = \sigma_{\max} and ∥A−1∥2=1/σmin⁡\lVert A^{-1} \rVert_2 = 1 / \sigma_{\min}.

$\kappa(A)$ and $\sigma\lt em>{\max}, \sigma\lt /em>{\min}$ — condition number

κ(A)=∥A∥∥A−1∥∈[1,∞)\kappa(A) = \lVert A \rVert \lVert A^{-1} \rVert \in [1, \infty) is dimensionless and depends on the chosen norm, with κ2(A)=σmax⁡/σmin⁡\kappa_2(A) = \sigma_{\max} / \sigma_{\min} in the 2-norm. Because AA is nonsingular, σmin⁡>0\sigma_{\min} > 0, so κ2(A)\kappa_2(A) is finite, though it can be arbitrarily large.

$H_n$ and $\mathbf 1$ — the Hilbert test family

(Hn)ij=1/(i+j−1)(H_n)_{ij} = 1 / (i + j - 1) uses 1-based indices i,j∈{1,…,n}i, j \in \{1, \dots, n\}, giving a symmetric positive definite matrix whose entries are exact rationals that mostly round when stored as doubles. Taking b=Hn1b = H_n \mathbf 1 in doubles means the exact solution of the stored floating-point system already differs slightly from 1\mathbf 1.

Let xx solve Ax=bAx = b and let x^\hat x be what came back. The residual r=b−Ax^r = b - A\hat x is computable; the error x−x^x - \hat x is not, since it needs xx. Subtract Ax^=b−rA\hat x = b - r from Ax=bAx = b:

x−x^=A−1r  ⇒  ∥x−x^∥≤∥A−1∥ ∥r∥,∥b∥≤∥A∥ ∥x∥  ⇒  1∥x∥≤∥A∥∥b∥.x - \hat x = A^{-1} r \;\Rightarrow\; \|x - \hat x\| \le \|A^{-1}\|\,\|r\|, \qquad \|b\| \le \|A\|\,\|x\| \;\Rightarrow\; \frac{1}{\|x\|} \le \frac{\|A\|}{\|b\|}.

Multiply the two:

∥x^−x∥∥x∥≤∥A∥ ∥A−1∥⏟κ(A) ∥r∥∥b∥.\frac{\|\hat x - x\|}{\|x\|} \le \underbrace{\|A\|\,\|A^{-1}\|}_{\kappa(A)}\, \frac{\|r\|}{\|b\|}.

This is M01's κ(x)=∣xf′(x)/f(x)∣\kappa(x) = |x f'(x)/f(x)| grown up: the worst-case amplification of a relative perturbation, owned by the problem. In the 2-norm κ2=σmax⁡/σmin⁡\kappa_2 = \sigma_{\max}/\sigma_{\min}, and with rounding-level data the relative error is about κϵ\kappa\epsilon: you lose about log⁡10κ\log_{10}\kappa of the 16 digits.

The Hilbert matrix Hij=1/(i+j−1)H_{ij} = 1/(i+j-1) is the classic ill-conditioned family. Take b=H1b = H\mathbf 1, solve with np.linalg.solve, and measure against the exact solution of the stored system, computed in rational arithmetic, so both the residual and the error below are free of rounding.

Figure 1
nn κ2(H)\kappa_2(H) ∥r∥/∥b∥\|r\|/\|b\| relative error bound κ∥r∥/∥b∥\kappa\|r\|/\|b\| rounding H,bH, b alone moved xx by
8 1.53e+10 1.57e-16 2.12e-09 2.39e-06 7.0e-08

The last column compares the exact solution of the stored system with 1\mathbf 1, the solution before HH and bb were rounded to doubles. That loss happened before the solver ran: M01's "the loss belongs to the question".

Reveal result (lock in your prediction above first)
24.579.51210⁻¹⁷10⁻¹³10⁻⁹10⁻⁵10⁻¹residualerrorboundHilbert order nrelative size

At n=12n = 12: relative residual 1.1e-16, relative error 7.8e-03, bound 1.8e+00. The residual is flat at rounding level for every nn; the error climbs with κ\kappa and stays under the bound. Both are measured in exact arithmetic, so nothing here is a rounded zero. We stop at 12, where κ\kappa has passed 1/ϵ1/\epsilon: beyond it κ\kappa of the stored matrix is no longer the κ\kappa of the Hilbert matrix.

When the error bound breaks
  • Assumes: you know κ(A)\kappa(A) in the norm you measure the error in
  • When it fails: κ\kappa needs ∥A−1∥\|A^{-1}\|; beyond κ≈1/ϵ\kappa \approx 1/\epsilon it is no longer well defined in doubles: for the Hilbert matrix of order 13 the exact κ2\kappa_2 is 5.6×10175.6 \times 10^{17}, the copy rounded to doubles has 2.2×10182.2 \times 10^{18}, and numpy reports 4.7×10174.7 \times 10^{17}
  • Check: np.linalg.cond, or a LAPACK estimate from the factors; treat κϵ\kappa\epsilon near 1 as no digits

4. Backward stability

Setting: the problem, its sets and its symbols

Problem. Given a nonsingular A∈Rn×nA \in \mathbb{R}^{n \times n}, b∈Rn∖{0}b \in \mathbb{R}^{n} \setminus \{0\} and a computed x^∈Rn∖{0}\hat x \in \mathbb{R}^{n} \setminus \{0\}, find the smallest relative perturbation (ΔA,Δb)(\Delta A, \Delta b) of the data for which x^\hat x is an exact solution, and relate it to the forward error ∥x^−x∥/∥x∥\lVert \hat x - x \rVert / \lVert x \rVert; compare an LU solve with x^=inv(A) b\hat x = \mathrm{inv}(A)\,b on HnH_n.

Symbol In Meaning
nn {2,3,…,12}\{2, 3, \dots, 12\} the order, set by the slider
A, bA,\ b Rn×n, Rn\mathbb{R}^{n \times n},\ \mathbb{R}^{n} the data, as in §3
x, x^x,\ \hat x Rn\mathbb{R}^{n} the exact solution A−1bA^{-1}b and the computed one
rr Rn\mathbb{R}^{n} the residual b−Ax^b - A\hat x
ΔA, Δb\Delta A,\ \Delta b Rn×n, Rn\mathbb{R}^{n \times n},\ \mathbb{R}^{n} perturbations of the data with (A+ΔA)x^=b+Δb(A + \Delta A)\hat x = b + \Delta b
ε\varepsilon [0,∞)[0, \infty) a relative size: ∥ΔA∥≤ε∥A∥\lVert \Delta A \rVert \le \varepsilon \lVert A \rVert and ∥Δb∥≤ε∥b∥\lVert \Delta b \rVert \le \varepsilon \lVert b \rVert
η(x^)\eta(\hat x) [0,∞)[0, \infty) the backward error: the least such ε\varepsilon
κ(A)\kappa(A) [1,∞)[1, \infty) ∥A∥ ∥A−1∥\lVert A \rVert\,\lVert A^{-1} \rVert, the 2-norm in the table
fl\mathrm{fl} R→F\mathbb{R} \to \mathbb{F} rounding to the nearest double; F⊂R\mathbb{F} \subset \mathbb{R} is the set of doubles
δ\delta R\mathbb{R} the relative rounding error of one operation, ∣δ∣≤ϵ/2\lvert \delta \rvert \le \epsilon / 2
HnH_n Rn×n\mathbb{R}^{n \times n} the Hilbert matrix of §3, with b=Hn1b = H_n \mathbf 1
ϵ\epsilon R>0\mathbb{R}_{>0} machine epsilon for doubles, 2−522^{-52}; not the ε\varepsilon above
How these symbols connect

Given the linear system Ax=bAx = b and a nonzero computed solution x^∈Rn\hat x \in \mathbb{R}^{n}, backward error analysis asks for perturbations (ΔA,Δb)∈Rn×n×Rn(\Delta A, \Delta b) \in \mathbb{R}^{n \times n} \times \mathbb{R}^{n} so that (A+ΔA)x^=b+Δb(A + \Delta A)\hat x = b + \Delta b holds exactly. Measuring their relative size by ε≥0\varepsilon \ge 0 yields the backward error η(x^)\eta(\hat x) as the smallest admissible ε\varepsilon, computed directly from the residual r=b−Ax^r = b - A\hat x and norms of AA, bb, and x^\hat x. Combining the algorithm-owned η(x^)\eta(\hat x) with the problem-owned κ(A)\kappa(A) bounds the forward error ∥x^−x∥/∥x∥\lVert \hat x - x \rVert / \lVert x \rVert, which the experiment compares for solve and inv(A) @ b on the dimensionless family HnH_n.

Symbol-by-symbol notes
$\varepsilon$ vs $\epsilon$ — perturbation bound against machine epsilon

ε≥0\varepsilon \ge 0 is a dummy variable ranging over candidate relative perturbation sizes such that ∥ΔA∥≤ε∥A∥\lVert \Delta A \rVert \le \varepsilon \lVert A \rVert and ∥Δb∥≤ε∥b∥\lVert \Delta b \rVert \le \varepsilon \lVert b \rVert. ϵ=2−52\epsilon = 2^{-52} is the fixed double-precision machine epsilon; an algorithm is backward stable when η(x^)\eta(\hat x) stays a modest multiple of ϵ\epsilon.

$\eta(\hat x)$ — backward error

Defined as the minimum feasible ε\varepsilon over all (ΔA,Δb)(\Delta A, \Delta b) making x^\hat x an exact solution; it is dimensionless and requires x^≠0\hat x \ne 0 (and b≠0b \ne 0 for relative scaling). Do not confuse η(x^)\eta(\hat x) with the relative residual ∥r∥/∥b∥\lVert r \rVert / \lVert b \rVert, which only perturbs bb and omits the ∥A∥∥x^∥\lVert A \rVert \lVert \hat x \rVert term in the denominator.

$\Delta A$ and $\Delta b$ — data perturbations

ΔA∈Rn×n\Delta A \in \mathbb{R}^{n \times n} carries the units of AA and Δb∈Rn\Delta b \in \mathbb{R}^{n} carries the units of bb, so the ratios ∥ΔA∥/∥A∥\lVert \Delta A \rVert / \lVert A \rVert and ∥Δb∥/∥b∥\lVert \Delta b \rVert / \lVert b \rVert are dimensionless. They are a thought-experiment device: they need not be the actual rounding errors made during execution.

$\mathrm{fl}$ and $\delta$ — floating-point rounding model

fl:R→F\mathrm{fl} : \mathbb{R} \to \mathbb{F} rounds a real number to its nearest IEEE 754 double in F⊂R\mathbb{F} \subset \mathbb{R}, satisfying fl(z)=z(1+δ)\mathrm{fl}(z) = z(1 + \delta) with ∣δ∣≤ϵ/2\lvert \delta \rvert \le \epsilon / 2 (away from overflow and underflow). This δ\delta is a per-operation rounding error, not the (1,1)(1, 1) pivot of §2.

§3 split the error into two factors. The algorithm owns ∥r∥/∥b∥\|r\|/\|b\|, the problem owns κ\kappa. Make the algorithm's share precise: the backward error of x^\hat x is the smallest relative change to the data that x^\hat x solves exactly. For perturbations of both AA and bb it has a closed form (Rigal and Gaches):

η(x^)=min⁡{ε:(A+ΔA)x^=b+Δb, ∥ΔA∥≤ε∥A∥, ∥Δb∥≤ε∥b∥}=∥r∥∥A∥ ∥x^∥+∥b∥.\eta(\hat x) = \min\{\varepsilon : (A + \Delta A)\hat x = b + \Delta b,\ \|\Delta A\| \le \varepsilon\|A\|,\ \|\Delta b\| \le \varepsilon\|b\|\} = \frac{\|r\|}{\|A\|\,\|\hat x\| + \|b\|}.

An algorithm is backward stable if η\eta is a modest multiple of ϵ\epsilon for every input. You met the idea in M01 without the name: a correctly rounded fl(a+b)=(a+b)(1+δ)\mathrm{fl}(a + b) = (a + b)(1 + \delta) is the exact sum of a(1+δ)a(1+\delta) and b(1+δ)b(1+\delta). Then perturbation theory finishes the job: when κη<1\kappa\eta \lt 1,

∥x^−x∥∥x∥≤2κη1−κη≈2κη.\frac{\|\hat x - x\|}{\|x\|} \le \frac{2\kappa\eta}{1 - \kappa\eta} \approx 2\kappa\eta .

Forward error ≈\approx condition number ×\times backward error. LU with partial pivoting is backward stable in practice (ρ\rho small). Multiplying by a computed inverse is not, even though A−1A^{-1} itself is computed well. Move nn and compare.

n=10n = 10 backward error η\eta forward error κη\kappa\eta κϵ\kappa\epsilon
np.linalg.solve(H, b) 6.9e-17 5.8e-05 1.1e-03 3.6e-03
np.linalg.inv(H) @ b 1.6e-05 2.4e-03 2.5e+08

κ2(H10)=1.6×1013\kappa_2(H_{10}) = 1.6 \times 10^{13}. Where κη≥1\kappa\eta \ge 1 the bound says nothing; the inverse's forward error can still sit near κϵ\kappa\epsilon, which is why the defect goes unnoticed. Backward stability is the stronger promise: the answer is the exact answer to data you could have been given.

24.579.51210⁻¹⁸10⁻¹⁴10⁻¹⁰10⁻⁶10⁻²LU solveinverse times bHilbert order nbackward error η
When backward stability breaks
  • Assumes: the answer is judged by the data it exactly solves, and that data was itself only known to about ϵ\epsilon
  • When it fails: a backward-stable answer to an ill-conditioned problem can still be wrong in every digit; stability bounds the algorithm's share, not the problem's
  • Check: compute η=∥r∥/(∥A∥∥x^∥+∥b∥)\eta = \|r\| / (\|A\|\|\hat x\| + \|b\|); near ϵ\epsilon means the solver did its job

5. Least squares: normal equations against QR

Setting: the problem, its sets and its symbols

Problem. Given A∈Rm×(d+1)A \in \mathbb{R}^{m \times (d+1)} with m>d+1m > d + 1 and full column rank, and b∈Rmb \in \mathbb{R}^{m}, find x∗=arg⁡min⁡x∈Rd+1∥Ax−b∥2x^* = \arg\min_{x \in \mathbb{R}^{d+1}} \lVert Ax - b \rVert_2, once through the normal equations and once through A=QRA = QR; compare each computed x^\hat x with x∗x^*.

Symbol In Meaning
mm N\mathbb{N} the number of data points, m=50m = 50
dd {1,2,…,15}\{1, 2, \dots, 15\} the polynomial degree, set by the slider
tit_i [0,1][0, 1] the sample points, equally spaced, i∈{1,…,m}i \in \{1, \dots, m\}
AA Rm×(d+1)\mathbb{R}^{m \times (d+1)} the Vandermonde matrix Aij=ti jA_{ij} = t_i^{\,j}, j∈{0,…,d}j \in \{0, \dots, d\}; full column rank
bb Rm\mathbb{R}^{m} the data, b=A1b = A\mathbf 1, so the fit is exact
x, x∗, x^x,\ x^*,\ \hat x Rd+1\mathbb{R}^{d+1} the coefficients, the minimiser (x∗=1x^* = \mathbf 1) and a computed one
ATAA^{\mathsf T}A R(d+1)×(d+1)\mathbb{R}^{(d+1) \times (d+1)} the Gram matrix of the normal equations, symmetric positive definite
QQ Rm×(d+1)\mathbb{R}^{m \times (d+1)} orthonormal columns, QTQ=IQ^{\mathsf T}Q = I
RR R(d+1)×(d+1)\mathbb{R}^{(d+1) \times (d+1)} upper-triangular and nonsingular
UU Rm×(d+1)\mathbb{R}^{m \times (d+1)} orthonormal columns: the left factor of the thin SVD A=UΣVTA = U\Sigma V^{\mathsf T}
Σ, V\Sigma,\ V R(d+1)×(d+1)\mathbb{R}^{(d+1) \times (d+1)} Σ=diag(σ1,…,σd+1)\Sigma = \mathrm{diag}(\sigma_1, \dots, \sigma_{d+1}) with σ1≥⋯≥σd+1>0\sigma_1 \ge \dots \ge \sigma_{d+1} > 0; VV orthogonal
κ(⋅)\kappa(\cdot) [1,∞)[1, \infty) the 2-norm condition number σmax⁡/σmin⁡\sigma_{\max} / \sigma_{\min} of a matrix of full column rank
θ\theta [0,π/2][0, \pi/2] the angle between bb and the range of AA; here θ=0\theta = 0
ϵ\epsilon R>0\mathbb{R}_{>0} machine epsilon for doubles, 2−522^{-52}
How these symbols connect

Sampling m=50m = 50 points ti∈[0,1]t_i \in [0, 1] and choosing a degree d∈{1,…,15}d \in \{1, \dots, 15\} builds the tall Vandermonde matrix A∈Rm×(d+1)A \in \mathbb{R}^{m \times (d+1)} of full column rank, which maps polynomial coefficients x∈Rd+1x \in \mathbb{R}^{d+1} to fitted values Ax∈RmAx \in \mathbb{R}^{m}. With data b=A1∈Rmb = A\mathbf 1 \in \mathbb{R}^{m}, the exact minimiser of ∥Ax−b∥2\lVert Ax - b \rVert_2 is x∗=1x^* = \mathbf 1 (angle θ=0\theta = 0). Two factorisations produce computed coefficients x^∈Rd+1\hat x \in \mathbb{R}^{d+1}: forming the square Gram matrix ATAA^{\mathsf T}A for the normal equations, or factorising A=QRA = QR directly; the thin SVD A=UΣVTA = U\Sigma V^{\mathsf T} defines κ(⋅)\kappa(\cdot) for comparing their conditioning in double precision (ϵ\epsilon). All quantities are dimensionless.

Symbol-by-symbol notes
$A$ — Vandermonde matrix of degree $d$

Has shape m×(d+1)m \times (d + 1) because a degree-dd polynomial c0+c1t+⋯+cdtdc_0 + c_1 t + \dots + c_d t^d has d+1d + 1 coefficients, indexed j∈{0,…,d}j \in \{0, \dots, d\} with Aij=ti jA_{ij} = t_i^{\,j}. Because the m=50m = 50 nodes tit_i are distinct and m>d+1m > d + 1, AA has full column rank d+1d + 1.

$Q$ and $R$ — thin QR factors

Q∈Rm×(d+1)Q \in \mathbb{R}^{m \times (d+1)} is rectangular with orthonormal columns, so QTQ=Id+1Q^{\mathsf T}Q = I_{d+1} (whereas QQTQQ^{\mathsf T} is the m×mm \times m orthogonal projector onto the column space of AA, not ImI_m). R∈R(d+1)×(d+1)R \in \mathbb{R}^{(d+1) \times (d+1)} is square, upper-triangular, and nonsingular whenever AA has full column rank.

$U$, $\Sigma$, $V$ — thin SVD of a tall matrix

Like QQ, U∈Rm×(d+1)U \in \mathbb{R}^{m \times (d+1)} has orthonormal columns (UTU=Id+1U^{\mathsf T}U = I_{d+1}), while Σ=diag(σ1,…,σd+1)\Sigma = \mathrm{diag}(\sigma_1, \dots, \sigma_{d+1}) and V∈R(d+1)×(d+1)V \in \mathbb{R}^{(d+1) \times (d+1)} are square with VTV=VVT=Id+1V^{\mathsf T}V = VV^{\mathsf T} = I_{d+1}. Do not confuse this UU with the upper-triangular factor UU of LU in §§1–2.

$\kappa(\cdot)$ for a rectangular matrix

For a tall matrix AA of full column rank, A−1A^{-1} does not exist, so κ2(A)\kappa_2(A) is defined via the singular values as σmax⁡/σmin⁡=σ1/σd+1∈[1,∞)\sigma_{\max} / \sigma_{\min} = \sigma_1 / \sigma_{d+1} \in [1, \infty). For a square SPD matrix such as ATAA^{\mathsf T}A, this agrees with ∥⋅∥2∥(⋅)−1∥2\lVert \cdot \rVert_2 \lVert (\cdot)^{-1} \rVert_2.

$\theta$ — residual angle

The angle in [0,π/2][0, \pi/2] between bb and its projection Ax∗Ax^* onto the column space of AA, satisfying sin⁡θ=∥b−Ax∗∥2/∥b∥2\sin\theta = \lVert b - Ax^* \rVert_2 / \lVert b \rVert_2 (when b≠0b \ne 0). Setting b=A1b = A\mathbf 1 makes θ=0\theta = 0 so the experiment isolates zero-residual behaviour.

Fit a polynomial of degree dd to 50 points tit_i in [0,1][0, 1]: the matrix Aij=ti jA_{ij} = t_i^{\,j} is 50×(d+1)50 \times (d+1), more rules than unknowns, so solve min⁡x∥Ax−b∥2\min_x \|Ax - b\|_2. To isolate the algorithms, take b=A1b = A\mathbf 1: the fit is exact and the right coefficients are all ones.

Normal equations. The gradient of ∥Ax−b∥2\|Ax - b\|^2 is 2A⊤(Ax−b)2A^\top(Ax - b); set it to zero: A⊤Ax=A⊤bA^\top A x = A^\top b, a square SPD system for Cholesky. With A=UΣV⊤A = U\Sigma V^\top, A⊤A=VΣ2V⊤A^\top A = V\Sigma^2 V^\top, so its singular values are σi2\sigma_i^2 and

κ(A⊤A)=κ(A)2.\kappa(A^\top A) = \kappa(A)^2 .

Forming A⊤AA^\top A has already thrown away what a κ2ϵ\kappa^2\epsilon error throws away, however good the solver after it.

QR. Write A=QRA = QR with orthonormal columns in QQ. An orthogonal map keeps lengths, so minimising ∥Ax−b∥\|Ax - b\| reduces to Rx=Q⊤bRx = Q^\top b, and κ(R)=κ(A)\kappa(R) = \kappa(A). Householder QR is backward stable, so the error is about κϵ\kappa\epsilon. The price is about twice the flops of the normal equations when the matrix is tall.

dd κ(A)\kappa(A) κ(A⊤A)\kappa(A^\top A) κ(A)2\kappa(A)^2 normal equations error QR error
6 2.0e+04 3.9e+08 3.9e+08 2.7e-09 6.1e-14

Predict first. At degree 11, κ(A)=1.2×108\kappa(A) = 1.2 \times 10^{8}. The normal equations' relative error in the coefficients is about

Choose a prediction

Check answer (without scripting)

Answer: 1e-1 or worse: almost every digit is gone. κ2ϵ\kappa^2\epsilon is of order 1. Squaring a condition number of 10810^8 uses up all sixteen digits.

Reveal result (lock in your prediction above first)
14.5811.51510⁻¹⁸10⁻¹²10⁻⁶10⁰10⁶normal equationsQRseriesdegree drelative error

At degree 11 the normal equations miss by 1.1e-01 and QR by 7.5e-09. Each method tracks its dashed line: QR follows κϵ\kappa\epsilon, the normal equations κ2ϵ\kappa^2\epsilon. With a non-zero fit residual even QR picks up a κ2tan⁡θ\kappa^2\tan\theta term, where θ\theta is the angle between bb and the range of AA; the normal equations pay κ2\kappa^2 whatever θ\theta is. An error of exactly 0 is drawn at 10−1710^{-17}, so the log axis can show it.

When QR least squares breaks
  • Assumes: AA has full column rank and the fit residual is small
  • When it fails: with rank deficiency RR has a near-zero diagonal and the coefficients are arbitrary in the null direction; with a large residual even QR's sensitivity carries a κ2tan⁡θ\kappa^2 \tan\theta term
  • Check: look at ∣rkk∣|r_{kk}| or the singular values; switch to the SVD when the smallest is near ϵσ1\epsilon\sigma_1

6. The SVD shows rank

Setting: the problem, its sets and its symbols

Problem. Given A∈Rm×nA \in \mathbb{R}^{m \times n} and a tolerance rtol>0\mathrm{rtol} > 0, find the numerical rank, the number of singular values σi>rtol σ1\sigma_i > \mathrm{rtol}\,\sigma_1, and the left singular vectors of the discarded σi\sigma_i, which name the dependent rows; apply it to the constraint Jacobian J(t)J(t), with and without a copied row.

Symbol In Meaning
m, nm,\ n N\mathbb{N} rows (rules) and columns (unknowns); here n=3n = 3 and m∈{3,4}m \in \{3, 4\}
A, J(t)A,\ J(t) Rm×n\mathbb{R}^{m \times n} a general matrix, and the example (10001011t)\begin{pmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 1 & 1 & t \end{pmatrix}, with (1,0,0)(1, 0, 0) appended as row 4 when the box is ticked
tt {10−16,10−15.5,…,1}\{10^{-16}, 10^{-15.5}, \dots, 1\} how far row 3 is from the sum of rows 1 and 2; the slider sets t=10kt = 10^{k}
U, VU,\ V O(m), O(n)O(m),\ O(n) orthogonal matrices of left and right singular vectors, A=UΣVTA = U\Sigma V^{\mathsf T}
Σ\Sigma Rm×n\mathbb{R}^{m \times n} zero except Σii=σi\Sigma_{ii} = \sigma_i
σi\sigma_i [0,∞)[0, \infty) the singular values, σ1≥σ2≥⋯≥σmin⁡(m,n)\sigma_1 \ge \sigma_2 \ge \dots \ge \sigma_{\min(m, n)}
κ2\kappa_2 [1,∞][1, \infty] σ1/σn\sigma_1 / \sigma_n for m≥nm \ge n; infinite when σn=0\sigma_n = 0
rtol\mathrm{rtol} (0,1)(0, 1) the relative threshold; otkd uses 10−1010^{-10}
rank⁡\operatorname{rank} {0,…,min⁡(m,n)}\{0, \dots, \min(m, n)\} the number of singular values above the threshold
ww Rm\mathbb{R}^{m} a left singular vector for a discarded σi\sigma_i: wTA≈0w^{\mathsf T}A \approx 0, and its nonzero entries are the rows in the dependence
How these symbols connect

Given any matrix A∈Rm×nA \in \mathbb{R}^{m \times n} — here the constraint Jacobian J(t)J(t) with n=3n = 3 unknowns and m∈{3,4}m \in \{3, 4\} rows controlled by tt and the duplicate-row toggle — the full SVD A=UΣVTA = U\Sigma V^{\mathsf T} separates orthogonal row and column bases U∈O(m)U \in O(m), V∈O(n)V \in O(n) from the ordered singular values σ1≥⋯≥σmin⁡(m,n)≥0\sigma_1 \ge \dots \ge \sigma_{\min(m, n)} \ge 0 in Σ\Sigma. Comparing each σi\sigma_i against the relative cutoff rtol σ1\mathrm{rtol}\,\sigma_1 yields the numerical rank⁡\operatorname{rank}, while κ2=σ1/σn\kappa_2 = \sigma_1 / \sigma_n measures conditioning when m≥nm \ge n. Any discarded singular value comes with a left singular vector w∈Rmw \in \mathbb{R}^{m} (a column of UU) whose nonzero components identify the dependent rows; all entries in the example are dimensionless.

Symbol-by-symbol notes
$U \in O(m)$ and $V \in O(n)$ — full SVD orthogonal factors

O(m)O(m) is the group of square m×mm \times m orthogonal matrices (UTU=UUT=ImU^{\mathsf T}U = UU^{\mathsf T} = I_m), and likewise V∈O(n)V \in O(n). Unlike the thin SVD of §5, the full SVD keeps all mm columns of UU even when m>nm > n, because the extra columns span the left null space where redundant-row dependencies live.

$\Sigma$ and $\sigma_i$ — rectangular diagonal matrix and singular values

Σ∈Rm×n\Sigma \in \mathbb{R}^{m \times n} has the same shape as AA, holding σ1≥σ2≥⋯≥σmin⁡(m,n)≥0\sigma_1 \ge \sigma_2 \ge \dots \ge \sigma_{\min(m, n)} \ge 0 on its main diagonal and zeros elsewhere. Zero singular values are allowed here (unlike §5's full-rank case), signalling exact rank deficiency.

$\kappa_2 \in [1, \infty]$ — extended condition number

Defined as σ1/σn\sigma_1 / \sigma_n for m≥nm \ge n, with the convention κ2=∞\kappa_2 = \infty when σn=0\sigma_n = 0 (and σ1>0\sigma_1 > 0). It differs from the truncated ratio σ1/σrank⁡\sigma_1 / \sigma_{\operatorname{rank}} computed after discarding singular values below rtol σ1\mathrm{rtol}\,\sigma_1.

$\mathrm{rtol}$ and $\operatorname{rank}$ — numerical rank

Exact algebraic rank jumps discontinuously at σi=0\sigma_i = 0, whereas floating-point rounding turns exact zeros into tiny nonzero numbers. Numerical rank counts σi>rtol σ1\sigma_i > \mathrm{rtol}\,\sigma_1 using a dimensionless relative threshold (otkd sets rtol=10−10\mathrm{rtol} = 10^{-10}), so scaling AA by any nonzero constant leaves the rank unchanged.

$w$ — left singular vector for a discarded $\sigma_i$

A unit column of UU paired with a singular value σi≤rtol σ1\sigma_i \le \mathrm{rtol}\,\sigma_1, or with one of the extra m−nm - n zero rows of Σ\Sigma when m>nm > n. Since wTA=σiviTw^{\mathsf T}A = \sigma_i v_i^{\mathsf T}, the linear combination ∑j=1mwjAj,:\sum_{j=1}^{m} w_j A_{j,:} is near zero, so the nonzero entries of ww pinpoint the rows involved in the dependence.

Every matrix factors as A=UΣV⊤A = U\Sigma V^\top with orthogonal U,VU, V and σ1≥σ2≥⋯≥0\sigma_1 \ge \sigma_2 \ge \dots \ge 0 on the diagonal of Σ\Sigma. Two facts make it the diagnostic of last resort. κ2=σ1/σn\kappa_2 = \sigma_1/\sigma_n, so §3's condition number is a ratio of singular values. And σn\sigma_n is the 2-norm distance from AA to the nearest singular matrix: 1/κ21/\kappa_2 is how far, in relative terms, the problem is from having no unique answer.

So rank in floating point is a decision with a tolerance: count the σi>rtol⋅σ1\sigma_i > \text{rtol}\cdot\sigma_1. otkd uses rtol=10−10\text{rtol} = 10^{-10} on the Jacobian of a mechanism's constraint rows. Take three rules in three unknowns, the third nearly the sum of the first two:

J(t)=(10001011t).J(t) = \begin{pmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 1 & 1 & t \end{pmatrix}.

For t≠0t \ne 0 it is invertible in exact arithmetic. Move tt, and add a duplicated rule, and watch the verdict otkd would reach.

Predict first. At t=10−12t = 10^{-12}, what rank does otkd's rule give J(t)J(t)?

Choose a prediction

Check answer (without scripting)

Answer: 2: the smallest singular value is below the threshold. σ3≈6×10−13\sigma_3 \approx 6 \times 10^{-13} against a threshold near 1.7×10−101.7 \times 10^{-10}. A rule that is the sum of two others to twelve digits adds no information a double can hold.

Reveal result (lock in your prediction above first)
10⁻¹⁶10⁻¹²10⁻⁸10⁻⁴10⁰10⁻¹⁷10⁻¹³10⁻⁹10⁻⁵10⁻¹σ1σ2σ3seriestsingular value
singular values rank rows verdict solver rows in the dependence condition as diagnose reports it σ1/σmin⁡\sigma_1/\sigma_{\min}
1.73e+00, 1.00e+00, 5.77e-05 3 3 square Newton, an LU solve per step (§1–2) none 3.00e+04 3.00e+04

diagnose reports the condition of the part it keeps, σ1/σrank\sigma_1/\sigma_{\text{rank}}: when the rank drops, that number falls back to order 1, because the near-dependence has been moved out of the conditioning and into the rank. The left singular vectors for the discarded σ\sigma name the rows in the dependence: with the copied row and tt visible, rows 1 and 4.

This is the bridge to the S track. System.classify counts the rank of the constraint Jacobian at the design pose with exactly this rule and picks the solver: rank below the number of unknowns is under-determined; rank equal to the number of rows is square and goes to Newton, whose step is np.linalg.solve with a lstsq fallback; more rows than rank is redundant and goes to least squares. diagnose is the same SVD, kept to name the redundant rows.

source: `open_textbook.otkd.solvers.system.System.classify` — `solvers/otkd/src/open_textbook/otkd/solvers/system.py:78`
def classify(self) -> str:
    """`square`, `redundant`, `under`, or `equilibrium`."""
    if self.springs:
        return "equilibrium"
    rank, _, _ = rank_and_null(self.jacobian(self.problem.q0()), rtol=1e-10)
    n = self.problem.n
    if rank < n:
        return "under"
    return "square" if len(self.constrained) == rank else "redundant"
source: `open_textbook.otkd.solvers.svd.diagnose` — `solvers/otkd/src/open_textbook/otkd/solvers/svd.py:23`
def diagnose(system: System, q: np.ndarray, *, rows: list[int] | None = None, rtol: float = 1e-10) -> Diagnosis:
    """Rank, left null space and participating row names at `q`.

    `rtol` governs both decisions: singular values at or below `rtol * s[0]` are
    treated as zero when computing `rank`, and rows whose largest left-null
    coefficient exceeds `rtol` are named in `dependent_rows`.
    """
    rows = system.constrained if rows is None else rows
    jac = system.jacobian(q, rows)
    rank, null, s = left_null(jac, rtol=rtol)
    names = [system.problem.rows[i].name for i in rows]
    if null.shape[1]:
        weight = np.max(np.abs(null), axis=1)
        dependent = tuple(name for name, w in zip(names, weight) if w > rtol)
    else:
        dependent = ()
    condition = float(s[0] / s[rank - 1]) if rank else float("inf")
    return Diagnosis(
        singular_values=s,
        rank=rank,
        redundancy=len(rows) - rank,
        dependent_rows=dependent,
        null_space=null,
        condition=condition,
    )
When a rank decision breaks
  • Assumes: a gap in the singular values separates the zero ones from the rest
  • When it fails: with no gap the rank depends on rtol, and a change of tolerance changes otkd's verdict between square, redundant and under-determined
  • Check: print the singular values beside the threshold 10−10σ110^{-10}\sigma_1 and look for several orders of magnitude of daylight

Practice

Practice A (steps partly filled). The book's matrix A=(1100001)A = \begin{pmatrix} 1 & 1000 \\ 0 & 1 \end{pmatrix} has ∥A∥∞=1001\|A\|_\infty = 1001 (largest absolute row sum) and A−1=(1−100001)A^{-1} = \begin{pmatrix} 1 & -1000 \\ 0 & 1 \end{pmatrix}, so ∥A−1∥∞=  ?\|A^{-1}\|_\infty = \;? Enter κ∞(A)=∥A∥∞∥A−1∥∞\kappa_\infty(A) = \|A\|_\infty\|A^{-1}\|_\infty.

Worked solution

∥A−1∥∞=1+1000=1001\|A^{-1}\|_\infty = 1 + 1000 = 1001, so κ∞=10012=1002001\kappa_\infty = 1001^2 = 1002001: about six digits lost. That is why a 10−310^{-3} change in b2b_2 moves x1x_1 by 1.

Practice B (with a hint). A least-squares fit with zero residual has κ(A)=105\kappa(A) = 10^5. You solve it with the normal equations in double precision. Enter log⁡10\log_{10} of the relative error you expect.

Worked solution

κ(A⊤A)=1010\kappa(A^\top A) = 10^{10}, so the error is about 1010×2.2×10−16=2.2×10−610^{10} \times 2.2 \times 10^{-16} = 2.2 \times 10^{-6}, and log⁡10≈−5.7\log_{10} \approx -5.7. QR would give about κϵ≈2×10−11\kappa\epsilon \approx 2 \times 10^{-11}. §5's table at degree 7 has κ(A)\kappa(A) near 10510^5: compare.

Hint 1

What is κ(A⊤A)\kappa(A^\top A) in terms of κ(A)\kappa(A)?

Hint 2

The error of a backward-stable solve is about κϵ\kappa\epsilon for the system it actually solves, with ϵ≈2.2×10−16\epsilon \approx 2.2 \times 10^{-16}.

Practice C (on your own). Newton's method on n=2000n = 2000 unknowns with a dense Jacobian converges in 6 steps. Enter the total elimination work in units of 10910^9 multiply-adds.

Worked solution

One factorisation per step at n3/3=2.67×109n^3/3 = 2.67 \times 10^9, times 6 steps: 16×10916 \times 10^9. The triangular solves add 6×20002=2.4×1076 \times 2000^2 = 2.4 \times 10^7, under 0.2 per cent.

Mixed practice. Choose the method, with M01 and M02 in the mix.

1. Each step of a Newton solve has a dense 300×300300 \times 300 Jacobian with κ≈104\kappa \approx 10^4. Which method for the step?

Question 1

2. Fit 6 coefficients to 500 measurements; the design matrix has κ≈109\kappa \approx 10^9. Which method?

Question 2

3. Evaluate (1−cos⁡x)/x2(1 - \cos x)/x^2 at x=10−8x = 10^{-8}. It returns 0; the limit is 12\tfrac12. What fixes it?

Question 3

4. A constraint Jacobian has singular values 2.1, 0.9, 3×10−152.1,\ 0.9,\ 3 \times 10^{-15}. Newton's iteration crawls. What next?

Question 4

Show answers and explanations

1. LU with partial pivoting. Square, dense and well conditioned: np.linalg.solve, about n3/3n^3/3 multiply-adds, backward stable, and κϵ≈10−12\kappa\epsilon \approx 10^{-12}.

2. QR, or the SVD if the rank is in doubt. QR keeps κ\kappa at 10910^9, so about 7 digits remain. The SVD also shows whether a coefficient is undetermined.

3. Rewrite it as 2 sin squared (x/2) over x squared. M01's cure: remove the subtraction of nearly equal numbers. The rewritten form returns 0.5 at x=10−8x = 10^{-8}.

4. Read the rank from the SVD and find the dependent rows. σ3/σ1≈10−15\sigma_3/\sigma_1 \approx 10^{-15} is below otkd's 10−1010^{-10}, so the rank is 2. The left singular vector names the rows to fix or to fit by least squares.

Check yourself (closed book)

1. Dense LU on nn unknowns takes 0.1 s. About how long for 2n2n?

Question 1
How sure?

2. Elimination without row swaps on (10−20111)x=(1,2)\begin{pmatrix} 10^{-20} & 1 \\ 1 & 1 \end{pmatrix} x = (1, 2) returns x1=0x_1 = 0, although κ∞(A)=4\kappa_\infty(A) = 4. Who is to blame?

Question 2
How sure?

3. A solve returns x^\hat x with ∥r∥/∥b∥=10−16\|r\|/\|b\| = 10^{-16}, and κ(A)=1012\kappa(A) = 10^{12}. What can you say about ∥x^−x∥/∥x∥\|\hat x - x\|/\|x\|?

Question 3
How sure?

4. Gaussian elimination with partial pivoting is backward stable. What does that promise?

Question 4
How sure?

5. Why do the normal equations A⊤Ax=A⊤bA^\top A x = A^\top b lose more digits than QR?

Question 5
How sure?

6. The singular values of a Jacobian are 1.7, 1.0, 6×10−131.7,\ 1.0,\ 6 \times 10^{-13}. otkd counts those above 10−10σ110^{-10}\sigma_1. What does it report?

Question 6
How sure?

7. Each Newton step solves J δ=−FJ\,\delta = -F. What does an ill-conditioned JJ at the root cost?

Question 7
How sure?

Show answers and explanations

1. About 0.8 s. Elimination does about n3/3n^3/3 multiply-add pairs, so doubling nn multiplies the work by 23=82^3 = 8.

2. The algorithm: the multiplier 1e20 swamps the second row. u22=1−1020u_{22} = 1 - 10^{20} rounds to −1020-10^{20}, so the 1 in row 2 is lost. Partial pivoting keeps every multiplier at most 1 in size and avoids it.

3. It is at most about 1e-4. Forward error ≤κ⋅∥r∥/∥b∥=1012×10−16\le \kappa \cdot \|r\|/\|b\| = 10^{12} \times 10^{-16}. Twelve of sixteen digits may be gone.

4. The computed answer exactly solves a nearby system. (A+ΔA)x^=b(A + \Delta A)\hat x = b with ∥ΔA∥/∥A∥\|\Delta A\|/\|A\| a modest multiple of ϵ\epsilon. The forward error is then at most about κ\kappa times that.

5. Forming A-transpose-A squares the condition number. The singular values of A⊤AA^\top A are σi2\sigma_i^2, so κ(A⊤A)=κ(A)2\kappa(A^\top A) = \kappa(A)^2. QR solves Rx=Q⊤bRx = Q^\top b with κ(R)=κ(A)\kappa(R) = \kappa(A).

6. Rank 2: one rule is numerically a combination of the others. The smallest singular value is below 10−10σ110^{-10}\sigma_1, so it counts as zero. The left singular vector for it names the rows in the dependence.

7. The step is only good to about kappa times epsilon, and near a singular J convergence slows. The step is a linear solve, so it inherits κ(J)ϵ\kappa(J)\epsilon. A singular JJ at the root is the nn-dimensional double root of M02.

Keep these:

  1. Elimination is A=LUA = LU: n3/3n^3/3 multiply-adds once, then n2n^2 per right-hand side.
  2. Pivot: a small pivot is the algorithm's failure, and row swaps remove it.
  3. Forward error ≤κ×\le \kappa \times backward error; log⁡10κ\log_{10}\kappa is the number of digits the problem takes.
  4. A small residual certifies the solver, not the answer. Solve; do not invert.
  5. Normal equations square κ\kappa; QR keeps it; the SVD shows rank, which is how otkd chooses between Newton and least squares.

Your connection

Name a linear solve you rely on (a fit, a calibration, a Newton step). Do you know its condition number? Which of its digits would you defend?

Answers

Practice A. κ∞=1001×1001=1002001\kappa_\infty = 1001 \times 1001 = 1002001.

Practice B. about −5.7-5.7, from κ(A)2ϵ=2.2×10−6\kappa(A)^2\epsilon = 2.2 \times 10^{-6}.

Practice C. 6×20003/3=16×1096 \times 2000^3/3 = 16 \times 10^9 multiply-adds.

Mixed practice. LU with partial pivoting; QR (or SVD); rewrite the formula; SVD rank and the dependent rows.

Where the maths is derived

Optional. These notes give the intuition and application; the sources below give the full derivations and proofs.

§1 Gaussian elimination is LU

§2 Partial pivoting

§3 Condition number and the error bound

§4 Backward stability

§5 Least squares: normal equations against QR

§6 The SVD shows rank


Compiled from notebooks/methods/m03_linear_systems.py — run locally with cd notebooks && uv run marimo edit methods/m03_linear_systems.py for live Python scratch cells.

Back to the book: Linear system · One matrix, two errors

Settings

Colour scheme

Light or dark pages. Your colour and layout stay the same.

Colour

The paper, ink and lettering. Your layout stays the same.

Quiet colours
Bold colours
Reading offline

Keep this book on this device and read it offline.