Algorithms using real numbers (continued) Noter ch.4 Algorithms - - PowerPoint PPT Presentation
Algorithms using real numbers (continued) Noter ch.4 Algorithms - - PowerPoint PPT Presentation
Algorithms using real numbers (continued) Noter ch.4 Algorithms using real numbers Representable numbers Rounding/truncation IEEE standard and Java Summation order Newton iteration More iteration
Algorithms using “real” numbers
- Representable numbers
- Rounding/truncation
- IEEE standard and Java
- Summation order
- Newton iteration
- More iteration
- Formula rewriting
Formula rewriting
- Example: computing by convergent series
- lim
→ , for being half the circumference of a
regular -gon inscribed in the unit circle. 1 .52 3 3.1
- Monotonicity: ⋯ ⋯
- Enough to compute side length , since
Formula rewriting
Computing from : From Pythagoras:
- 2
- 2
- 1 1
Leading to 1 2 4 and lim
→ ,
for
- Radius = 1
Pseudocode for computing approximation to
QUIZ
Why do we see this behaviour?
- 1. Stop criterium is bad, we should use while ( )
- 2. Stop criterium is bad, we should use while ( 10)
- 3. Stop criterium is ok, but
2 4 ; leads to catastrophic cancellation
- 4. Some other reason
- 5. I don’t know
- 4
2 4
- 3
1 3 6 1.732 .2680 .5177 3.106 12 1.932 .06800 .2607 3.128 24 1.983 .01700 .1304 3.129 48 1.995 .005000 .07071 3.394 96 1.998 .002000 .04473 4.296 192 1.999 .001000 .03162 6.070 384 1.999 .001000 .03162 12.14 ⋮ ⋮ ⋮ ⋮ ⋮ 6; 1; 3; do { 2 4 ; ; 2; } while ( ) Result from execution
Formula rewriting
- What goes wrong?
– It seems →∞ rather than → – 4 is so close to 2 that the subsequent subtraction 2 4 leads to catastrophic cancellation
- Rewrite formula
2 4 4 4 2 4
- 2
4
Catastrophic cancellation No Cancellation
Pseudocode for computing approximation to
QUIZ
What is the result of execution in F(10,4,-99,99)?
- 1. Infinite loop (execution never stops)
- 2. Stops with Infinity
- 3. Stops with incorrect approximation 3.128
- 4. Stops with correct approximation 3.141
- 5. Something else happens
- 6. I don’t know
Catastrophic cancellation 2
- 4
2 4
- 3
1 3 6 1.732 3.732 .5175 3.105 12 1.932 3.932 .2609 3.130 24 1.982 3.982 .1307 3.136 48 1.995 3.995 .06541 3.139 96 1.998 3.998 .03272 3.141 192 1.999 3.999 .01636 3.141 stop ⋮ ⋮ ⋮ ⋮ ⋮ 6; 1; 3; do { / 2 4 ; ; 2; } while ( ) Result from execution
?
- Try java double using
2 4
- The stopping criteria
while ( ) makes it stop, but only 8 out of 17 digits correct!
- More iterations does not
improve the approximation! (on the contrary)
- Try java double using
/ 2 4 ;
- The stopping criteria
while ( ) makes it stop, and the value is a good approximation of
Catastrophic cancellation
- Problem
– in some computation two terms of approximately identical size are subtracted: – where
- Warning:
– E will be computed with few or no significant digits – results are corrupted and loops may not terminate
- Advice:
– rewrite formula to avoid catastrophic cancellation – sometimes the following simple reformulation is useful:
QUIZ
We want to compute 2000 2000 30 in the number system F(10, 4,−99, 99) with truncation. The result is 1.000. We want to rewrite the underlying expression to minimize (catastrophic) cancellation. Which expression is expected to give a better result (and also mathematically equivalent)? 1.
- 2.
- 3.
- /
- 4. 1
1 /
- 5. None of these
- 6. I don’t know
Catastrophic cancellation 3 2000 2000 30 ?
Summary of advice
- Add terms in increasing order. If possible add terms of
equal size only.
- Don’t test whether two limited precision numbers are
- equal. Test whether numbers are sufficiently close. The
meaning of “sufficiently close” depends on the context.
- Mathematical expressions should be reformulated to
avoid catastrophic cancellation.
Computing
Some algorithms are faster than
- thers
Computing
- Buffon’s needle (Le Clerc, 1777)
- In/circumscribed polygon (Archimedes,
287-212, B.C.)
- Arithmetic-geometric mean (Salamin-Brent
1976)
http://crd.lbl.gov/~dhbailey/dhbpapers/pi-quest.pdf
Buffon’s needle
(Le Clerc, 1777)
Applet simulator at http://www.ventrella.com/Buffon/index.html
- Throw some needles of length
- n a
floor covered with planks of width 1
- Count those needles that land
across a crack between two planks
- Assuming needles are rotated
uniformly randomly, the probability
- f a needle landing across a gap is
- #needles
#crossings
- Experiment:
- 3.147663175
- Need 10 iterations to compute digits
- To get 1 additional digit throw 100 times
as many needles (no guarantees – when using randomness)
Inscribed/circumscribed polygon
(Archimedes, 287-212 B.C.)
- Let (and ) be length of
circumscribed (and inscribed) regular 6 ⋅ 2 -gons relative to a circle of diameter 1
- Mutually recursive formulae:
2 3 3 2
- Archimedes obtained
- using 4
- Zǔ Chōngzhī (429-500) obtained
- using 11
- Ludolph van Ceulen (1540-1610)
calculated 34 digits of
http://www.mathsisgoodforyou.com/applets/calculatingPi.htm
- Need iterations to compute digits
- To get 1 additional digit make 1 more
iterations
Inscribed/circumscribed polygon
2 3 3 2
- Need iterations to compute digits
- To get 1 additional digit make 1 more
iterations
Arithmetic-geometric mean
(Gauss-Legendre; Salamin-Brent, 1976)
1 1 2 1 2 2
- 2
2
- lim
→
- Need log iterations to compute digits
- To double the number of digits make 1
more iteration
http://www.apfloat.org/apfloat_java/applet/pi.html
- 1
3.187672642712109 2 3.141680293297657 3 3.14159265389546