Marcin Ciura Google Poland Krakw, May 12, 2011 Reals in Python - - PowerPoint PPT Presentation

marcin ciura
SMART_READER_LITE
LIVE PREVIEW

Marcin Ciura Google Poland Krakw, May 12, 2011 Reals in Python - - PowerPoint PPT Presentation

Exact Real Arithmetic in Python Marcin Ciura Google Poland Krakw, May 12, 2011 Reals in Python float , math IEEE 754 double arithmetic built-in type, standard library Reals in Python decimal.Decimal decimal floating-point


slide-1
SLIDE 1

Exact Real Arithmetic in Python

Marcin Ciura

Google Poland Kraków, May 12, 2011

slide-2
SLIDE 2

Reals in Python

float, math

◮ IEEE 754 double arithmetic ◮ built-in type, standard library

slide-3
SLIDE 3

Reals in Python

decimal.Decimal

◮ decimal floating-point arithmetic ◮ since Python 2.4

slide-4
SLIDE 4

Reals in Python

gmpy.mpf

◮ multiple-precision arithmetic ◮ third-party library

slide-5
SLIDE 5

Example: Chaos Theory

logistic map x0 = 0.671875 xn+1 = 4xn(1 − xn)

slide-6
SLIDE 6

Example: Chaos Theory

n exact decimal gmpy.mpf float 0.671875 0.671875 0.671875 0.671875 1 0.881835 0.881835 0.881835 0.881835 2 0.416805 0.416805 0.416805 0.416805 3 0.972314 0.972314 0.972314 0.972314 4 0.107675 0.107675 0.107675 0.107675

slide-7
SLIDE 7

Example: Chaos Theory

n exact decimal gmpy.mpf float 53 0.717980 0.717980 0.717980 0.749943 54 0.809938 0.809938 0.809937 0.750112 55 0.615751 0.615751 0.615755 0.749775 56 0.946406 0.946406 0.946403 0.750449 57 0.202886 0.202886 0.202897 0.749100

slide-8
SLIDE 8

Example: Chaos Theory

n exact decimal gmpy.mpf float 68 0.910298 0.910298 0.893725 0.000935 69 0.326622 0.326622 0.379919 0.003740 70 0.879760 0.879760 0.942322 0.014904 71 0.423127 0.423127 0.217403 0.058727 72 0.976362 0.976362 0.680555 0.221116

slide-9
SLIDE 9

Example: Chaos Theory

n exact decimal gmpy.mpf float 91 0.035427 0.023866 0.241684 0.487988 92 0.136690 0.093186 0.733092 0.999422 93 0.472024 0.338009 0.782670 0.002306 94 0.996869 0.895036 0.680388 0.009206 95 0.012483 0.375783 0.869839 0.036486

slide-10
SLIDE 10

Example: Chaos Theory corollary

for some applications, multiple-precision arithmetic is not good enough

we must go deeper

slide-11
SLIDE 11

Reals in Python

◮ float ◮ decimal.Decimal ◮ gmpy.mpf

round-off errors (begot numerical analysis) fast

slide-12
SLIDE 12

Exact Reals

◮ continued fractions ◮ scaled integers ◮ Möbius transformations ◮ other methods

no round-off errors slowish

slide-13
SLIDE 13

Outline

So far

◮ Introduction

Now

◮ Continued fractions ◮ Gosper’s algorithm ◮ Python implementation ◮ Live demo

slide-14
SLIDE 14

Continued Fractions

a0 + 1 a1 + 1 a2 + 1 a3 + 1 ... ak partial quotients [a0; a1, a2, a3, . . . , ak]

slide-15
SLIDE 15

Continued Fractions

byproduct of Euclid’s gcd algorithm 96 = 1 × 65 + 31 65 = 2 × 31 + 3 31 = 10 × 3 + 1 3 = 3 × 1 96 65 = [1; 2, 10, 3]

slide-16
SLIDE 16

Continued Fractions

◮ 1 : 1 correspondence with reals ◮ finite c.f. – rationals ◮ infinite c.f. – irrationals ◮ truncated c.f. – best approximations ◮ usually ∼ 0.97027 partial quotients per digit

(e.g. 971 p.q. for initial 1000 digits of π)

slide-17
SLIDE 17

Continued Fractions

quadratic irrationals – periodic c.f. √ 2 = [1; 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, . . .] − √ 2 = [−2; 1, 1, 2, 2, 2, 2, 2, 2, 2, . . .] √ 3 = [1; 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, . . .]

slide-18
SLIDE 18

Continued Fractions

e1/n and tan(1/n) – patterns in c.f. e = [2; 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, . . .] e1/2 = [1; 1, 1, 1, 5, 1, 1, 9, 1, 1, 13, . . .] tan(1/2) = [0; 1, 1, 4, 1, 8, 1, 12, 1, 16, . . .]

slide-19
SLIDE 19

Continued Fractions

  • ther irrationals – irregular c.f.

π = [3; 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, . . .]

3

√ 2 = [1; 3, 1, 5, 1, 1, 4, 1, 1, 8, 1, 14, . . .]

slide-20
SLIDE 20

Continued Fractions

◮ Gosper, 1972: +, −, ×, / ◮ Newton, 1671:

◮ Brouncker, 1656: π ◮ Ciura, 2007: tan, arctan, exp, log

slide-21
SLIDE 21

Gosper’s algorithm

bihomographic function z(x, y) = axy + bx + cy + d exy + fx + gy + h =

  • a b c d

e f g h

  • (x, y)
slide-22
SLIDE 22

Gosper’s algorithm

special cases of bihomographic function x + y = 0xy + 1x + 1y + 0 0xy + 0x + 0y + 1 x − y = 0xy + 1x − 1y + 0 0xy + 0x + 0y + 1 xy = 1xy + 0x + 0y + 0 0xy + 0x + 0y + 1 x/y = 0xy + 1x + 0y + 0 0xy + 0x + 1y + 0

slide-23
SLIDE 23

Gosper’s algorithm

lazy evaluation of bihomographic function if [redacted] then emit r and change coeffs to

  • e

f g h a − er b − fr c − gr d − hr

  • else if [redacted] then request p from x and

change coeffs to

  • c + ap d + bp a b

g + ep h + fp e f

  • else request q from y and

change coeffs to

  • b + aq a d + cq c

f + eq e h + gq g

slide-24
SLIDE 24

Gosper’s algorithm

+ π [ ] √ 2 [ ]

  • 0 1 1 0

0 0 0 1

  • [ ]
slide-25
SLIDE 25

Gosper’s algorithm

+ π [3] √ 2 [ ]

  • 1 3 0 1

0 1 0 0

  • [ ]

3

slide-26
SLIDE 26

Gosper’s algorithm

+ π [3; 7] √ 2 [ ]

  • 7 22 1 3

7 0 1

  • [ ]

7

slide-27
SLIDE 27

Gosper’s algorithm

+ π [3; 7] √ 2 [1]

  • 29 7 4 1

7 0 1 0

  • [ ]

1

slide-28
SLIDE 28

Gosper’s algorithm

+ π [3; 7] √ 2 [1; 2]

  • 65 29 9 4

14 7 2 1

  • [ ]

2

slide-29
SLIDE 29

Gosper’s algorithm

+ π [3; 7] √ 2 [1; 2]

  • 14 7 2 1

9 1 1 0

  • [4]

4

slide-30
SLIDE 30

Gosper’s algorithm

+ π [3; 7] √ 2 [1; 2, 2]

  • 35 14 5 2

19 9 2 1

  • [4]

2

slide-31
SLIDE 31

Gosper’s algorithm

+ π [3; 7, 15] √ 2 [1; 2, 2]

  • 530 212 35 14

287 136 19 9

  • [4]

15

slide-32
SLIDE 32

Gosper’s algorithm

+ π [3; 7, 15] √ 2 [1; 2, 2]

  • 287 136 19 9

243 76 16 5

  • [4; 1]

1

slide-33
SLIDE 33

Gosper’s algorithm

+ π [3; 7, 15] √ 2 [1; 2, 2]

  • 243 76 16 5

44 60 3 4

  • [4; 1, 1]

1

slide-34
SLIDE 34

Gosper’s algorithm

+ π [3; 7, 15] √ 2 [1; 2, 2, 2]

  • 562 243 37 16

148 44 10 3

  • [4; 1, 1]

2

slide-35
SLIDE 35

Gosper’s algorithm

+ π [3; 7, 15] √ 2 [1; 2, 2, 2, 2]

  • 1367 562 90 37

340 148 23 10

  • [4; 1, 1]

2

slide-36
SLIDE 36

Gosper’s algorithm

+ π [3; 7, 15] √ 2 [1; 2, 2, 2, 2, 2]

  • 3296 13672 217 90

828 340 56 23

  • [4; 1, 1]

2

slide-37
SLIDE 37

Gosper’s algorithm

+ π [3; 7, 15, 1] √ 2 [1; 2, 2, 2, 2, 2]

  • 3513 14572 3296 1367

884 363 828 340

  • [4; 1, 1]

1

slide-38
SLIDE 38

Gosper’s algorithm

+ π [3; 7, 15, 1] √ 2 [1; 2, 2, 2, 2, 2, 2]

  • 8483 35132 7959 3296

2131 884 1996 828

  • [4; 1, 1]

2

slide-39
SLIDE 39

Gosper’s algorithm

+ π [3; 7, 15, 1] √ 2 [1; 2, 2, 2, 2, 2, 2]

  • 2131 884 1996 828

2090 861 1971 812

  • [4; 1, 1, 3]

3

slide-40
SLIDE 40

Gosper’s algorithm

+ π [3; 7, 15, 1] √ 2 [1; 2, 2, 2, 2, 2, 2]

  • 2090 861 1971 812

41 23 25 16

  • [4; 1, 1, 3, 1]

1

slide-41
SLIDE 41

Gosper’s algorithm

+ π [3; 7, 15, 1, . . .] √ 2 [1; 2, 2, 2, 2, 2, 2, . . .]

ad nauseam

[4; 1, 1, 3, 1, . . .]

slide-42
SLIDE 42

Gosper’s algorithm

slide-43
SLIDE 43

Gosper’s algorithm

× √ 2 [ ] √ 2 [ ]

  • 1 0 0 0

0 0 0 1

  • [ ]
slide-44
SLIDE 44

Gosper’s algorithm

× √ 2 [1] √ 2 [ ]

  • 1 0 1 0

0 1 0 0

  • [ ]

1

slide-45
SLIDE 45

Gosper’s algorithm

× √ 2 [1] √ 2 [1]

  • 1 1 1 1

1 0 0 0

  • [ ]

1

slide-46
SLIDE 46

Gosper’s algorithm

× √ 2 [1] √ 2 [1; 2]

  • 3 1 3 1

2 1 0 0

  • [ ]

2

slide-47
SLIDE 47

Gosper’s algorithm

× √ 2 [1; 2] √ 2 [1; 2]

  • 9 3 3 1

4 2 2 1

  • [ ]

2

slide-48
SLIDE 48

Gosper’s algorithm

× √ 2 [1; 2] √ 2 [1; 2, 2]

  • 21 9 7 3

10 4 5 2

  • [ ]

2

slide-49
SLIDE 49

Gosper’s algorithm

× √ 2 [1; 2, 2] √ 2 [1; 2, 2]

  • 49 21 21 9

25 10 10 4

  • [ ]

2

slide-50
SLIDE 50

Gosper’s algorithm

× √ 2 [1; 2, 2] √ 2 [1; 2, 2, 2]

  • 119 49 51 21

60 25 24 10

  • [ ]

2

slide-51
SLIDE 51

Gosper’s algorithm

× √ 2 [1; 2, 2, 2] √ 2 [1; 2, 2, 2]

  • 289 119 119 49

144 60 60 25

  • [ ]

2

slide-52
SLIDE 52

Gosper’s algorithm

× √ 2 [1; 2, 2, 2] √ 2 [1; 2, 2, 2, 2]

  • 697 289 287 119

348 144 145 60

  • [ ]

2

slide-53
SLIDE 53

Gosper’s algorithm

× √ 2 [1; 2, 2, 2, 2] √ 2 [1; 2, 2, 2, 2]

  • 1681 697 697 289

841 348 348 144

  • [ ]

2

slide-54
SLIDE 54

Gosper’s algorithm

× √ 2 [1; 2, 2, 2, 2] √ 2 [1; 2, 2, 2, 2, 2]

  • 4059 1681 1683 697

2030 841 840 348

  • [ ]

2

slide-55
SLIDE 55

Gosper’s algorithm

× √ 2 [1; 2, 2, 2, 2, 2] √ 2 [1; 2, 2, 2, 2, 2]

  • 9801 4059 4059 1681

4900 2030 2030 841

  • [ ]

2

slide-56
SLIDE 56

Gosper’s algorithm

× √ 2 [1; 2, 2, 2, 2, 2] √ 2 [1; 2, 2, 2, 2, 2, 2]

  • 23661 9801 9799 4059

11830 4900 4901 2030

  • [ ]

2

slide-57
SLIDE 57

Gosper’s algorithm

× √ 2 [1; 2, 2, 2, 2, 2, 2] √ 2 [1; 2, 2, 2, 2, 2, 2]

  • 57121 23661 23661 9801

28561 11830 11830 4900

  • [ ]

2

slide-58
SLIDE 58

Gosper’s algorithm

× √ 2 [1; 2, 2, 2, 2, 2, 2] √ 2 [1; 2, 2, 2, 2, 2, 2]

boring, giving up

[2] 2

slide-59
SLIDE 59

Gosper’s algorithm

× √ 2 [1; 2, 2, 2, 2, 2, 2] √ 2 [1; 2, 2, 2, 2, 2, 2]

boring, giving up

[2; ∞] ∞

slide-60
SLIDE 60

Gosper’s algorithm

◮ irrational arguments, rational result –

undecidable

◮ give up after 100 resultless iterations ◮ risk of incorrect result at most

1.5 × 10−31 × number of partial quotients

slide-61
SLIDE 61

What’s needed

slide-62
SLIDE 62

What’s needed

◮ integer bignums ◮ lazy evaluation ◮ garbage collection

slide-63
SLIDE 63

Python has got it all

◮ long type ◮ generators ◮ garbage collection

slide-64
SLIDE 64

Implementation

◮ source generators: c.f. for rational numbers,

canned sequences of p.q., and π

◮ intermediate generators: bihomographic

function, homographic function, exp, log, tan, atan, and sqrt

◮ sinks: type conversions, __cmp__, __str__,

and __repr__

slide-65
SLIDE 65

Implementation

◮ functions and methods return (chained)

generators

  • 1 0

0 2

  • tan
  • 0 2 0 0

1 0 0 1

  • sin

◮ calling a sink makes it request p.q. from its

inputs, they request p.q. from theirs, etc.

◮ generated partial quotients are cached

slide-66
SLIDE 66

Implementation

◮ cf.cf ◮ http://tnij.org/continued-fractions/ ◮ 2000 lines of Python ◮ function parity with math

slide-67
SLIDE 67

Like a Computer Algebra System

>>> (sqrt(2) - 1) * (sqrt(2) + 1) 1.

slide-68
SLIDE 68

Like a Computer Algebra System

>>> sqrt(5 + 2*sqrt(6)) == sqrt(2) + sqrt(3) True

slide-69
SLIDE 69

Like a Computer Algebra System

>>> [sin(x)**2 + cos(x)**2 for x in ... [pi * random.random(), ... pi * random.random(), ... pi * random.random()]] [1., 1., 1.]

slide-70
SLIDE 70

Like a Computer Algebra System

>>> [cos(3*x)/cos(x) == ... cos(x)**2 - 3*sin(x)**2 for x in ... [pi * random.random(), ... pi * random.random(), ... pi * random.random()]] [True, True, True]

slide-71
SLIDE 71

Like a Computer Algebra System

>>> [x == x + cf(10)**-1000 for x in ... [cf(random.random()), ... cf(random.random()), ... cf(random.random())]] [False, False, False]

slide-72
SLIDE 72

. . . But Not Quite

>>> sqrt(2) == sqrt(2) + cf(10)**-1000 True

slide-73
SLIDE 73

Possible Applications

◮ chaos theory ◮ computational geometry ◮ computational number theory ◮ educational applications

slide-74
SLIDE 74

Live Demo