In affine arithmetic, each input or computed quantity x is represented by a formula x = x 0 + x 1 ϵ 1 + x 2 ϵ 2 + {\displaystyle x=x_{0}+x_{1}\epsilon _{1}+x_{2}\epsilon _{2}+{}} ⋯ {\displaystyle \cdots } + x n ϵ n {\displaystyle {}+x_{n}\epsilon _{n}} where x 0 , x 1 , x 2 , {\displaystyle x_{0},x_{1},x_{2},} … , {\displaystyle \dots ,} x n {\displaystyle x_{n}} are known floating-point numbers, and ϵ 1 , ϵ 2 , … , ϵ n {\displaystyle \epsilon _{1},\epsilon _{2},\dots ,\epsilon _{n}} are symbolic variables whose values are only known to lie in the range [-1,+1].
Thus, for example, a quantity X which is known to lie in the range [3,7] can be represented by the affine form x = 5 + 2 ϵ k {\displaystyle x=5+2\epsilon _{k}} , for some k. Conversely, the form x = 10 + 2 ϵ 3 − 5 ϵ 8 {\displaystyle x=10+2\epsilon _{3}-5\epsilon _{8}} implies that the corresponding quantity X lies in the range [3,17].
The sharing of a symbol ϵ j {\displaystyle \epsilon _{j}} among two affine forms x {\displaystyle x} , y {\displaystyle y} implies that the corresponding quantities X, Y are partially dependent, in the sense that their joint range is smaller than the Cartesian product of their separate ranges. For example, if x = 10 + 2 ϵ 3 − 6 ϵ 8 {\displaystyle x=10+2\epsilon _{3}-6\epsilon _{8}} and y = 20 + 3 ϵ 4 + 4 ϵ 8 {\displaystyle y=20+3\epsilon _{4}+4\epsilon _{8}} , then the individual ranges of X and Y are [2,18] and [13,27], but the joint range of the pair (X,Y) is the hexagon with corners (2,27), (6,27), (18,19), (18,13), (14,13), (2,21) — which is a proper subset of the rectangle [2,18]×[13,27].
Affine forms can be combined with the standard arithmetic operations or elementary functions, to obtain guaranteed approximations to formulas.
For example, given affine forms x , y {\displaystyle x,y} for X and Y, one can obtain an affine form z {\displaystyle z} for Z = X + Y simply by adding the forms — that is, setting z j {\displaystyle z_{j}} ← {\displaystyle \gets } x j + y j {\displaystyle x_{j}+y_{j}} for every j. Similarly, one can compute an affine form z {\displaystyle z} for Z = α {\displaystyle \alpha } X, where α {\displaystyle \alpha } is a known constant, by setting z j {\displaystyle z_{j}} ← {\displaystyle \gets } α x j {\displaystyle \alpha x_{j}} for every j. This generalizes to arbitrary affine operations like Z = α {\displaystyle \alpha } X + β {\displaystyle \beta } Y + γ {\displaystyle \gamma } .
A non-affine operation Z {\displaystyle Z} ← {\displaystyle \gets } F ( X , Y , {\displaystyle F(X,Y,} … {\displaystyle \dots } ) {\displaystyle )} , like multiplication Z {\displaystyle Z} ← {\displaystyle \gets } X Y {\displaystyle XY} or Z {\displaystyle Z} ← {\displaystyle \gets } sin ( X ) {\displaystyle \sin(X)} , cannot be performed exactly, since the result would not be an affine form of the ϵ i {\displaystyle \epsilon _{i}} . In that case, one should take a suitable affine function G that approximates F to first order, in the ranges implied by x {\displaystyle x} and y {\displaystyle y} ; and compute z {\displaystyle z} ← {\displaystyle \gets } G ( x , y , {\displaystyle G(x,y,} … {\displaystyle \dots } ) + z k ϵ k {\displaystyle )+z_{k}\epsilon _{k}} , where z k {\displaystyle z_{k}} is an upper bound for the absolute error | F − G | {\displaystyle |F-G|} in that range, and ϵ k {\displaystyle \epsilon _{k}} is a new symbolic variable not occurring in any previous form.
The form z {\displaystyle z} then gives a guaranteed enclosure for the quantity Z; moreover, the affine forms x , y , {\displaystyle x,y,} … {\displaystyle \dots } , z {\displaystyle ,z} jointly provide a guaranteed enclosure for the point (X,Y,...,Z), which is often much smaller than the Cartesian product of the ranges of the individual forms.
Systematic use of this method allows arbitrary computations on given quantities to be replaced by equivalent computations on their affine forms, while preserving first-order correlations between the input and output and guaranteeing the complete enclosure of the joint range. One simply replaces each arithmetic operation or elementary function call in the formula by a call to the corresponding AA library routine.
For smooth functions, the approximation errors made at each step are proportional to the square h2 of the width h of the input intervals. For this reason, affine arithmetic will often yield much tighter bounds than standard interval arithmetic (whose errors are proportional to h).
In order to provide guaranteed enclosure, affine arithmetic operations must account for the roundoff errors in the computation of the resulting coefficients z j {\displaystyle z_{j}} . This cannot be done by rounding each z j {\displaystyle z_{j}} in a specific direction, because any such rounding would falsify the dependencies between affine forms that share the symbol ϵ j {\displaystyle \epsilon _{j}} . Instead, one must compute an upper bound δ j {\displaystyle \delta _{j}} to the roundoff error of each z j {\displaystyle z_{j}} , and add all those δ j {\displaystyle \delta _{j}} to the coefficient z k {\displaystyle z_{k}} of the new symbol ϵ k {\displaystyle \epsilon _{k}} (rounding up). Thus, because of roundoff errors, even affine operations like Z = α {\displaystyle \alpha } X and Z = X + Y will add the extra term z k ϵ k {\displaystyle z_{k}\epsilon _{k}} .
The handling of roundoff errors increases the code complexity and execution time of AA operations. In applications where those errors are known to be unimportant (because they are dominated by uncertainties in the input data and/or by the linearization errors), one may use a simplified AA library that does not implement roundoff error control.
Affine arithmetic can be viewed in matrix form as follows. Let X 1 , X 2 , {\displaystyle X_{1},X_{2},} … , {\displaystyle \dots ,} X m {\displaystyle X_{m}} be all input and computed quantities in use at some point during a computation. The affine forms for those quantities can be represented by a single coefficient matrix A and a vector b, where element A i , j {\displaystyle A_{i,j}} is the coefficient of symbol ϵ j {\displaystyle \epsilon _{j}} in the affine form of X i {\displaystyle X_{i}} ; and b i {\displaystyle b_{i}} is the independent term of that form. Then the joint range of the quantities — that is, the range of the point ( X 1 , X 2 , {\displaystyle (X_{1},X_{2},} … , {\displaystyle \dots ,} X m ) {\displaystyle X_{m})} — is the image of the hypercube U n = [ − 1 , + 1 ] n {\displaystyle U^{n}=[-1,+1]^{n}} by the affine map from U n {\displaystyle U^{n}} to R m {\displaystyle R^{m}} defined by ϵ {\displaystyle \epsilon } → {\displaystyle \to } A ϵ + b {\displaystyle A\epsilon +b} .
The range of this affine map is a zonotope bounding the joint range of the quantities X 1 , X 2 , {\displaystyle X_{1},X_{2},} … , {\displaystyle \dots ,} X m {\displaystyle X_{m}} . Thus one could say that AA is a "zonotope arithmetic". Each step of AA usually entails adding one more row and one more column to the matrix A.
Since each AA operation generally creates a new symbol ϵ k {\displaystyle \epsilon _{k}} , the number of terms in an affine form may be proportional to the number of operations used to compute it. Thus, it is often necessary to apply "symbol condensation" steps, where two or more symbols ϵ k {\displaystyle \epsilon _{k}} are replaced by a smaller set of new symbols. Geometrically, this means replacing a complicated zonotope P by a simpler zonotope Q that encloses it. This operation can be done without destroying the first-order approximation property of the final zonotope.
Affine arithmetic can be implemented by a global array A and a global vector b, as described above. This approach is reasonably adequate when the set of quantities to be computed is small and known in advance. In this approach, the programmer must maintain externally the correspondence between the row indices and the quantities of interest. Global variables hold the number m of affine forms (rows) computed so far, and the number n of symbols (columns) used so far; these are automatically updated at each AA operation.
Alternatively, each affine form can be implemented as a separate vector of coefficients. This approach is more convenient for programming, especially when there are calls to library procedures that may use AA internally. Each affine form can be given a mnemonic name; it can be allocated when needed, be passed to procedures, and reclaimed when no longer needed. The AA code then looks much closer to the original formula. A global variable holds the number n of symbols used so far.
On fairly long computations, the set of "live" quantities (that will be used in future computations) is much smaller than the set of all computed quantities; and ditto for the set of "live" symbols ϵ j {\displaystyle \epsilon _{j}} . In this situation, the matrix and vector implementations are too wasteful of time and space.
In such situations, one should use a sparse implementation. Namely, each affine form is stored as a list of pairs (j, x j {\displaystyle x_{j}} ), containing only the terms with non-zero coefficient x j {\displaystyle x_{j}} . For efficiency, the terms should be sorted in order of j. This representation makes the AA operations somewhat more complicated; however, the cost of each operation becomes proportional to the number of nonzero terms appearing in the operands, instead of the number of total symbols used so far.
This is the representation used by LibAffa.