Two errors, and whose fault they are

The units the matrix is measured in

One linear system, written twice. The rows of the second are the rows of the first in different units, the solution is identical to the last bit, and the condition number has moved by eight orders of magnitude. One of those two numbers is a fact about the problem and the other is a fact about the notation.

Worth reading first: The condition number is an amplifier · The exact answer to a nearby problem.

A linear system carries units. The first row of Ax = b might be a force balance in newtons and the second a displacement in metres; the first unknown might be a length and the third an angle. None of that is written down anywhere in the array of numbers a solver receives, and all of it is present in the numbers themselves.

Multiplying one equation through by a thousand changes nothing about the problem. The same x satisfies the scaled equation — exactly, in exact arithmetic, and in floating point too, because both sides are scaled together and the solution is never touched. A reader who has followed the condition number is an amplifier would expect the sensitivity of the answer to be unchanged as well.

It is. The condition number is not.

Two condition numbers of one 8×8 system, as its rows are put into different unitsFour curves against the spread of the row units, in decades. κ_∞ of the scaled matrix rises from 9.83 to 1.9·10¹⁰ while the componentwise condition number stays at 6.98 throughout — the same system, the same solution, and one of the two numbers is a fact about the units. Hilbert's two numbers are drawn flat beside them at 3.4·10¹⁰ and 1.2·10¹⁰: a matrix whose sensitivity no scaling repairs.0246810110²10⁴10⁶10⁸10¹⁰10¹²spread of the row units (decades)condition numberκ_∞(DA)cond(DA)Hilbert κ_∞Hilbert condone system, two numbersκ_∞ at no spread9.8κ_∞ at 10 decades1.9·10¹⁰cond, either end7Hilbert, equilibrated1.3·10¹⁰the solution is the same at every spreadand one of these curves knows it
Fig. 1 An 8×8 system whose rows are progressively put into different units, from all-the-same on the left to ten decades apart on the right. κ_∞ climbs almost exactly with the spread. The componentwise condition number, drawn flat through it, is the same number at every point — and Hilbert’s two numbers, drawn beside them, are both large and neither moves. Drag the spread.

What the picture is of

The matrix on the left of that figure is an ordinary one: random entries, a strengthened diagonal, a condition number of about 3.4. It is as far from a hard problem as a matrix gets.

Every point to the right of it is that same matrix with its rows multiplied by powers of ten, spread evenly over the number of decades on the axis. The right-hand side is scaled with them, so the solution is untouched. The right-hand end of the axis, at ten decades, is a matrix whose first row is in units 10¹⁰ times larger than its last.

κ_∞ at that end is 1.9·10¹⁰. It was 9.8.

The solution is the same. It is the same because the site’s habit of building problems with a known answer applies here as everywhere else — b was formed from an x of small integers, so the true answer is an input rather than an estimate. At eight decades of spread, where κ₂ has gone from 3.4 to 1.3·10⁸, the forward error of the scaled solve is 7.3·10⁻¹⁵ — not 10⁻⁸, which is what κ = 10⁸ would lead a reader to budget for.

Two condition numbers of one 8×8 system, as its rows are put into different unitsFour curves against the spread of the row units, in decades. κ_∞ of the scaled matrix rises from 9.83 to 223 while the componentwise condition number stays at 6.98 throughout — the same system, the same solution, and one of the two numbers is a fact about the units. Hilbert's two numbers are drawn flat beside them at 3.4·10¹⁰ and 1.2·10¹⁰: a matrix whose sensitivity no scaling repairs.012110²10⁴10⁶10⁸10¹⁰10¹²spread of the row units (decades)condition numberκ_∞(DA)cond(DA)Hilbert κ_∞Hilbert condone system, two numbersκ_∞ at no spread9.8κ_∞ at 2 decades223cond, either end7Hilbert, equilibrated1.3·10¹⁰the solution is the same at every spreadand one of these curves knows it
Fig. 2 The same ladder over two decades of units. The separation begins immediately: two decades of units is two decades of gap between the two numbers, and there is no threshold below which the effect switches on.

Two definitions of one word

The reason is that there are two ways to say how much can a small change in the data move the answer, and they differ in what “small” means.

Normwise. Perturb A by a matrix E with ‖E‖ ≤ ε‖A‖. That is one number describing the whole matrix, and κ(A) is exactly the amplification factor from it to the answer.

Componentwise. Perturb each entry a_ij by at most ε|a_ij| — every entry by a small fraction of itself. That is what rounding the data does, what storing it in a narrower format does, and what a measurement error usually looks like.

On a matrix whose rows are all the same size the two are close together. On a badly scaled matrix they are not, and the reason is worth saying plainly: a normwise perturbation of a badly scaled matrix perturbs the tiny row by the size of the big one. The row whose entries are 10⁻⁵ is being moved by ε·10⁵. Of course the answer falls apart; the perturbation obliterated an equation. It is a stupid model of what a machine does to a matrix, and κ is the exact amplification factor for it.

The componentwise number — Skeel’s, written cond(A) = ‖ |A⁻¹| |A| ‖_∞, with the absolute values taken entry by entry before the product — answers the second question, and it does not notice the units at all.

The invariance is exact

Not approximate. Write A = D·A₀ with D diagonal and positive. Then A⁻¹ = A₀⁻¹D⁻¹, so

|A⁻¹| |A|  =  |A₀⁻¹| |D⁻¹| |D| |A₀|  =  |A₀⁻¹| |A₀|

because |D⁻¹||D| is the identity, entry by entry, before any norm is taken. There is nothing to estimate: the two diagonal factors cancel inside the absolute values, and the componentwise condition number of the scaled matrix and the unscaled one are the same number.

assertSkeelIsInvariantUnderRowScaling checks it at 10⁻⁸ relative rather than exactly, for the one reason worth stating: the two numbers are computed by inverting two different matrices, and the inversions round. The algebra is exact and the arithmetic is not, which is the sentence this site is named for, arriving in its own library.

It is also checked against a second scaling — random, twelve decades wide, unrelated to the one the test matrix was built with — because an invariance verified only on the construction that produced it is an invariance verified against itself.

How much a perturbation of the right-hand side is amplified, κ = 10⁶The cumulative distribution of the amplification factor over two hundred random perturbation directions, with the condition number marked as the upper limit.110¹10²10³10⁴10⁵10⁶10⁷00.250.50.751amplification of the input perturbationfraction of directions at or belowκ = 10·10⁵worst found 7.6·10⁵6×6, 200 directionsmedian reaches 0.29 of κ
Fig. 3 What a condition number is the amplification factor for: a normwise perturbation, and how much of the available amplification a random one achieves. Everything in this essay is the observation that the perturbation model in that sentence is a choice.

Equilibration, and what a theorem does not say

If κ is a fact about the units, the obvious repair is to fix the units: divide each row by its own largest entry, so every row has ∞-norm exactly 1. That is equilibration, and it is what LAPACK’s xGEEQU computes and xGESVX applies.

Van der Sluis’s theorem says the result is within a factor of n of the best any row scaling can achieve. That is a bound, and a bound of n is compatible with equilibration being fairly bad in practice — a factor of eight on an 8×8 is not nothing.

So it is measured. assertEquilibrationIsNearlyOptimal runs 1,500 random diagonal scalings over the same range, then a coordinate refinement from the best of them, and asks how much better than equilibration the search can get. The answer is 1.30, against a bound of 6. The theorem is loose and the practice is close to optimal, and the second sentence is the one a reader can use.

And the matrix it does nothing for

Here is the trap the previous section sets, and it is worth walking into deliberately.

Scale the rows and the ill-conditioning goes away is false. It is false for the matrix this site has used since its first commit.

Two condition numbers of one 12×12 system, as its rows are put into different unitsFour curves against the spread of the row units, in decades. κ_∞ of the scaled matrix rises from 15 to 3.1·10¹⁰ while the componentwise condition number stays at 13.2 throughout — the same system, the same solution, and one of the two numbers is a fact about the units. Hilbert's two numbers are drawn flat beside them at 3.8·10¹⁶ and 1.1·10¹⁶: a matrix whose sensitivity no scaling repairs.0246810110³10⁶10⁹10¹²10¹⁵10¹⁸spread of the row units (decades)condition numberκ_∞(DA)cond(DA)Hilbert κ_∞Hilbert condone system, two numbersκ_∞ at no spread15κ_∞ at 10 decades3.1·10¹⁰cond, either end13Hilbert, equilibrated1.3·10¹⁶the solution is the same at every spreadand one of these curves knows it
Fig. 4 The same ladder at n = 12, with the Hilbert matrix’s two numbers drawn through it. Hilbert’s rows already have comparable norms, so equilibrating it moves κ_∞ by a factor of about three out of 10¹³ — and its componentwise number is large as well, which is the honest signal that its sensitivity is in the matrix rather than in how it was written down.

The badly scaled matrix has κ_∞ and cond seven orders of magnitude apart. The Hilbert matrix has them within three, and both enormous. That gap between the two numbers is the diagnostic: a matrix whose normwise condition number is far above its componentwise one is badly described, and a matrix where the two agree is badly conditioned. The first is repairable by a diagonal matrix and the second is not repairable at all.

An answer that is known measured the Hilbert system’s solution against the exact rational answer and found 0.049 relative error at n = 13. No scaling recovers those digits, and the reason is now expressible in one number.

The exact solution of a 13×13 Hilbert system beside the computed oneTwo columns of numbers: the exact answer, which is the integers one to thirteen, and the answer double-precision elimination returns, with the number of correct digits beside each.H13 x = b, b formed exactly so that x = (1, 2, …, 13)12345678910111213exact1.00002.00003.00133.97865.18864.993010.46720.048921.2657-2.575319.21478.906013.5113computed6.6 correct digits4.8 correct digits3.4 correct digits2.3 correct digits1.4 correct digit0.8 correct digitno correct digitsno correct digitsno correct digitsno correct digitsno correct digits0.6 correct digit1.4 correct digitbackward error2.2·10⁻¹⁷κ = 1.7·10¹⁸The algorithm solved a neighbouring problem perfectly. That problem's answer is this one.right-hand side built in BigInt rationalsthe truth is known
Fig. 5 Hilbert at n = 13, where the exact answer is known in closed form rather than estimated. The digits this figure is about are lost to the matrix and not to its units, which is why no scaling recovers them.

What the bounds actually predict

The two condition numbers are not decorations. Each is the constant in a perturbation bound, and the bounds can be run against a measurement.

Two perturbation bounds and the error that was measured, on a 8×8 matrix spread over 8 decades of unitsThree curves against the size of an entrywise relative perturbation. The normwise bound κ_∞·ε is a valid bound and sits 4·10⁷ times above the componentwise one cond(A, x)·ε, which is also a bound and is nearly attained by the worst of forty random perturbations at each size.10⁻¹⁴10⁻¹³10⁻¹²10⁻¹¹10⁻¹⁰10⁻⁹10⁻¹⁵10⁻¹²10⁻⁹10⁻⁶10⁻³1relative size of the entrywise perturbationrelative forward errorκ_∞ · εcond(A,x) · εmeasuredboth bounds holdκ_∞(A)1.9·10⁸cond(A, x)4.8ratio of the bounds4·10⁷both curves above the data are boundsand only one of them is a measurement
Fig. 6 Forty random entrywise perturbations at each of six sizes, on the same 8×8 spread over eight decades of units, with the worst forward error each produced. Both dashed lines are valid bounds. The upper one, κ_∞·ε, is above the data by nearly eight orders of magnitude — κ_∞ is 1.9·10⁸ and cond(A, x) is 4.8. The lower one is nearly attained. Drag the spread of the units.

The perturbation is entrywise and relative: each entry moved by up to ε of itself, which is what storing the matrix in a shorter format does. At ε = 10⁻⁹ the worst error over forty draws is 2.1·10⁻⁹. The componentwise bound is 4.8·10⁻⁹. The normwise bound is 0.19.

Both are bounds. Only one of them is a prediction.

Two perturbation bounds and the error that was measured, on a 8×8 matrix spread over 0 decades of unitsThree curves against the size of an entrywise relative perturbation. The normwise bound κ_∞·ε is a valid bound and sits 2.1 times above the componentwise one cond(A, x)·ε, which is also a bound and is nearly attained by the worst of forty random perturbations at each size.10⁻¹⁴10⁻¹³10⁻¹²10⁻¹¹10⁻¹⁰10⁻⁹10⁻¹⁵10⁻¹⁴10⁻¹³10⁻¹²10⁻¹¹10⁻¹⁰10⁻⁹10⁻⁸10⁻⁷relative size of the entrywise perturbationrelative forward errorκ_∞ · εcond(A,x) · εmeasuredboth bounds holdκ_∞(A)9.8cond(A, x)4.8ratio of the bounds2.1both curves above the data are boundsand only one of them is a measurement
Fig. 7 The same measurement with no spread of units at all. The two bounds sit almost on top of each other, because on a matrix whose rows are all the same size a normwise perturbation is a reasonable model of an entrywise one. This is the negative control: the gap in the figure above is produced entirely by the scaling.

Running the same figure at zero spread is not a formality. It is the check that the argument is about the scaling and not about something else in the construction — the site’s own rule, learned the hard way in an earlier phase, that a test problem must differ in the property under test and nothing else.

Two perturbation bounds and the error that was measured, on a 12×12 matrix spread over 8 decades of unitsThree curves against the size of an entrywise relative perturbation. The normwise bound κ_∞·ε is a valid bound and sits 3.7·10⁷ times above the componentwise one cond(A, x)·ε, which is also a bound and is nearly attained by the worst of forty random perturbations at each size.10⁻¹⁴10⁻¹³10⁻¹²10⁻¹¹10⁻¹⁰10⁻⁹10⁻¹⁵10⁻¹²10⁻⁹10⁻⁶10⁻³1relative size of the entrywise perturbationrelative forward errorκ_∞ · εcond(A,x) · εmeasuredboth bounds holdκ_∞(A)3.1·10⁸cond(A, x)8.5ratio of the bounds3.7·10⁷both curves above the data are boundsand only one of them is a measurement
Fig. 8 The same comparison at n = 12. The gap between the two bounds is set by the spread of the units and not by the size of the matrix, which is what makes it a statement about the description rather than about the problem.

Which number a library reports

Every library reports the normwise one, and reports an estimate of it rather than the number itself. numpy.linalg.cond, MATLAB’s cond and rcond, LAPACK’s xGECON: all normwise, and an estimate that can be fooled is about how the estimate is arrived at.

Almost nothing reports the componentwise number, because computing it needs |A⁻¹| — the entries of the inverse, not merely its action — and forming an inverse costs more than the solve did. LAPACK’s expert drivers do compute a componentwise error bound by a cheaper route, and the fact that they are called expert drivers is a reasonable summary of how often anybody reaches for them.

So the number a reader is most likely to have in hand is the one that is a property of the units, and the number that would tell them what to expect is the one nobody computes.

Backward and forward error against the condition numberA log–log plot over twelve decades of condition number. The backward error is a flat line at ten to the minus sixteen; the forward error rises in proportion to the condition number.110²10⁴10⁶10⁸10¹⁰10¹²10¹⁴10⁻¹⁷10⁻¹⁴10⁻¹¹10⁻⁸10⁻⁵10⁻²10¹condition number κ(A)relative errorforward errorbackward errorpredicted: κ · u30×30, 20 seeds per κ; dashed is the worstthe problem worsens, not the method
Fig. 9 The two errors a solver can and cannot see. A badly scaled matrix leaves the first at rounding and moves the second by whatever the units decided, which is the mechanism the next essay in this pair takes apart.

What this does to the site’s own identity

The spine of this site is

forward error  ⪅  condition number × backward error

and it has been used to separate two authors of a wrong answer: the algorithm, through the backward error, and the problem, through the condition number.

This essay puts a third author in the middle. The condition number is not a property of the problem alone; it is a property of the problem and of how it was written down. The identity still holds — it is an inequality about norms and nothing here disturbs it — but the term that was supposed to carry “how hard this problem is” turns out to carry “how hard this problem is, in these units”, and the second is a decision somebody made before the solver was called.

The identity has a componentwise form which does not have that problem:

forward error  ⪅  cond(A, x) × componentwise backward error

and the figure above is that inequality, measured, with the ordinary form drawn above it for scale.

Backward error, forward error and the condition number, with measured valuesTwo boxes at the top — the problem posed and the nearby problem the algorithm answered exactly — and two answers below them, with the distances between all four labelled by numbers from a Hilbert solve.the problem you posedA = H10b = A·(1, 2, …, 10)the problem it answered exactlyA + δA, b + δb‖δ‖ / ‖A‖ = 2.3·10⁻¹⁷the answer you wantedx = (1, 2, …, 10), exactlythe answer you gotx̂, wrong by 2.7·10⁻⁴ relativebackward error 2.3·10⁻¹⁷forward error 2.7·10⁻⁴κ = 1.6·10¹³κ · η = 3.6·10⁻⁴, and the measured forward error is 2.7·10⁻⁴.The algorithm is not at fault. The problem is.H10, LU with partial pivotingresidual and error differ
Fig. 10 The identity as this site first drew it: forward error, backward error and the condition number that multiplies them. Everything in this essay is an argument about which condition number belongs in the middle term.

Where badly scaled matrices come from

It would be comfortable to file all of this under badly written input, and it is worth resisting, because the matrices that arrive in this state arrive there honestly.

A physical model with more than one quantity in it. A structural analysis couples displacements in metres to rotations in radians and forces in newtons; a circuit couples volts to amps to coulombs. The stiffness matrix that comes out has blocks differing by whatever the constants of the physics differ by, and nobody chose those constants.

A constrained problem. A Lagrange multiplier row is a constraint written in whatever units the constraint is natural in, next to rows written in the units of the objective. The penalty method’s whole idea is to multiply one block by a large number, which is a row scaling performed on purpose, and the large number is the parameter being swept.

Finite differences on a graded mesh. A discretisation whose cells vary in size by three orders of magnitude produces rows scaled by the cell size to some power, and the grading is there because the solution varies fast somewhere and slowly elsewhere.

And time. An implicit time step of Δt multiplies one block by Δt and leaves another alone, so the condition number a solver reports changes as the step is refined even when the underlying operator does not.

In every one of those the scaling is a consequence of the modelling rather than a mistake in typing. That is why the repair is a routine in every serious library rather than advice, and why a κ read off an unscaled assembled matrix is so often a number about the assembly.

What a solver can do about it, and when

A library that equilibrates does three things and they are worth separating, because only the first is free.

Compute D. One pass over the matrix, n numbers out, and — in the version LAPACK uses — every multiplier rounded to a power of two, so the scaled matrix is exactly the unscaled one with exponents adjusted and the repair introduces no rounding of its own. equilibrateRowsByPowersOfTwo does that here for the same reason. A scaling that costs accuracy to apply is a scaling that has to be argued for; this one does not.

Decide whether to apply it. LAPACK’s rule is a threshold on the ratio of the largest row norm to the smallest, and the default is not to scale unless the ratio is extreme. That is a decision made for the caller, silently, and it means two runs of the same driver on two matrices can differ in whether an equilibration happened at all.

Report which condition number. Having scaled, the expert driver reports the condition number of the scaled matrix, which is the honest one for the solve it performed and is not the condition number of the matrix the caller handed it. Two libraries can therefore report condition numbers orders of magnitude apart for the same input and both be right about a different question.

None of that is hidden — it is in the documentation of every routine involved. What it is, is easy to read past, and the number that survives into a user’s notebook is a single scalar with no record of which of those three decisions produced it.

Two condition numbers of one 6×6 system, as its rows are put into different unitsFour curves against the spread of the row units, in decades. κ_∞ of the scaled matrix rises from 8.92 to 1.2·10⁸ while the componentwise condition number stays at 6.35 throughout — the same system, the same solution, and one of the two numbers is a fact about the units. Hilbert's two numbers are drawn flat beside them at 2.9·10⁷ and 1.1·10⁷: a matrix whose sensitivity no scaling repairs.02468110²10⁴10⁶10⁸spread of the row units (decades)condition numberκ_∞(DA)cond(DA)Hilbert κ_∞Hilbert condone system, two numbersκ_∞ at no spread8.9κ_∞ at 8 decades1.2·10⁸cond, either end6.4Hilbert, equilibrated1.2·10⁷the solution is the same at every spreadand one of these curves knows it
Fig. 11 The ladder at n = 6, where the two inversions are most accurate and the invariance is cleanest. The componentwise number agrees with itself across the whole sweep to the last two digits.

Column scaling is a different question, and a worse one

Everything above is about scaling rows, which is scaling equations. Scaling columns is scaling unknowns — measuring a length in millimetres instead of kilometres — and it is not benign in the same way.

A row scaling leaves x alone. A column scaling changes x, because A(Dy) = b means y = D⁻¹x, and the error in y is not the error in x. So “which x is accurate” becomes a question about which units the answer is wanted in, and there is no scaling-invariant statement available: an answer accurate in metres can be inaccurate in kilometres if the two components differ enormously in size.

That is why the equilibration a library performs is a row scaling, and why the column scaling it offers has to be asked for and comes with a warning. The asymmetry is not an oversight; the two operations do different things to the object being solved for.

The same three schemes on a problem with no layer in itWorst nodal error against the grid size, both axes logarithmic, for a manufactured smooth solution on the identical operator at ε = 0.001. Adding no diffusion gives 0.0016, 4·10⁻⁴, 10⁻⁴, falling by four at each refinement. The tuned diffusion gives 0.092, 0.043, 0.018 — 57 times worse at the coarsest grid, and falling more slowly.10²10⁻⁴10⁻³10⁻²10⁻¹grid points nworst nodal errorupwindtunedcentralthe same tuning, another problemtuned ÷ central at n = 3157tuned ÷ central at n = 127182central's error at the finest grid10⁻⁴exact on the problem it was derived fromand harmful on the one beside it
Fig. 12 The site’s standing picture of a computation answering a question next to the one that was asked. A condition number reported in the wrong units is a smaller version of the same thing: a correct answer about a matrix nobody meant.
Four pivot rules on [[ε, 1], [1, 1]] with ε = 10⁻¹⁷, before and after its first row is scaled by 1/εEight bars of forward error. On the matrix as given, no pivoting loses the answer entirely and all three pivot rules return it to rounding. Multiply the first row by 1/ε — which does not change the solution — and partial pivoting makes no interchange at all and returns the same wrong answer that no pivoting does. Scaled partial pivoting and complete pivoting are unmoved.forward error, relative to a solution of exactly (1, 1)no pivoting · as given1 0 interchangesno pivoting · rows scaled1 0 interchangespartial · as given0 1 interchangepartial · rows scaled1 0 interchangesscaled partial · as given0 1 interchangescaled partial · rows scaled0 1 interchangecomplete · as given0 1 interchangecomplete · rows scaled0 1 interchangethe same problem twicepartial, as given10⁻¹⁸partial, rows scaled1its relative residual10⁻¹⁷complete, rows scaled10⁻¹⁸the two systems have the same solutionand one pivot rule cannot see it
Fig. 13 And the same scaling inside the elimination rather than beside it. Partial pivoting compares entries across rows, so it compares numbers carrying different units, and the next essay but one is about what that costs.

What is worth carrying

A condition number is a number about a description. It is exactly the amplification factor for the normwise perturbation model, that model is a poor description of what a machine does to a badly scaled matrix, and the whole of the discrepancy in this essay follows from those two sentences.

The gap between κ and cond is the diagnostic, and it is cheap to reason about even when it is expensive to compute. If the rows of a matrix differ enormously in size, expect κ to be reporting that fact and not much else. If they do not, expect κ to be telling the truth.

And a repair that works has to be checked against a case where it does not. The Hilbert curve in the hero figure is there because the previous three paragraphs are the kind of argument that generalises itself in a reader’s memory into a rule, and the rule is wrong.

The next essay takes the componentwise number seriously as an object rather than as a foil: a condition number scaling cannot move. The one after it takes the same question into the elimination itself, where the pivot rule turns out to be reading the units too — the pivot that reads the units.

What links here

Computed from the collection, not written here: the essays that point at this one.

Reads more easily once this is understood

Essays that name this one as worth reading first.

Shares its objects with

Essays that name at least two of the same things, and that neither author linked.

Named objects

A flat tag is an object no other essay names yet.

Componentwise condition numberCondition numberEquilibrationExact ground truthForward errorHilbert matrixPerturbationRow scalingScaled norm