Sorted sparse dot product 2026-10-31
For two sparse vectors with the same dimension and sorted nonzero entries, merge their coordinate lists. Equal indices contribute their product and advance both cursors. If one index is smaller, advance only that cursor: the other vector has zero at that coordinate, because all its smaller indices have already been processed and all remaining indices are larger. This proves a loop invariant consisting of the accumulated inner product over settled indices. Every iteration consumes at least one stored entry, giving time complexity and constant auxiliary space for nonzero entries. The method avoids work proportional to the ambient dimension. Its floating-point arithmetic error remains distinct from its exact algorithm correctness.
For finite real numbers , a Givens rotation that annihilates is defined mathematically by
Here hypot means an evaluation of the Euclidean norm of designed to avoid unnecessary range failures in floating-point arithmetic. The zero case selects the identity matrix, avoiding division by zero. If , then ; if , then . Thus the retained component is nonnegative, including when . In exact arithmetic, and .
Computing the square root of a*a + b*b directly can fail even when the final norm is representable. In binary64 floating-point arithmetic, makes the squares overflow, whereas is finite. With the squares round to zero, although is representable. A range-safe hypot avoids these intermediate failures.
The simple divisions still need care when is a subnormal number: rounding can cause large relative errors. For the smallest positive binary64 subnormal number , taking rounds to ; dividing by that rounded value gives , which is not an orthogonal matrix. For nonzero finite inputs, compute the coefficients before rescaling the norm:
At least one scaled component has absolute value one, and . This construction keeps the coefficients meaningful even when rounding is severe. It cannot make an unrepresentably large finite, preserve an arbitrarily tiny against underflow, or repair inputs already rounded to zero. The row updates themselves can also overflow if their exact results exceed the available range.
LAPACK's DLARTG uses scaling for numerical stability but a different sign convention: for it chooses and ; for it returns . Its block still has the lower-left minus sign. Do not combine its returned coefficients with an assumed nonnegative .[ref]
A nonzero pivot element can still be a poor numerical choice. Partial pivoting bounds the absolute values of the elimination multipliers by one in exact arithmetic, because each numerator is no larger than the selected denominator. This usually improves numerical stability, but does not prevent every possible growth of intermediate entries or guarantee accurate answers for every linear system.[ref]
To see the mechanism without depending on hardware, define a toy floating-point arithmetic model. The operator rounds an exact real number to the nearest value with three significant decimal digits; halfway cases choose the even final digit, and . Allow an unbounded exponent range. Round the inputs and the result of every addition, subtraction, multiplication and division separately; a product followed by a subtraction rounds twice. This is a prescribed arithmetic model, not a measurement of a physical computer. All inputs in the following original example are represented exactly:
Without a row interchange, the elimination multiplier is . The last coefficient and right-hand side become
Two distinct exact values now have the same rounded representation. Back substitution therefore returns
The small difference carrying information about was lost during elimination; the final subtraction only exposes that loss. Dividing by the tiny pivot element amplifies sensitivity to the lost information.
With partial pivoting, interchange the two rows, including their right-hand sides, to start from . Now , and the rounded updates are
Back substitution gives and . Both paths round intermediate values, but only the first creates million-sized entries and then divides a lost difference by .
The true solution follows from the original second equation :
Evaluate the linear-system residual against the original data in exact arithmetic, separately from the three-digit solve. With , the maximum norm, the results are
The respective relative linear-system residuals are and . Computing the diagnostic in higher precision matters: a check made only against the rounded reduced equations would miss the original error.
Figure 1.
The same three-digit floating-point arithmetic model gives without a row interchange and with partial pivoting; exact linear-system residuals expose the difference
. Details.
This matrix is well conditioned in the maximum norm: its induced matrix norm is , while
The failure therefore belongs to this unpivoted algorithm, rather than a large condition number. In general, however, partial pivoting cannot remove sensitivity inherent in the data. For any approximate solution of an invertible matrix system, ; the induced matrix norm inequality and give
A small linear-system residual controls solution error only in conjunction with the condition number; numerical stability and sensitivity are different questions.[ref]
Unit roundoff 2026-10-14
For floating-point arithmetic with precision in radix and rounding to nearest, unit roundoff is the half-gap scale . If a nonzero exact real number lies in the normal finite range, its nearest rounded value satisfies
Indeed, the absolute distance to the nearest value is at most , and gives the bound. For a correctly rounded addition, subtraction, multiplication or division, apply this estimate to the exact result of that one operation on the stored operands. The bound does not include earlier roundoff error or automatically bound a whole algorithm. Results outside the finite range and tiny subnormal numbers require separate treatment. Rounding in a directed mode also changes the bound.[ref]