Exact real arithmetic. Keith Briggs

Similar documents
Implementing exact real arithmetic in python, C++ and C

Today s class. Roots of equation Finish up incremental search Open methods. Numerical Methods, Fall 2011 Lecture 5. Prof. Jinbo Bi CSE, UConn

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

Sage Reference Manual: Diophantine approximation

f( x ), or a solution to the equation f( x) 0. You are already familiar with ways of solving

Numerical computing. How computers store real numbers and the problems that result

LECTURE 0: Introduction and Background

Classes of Real Numbers 1/2. The Real Line

Computational Mathematics: Models, Methods and Analysis. Zhilin Li

Using Arithmetic of Real Numbers to Explore Limits and Continuity

An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s.

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Reals 1. Floating-point numbers and their properties. Pitfalls of numeric computation. Horner's method. Bisection. Newton's method.

CS321 Introduction To Numerical Methods

F28PL1 Programming Languages. Lecture 11: Standard ML 1

Definition. A Taylor series of a function f is said to represent function f, iff the error term converges to 0 for n going to infinity.

G Programming Languages - Fall 2012

Exercises for a Numerical Methods Course

Numerical Computations and Formal Methods

Computer Systems C S Cynthia Lee

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Arbitrary Precision and Symbolic Calculations

EXERCISE Which of the following is irrational? (C) 7 (D) 81 (A) (B)

ELEMENTARY NUMBER THEORY AND METHODS OF PROOF

Rational and Irrational Numbers

CSI33 Data Structures

3.5 Floating Point: Overview

Odd-Numbered Answers to Exercise Set 1.1: Numbers

Floating-point numbers. Phys 420/580 Lecture 6

1.2 Round-off Errors and Computer Arithmetic

or 5.00 or 5.000, and so on You can expand the decimal places of a number that already has digits to the right of the decimal point.

Skill 1: Multiplying Polynomials

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

CS 11 Haskell track: lecture 1

Description Hex M E V smallest value > largest denormalized negative infinity number with hex representation 3BB0 ---

Fall Recursion and induction. Stephen Brookes. Lecture 4

Simply-Typed Lambda Calculus

Unit 3. Operators. School of Science and Technology INTRODUCTION

Lecture 16: Static Semantics Overview 1

CGF Lecture 2 Numbers

Scientific Computing. Error Analysis

Algebra 1 Review. Properties of Real Numbers. Algebraic Expressions

Zheng-Liang Lu Java Programming 45 / 79

Table : IEEE Single Format ± a a 2 a 3 :::a 8 b b 2 b 3 :::b 23 If exponent bitstring a :::a 8 is Then numerical value represented is ( ) 2 = (

Maths: Phase 5 (Y12-13) Outcomes

Floating-Point Arithmetic

Binary floating point encodings

The design recipe. Readings: HtDP, sections 1-5. (ordering of topics is different in lectures, different examples will be used)

CSCI 270: Introduction to Algorithms and Theory of Computing Fall 2017 Prof: Leonard Adleman Scribe: Joseph Bebel

MAINE ASSOCIATION OF MATH LEAGUES RULES GOVERNING QUESTIONS, ANSWERS, AND GRADING

LECTURE 16. Functional Programming

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

Macro Programming Reference Guide. Copyright 2005 Scott Martinez

Computer arithmetics: integers, binary floating-point, and decimal floating-point

Lectures 4 and 5 (Julian) Computer Programming: Skills & Concepts (INF-1-CP1) double; float; quadratic equations. Practical 1.

Chapter 1. Fundamentals of Higher Order Programming

Lecture 5: Lazy Evaluation and Infinite Data Structures

Continuity and Limits of Programs

CS177 Python Programming. Recitation 2 - Computing with Numbers

Contents. Hilary Term. Summary of Numerical Analysis for this term. Sources of error in numerical calculation. Solving Problems

Logical and Function Constructions

Generating a Minimal Interval Arithmetic Based on GNU MPFR

John Perry. Spring 2017

Basic types and definitions. Chapter 3 of Thompson

22c:111 Programming Language Concepts. Fall Types I

Math 3 Coordinate Geometry Part 2 Graphing Solutions

Cantor s Diagonal Argument for Different Levels of Infinity

Mathematics. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Last class. CS Principles of Programming Languages. Introduction. Outline

Lecture 1. 1 Notation

Beating Hand-Tuned Assembly. David Richardson

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions.

Central Valley School District Math Curriculum Map Grade 8. August - September

2 Computation with Floating-Point Numbers

Math Lab- Geometry Pacing Guide Quarter 3. Unit 1: Rational and Irrational Numbers, Exponents and Roots

Formal Verification of a Floating-Point Elementary Function

DEPARTMENT - Mathematics. Coding: N Number. A Algebra. G&M Geometry and Measure. S Statistics. P - Probability. R&P Ratio and Proportion

COURSE: NUMERICAL ANALYSIS. LESSON: Methods for Solving Non-Linear Equations

STEPHEN WOLFRAM MATHEMATICADO. Fourth Edition WOLFRAM MEDIA CAMBRIDGE UNIVERSITY PRESS

Rigorous Estimation of Floating- point Round- off Errors with Symbolic Taylor Expansions

CS321. Introduction to Numerical Methods

Julia Calculator ( Introduction)

Up next. Midterm. Today s lecture. To follow

Floating Point Puzzles The course that gives CMU its Zip! Floating Point Jan 22, IEEE Floating Point. Fractional Binary Numbers.

Floating point. Today! IEEE Floating Point Standard! Rounding! Floating Point Operations! Mathematical properties. Next time. !

Chapter 1: Interval computation

Math Interim Mini-Tests. 3rd Grade Mini-Tests

Scheme Quick Reference

Outline and Reading. Analysis of Algorithms 1

Unit 7 Number System and Bases. 7.1 Number System. 7.2 Binary Numbers. 7.3 Adding and Subtracting Binary Numbers. 7.4 Multiplying Binary Numbers

3x - 5 = 22 4x - 12 = 2x - 9

Systems I. Floating Point. Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties

Chapter 11 :: Functional Languages

Programming Project #2: Solving Quadratic Equations Date Due: Monday 25 September 2017

(Refer Slide Time: 02:59)

Floating Point January 24, 2008

Florida Math 0018 Correlation of the ALEKS course Florida Math 0018 to the Florida Mathematics Competencies - Lower

Math 126 Final Examination SPR CHECK that your exam contains 8 problems on 8 pages.

Transcription:

Exact real arithmetic Keith Briggs Keith.Briggs@bt.com more.btexact.com/people/briggsk2/xr.html 2002 Nov 20 15:00 Typeset in L A T E X2e on a linux system Exact real arithmetic p.1/35

Complexity Outline R e s earc h What s the problem? Representation of reals Processing of reals Applications Exact real arithmetic p.2/35

Real numbers Integers: Rationals: Irrationals: all the rest: irrationals algebraics transcendentals Exact real arithmetic p.3/35

Digit expansions Exact real arithmetic p.4/35

'!! ( " " ),* + % -! % % % + * % % IEEE floating point Inf NaN Problems!!! % $ "#, but the exact result is *, In %, %,,, +, Exact real arithmetic p.5/35

. * ( ( Another floating point disaster $. "0/. " 30 20 10 log(relative error) 0 10 20 30 40 50 0 5 10 15 20 25 30 decimal digits Exact real arithmetic p.6/35

3 $ 4 4 4 2 7 # 65 Continued fraction arithmetic 1 2 $ Studied by Gosper, Vuillemin, Liardet, Stambul, Ménissier-Morain, Potts. Only easy operation: For etc., inefficient. For exp # 86 9. sin etc., very inefficient! How do we convert inputs to required cf form? Exact real arithmetic p.7/35

Constructing irrationals necessarily involves a limit of a sequence of rationals: digit expansions (possibly with negative digits) symbolic dynamics of an expanding map continued fractions nested intervals with rational endpoints Dedekind cuts Conway s construction Exact real arithmetic p.8/35

Representing irrationals Traditional digit expansions converge strictly from below, so Digit expansions with negative digits Continued fractions Möbius maps (LFTs) Non-integer base - e.g. golden ratio Does something even better exist? We want a representation that allows the computation of more significant digits first Exact real arithmetic p.9/35

Computing with limits Can we compute with limits? Conventionally, no; but what if all our limits converge at the same rate? Exact real arithmetic p.10/35

? > = ; ;: A A D @ B Data flow 1 Example: solve a quadratic CB Exact real arithmetic p.11/35

Data flow 2 2 a 4 * b c * sqr * - sqrt - / output Exact real arithmetic p.12/35

What s wrong with that? 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 Exact real arithmetic p.13/35

E F K J H G F L ; ; I M # N # B-expansions 1 For, consider a function such that for fixed % ( ) Exact real arithmetic p.14/35

' ' N N E MO MO ER E Q B-expansions 2 In other words, Example:, 'P N OM NB: we reduce all computation with integer calculations. s to Exact real arithmetic p.15/35

Data flow 3 2 a 4 * b c * sqr * - sqrt - / output Exact real arithmetic p.16/35

Addition 1 S 3 ' P N Q N ' S N ' ' R T UWV XZY \[ G ]_^ ` \ a V X a Y ] T b T c\ V \ G X ] XY \ G X ] X ]de f ^ ` \ a V X a Y ] T g h d X \T V \ G X ] XZY \ G X ]]de ^ ` \ a V X a Y ] T b h d X \TV G X ] XZY \ G X ] ^ ` i j \ a V X a Y ] Tde g h d X \TV G X ] ^ ` i j a V Tde X T Y \ G X ] ^ ` i j a Y Tde b h Exact real arithmetic p.17/35

The big challenge Produce efficient software which hides the bottom-up data flow and can be used by a programmer as if it were conventional topdown code Not possible in Fortran, C, Java, Possible in python, Haskell, clean, ML, ocaml, (and C++) The answer: lazy evaluation Exact real arithmetic p.18/35

) ( B " M B M A M B ) Algebraic number construction Given a polynomial with integer coefficients, and integers, we can compute the sign of at with only integer operations. def sign_at(p,a,k): n,w=len(p),p[0] for j in 1..n: w*=a w+=p[j]*bˆ(k*j) return w>0 Thus, given a bracketed root: sign " ' sign " ' we may refine it by bisection to any desired accuracy. Exact real arithmetic p.19/35

k # l ' k N n O - + + + + - * N N N N Transcendentals exp arctan sin etc. can be computed if we can implement an approximating function such that k ' k OM m m m m This can be achieved using Taylor polynomials with bounds on the error term. For, use Oo Exact real arithmetic p.20/35

Python implementation 1 www.python.org improved perl much cleaner syntax than java but just as powerful portable functional features - lambda operator overloading free! Exact real arithmetic p.21/35

Lambdas def +(x,y): return lambda n: (x(n+2)+y(n+2)+2)/4 def sqrt(x): return lambda n: sqrt(x(2*n)) Exact real arithmetic p.22/35

Lazy functional programming Principle: Do not evaluate arguments until necessary def if_nonzero(f,x): " return a function which computes f if x is nonzero, else None " if x: return lambda x: f(x) return None Exact real arithmetic p.23/35

Comparison Equality is undecidable, but This is a system for proving inequalities def (x,y): n=1 while 1: if x(n)y(n) 1: return true if x(n)>y(n)+1: return false n++ Exact real arithmetic p.24/35

Python implementation in action Exact real arithmetic p.25/35

Python implementation in action > import XR > x=exp(pi()*sqrt(xr(163))) > print floor(x)x 1 > 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>1... x=4*x*(1 x)... 0 1 0 1 0 1 1 0 0 1 Exact real arithmetic p.26/35

C ++ implementation #include "XR.h" int main() { XR x=exp(pi()*sqrt(xr(163))); coutfloor(x)x; XR e=exp(xr(1)); coutcontfrac(e); coutcos(2*pi()/7).dec(50); ZZ coeffs[]={8,4, 4, 1}; coutpolyroot(coeffs,0,1,0).dec(50); x=xr(q(1,3)); for (int i=0; i10; i++) { cout2*x>1; x=4*x*(1 x); } return 0; } Exact real arithmetic p.27/35

Features of my implementations python, C ++ All integer arithmetic Caching Easily integrated with existing code C ++ version is fully compiled Applications in computational number theory, computer-assisted proofs, computer algebra, Exact real arithmetic p.28/35

' ' S S 2 ' S 2 p ' S p ' p Computational geometry applications 1 Simple planar example: Line through ; is the point to the left or right of the line? $ ' S $ 2 2 S $ $ Exact real arithmetic p.29/35

3 ' ' ' 1 ' ' S 2 $ $ 2 Computational geometry applications 2 This is determined by sign S S $ S $ With arithmetic, the sign is always determined correctly and with the minimal necessary computation Exact real arithmetic p.30/35

2 ) Example applications 1. Simultaneous Diophantine approximation algorithms Typical subproblem: given $ Is $ 2, or not? $ $ $ $ 2. Is exp 2 '% - 2 2 2 an integer? x=exp(pi*sqrt(xr(163))) f=floor(x) print x>f, xf+1 # => true, true Exact real arithmetic p.31/35

Problems 1. Memory demands 2. Non-incrementality 3. The floor function does not terminate on integer imputs 4. This makes implementing exp etc. hard 5. (Verified) decimal output Exact real arithmetic p.32/35

Maths software Mathematics q mathematical software Numerical analysis Statistics Computer algebra Computational number theory Combinatorics and graph theory Finite groups Theorem proving r stsuststsustsusvstsusts stsuststsusvsuststsuststw What about the other direction? Exact real arithmetic p.33/35

Conclusion It s worth rethinking how we represent numbers and do arithmetic, especially for distributed applications Exact real arithmetic p.34/35

x Bibliography V Ménissier-Morain: Arithmétique exacte, conception, algorithmique et performances d une implémentation informatique en précision arbitraire Paris thesis 1994 calfor.lip6.fr/ vmm/ J R Harrison: Theorem proving with the real numbers Cambridge thesis 1996 www.ftp.cl.cam.ac.uk/ftp/papers/reports/ K M Briggs: XR homepage more.btexact.com/people/briggsk2/xr.html Exact real arithmetic p.35/35