The Matrix Condition Number
Learning Objectives
- Define , compute it in the 1-, 2- and -norms, and read it as the inverse of the relative distance to a singular matrix.
- Derive the bound and state the hypothesis needed when itself is perturbed.
- Explain why a tiny residual does not mean a tiny error, and connect the two through .
- Use " costs up to digits" as a ceiling, not a prediction.
- Measure the Hilbert matrix losing digits, and tell a bad condition number from a small determinant or from bad scaling.
Context & Motivation
conditioning-and-numerical-stability separates the sensitivity of a problem from the quality of an algorithm. This lesson puts a number on that sensitivity for . The number matters because solvers such as lu-decomposition-and-partial-pivoting and iterative-methods-jacobi-and-gauss-seidel hand back a vector with no comment attached. Later in this lesson, a Hilbert system is solved with a relative residual of about and a relative error of . Nothing in the output hints at it, and numpy.linalg.solve raises no warning. The condition number is what predicts this.
Core Theory
Definition
For an invertible square matrix and an induced norm,
and when is singular. Three facts follow directly. Since , always . Since , for . And in the 2-norm , the ratio of the longest to the shortest axis of the ellipsoid that makes from the unit sphere; for a symmetric positive definite matrix this is . The value depends on the norm, usually mildly: the Hilbert matrix has and .
Perturbing the right-hand side
Hypotheses. is invertible, , and . Conclusion.
The proof is two lines. Subtracting gives , so . And gives , that is, . Multiply the two. Equality needs both inequalities tight at once: in the direction stretches most and in the direction stretches most. Example 1 attains it, so the bound cannot be improved in general.
Perturbing the matrix
Hypotheses. is invertible, , and . Conclusion. is invertible, and the solution of satisfies
The hypothesis is not decoration. The smallest that makes singular has , so is exactly the inverse of the relative distance to a singular matrix. A perturbation that size can already leave no solution at all, so no bound can cover it (Example 2).
Residual versus error
For a computed , the residual is . Then solves exactly the system with right-hand side , so the first theorem gives
The residual measures backward error, and it is computable. The error is forward error, and it is not. An iteration from iterative-methods-jacobi-and-gauss-seidel that stops on a small residual has to pay this factor too.
Gaussian elimination with partial pivoting normally returns near unit roundoff (about in float64, in float32). Since , this gives , about . The ratio does not have to be near : for with its smallest right singular vector it is , while . Hence the working rule: with , expect to lose up to of the roughly 16 (float64) or 7 (float32) significant digits. It is a ceiling from a worst-case bound, not a forecast.
The Hilbert matrix
has entries for . It is symmetric positive definite, and its inverse has integer entries (scipy.linalg.invhilbert with exact=True returns them as integers). It is a Gram matrix:
So is what appears when functions are fitted by polynomials in the monomial basis on , and its bad conditioning says that are nearly linearly dependent there. That thread continues in least-squares-curve-fitting. The known growth is , a factor approaching per unit of , which is about digits per row.
Worked Examples
Example 1: nearly parallel rows, and the bound attained
Take
Then and , so . Also . Changing by , to , gives . The relative change in is and in it is , so the amplification is , half the bound.
The worst direction is . With , exact arithmetic gives . Now , the relative change in is , and their ratio is , exactly .
Example 2: when the hypothesis on fails
Keep and and change only , using exact fractions. Write .
textnew a22 rho solution x rel. change full bound first-order kappa*dA/A 1.00009 0.200 (0.8889, 1.1111) 0.111 0.250 0.200 1.000001 1.980 (-98, 100) 99 not valid 1.980 1 2.000 none (singular) undefined not valid 2.000
At the bound holds with room to spare. At the hypothesis is gone, the formula's denominator is negative, and the first-order estimate is wrong by a factor of . The last row has no solution at all, because is outside the range of the singular matrix.
Example 3: the Hilbert matrix at real size
Solve with numpy.linalg.solve in float64. The true solution is , and is computed in exact rationals and then rounded, so it is as good as float64 allows. Values of come from 200-digit eigenvalues, and the residual column is .
textn kappa_2 rel. residual rel. error correct digits 16 - log10(kappa) 4 1.55e+04 0.00e+00 2.91e-13 12.5 11.8 6 1.50e+07 6.86e-17 9.61e-11 10.0 8.8 8 1.53e+10 3.27e-17 2.75e-07 6.6 5.8 10 1.60e+13 2.83e-17 3.15e-04 3.5 2.8 12 1.71e+16 8.21e-17 2.95e-01 0.5 -0.2 14 1.85e+19 2.06e-17 4.48e+00 -0.7 -3.3 16 2.02e+22 2.42e-17 6.35e+00 -0.8 -6.3
The residual stays below while the error climbs to and past it. Up to the rule tracks the error, about one digit pessimistic. After that the ceiling is too high to be informative: at the error is , nowhere near . The ratio rises from at to at , moving toward .
The same matrix sizes break the tools that measure them:
textn exact kappa_2 numpy.linalg.cond 12 1.71e+16 1.68e+16 13 5.63e+17 2.50e+18 14 1.85e+19 1.89e+17 16 2.02e+22 4.35e+17
Past , the computed is rounding noise, and the answer can be wrong in either direction. scipy.linalg.solve emits a LinAlgWarning at (reported rcond ) and none at ; numpy.linalg.solve emitted no warning at any size tried.
Example 4: the data alone does the damage
Is the solver to blame? Take the stored float64 and and solve them in exact rational arithmetic, so no operation rounds at all.
textn exact solve of stored data numpy.linalg.solve 8 2.91e-07 2.75e-07 10 2.42e-04 3.15e-04 12 1.43e-01 2.95e-01 14 4.20e+00 4.48e+00
Rounding , and so on when storing the matrix already costs the same digits. This is conditioning, not instability. The stored matrix is measurably different too: has true smallest eigenvalue , and its float64 copy has , so it is not even positive definite. For , the relative distance to singularity is , below .
The same solver on the same size with a good matrix gives the control: the tridiagonal matrix with on the diagonal and beside it, , has , and the error for is .
float32 runs out sooner, as the rule says: the error is at , at and at . Because the Hilbert entries are known exactly, more digits do fix this case: with mpmath's lu_solve at 50 decimal digits, gives a relative 2-norm error between and , depending on how is rounded. With measured data, extra working precision cannot restore what the data never had.
Example 5: determinant and scaling are different questions
textmatrix det kappa_2 0.1 * I (size 20) 1e-20 1 [[1, 1e8], [0, 1]] 1 1e+16 [[1, 2], [3, 4]] -2 14.9 [[1e10, 2e10], [3, 4]] -2e10 2.5e+10
The determinant scales with the size of the entries; does not. The last two rows are the same equations with the first one multiplied by , as happens when one equation is written in different units. jumps by nine orders, yet numpy.linalg.solve loses nothing: over 2000 random right-hand sides the worst relative error was . Rescaling rows or columns changes , so equilibrate before reading it as a verdict.
Common Misconceptions & Pitfalls
- "A small residual means an accurate answer." Only when is moderate. In Example 3, has residual and error .
- "A small determinant means ill-conditioned." of size 20 has determinant and . A unit determinant can sit beside .
- " means exactly digits are lost." It is a worst-case ceiling. The scaled matrix of Example 5 lost none, and the Hilbert solves lost about one digit fewer than the rule.
- "A better solver will fix it." Exact arithmetic on the stored data was just as bad (Example 4). The usual cures are reformulation (a better basis, as in
least-squares-curve-fitting), rescaling, or exact data with more precision. - "
condalways gives the true number." Past about it reports noise (Example 3). - "The bound holds for any perturbation." It needs (Example 2).
Summary
bounds how a relative change in or is amplified in , the bound is attained, and for changes in it needs . In the 2-norm it is , and its inverse is the relative distance to singularity. The residual measures backward error and turns into forward error only after multiplying by , which gives the ceiling of about digits lost. The Hilbert matrix, the Gram matrix of the monomials on , shows it in full: grows by nearly 34 per row, leaves about half a digit, and exact arithmetic on the stored data does no better. is not the determinant, and it changes with scaling.