Keith Briggs Keith.Briggs@bt
Transcript of Keith Briggs Keith.Briggs@bt
Exact real arithmetic
Keith Briggs
http://more.btexact.com/people/briggsk2/xr-kent-talk-pp.pdf
University of Kent Computing Laboratory 2004 Feb 10 1400
typeset 2004 February 9 12:18 in pdfLATEX on a linux system
Exact real arithmetic 1 of 32
Contents. Outline
. IEEE floating point
. Another floating point disaster
. Irrationals
. Digit expansions
. What's the problem?
. Continued fraction arithmetic
. Scaled-integer representation
. Data flow in XR system
. Some easy operations
. Addition and multiplication
. The big software challenge
. Algebraic number construction
. Transcendental functions
Exact real arithmetic 2 of 32
. exp
. Computable bounds
. My python implementation
. Lambdas
. Comparison
. Python implementation in action
. C++ implementation in action
. Features of my implementations
. Computational geometry application
. Simultaneous Diophantine approximation application
. Limitations
. Maths ⇐⇒ software
. Conclusion
. Bibliography
Exact real arithmetic 3 of 32
Outline
☞ what's the problem?
☞ representation and processing of reals
☞ my implementation of the Boehm scheme
☞ applications
☞ notation. integers: Z = { . . . ,−2,−1, 0, 1, 2, . . . }. rationals: Q = { p/q; p, q ∈ Z }. R\Q = { algebraics ∪ transcendentals }. bxe is the nearest integer to x
Keith Briggs Exact real arithmetic 4 of 32
IEEE floating point
☞ finite set of rationals and approximate operations:
F = { m 2e : |m| < 253, |e| < 1024, NaN, Inf }O = { +,−, ∗, / }, round to even or nearest
☞ problems!
x0 = F(0.9)xk+1 = F(3.999)∗xk∗(1−xk) k = 0, 1, 2, . . .
☞ in F, x53 > 0.5, but the exact result is
x53 = 0.130235874811773733039643730080570 . . .
Keith Briggs Exact real arithmetic 5 of 32
Another floating point disaster
u0 = e−1uk = k uk−1−1 k = 1, . . . , 25
0 5 10 15 20 25 30−50
−40
−30
−20
−10
0
10
20
30
decimal digits
log(
rela
tive
erro
r)
Keith Briggs Exact real arithmetic 6 of 32
Irrationals
☞ construction conventionally involves a limit of a sequence ofrationals
. rate of convergence is not specified
. traditional digit expansions converge from below
. what about negative digits?
☞ other possibilities:. symbolic dynamics of an expanding map. continued fractions, Möbius maps, LFTs. nested intervals with rational endpoints. non-integer base - e.g. golden ratio. does something even better exist?
We want a representation that allows thecomputation of more significant digits first
Keith Briggs Exact real arithmetic 7 of 32
Digit expansions
x 7→ f(x)digit=branch number
Keith Briggs Exact real arithmetic 8 of 32
What's the problem?
☞ example: find sign of one root x = −b+√
b2−4ac2a of quadratic
ax2+bx+c = 0
a
** b
sqr
-
c
*
2 4
output
/
-
sqrt
. input nodes don't know how much precision to send
. all input nodes send data, even if it eventually may not be needed
. to recalculate requires the whole tree to be re-evaluated
Keith Briggs Exact real arithmetic 9 of 32
Continued fraction arithmetic
x = [x0;x1, x2, x3, . . .] = x0+1
x1+1
x2+· · ·
☞ studied by Gosper, Vuillemin, Liardet, Stambul, Ménissier-Morain, Potts
☞ only easy operation: ax+bcx+d
☞ for +, ∗ etc., inefficient
☞ for exp, sin etc., very inefficient!
☞ problem:
How do we convert inputs to cf form?
Keith Briggs Exact real arithmetic 10 of 32
Scaled-integer representation (Boehm)
☞ for x ∈ R, consider a function x : Z+ → Z such that
| Bn x−x(n) | < 1
for fixed B = 2, 3, 4, . . .
☞ x : Z+ → Z ⇔ x ∈ XR
☞ in other words: x(n)−1Bn
< x <x(n)+1
Bn
☞ example: x ∈ Q, x(n) ≡ bBn xc
All computation with XRs is reduced tolarge integer operations
Keith Briggs Exact real arithmetic 11 of 32
Data flow in XR system
a
** b
sqr
-
c
*
2 4
output
/
-
sqrt
Keith Briggs Exact real arithmetic 12 of 32
Some easy operations
☞ |x|(n) ≡ |x(n)|
☞ [−x](n) ≡ −x(n)
☞ [sqrt(x)](n) ≡√
x(2n)
☞ for m ∈ Z, [m x](n) ≡ bm x(n+p)/2p e, p = 1+log2 |m|
☞ for m ∈ Z∗, [x/m](n) ≡ bx(n)/m e
☞ comparison: if for some n, x(n) and y(n) differ by morethan 1, they are unequal
☞ caching: x(n) can be evaluated as bx(m)/2m−n e if x(m) form > n is available
Keith Briggs Exact real arithmetic 13 of 32
Addition and multiplication (for B = 2)
[x+y](n) ≡ b (x(n+2)+y(n+2)+2) /4 c
| [x+y](n)−2n(x+y) |= | b (x(n+2)+y(n+2)+2 ) /4c−2n(x+y) |6 1/2+| (x(n+2)+y(n+2))/4−2n(x+y) |= 1/2+| x(n+2)+y(n+2)−2n+2(x+y) | / 4
6 1/2+| x(n+2)−2n+2 x | / 4+| y(n+2)−2n+2 y | / 4< 1
[x∗y](n) ≡ b(x(q+r+1)y(p+s+1)+2p+q+3
)/2p+q+4 c
where
r = b (n+2)/2 c, s = n+2−r, p = b log2 |x(r)| c, q = b log2 |x(s)| c
Keith Briggs Exact real arithmetic 14 of 32
Algebraic number construction
☞ given a polynomial p with integer coefficients, and integersa, k > 0, we can compute the sign of p at a/Bk with onlyinteger operations
def signat(p,a,k): # return the sign of polynomial p at a/B^kn,w=len(p),p[0]for j in 1. .n:w*=aw+=p[j]*B^(k*j)
return w>0
☞ thus, given a bracketed root:
sign p(a/Bk) < 0, sign p(b/Bk) > 0
we may refine it by bisection to any desired accuracy
Keith Briggs Exact real arithmetic 15 of 32
Transcendental functions
☞ f = exp, arctan, sin etc. can be computed if we can imple-ment an approximating function f : Q×Z+ → Z such that∣∣∣ Bn f(q)−f(q, n)
∣∣∣ < 1
☞ we need approximating functions which give us a priori errorbounds
☞ or, at least, error bounds must be computable in rationalarithmetic
☞ for π, use
π =∞∑
n=0
[4
8n+1− 2
8n+4− 1
8n+5− 1
8n+6
]1
16n
Keith Briggs Exact real arithmetic 16 of 32
exp
☞ easy to get exp(x) with bounded absolute error if we know|x| < 1: use Padé approximants:
max|x|<1
| ex−Rn/n(x) | 6 8(n!)2
(2n)!(2n+1)!
. we can't test |x| < 1!. . .
. but we can safely find k such that |x/2k| < 1
Keith Briggs Exact real arithmetic 17 of 32
The big software challenge
Produce efficient software which hides thebottom-up data flow and can be used bya programmer as if it were conventionaltop-down code
☞ needed features:. λ anonymous function constructor. operator overloading
☞ not easily possible in Fortran, C, java, . . .
☞ easy in python, Haskell, clean, ML, ocaml
☞ possible in C++! (with tricks)
Keith Briggs Exact real arithmetic 18 of 32
My python implementation
☞ www.python.org
☞ `perl done right'
☞ much cleaner syntax than java
☞ portable (Linux, Unix, Mac, . . . , Windows)
☞ functional features - lambda, map, filter, reduce
☞ operator overloading
☞ free
Keith Briggs Exact real arithmetic 19 of 32
Lambdas
def +(x,y):# compute the sum of x and y for B=2return lambda n: (x(n+2)+y(n+2)+2)/4
def sqrt(x):# compute the square root of x for B=2
return lambda n: sqrt(x(2*n))
☞ possibility of distributed computation: a lambda may beexecuted remotely
Keith Briggs Exact real arithmetic 20 of 32
Comparison
This is a system for proving inequalities
. . . but, equality is undecidable
def <(x,y):# return true if x<y, false if x>y, else don’t returnn=1while 1:if x(n)<y(n)−1: return trueif x(n)>y(n)+1: return false
n+=1
def max(x,y):# compute the maximum of x and y without comparison
return (x+y+abs(x−y))/2
Keith Briggs Exact real arithmetic 21 of 32
Python implementation in action
> import XR> e=exp(XR(1))> print contfrac(e)[2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10, 1, 1, 12, 1, 1, 14]> print cos(2*pi()/7).dec(50)0.62348980185873353052500488400423981063227473> print polyroot([8,4,−4,−1],0,1,0).dec(50)0.62348980185873353052500488400423981063227473> x=XR(Q(1,3))> for i in range(10):print 2*x>1x=4*x*(1−x)
0 1 0 1 0 1 1 0 0 1
Keith Briggs Exact real arithmetic 22 of 32
C++ implementation in action
#include "XR.h"
int main() {XR e=exp(XR(1));cout<<contfrac(e);cout<<cos(2*pi()/7).dec(50);ZZ coeffs[ ]={8,4,−4,−1};cout<<polyroot(coeffs,0,1,0).dec(50);x=XR(Q(1,3));for (int i=0; i<10; i++) {cout<<2*x>1;x=4*x*(1−x);}}
Keith Briggs Exact real arithmetic 23 of 32
My C implementation
☞ more.btexact.com/people/briggsk2/xrc.html
☞ portable ISO C
☞ Uses pointers to construct DAG representing compositions oflambdas
☞ Function call ust be written for every arithmetic operation
☞ C++ wrapper
Keith Briggs Exact real arithmetic 24 of 32
Features of my implementations
☞ python, C++, C
☞ all integer arithmetic, using gmp
☞ automatic caching of all intermediate results
☞ easily integrated with existing code
☞ C, C++ versions are fully compiled
☞ possible application areas:. computational number theory. computational geometry. computer-assisted proofs in analysis. computer algebra
Keith Briggs Exact real arithmetic 25 of 32
Computational geometry application
☞ Simple planar example: line through (x1, y1), (x2, y2); is thepoint (x0, y0) to the left or right of the line?
��
��
��
��
��
��
��
��
��
b (x1, y1)
b (x2, y2)
b (x0, y0)
☞ This is determined by sign ([(y1−y0)(x2−x1)−(x1−x0)(y2−y1)])
With XR arithmetic, the sign is alwaysdetermined correctly and with the minimalnecessary computation
Keith Briggs Exact real arithmetic 26 of 32
Simultaneous Diophantine approximation application
☞ typical subproblem: given. x1, x2 ∈ R. p1, p2, q ∈ Z. e1 = |qx1−p1|, e2 = |qx2−p2|
☞ determine whether e1 > e2. very efficiently solved in XR with trivial coding by user
☞ is exp(π√
163) an integer?
x=exp(pi*sqrt(XR(163)))
print x>floor(x) # prints ’True’
Keith Briggs Exact real arithmetic 27 of 32
Limitations
☞ memory demands
☞ non-incrementality
☞ the floor function does not terminate on integer inputs
☞ transcendental functions still incomplete
☞ no verified decimal output
Keith Briggs Exact real arithmetic 28 of 32
Mathematics?⇐⇒ software
☞ mathematics =⇒ mathematical software. Numerical analysis. Statistics. Computer algebra. Computational number theory. Combinatorics and graph theory. Finite groups. Theorem proving. . . .
☞ what about the other direction?
Keith Briggs Exact real arithmetic 29 of 32
Conclusion
☞ it's worth rethinking how we represent numbers and doarithmetic
☞ lots of future work possible:. granularity - automatic choice for B?. optimize caching strategy. log, sin, cos etc.. non-functional - graph method. parallel and multi-threaded techniques. characterize the set XR. complexity analysis (not assuming each ∗, + etc. has the same cost)
Keith Briggs Exact real arithmetic 30 of 32
Bibliography 1
☞ H-J Boehm, R Cartwright, M Riggle, and M J O'Donnell: Exactreal arithmetic: A case study in higher order programmingdev.acm.org/pubs/citations/proceedings/lfp/319838/p162-boehm/
☞ V Ménissier-Morain: Arithmétique exacte, conception, algo-rithmique et performances d'une implémentation informatiqueen précision arbitraire Paris thesis 1994 calfor.lip6.fr/∼vmm/
☞ J R Harrison: Theorem proving with the real numbers Cam-bridge thesis 1996 www.ftp.cl.cam.ac.uk/ftp/papers/reports/
☞ P Potts: Exact Real Arithmetic using Möbius Transformationswww.purplefinder.com/∼potts/
Keith Briggs Exact real arithmetic 31 of 32
Bibliography 2
☞ Computability and Complexity in Analysis Networkwww.informatik.fernuni-hagen.de/import/cca/
☞ K Weihrauch: Computable Analysiswww.informatik.fernuni-hagen.de/import/thi1/klaus.weihrauch/book.html
☞ P Liardet and P Stambul: Algebraic Computations with Con-tinued Fractions www.idealibrary.com/links/doi/10.1006/jnth.1998.2274
☞ K M Briggs: XR homepage more.btexact.com/people/briggsk2/XR.html;This talk: more.btexact.com/people/briggsk2/xr-kent-talk-pp.pdf
Keith Briggs Exact real arithmetic 32 of 32