TL;DR
The condition number of a matrix measures how much the matrix can magnify errors. For an invertible matrix it is the ratio of its largest stretch to its smallest stretch, and a large value means that a tiny change in the data can cause a large change in the answer of a linear system. It tells you in advance how many digits of an answer you can trust.
Builds on: Matrix Norm, Matrix Inverse, Singular Value Decomposition (SVD) · Leads to: Preconditioning
Where it's used
Engineering simulations. A structural engineer who solves for how a frame bends under load, or a geophysicist who fits a model to measured signals, reduces the problem to a linear system . The matrix comes from measurements and modelling choices that are never exact. Before trusting the answer, the engineer checks the condition number of .
Curve fitting. Fitting a polynomial of high degree, as in linear regression, to data over a wide range of inputs leads to a matrix whose columns are nearly the same direction. Its condition number is large, so the fitted coefficients can change a lot when the data changes in the last digit.
Numerical libraries. Standard linear algebra routines often return an estimate of the condition number along with the solution, so the caller can see that the answer may be unreliable.
Optimization. The speed at which gradient descent converges on a quadratic problem depends on the condition number of its Hessian matrix. A large value means slow progress along the flat directions, which is why Preconditioning exists.
How it works
Take one tiny instance. The matrix is diagonal, and it acts on a vector by sending it to . It stretches the first coordinate by 100 and leaves the second alone. We solve for , then see what a small change in does to .
- Find the stretches. stretches horizontal vectors by 100 and vertical vectors by 1. The largest stretch is 100 and the smallest is 1. Check: the ratio of largest to smallest is 100.
- Solve for the original answer. The vector is mapped to , so . Check: .
- Change the data slightly. Replace with , so the error in the data is . The relative size of this change is percent.
- Solve again. The vector is mapped to , so . The error in the answer is , and its relative size is percent. Check: .
- Compare. A 1 percent change in the data became a 100 percent change in the answer. The amplification factor is 100, the same as the ratio of largest to smallest stretch. That ratio is the condition number.
flowchart LR B[b, exact data] --> X[x = A inverse b] B2[b plus small error] --> X2[x plus error in x] X --> C[compare] X2 --> C
The math
For an invertible matrix , the condition number in a chosen norm is
where is the matrix norm (see Matrix Norm), the largest ratio of output length to input length, with lengths measured by a vector norm, and is the Matrix Inverse.
Step 1 in symbols: the stretch ratio
In the 2-norm, equals the largest singular value , and equals , where is the smallest singular value (see Singular Value Decomposition (SVD)). So
For a symmetric positive definite matrix the singular values equal the eigenvalues, so . For the singular values are 100 and 1, so .
Step 2 in symbols: solving
For the diagonal matrix, . Then , which matches step 2 of the walkthrough.
Steps 3 and 4 in symbols: the error bound
Let solve , and let solve the equation with the perturbed data . Subtracting the two equations gives , so . Taking norms,
Also , so , which gives . Multiplying the two inequalities,
The left side is the relative error in the answer and the right side is the condition number times the relative error in the data. In the walkthrough, and the relative error in the data is 0.01, so the bound allows 1. The actual relative error was 1, so the bound is reached in this case.
Step 5 in symbols: reading the bound
The inequality says that the relative error in is at most times the relative error in . The condition number is a property of alone. The vector only decides which answer you are looking at.
Worked examples
- Let , the 2 by 2 identity. Its singular values are both 1, so .
- Solving gives .
- Change the data by . The answer changes by the same amount, .
- The relative change in the data and in the answer are equal, so the amplification is 1.
- Interpretation: a matrix with condition number 1 passes data errors through unchanged. Nothing is lost in solving.
- Let . Its condition number is .
- Solve . Then , since and .
- Change the data by . The relative size of the change is .
- The answer changes by . Its relative size is .
- Compare: . The amplification is about 1.37, which is below the bound of 4. The bound is a worst case, and this direction is milder.
- Interpretation: the matrix can magnify errors by up to 4 times, and here the amplification was smaller.
This is a realistic case: two measurements that are almost the same, so the matrix is close to singular. Let . The matrix is symmetric, so its condition number is the ratio of its eigenvalues.
- The trace is , and the Determinant is .
- The larger eigenvalue is about . The product of the two eigenvalues is the determinant, so .
- The condition number is .
- Suppose the data is accurate to one part in a million, a relative error of . The bound gives a relative error in the answer of up to , which is 4 percent.
- Interpretation: data accurate to about 6 digits gives an answer with only about 1 or 2 correct digits, because digits are lost to the matrix.
Try it: a system has condition number , and its data is accurate to 8 digits, a relative error of . What is the worst-case relative error in the answer? Answer: , so up to 1 percent.
Common mistakes and tradeoffs
It is tempting to think that a small residual means a correct answer. In fact the residual measures how well the equation is satisfied, and the error in the answer can be up to times the relative residual. For a matrix with a large condition number, a solution can satisfy the equation almost exactly and still be far from the true answer.
It is tempting to think that a condition number of 10 is bad and one of 1,000 is terrible in every case. In fact a condition number of costs about digits of accuracy. Whether that matters depends on how many correct digits your data has. Losing one digit out of 15 is harmless for many problems.
It is tempting to think that depends on the right-hand side . In fact depends only on the matrix. The bound is a worst case over all , and a particular may do much better.
Tradeoffs: computing exactly needs the singular values or the inverse, which costs about as much as solving the system by Gaussian elimination. Routines therefore use cheaper estimates. The value also depends on the norm chosen, although different norms give values that differ only by a bounded factor. Use to decide how much to trust an answer, not as the only test. When is large, the usual remedy is to change the problem: rescale the variables, choose a better basis, or use a Preconditioning step that reduces the effective condition number before an iterative solver such as Conjugate Gradient runs.
History
In 1948 Alan Turing, in a paper on rounding errors in matrix computations, used the term "condition number" for a quantity that measures how sensitive the solution of a linear system is to errors. Before that, von Neumann and Goldstine had studied how rounding errors grow when large matrices are inverted, in the late 1940s. The idea caught on as electronic computers made large linear systems routine and it became clear that a computed answer can be wrong in its leading digits even when the arithmetic is careful. Numerical libraries now report condition estimates as a standard safeguard.
Recap
- The condition number of an invertible matrix is . In the 2-norm this is the ratio of largest to smallest singular value.
- For , the relative error in is at most times the relative error in .
- A large condition number means that many digits of the answer can be lost, and it depends on the matrix, not the data.
- Read next: Preconditioning for changing a system to reduce its condition number, and Conjugate Gradient for a solver whose speed depends on it.
Check yourself
QuestionWhat is the condition number of the matrix diag(4, 2) in the 2-norm? Give a number.Show answer
The singular values are 4 and 2, so .
QuestionA matrix has condition number 1000, and the data has a relative error of 0.001. What is the worst-case relative error in the solution?Show answer
The bound gives , so the relative error in the answer can be as large as 100 percent. The answer could have no correct digits in the worst case.
QuestionWhy is a small residual not enough to trust a solution of an ill-conditioned system?Show answer
The residual measures how well the equation holds, not how close is to the true answer. The error in can be up to times the relative residual, so with a large a solution that satisfies the equation almost exactly can still be far off.
QuestionA solver returns an answer with a relative residual of for a system with . Roughly how many correct digits can the answer have?Show answer
The bound gives a relative error of up to . So the answer may be right only to about two digits, even though the residual looks tiny.