Conditioning of Linear Systems
How much can the solution of change when or change slightly, for example by measurement or rounding errors? The answer is governed by one number, the condition number . It tells you how many digits of the solution to trust, before any algorithm is chosen.
The bound
If , then and , so
A similar bound holds for perturbations of : . In the 2-norm, ; always , and for a singular matrix.
Rule of thumb. A backward stable solver in double precision gives a relative error of about : you lose digits.
The Hilbert matrix
The Hilbert matrix is a classic ill-conditioned example. Solve with a known solution of all ones and watch the digits disappear:
import numpy as np
from scipy.linalg import hilbert
for n in (4, 8, 12, 16):
H = hilbert(n)
x_true = np.ones(n)
b = H @ x_true
x = np.linalg.solve(H, b)
err = np.linalg.norm(x - x_true) / np.linalg.norm(x_true)
res = np.linalg.norm(b - H @ x) / np.linalg.norm(b)
print(f"n = {n:2d}: cond = {np.linalg.cond(H):.1e}, relative error = {err:.1e}, relative residual = {res:.1e}")n = 4: cond = 1.6e+04, relative error = 3.9e-13, relative residual = 4.1e-17
n = 8: cond = 1.5e+10, relative error = 2.3e-07, relative residual = 1.1e-16
n = 12: cond = 1.8e+16, relative error = 1.8e-01, relative residual = 1.5e-16
n = 16: cond = 4.3e+17, relative error = 2.1e+00, relative residual = 1.2e-16
The error follows until no digits are left, yet the residual stays tiny: the solver is backward stable, and the computed exactly solves a nearby system. A small residual does not mean a small error when is large.
Residual versus error
So a residual check certifies the solution only if is known to be moderate.
Estimating cheaply
Computing or the SVD to get costs more than the solve itself. LAPACK estimates from the LU factors in (Hager's / Higham's estimator):
import numpy as np
import scipy.linalg as sla
from scipy.linalg import hilbert
rng = np.random.default_rng(6)
for name, A in [("random 200x200", rng.normal(size=(200, 200))), ("Hilbert 10", hilbert(10))]:
lu, piv = sla.lu_factor(A)
rcond = sla.lapack.dgecon(lu, np.linalg.norm(A, 1), norm="1")[0]
print(f"{name:15s}: estimate 1/rcond = {1 / rcond:.3e}, exact cond_1 = {np.linalg.cond(A, 1):.3e}")random 200x200 : estimate 1/rcond = 1.478e+04, exact cond_1 = 1.478e+04
Hilbert 10 : estimate 1/rcond = 3.536e+13, exact cond_1 = 3.535e+13
The estimate is usually within a small factor, which is all that matters for counting digits.
Remedies for ill-conditioning
- Rescale rows and columns (equilibration) when the trouble comes from units: of is but trivially fixable.
- Reformulate: avoid normal equations (Normal Equations), monomial bases (Polynomial Interpolation).
- Regularize: replace by (ridge) or truncate small singular values.
In software
np.linalg.cond(A, p)computes the exact condition number (via SVD for ).scipy.linalg.solvewarns withLinAlgWarning: Ill-conditioned matrix (rcond=…)when the estimated reciprocal condition number is below machine precision.
Exercises
Exercise 1: Digits
. How many correct digits can you expect from np.linalg.solve?
Answer
About 8 to 9.
Solution
.
Exercise 2: Condition of a diagonal matrix
What is of ?
Answer
Solution
The singular values are ; .
Exercise 3: Sensitivity experiment (code)
For and , perturb by and compare the relative change in with times the relative change in .
Answer
The solution changes from to : relative change , while .
Solution
import numpy as np
A = np.array([[1.0, 1.0], [1.0, 1.0001]])
b = np.array([2.0, 2.0001]); db = np.array([0.0, 1e-4])
x = np.linalg.solve(A, b); x2 = np.linalg.solve(A, b + db)
print(np.round(x, 6) + 0, np.round(x2, 6) + 0)
rel_x = np.linalg.norm(x2 - x) / np.linalg.norm(x)
bound = np.linalg.cond(A) * np.linalg.norm(db) / np.linalg.norm(b)
print(f"cond {np.linalg.cond(A):.4e}, relative change in x {rel_x:.4f}, bound {bound:.4f}")[1. 1.] [0. 2.]
cond 4.0002e+04, relative change in x 1.0000, bound 1.4142
Test yourself
The condition number of in the 2-norm is…
- the number of rows
A tiny residual guarantees a small error when…
- always
- is moderate
- is large
- never
Is a small determinant a sign of ill-conditioning?
- Yes, always
- No; but
- Only for symmetric matrices
- Only if negative