Orthogonality, measured

A repeated rotation drifts in a straight line

Composing rotation matrices drifts away from orthogonality, which is why graphics and robotics code re-orthonormalises. Rounding errors have no preferred sign, so a product of k rotations should lose orthogonality like the root of k, and it does when every factor is new: slope 0.52 over a million products. Multiply by the same factor every step, as a fixed-step integrator does, and the slope is 1.00. The factor's own rounding is one defect met in the same direction every time. At a million products in binary32 that is 622,000 units of roundoff against 2,400, a loss of 3.7 per cent. One polar step removes it at any period from ten products to ten thousand, the error in the rotation falling twenty times and hardly moving with how often it is run. Gram–Schmidt every ten products is six times worse, and for a reason that is not rounding.

Worth reading first: Orthogonal is a number · Two Gram–Schmidts.

Orthogonal is a number made “QQ is orthogonal” into a measurable quantity, ∥QTQ−I∥\|Q^{\mathsf T}Q - I\|, and an inner product with no fixed sign looked underneath it at its entries. A reflection cannot stop being one mentioned, on its way to another point, that an explicit rotation matrix has no such structural protection: “repeated composition drifts, which is why graphics and robotics code” re-orthonormalises a rotation every so often. That is one of the most widely known facts in the applied use of matrices, and the question of how fast the drift goes has so far been left open here.

There are two candidate answers, and the choice matters. A product of kk orthogonal factors in floating point has a standard error bound that grows linearly in kk: each multiplication adds its rounding to the total. But the bound is a bound, and rounding errors have no preferred sign, so they should largely cancel. A random walk would give k\sqrt{k}. Three walks and one bound found exactly that gap for a plain sum, where the bound says nn and the measurement says n\sqrt{n}. For a product, kk of a million is either 10610^6 units of roundoff or 10310^3, and in binary32 that is the difference between a matrix that has visibly stopped being a rotation and one that is fine. In the event the right answer is both. Which one applies depends on something the bound says nothing about.

Fresh factors walk

The measurement composes rotations of order three, each a product of three small plane rotations with random angles up to a tenth of a radian, as an attitude integrator would. It works in binary32, with the entries of each factor rounded on arrival, and reports the Frobenius norm of QTQ−IQ^{\mathsf T}Q - I in units of the roundoff, 2−242^{-24}. Medians of five draws are reported.

With a fresh factor at every step, the loss at 1, 10, 100, 1,000 and on up to a million products is 3, 4, 24, 51, 150, 775 and 2,404. The fitted slope from a hundred products on is 0.52. So this is a random walk, in close to its textbook form. At a million products the loss is 2,404 units, or 1.4⋅10−41.4 \cdot 10^{-4}, about twice the unit in the fourth decimal.

The loss of orthogonality of a binary32 product of rotations of order 3 against the number of products, drawn from one factor repeated, with lines of slope a half and slope one for reference, median of five drawsFrom one factor repeated, in units of the roundoff: 2 at 1, 11 at 10, 59 at 100, 639 at 1000, 6081 at 10000, 61626 at 100000, 622127 at 1000000. The fitted slope from a hundred products on is 1.00.110¹10²10³10⁴10⁵10⁶110¹10²10³10⁴10⁵10⁶products‖QᵀQ − I‖ in units of the roundoffthe productslope oneslope a halfbinary32 · one factor repeatedthe same arithmetic, two laws
Fig. 1 The loss of orthogonality of a binary32 product of rotations of order 3 against the number of products, with lines of slope a half and slope one for reference. The dial sets the supply of factors.

One factor does not

Now use a single factor every step. This is hardly an unusual case. A rigid body turning at a constant rate under a fixed time step is advanced by the same rotation matrix again and again. So is a loop that animates by a fixed increment, and so is an orbit propagated with one precomputed step.

The loss is 2, 11, 59, 639, 6,081, 61,626 and 622,127, and the fitted slope is 1.00. It rides the line of slope one from the first few products to the end of the sweep. At a million products the loss is 3.7 per cent, in a matrix that is meant to be a rotation. Its columns are no longer of unit length to two digits. So that same arithmetic, on the same format with the same order, has moved from the lower bound’s law to the upper bound’s, for a loss 260 times as large.

The explanation is simple once it is suspected. The factor PP was rounded once, when it was formed, and it now has a defect: PTP−IP^{\mathsf T}P - I is about a unit of roundoff, and it has a definite direction. In particular PP is not quite of determinant one, and every multiplication by it scales the product’s determinant by the same amount in the same direction. There is nothing to cancel. Each new product’s own arithmetic rounding does still walk, at the slope of a half, and it is small beside this. So the linear term here is not the sum of arithmetic errors at all, which is what the classical bound counts. It is the same one error, in the data, reused kk times. That is the same kind of finding as the direction the error leans, where a bias a fraction of a unit in size overcomes a random walk given enough steps.

The loss of orthogonality of a product of rotations in binary32 against the number of products, for five supplies of factors, median of five drawsone factor repeated: 2, 11, 59, 639, 6081, 61626, 622127, slope 1.00; a pool of 4: 2, 6, 36, 223, 1443, 15873, 154272, slope 0.91; a pool of 64: 2, 4, 22, 131, 788, 6213, 65420, slope 0.86; a pool of 1024: 2, 4, 17, 64, 287, 1298, 13185, slope 0.71; a fresh factor each time: 3, 4, 24, 51, 150, 775, 2404, slope 0.52.110¹10²10³10⁴10⁵10⁶110¹10²10³10⁴10⁵10⁶products‖QᵀQ − I‖ in units of the roundoffone factor repeateda pool of 4a pool of 64a pool of 1024a fresh factor each timebinary32 · order 3 · five drawsa fixed defect is a straight line
Fig. 2 The loss of orthogonality of a product of rotations in binary32 against the number of products, for five supplies of factors, median of five draws.

A finite pool of factors lies between the two, and this shows the mechanism in a form that can be read. With four factors the slope is 0.91, and the loss at a million is 154,000. With 64 it is 0.86 and 65,000, and with 1,024 it is 0.71 and 13,000. Each pool’s factors have defects of random sign, whose average over the pool is smaller than one defect by about the square root of the pool’s size. So a pool’s loss starts on the walk’s line, and it leaves that line once the steady mean of its defects has added up to more than the walk: a little past a thousand products for a pool of 1,024, and at about a hundred for a pool of four. Those crossings are about where a pool of mm should cross, near k≈mk \approx m, since that is where k/mk/\sqrt{m} reaches k\sqrt{k}. A pool of one crosses at once. That is a law with two terms: k\sqrt{k} times a constant from the arithmetic, and kk over the root of the supply, from the defects.

A designer who cannot avoid a repeated factor has a way out. Since the defect is a property of one matrix, it is enough to make that matrix’s defect small: compute the factor in higher precision, polish it once by a polar step, and round. That does not remove it. Rounding to binary32 leaves about half a unit per entry, and nothing about that can be repaired within binary32. Choosing among nearby representable matrices for one whose determinant is closest to one would shrink it further, and that has not been tried here.

Half of the loss is the error

So far this has been a measurement of a property, of how orthogonal the product is, and that is not the same as whether it is the right rotation. A rotation that has lost orthogonality may yet be a good approximation to the correct one, or not. Carrying the same product in binary64 alongside, and using it as the reference for the rotation that should have resulted, gives a second number.

Two errors of the same product in binary32, against the number of products: its loss of orthogonality and its distance from the product in binary64, for one repeated factor and for fresh factorsone factor: orthogonality: 2, 11, 59, 639, 6081, 61626; one factor: rotation: 1, 6, 30, 320, 3045, 30820; fresh: orthogonality: 3, 4, 24, 51, 150, 775; fresh: rotation: 1, 2, 12, 34, 91, 363 at 1, 10, 100, 1000, 10000, 100000 products.110¹10²10³10⁴10⁵110¹10²10³10⁴10⁵productsunits of the roundoffone factor: orthogonalityone factor: rotationfresh: orthogonalityfresh: rotationbinary32 · order 3 · no repairthe loss is twice the error
Fig. 3 Two errors of the same product in binary32, against the number of products: its loss of orthogonality and its distance from the product in binary64, for one repeated factor and for fresh factors.

The two are locked together, the first at about twice the second, at every count on both supplies. For a repeated factor at a hundred thousand products, the loss is 61,626 and the distance from the true rotation is 30,820. For fresh factors they are 775 and 363. That factor of two has a simple source. A symmetric perturbation of size ee in a matrix QQ gives a loss of 2e2e in QTQ−IQ^{\mathsf T}Q - I, since the square doubles it, and so on these runs the error is nearly all symmetric. Its effect on the product is a stretching, which is not a mistake in direction: the rotation has grown or shrunk along some axes, and the angle it represents is close to correct. And this is a useful fact, since a stretch is exactly what a re-orthonormalisation can remove.

How little repair it needs

The distance of a binary32 product of a hundred thousand rotations from the binary64 product, by repair and by how often it is applied, for one repeated factor and for fresh factorsone factor repeated, one polar step: 1625, 1741, 1614, 1529; one factor repeated, Gram–Schmidt: 10372, 1567, 1698, 2594; fresh factors, one polar step: 255, 231, 226, 291; fresh factors, Gram–Schmidt: 223, 320, 338, 323 at periods 10, 100, 1000, 10000. With no repair, 30820 and 363.100100010000one factor repeated, one polar stepevery 10 products1625every 100 products1741every 1000 products1614every 10000 products1529one factor repeated, Gram–Schmidtevery 10 products10372every 100 products1567every 1000 products1698every 10000 products2594fresh factors, one polar stepevery 10 products255every 100 products231every 1000 products226every 10000 products291fresh factors, Gram–Schmidtevery 10 products223every 100 products320every 1000 products338every 10000 products323error in the rotation, units of the roundoffno repair: 30820 and 363how often barely matters
Fig. 4 The distance of a binary32 product of a hundred thousand rotations from the binary64 product, by repair and by how often it is applied, for one repeated factor and for fresh factors.

Two repairs are compared. One polar step uses an iteration that needs no inverse, Q←Q(3I−QTQ)/2Q \leftarrow Q(3I - Q^{\mathsf T}Q)/2, and an iteration that only multiplies studied that form at length. Modified Gram–Schmidt on the columns is what many a piece of graphics code does. Each is applied every 10, 100, 1,000 or 10,000 products over a run of a hundred thousand.

With no repair on a repeated factor the distance from the true rotation is 30,820 units. With one polar step, it is 1,625 at a period of ten, 1,741 at a hundred, 1,614 at a thousand, and 1,529 at ten thousand. So the error falls by a factor of 19 and does not depend on how often the repair is run, across three decades of period. This is the part that is counterintuitive. Since a stretch is removed the moment a polar step is taken, a stretch accumulated over ten thousand products comes out as well as one accumulated over ten, and a polar step’s own rounding is small. Even repair every ten thousand products is as good as anything.

What remains, about 1,600 units, is not a stretch at all. It is an error in the angle: the factor’s rounded form corresponds to a very slightly wrong rotation, and kk steps of that make a wrong angle of kk times its size. No orthogonalisation can touch that, since an orthogonal matrix with the wrong angle is perfectly orthogonal. That part grows in direct proportion to kk whatever else is done. It should be stated clearly, since it is what a person reading this as a clean bill of health would miss. A rotation matrix that has been re-orthonormalised is an orthogonal matrix, and it is not necessarily the right one.

So a schedule is decided by the permissible loss, not by accuracy. With a defect of 0.62 units a product here, a loss below 10−510^{-5} at any moment means a period of about 270 products. A tolerance of 10−310^{-3} allows 27,000. The repair costs about as much as two products, so at a period of 270 it costs under one per cent, and there is no reason to run it more often than the tolerance says.

The repair with a bias of its own

On fresh factors the two repairs behave alike. On a repeated factor they do too, except in one place. Gram–Schmidt run every ten products leaves the rotation 10,372 units from the truth, against 1,625 for the polar step, more than six times worse. At a period of a hundred it is 1,567, as good as polar, and at a thousand it is 1,698.

So the repair done most diligently has the largest error. Gram–Schmidt is not symmetric in the columns: it keeps the first column’s direction and adjusts the rest against it. That is a rotation error of its own, small each time, of the size of the skew part of whatever has accumulated, and it has a fixed sign when the defect being repaired is the same at each visit. Run often, it applies that small rotation many times in the same sense. A polar step takes the nearest orthogonal matrix, and it is symmetric by construction, so it makes no such choice. The nearest orthogonal matrix measures this as a difference in distance at a single application, where it is small. A very small difference that repeats with a fixed sign is the same shape of problem as the one this essay began with, and that seems to be what has happened.

The refusal that follows is that a re-orthonormalisation done often is not always a re-orthonormalisation done well. An honest rule for Gram–Schmidt here would be to run it rarely, and this reverses the instinct many people will have about it.

The format and the order

The loss of orthogonality at order 8 against the number of products, for one repeated factor and for a pool of 64, in binary32order 8, one factor repeated: 6, 14, 83, 694, 6863; order 8, a pool of 64: 6, 19, 54, 155, 534.110¹10²10³10⁴110¹10²10³10⁴10⁵products‖QᵀQ − I‖ in units of the roundofforder 8, one factor repeatedorder 8, a pool of 64binary32 · order 8the same line at a higher start
Fig. 5 The loss of orthogonality at order 8 against the number of products, for one repeated factor and for a pool of 64, in binary32.

The same pattern holds for a larger factor, as this figure shows, and the rest of this section reads it beside the format’s own effect.

The loss of orthogonality against the number of products, in each format's own roundoff, for one repeated factor and for fresh factors, in binary32 and binary64binary32, repeated: 2, 11, 59, 639, 6081, 61626; binary64, repeated: 5, 21, 156, 1507, 14973, 149542; binary32, fresh: 3, 4, 24, 51, 150, 775; binary64, fresh: 3, 7, 24, 84, 261, 1189.110¹10²10³10⁴10⁵110¹10²10³10⁴10⁵productsunits of the format's roundoffbinary32, repeatedbinary64, repeatedbinary32, freshbinary64, freshorder 3 · five drawsthe units agree; the format only sets u
Fig. 6 The loss of orthogonality against the number of products, in each format’s own roundoff, for one repeated factor and for fresh factors, in binary32 and binary64.

The same runs in binary64 give the same two laws in that format’s units. Repeated, the loss is 149,542 units at a hundred thousand products, where binary32 had 61,626. Fresh, it is 1,189 against 775. Those are close, given how much of each is the size of one factor’s defect, and binary64’s defect here happens to be a larger fraction of its unit than binary32’s. So going to binary64 buys nine decades, the size of the unit, and it does not change the law. A binary64 product with a repeated factor still loses about 2⋅10−112 \cdot 10^{-11} per hundred thousand steps. That is small in almost every application, and it explains why the problem rarely shows there and is so familiar in binary32 on a graphics card.

Going to order 8 keeps the shape as well. A repeated orthogonal factor of order 8 loses 6, 14, 83, 694 and 6,863 units at 1 to 10,000 products, about one per step, and a pool of 64 loses 534 at ten thousand, at a slope well below that. Each entry in a larger matrix contributes a little more defect, which accounts for the higher starting point. That matches the size effect the plateau is set by the length measured for a reflector’s error, though the two are not the same mechanism.

Why the bound and the walk are both right

The classical bound and the random walk disagree about a product of rotations only because neither of them states what it has assumed. The bound counts every rounding at full size in the worst direction, and for kk products it reports about kk units. It is correct and very loose on fresh factors, by a factor of about 400 at a million products. On a repeated factor it is tight to about half a unit a step, which is as close as a general bound ever gets. So in this one problem the worst case is not a remote pathology but what happens to a code using a constant time step, and a person who has read that bounds like this are always pessimistic would be surprised.

The walk has assumed that the errors at different steps are independent. Each new product’s arithmetic does supply errors of that kind. But the factor is an input to all of them, and what it brings to each step is one number used again. That is a statement about the structure of the data rather than about rounding, and it could not be found by studying the arithmetic alone. A test on a random sequence of rotations, which is how such a thing would usually be tried, would pass with the walk’s slope and would never exercise the case a fixed-step integrator actually runs. What a single draw cannot report warned about a sample’s spread. This is a further warning, about what is being sampled over, because independent random inputs have a way of making a bias vanish.

What was held fixed

Three angles of a tenth of a radian were used, so each factor is a small rotation, and every factor is formed by double-precision trigonometry and then rounded. A factor computed in the format itself, with its own sine and cosine rounding, would have a larger defect; a quaternion would have a different problem. Five draws cannot characterise a tail, and at a single factor one draw’s defect can differ from another’s by a factor of a few, which is hidden in the median. The reference for the rotation is binary64, which has its own drift of the size measured here and which is small enough to neglect at these counts. No integration error is counted: these are pure products of supplied factors. And the loss is read at the end of each decade rather than across it, so its peak between repairs is whatever a period’s worth of drift comes to, and it is not in the figures.

Still open: a factor chosen for its defect, and the quaternion’s own drift

Choosing the factor. Rounding a rotation to binary32 leaves a defect set by its entries. Among the eight or so representable neighbours of each entry there is a matrix of smallest defect. The prediction with a sign is that a small search over those neighbours, for one factor, reduces the linear rate by a factor of at least ten, from 0.62 units a product to under 0.06, so that one search at setup is worth nine in ten of all the repairs that would otherwise be scheduled, and that this does nothing about the angle’s error, which stays where it was.

A representation with no stretch to lose. A quaternion carries a rotation in four numbers, and the same repeated multiplication scales its norm rather than losing orthogonality. The prediction is that its norm drifts with the same linear law at a comparable rate per step, so that it needs the same schedule of one normalisation, and that its normalisation costs far less than any matrix repair while leaving the same angle error, since the angle’s error comes from the factor and not from the repair.

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.

Binary32Gram–SchmidtLoss of orthogonalityOrthogonalityPolar decompositionUnit roundoff