Elimination, and the swap

A rule that is correct and unusable

Cramer's rule gives every component of the solution in closed form, in terms of determinants, and it is a theorem. On two-by-two systems whose rows are nearly parallel it returns an answer with a backward error of 458 units of roundoff where elimination returns 1.3 — on a matrix whose condition number is 32,000 and which elimination solved perfectly.

Worth reading first: The number that decides nothing · The exact answer to a nearby problem · The swap that is not optional.

Cramer’s rule is the most satisfying formula in elementary linear algebra. Every component of the solution, in closed form, with no algorithm in it:

xⱼ  =  det(Aⱼ) / det(A),   where Aⱼ is A with its jth column replaced by b

It is correct. It is a theorem, it is proved in every first course, and it has no hypotheses beyond det A ≠ 0. It is also, at 2×2, four multiplications and two subtractions written out by hand, which is what makes it the version everybody actually uses — not at n = 10, where the objection is obvious, but inside a line intersection, a Newton step, a barycentric coordinate, a two-parameter fit.

This essay is about what that costs, and the usual objection is the wrong one.

Backward error of Cramer's rule and of elimination on 11974 2×2 systems at 24 bitsThe share of systems whose normwise backward error exceeds a given multiple of the unit roundoff. Elimination never exceeds 1.2u. Cramer's rule reaches 395u on the same systems, on a matrix whose condition number is 3.39·10⁶ and which elimination solved to 0.3u. The control curve, over uniformly random 2×2 systems, stops at 4.9u — so the failure belongs to the near-parallel family and not to a choice of scale.10⁻²10⁻¹110¹10²10³10⁴10⁻⁴10⁻³10⁻²10⁻¹1backward error, in units of ushare of systems above ituCramereliminationCramer, control24-bit arithmeticworst Cramer, in u395worst elimination, in u1.2control, worst Cramer4.9κ of the worst system3.4·10⁶one derivation, two computationsand only one of them is stable
Fig. 1 The share of 2×2 systems whose normwise backward error exceeds a given multiple of the unit roundoff, for Cramer’s rule and for elimination with a row interchange, on the same systems. The dashed curve is the control. Drag the precision: the axis is in units of that precision’s own roundoff, so a method that merely needed more digits would not move.

The objection everybody knows, and its size

The standard complaint is arithmetic. At n × n, Cramer’s rule computes n + 1 determinants; done by cofactor expansion that is n! terms each, and the number quoted is that solving a 20×20 system this way would take longer than the age of the universe.

It is true and it is not the objection, because nobody computes a determinant by cofactor expansion either. Compute each determinant by LU — the only sensible way, and the way the previous essay describes — and Cramer’s rule costs n + 1 factorisations against elimination’s one:

n LU solve Cramer via LU Cramer via cofactors
4 43 213 72
10 667 7,333 3.3·10⁷
20 5,333 112,000 4.6·10¹⁹

A factor of n + 1 is a bad constant and it is a constant. It would not stop anybody using Cramer’s rule at n = 2, where the factor is 3 and the whole computation is six operations.

Gaussian elimination on a 4×4, one step at a timeFour copies of the same matrix: as given, and after each of the three elimination steps. The pivot in use is outlined and the entries reduced to zero are greyed.21-13-3-121-212-443-12as givenrows in the order 1 2 3 443-1201.251.252.502.51.5-30-0.5-0.52after step 1pivot 443-1202.51.5-3000.5400-0.21.4after step 2pivot 2.543-1202.51.5-3000.540003after step 3pivot 0.5‖PA − LU‖/‖A‖0largest multiplier0.75row order 4 3 2 1the pivot is chosen
Fig. 2 What elimination actually does, one step at a time. Cramer’s rule performs the equivalent of this n + 1 times over, on n + 1 matrices that differ in one column.

The objection that matters

Elimination with a row interchange is backward stable. That word has a precise meaning on this site and it is the meaning the site’s spine is built from: the computed x̂ is the exact solution of (A + ΔA)x̂ = b + Δb with ‖ΔA‖ a small multiple of u‖A‖. The algorithm answered a question next door to the one it was asked, and everything else that goes wrong is the problem’s fault and can be predicted from the condition number.

Cramer’s rule has no such bound, and it is not that the bound is worse — there is no theorem of that shape for it at all.

Measured: forty thousand 2×2 systems whose two rows are nearly parallel, at 24 significand bits. The normwise backward error

η(x̂)  =  ‖b − Ax̂‖ / (‖A‖ ‖x̂‖ + ‖b‖)

is what decides the question, and it is attained — there is a perturbation of exactly that size for which x̂ is the exact answer.

Elimination’s worst over the whole sweep is 1.3u. Cramer’s rule’s worst is 458u.

What the solver reported, and what it achievedFinal relative residual and final relative error against the working precision, on a logarithmic vertical axis. The residual sits at the stopping tolerance at every precision — the method converged, by every test available to it. The error runs from 4.91·10⁻¹³ at 53 bits to 0.00501 at 8, tracking the unit roundoff, which is drawn beside it.614223038465410⁻¹³10⁻¹⁰10⁻⁷10⁻⁴working significand bitsrelative sizeerrorresidualuthe gap the solver cannot seeerror ÷ residual at 24 bits2.3·10⁵error ÷ residual at 16 bits5.2·10⁷error ÷ residual at 8 bits3.9·10¹⁰every convergence test passesand the answer is wrong in proportion to u
Fig. 3 Why the backward error is the number that settles this and the forward error is not: a small residual does not imply a small error, and a large residual does imply that the algorithm, not the problem, is responsible.

The system it fails on is not a hard system

This is the part worth dwelling on, because the natural defence is that the sweep found a nasty matrix.

The worst case has a condition number of 3.2·10⁴. That is a mildly awkward matrix and nothing more — four or five digits of the answer are at risk from the conditioning, which is a perfectly ordinary situation.

Elimination solved that system to 1.3u.

So the difficulty is not in the matrix. Two algorithms, the same six numbers in, algebraically identical outputs, and one of them returns the exact answer to a nearby problem while the other returns something no nearby problem has as its answer. That is the site’s identical algebra, different arithmetic thread in its smallest available instance: a 2×2.

Elimination with and without pivoting, ε = 10⁻¹⁷The same two-by-two system solved twice. With a row swap the answer is exact; without one the upper triangular factor contains an entry of order one over epsilon and the second component of the answer is wrong.[ ε 1 ; 1 1 ] x = [ 1 ; 2 ], exact answer (1.000000, 1.000000)with partial pivoting1101U after elimination1.0000001.000000computed xbackward error 0forward error 0without10⁻¹⁷10-1·10¹⁷U after elimination0.0000001.000000computed xbackward error 0.25forward error 0.71no error is raisedgrowth 10¹⁷
Fig. 4 The other 2×2 on this site where one algebraically correct route destroys the answer and another does not. There the difference was a row interchange; here it is which formula the six numbers go through.

Where the digits go

The mechanism is cancellation, and it is worth following through.

For a 2×2, Cramer’s rule computes

d  = a₁₁a₂₂ − a₁₂a₂₁
x₁ = (b₁a₂₂ − a₁₂b₂) / d
x₂ = (a₁₁b₂ − b₁a₂₁) / d

When the two rows are nearly parallel, a₁₁a₂₂ and a₁₂a₂₁ agree to several digits, and their difference loses those digits — cancellation, exactly as the arithmetic field describes it. The two products are computed correctly and the subtraction is correct; what is lost is that the inputs to the subtraction were rounded, and rounding them at a relative 10⁻⁷ perturbs a difference that is itself 10⁻⁵ of them by a relative 10⁻².

So far this is conditioning, and conditioning is nobody’s fault. The reason it becomes the algorithm’s fault is what happens next. The two numerators cancel too, independently, and the three errors do not have a common factor. If d were merely inaccurate by a relative δ, both components of x would be scaled by 1 + δ and the residual would be δ‖b‖ — which is small when ‖A‖‖x‖ is large, and the backward error would survive. What actually happens is that d and the two numerators are wrong by three unrelated amounts, so x̂ is not a scalar multiple of anything, and Ax̂ − b is a vector with nothing small about it.

Elimination avoids this because of the interchange. The largest entry becomes the pivot, the multiplier is at most one in magnitude, and no intermediate quantity grows relative to the data — the growth factor, which the elimination field is built around, is bounded by 2 for a 2×2.

Relative error of two algebraically identical expressions for (1 − cos x)/x², in binary32A log–log plot of relative error against x. The expression written as it reads loses accuracy as x falls and is eventually wrong in every digit; the rearranged form stays at rounding level.10⁻⁸10⁻⁶10⁻⁴10⁻²110⁻⁹10⁻⁶10⁻³1xrelative error of the computed value(1 − cos x)/x², as written2 sin²(x/2)/x²no digits left at allbinary32 throughoutone function, two spellings · zero below 3.5·10⁻⁴
Fig. 5 The subtraction at the centre of it. The digits that cancel were never wrong; what is wrong is that the digits which survive were the rounding of the inputs.
Growth factor under partial pivoting to n = 24: the bound, the worst case, and realityGrowth factor against matrix size on a logarithmic vertical axis. The two-to-the-n bound rises as a straight line; Wilkinson's matrix sits exactly on it; random matrices stay near one.0510152025110¹10²10³10⁴10⁵10⁶10⁷10⁸10⁹10¹⁰matrix size ngrowth factor max|u| / max|a|the 2ⁿ⁻¹ boundworst of 30 randommedian randomWilkinson's matrix sits on the bound30 Gaussian matrices per sizeat n = 24: bound 8.4·10⁶, worst 3.6
Fig. 6 And the quantity elimination controls and Cramer’s rule does not: the largest intermediate against the largest entry of the data. A pivoted elimination keeps it small by construction; a determinant formula has no intermediate it is allowed to reorder.

That last clause is the structural difference and it is worth stating on its own. Elimination is an algorithm — it has choices, and the choice it makes at every step is precisely the one that keeps its intermediates from growing. Cramer’s rule is a formula: the products it forms are named in advance, there is no step at which anything is compared to anything, and there is therefore no place to put a safeguard. A closed form has nowhere to be careful.

The same sentence explains why the failure cannot be repaired by computing the three determinants more accurately. Each of them is already computed as accurately as its own inputs allow; the loss happens in the subtraction that defines a determinant, and there is no reformulation of ad − bc that does not subtract. What elimination does instead is never form ad − bc at all — it forms d − (c/a)·b and multiplies by a implicitly, which is the same number by a route whose intermediates are bounded by the data.

The control, which is the half that makes it a finding

A sweep chosen to break a method will break it. The result above is worth having only because the same measurement, on a uniformly random 2×2 with entries spread over two decades, separates almost nothing: Cramer’s worst is 8.4u against elimination’s 1.4u.

That is the honest size of the everyday case. Cramer’s rule at 2×2 on ordinary data is a bad method — six times the backward error, consistently — and not a broken one. It becomes broken where the determinant cancels, which is exactly where a 2×2 solve is being asked to do something interesting: two lines that nearly coincide, two constraints that nearly agree, a Newton step near a fold.

The cases a 2×2 solver exists to handle are the cases the formula fails on, which is a worse property than failing at random.

Backward error of Cramer's rule and of elimination on 12000 2×2 systems at 53 bitsThe share of systems whose normwise backward error exceeds a given multiple of the unit roundoff. Elimination never exceeds 1.4u. Cramer's rule reaches 248u on the same systems, on a matrix whose condition number is 3.37·10⁶ and which elimination solved to 0.3u. The control curve, over uniformly random 2×2 systems, stops at 6.9u — so the failure belongs to the near-parallel family and not to a choice of scale.10⁻²10⁻¹110¹10²10³10⁴10⁻⁴10⁻³10⁻²10⁻¹1backward error, in units of ushare of systems above ituCramereliminationCramer, control53-bit arithmeticworst Cramer, in u248worst elimination, in u1.4control, worst Cramer6.9κ of the worst system3.4·10⁶one derivation, two computationsand only one of them is stable
Fig. 7 The same comparison in double precision. Elimination is at 1.4u and Cramer’s rule at 391u — the same statement, in units eight orders of magnitude smaller.
Backward error of Cramer's rule and of elimination on 10522 2×2 systems at 16 bitsThe share of systems whose normwise backward error exceeds a given multiple of the unit roundoff. Elimination never exceeds 1.1u. Cramer's rule reaches 63u on the same systems, on a matrix whose condition number is 6.98·10⁴ and which elimination solved to 0.0u. The control curve, over uniformly random 2×2 systems, stops at 3.0u — so the failure belongs to the near-parallel family and not to a choice of scale.10⁻²10⁻¹110¹10²10³10⁴10⁻⁴10⁻³10⁻²10⁻¹1backward error, in units of ushare of systems above ituCramereliminationCramer, control16-bit arithmeticworst Cramer, in u63worst elimination, in u1.1control, worst Cramer3κ of the worst system7·10⁴one derivation, two computationsand only one of them is stable
Fig. 8 And at sixteen bits, where the tail is shorter — 63u — because the family’s near-parallel structure is itself quantised by the format. The gap is a factor of 57 rather than 350, and it is the same gap.

Precision does not buy it back

The axis of every one of those figures is in units of the arithmetic’s own roundoff, and that choice is the argument.

If Cramer’s rule were merely imprecise — if it needed a few more digits than elimination — then measuring both in units of u would collapse the two curves onto each other, because both would scale with the format. They do not. Elimination sits at a bounded multiple of u at 16, 24, 32, 43 and 53 bits; Cramer’s rule’s tail is at tens to hundreds of u wherever it is measured.

That is what backward stability is, stated as a measurement rather than as a definition: a stable method’s error, in units of the precision, does not depend on the precision. Doubling the mantissa buys a stable method eight more correct digits and buys an unstable one eight more digits of an answer that still does not solve any nearby problem.

Forward error of a 8×8 Hilbert solve at eight precisionsA bar for each significand width from 12 to 53 bits showing the relative error in the computed solution, with the condition number times the unit roundoff marked as a prediction.κ = 1.5·10¹⁰ · the exact answer is (1, 2, …, 8)12 bits4.116 bits1.620 bits1024 bits0.530 bits0.5636 bits0.005943 bits5.5·10⁻⁶53 bits3.2·10⁻⁸dashed: κ · unit roundoffone matrix, eight arithmeticsmeasured against a known answer
Fig. 9 The site’s standing picture of what precision buys. Every curve on it belongs to a method whose error is a constant times u; a method without that property does not have a curve on this axis at all.
The same problem solved at eight precisionsRelative error against the number of significand bits, on a logarithmic vertical axis. The error runs from 460 at 8 bits to 5.53·10⁹ at 53 — it rises with the precision. The horizontal line is the floor the four filters reach, 0.1406, which no precision approaches.614223038465410⁻¹10²10⁵10⁸significand bitsrelative errorthe filters' floorthe solvenot a knoberror at 8 bits460error at 53 bits5.5·10⁹the filters' floor0.14more precision, more errorrounding perturbs the operator
Fig. 10 And the case where more precision does not help, which is the shape of the argument above: if the failure is not a shortage of digits, digits do not repair it.

Where it is still written

Cramer’s rule survives in code, and mostly in three places.

Geometry kernels. A line–line intersection, a barycentric coordinate, the circumcentre of a triangle. These are 2×2 and 3×3, the data is often exact — integer or fixed-point coordinates — and where the data is exact the whole argument above is void. The exact-input case is the same exemption the determinant’s sign gets: an exact function of exact data is exact.

Symbolic and interval work. In exact rational arithmetic Cramer’s rule has no error at all, and in interval arithmetic it gives an enclosure. It is a poor choice for cost and a fine one for correctness.

Proofs and derivations. As an object it is indispensable — the adjugate, the closed form for the inverse, the derivative of a determinant. None of that is a computation.

What it is not is a solver, and the distinction is the same one the whole phase is about: a formula that is exact in the algebra is not thereby a computation of what it names.

The width of a guaranteed bracket, and the error it guarantees, at 24 bitsTwo rising curves on logarithmic axes. The upper one, the bracket width, rises faster than the lower one, the actual error.10¹10²10³10⁴10⁻¹¹10⁻¹⁰10⁻⁹10⁻⁸10⁻⁷10⁻⁶10⁻⁵10⁻⁴10⁻³terms summedrelative widthbracket widthactual errora bound with no tolerance in itbracket contains the truth7width at 10,000 terms4.4·10⁻⁴error it bounds2.4·10⁻⁶every sum computed twice, up and downtrue at every size, useful at none of the large ones
Fig. 11 The interval route, where a formula’s job is to bracket rather than to approximate. There, closed form is an advantage.
The Krawczyk operator on a box of half-width 0.4Two rectangles in the plane. The outer one is the box handed in; the inner one is K(X), the image the operator returns; and the marked point is the root (√2, √2), known in closed form. The image is narrower than the box — 0.2263 against 0.8 — and the verdict is: exactly one root.11.522.511.52xyexactly one roota verdict, not a bound‖I − C F′(X)‖0.28width of X0.8width of K(X)0.23strictly inside is a proofand overlapping is nothing at all
Fig. 12 And the verdict such a computation returns, which is a proof rather than an estimate. Cramer’s rule in that setting answers a question it can actually answer.

Two rules that look alike and are not

It is worth separating Cramer’s rule from a formula it is often filed beside: the 2×2 inverse,

A⁻¹ = (1/det A) [ a₂₂  −a₁₂ ; −a₂₁  a₁₁ ]

which is the same arithmetic wearing different clothes, and inherits the same defect exactly — as it must, since x = A⁻¹b computed this way is Cramer’s rule. The inverse that is never formed takes the general version of that apart, and finds the same shape of result at every size: the backward error separates and the forward error does not.

The difference between the two essays is where the loss comes from. There it is the assembly of n independently-perturbed columns; here it is a single division by a number that cancelled. Same conclusion, different mechanism, which is what makes them two essays.

Backward and forward error of two routes to x, on 30×30 systems across eight decades of κBoth routes start from the same LU factorisation. The LU solve's backward error is 2.2·10⁻¹⁷ at every conditioning — a flat line at the unit roundoff. Multiplying by the explicitly formed inverse gives 4.5·10⁻⁵ at κ = 10¹⁴, a slope of 0.94 against κ. The two forward errors, drawn above them, are 2.8·10⁻⁴ and 0.015 — within a factor of 54, which is why the difference between the two methods is invisible to anyone measuring how wrong the answer is.10²10⁴10⁶10⁸10¹⁰10¹²10¹⁴10⁻¹⁸10⁻¹⁵10⁻¹²10⁻⁹10⁻⁶10⁻³1κ₂(A)relative errorforward, A⁻¹bforward, A\bbackward, A⁻¹bbackward, A\bat κ = 10¹⁴η, LU solve2.2·10⁻¹⁷η, via the inverse4.5·10⁻⁵forward, LU solve2.8·10⁻⁴forward, via the inverse0.015one factorisation, two ways to use itand one of them forfeits the backward error
Fig. 13 The general case. Two routes from one factorisation, and the backward errors separate by twelve orders of magnitude while the forward errors stay within a factor of fifty.
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. 14 The two errors, and which of them a method controls. Cramer’s rule controls neither; elimination controls the first and the problem decides the second.

Three by three, and where the rule is taught

At n = 3 Cramer’s rule is still written by hand — in a graphics kernel, a barycentric coordinate, a plane–plane–plane intersection — and the arithmetic changes shape enough to be worth a paragraph.

The 3×3 determinant is a sum of six products of three entries, so a single evaluation has two subtractions of nearly equal quantities rather than one, and the cancellation compounds. The conditioning of a 3×3 solve is no worse than a 2×2’s for the same κ, and the number of independent roundings in the closed form is roughly three times larger. Everything this essay measures gets worse and nothing gets better.

What does change is the alternative. At n = 3 a pivoted elimination is 17 operations against Cramer’s rule’s 51, so the cost argument now points the same way as the stability one — which it did not at n = 2, where the two methods are six operations against six. The 2×2 case is the only one where the formula is genuinely cheaper, and it is the only one where anybody has a reason to defend it.

And the rule is taught for a reason that survives all of this. Cramer’s rule is how a first course proves that a solution exists and is unique when det A ≠ 0, and the proof is constructive and short. It is also how the inverse’s closed form is derived, how the derivative of a determinant is obtained, and how the adjugate is defined. None of those is a computation, and none of them is diminished by the measurement in this essay.

The failure is entirely in the transfer: a formula proved as an existence argument, carried into a setting where the question is how accurately, with no step in between at which anybody says the two questions are different. That is the same transfer this collection’s whole refutations index is built out of — a theorem, learnt properly, taken one field past the one it was proved over.

Gaussian elimination on a 4×4, one step at a timeFour copies of the same matrix: as given, and after each of the three elimination steps. The pivot in use is outlined and the entries reduced to zero are greyed.21-13-3-121-212-443-12as givenrows in the order 1 2 3 443-1201.251.252.502.51.5-30-0.5-0.52after step 1pivot 443-1202.51.5-3000.5400-0.21.4after step 2pivot 2.543-1202.51.5-3000.540003after step 3pivot 0.5‖PA − LU‖/‖A‖0largest multiplier0.75row order 4 3 2 1the pivot is chosen
Fig. 15 The alternative, one step at a time, at the size where its cost advantage begins. Every step of it is a comparison and a choice, which is what a closed form has nowhere to put.

What is worth carrying

Correct and stable are different properties, and only one of them is proved in the first course. Cramer’s rule is correct in the sense a theorem is correct. It is unstable in the sense a measurement is unstable, and no amount of care in stating the theorem changes the measurement.

The cost objection is the small one. A factor of n + 1 is a constant; a backward error with no bound on it is not. A reader who avoids Cramer’s rule because it is slow has the right conclusion for a reason that would not survive a 2×2.

And the cases it fails on are the cases it exists for. A uniformly random 2×2 separates the two methods by a factor of six. A pair of nearly parallel lines separates them by 350. The second is what a 2×2 solve is usually being asked about.

The next essay takes the same measurement to the object Cramer’s rule is a special case of: the inverse that is never formed.

What links here

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

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.

Backward errorCancellationCondition numberDeterminantFlop countLU factorisationPivotingUnit roundoff