algorithms using real numbers continued
play

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


  1. Algorithms using “real” numbers (continued) Noter ch.4

  2. Algorithms using “real” numbers • Representable numbers • Rounding/truncation • IEEE standard and Java • Summation order • Newton iteration • More iteration • Formula rewriting

  3. 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 � � � �

  4. Formula rewriting From Pythagoras: Computing � �� from � � : � � � � � � � � ��� 2 � � � � 1 � � � � 1 2 Radius = 1 � �� Leading to � � � 1 � 4 � � �� � �� � 2 � � � and �� � � � lim �→� � � , for � � � �

  5. Result from execution QUIZ � � �� � �� 4 � � �� 4 � � �� 2 � 3 1 3 Pseudocode for computing approximation to � 6 1.732 .2680 .5177 3.106 12 1.932 .06800 .2607 3.128 � � 6 ; � � � 1 ; � � � 3 ; 24 1.983 .01700 .1304 3.129 do { 48 1.995 .005000 .07071 3.394 4 � � �� ; � �� � 2 � 96 1.998 .002000 .04473 4.296 192 1.999 .001000 .03162 6.070 � �� � �� �� ; � � 2� ; 384 1.999 .001000 .03162 12.14 } while ( � �� � � � ) ⋮ ⋮ ⋮ ⋮ ⋮ 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 ) 4 � � �� ; leads to catastrophic cancellation 3. Stop criterium is ok, but � �� � 2 � 4. Some other reason 5. I don’t know

  6. 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 � � � � � � � � � � � � � � � � � � � � � � 4 � 4 � � �� � � 4 � � �� � � �� � 2 � 4 � � �� � 2 � 4 � � �� 2 � Catastrophic cancellation No Cancellation

  7. Result from execution QUIZ Catastrophic � � �� � �� 4 � � �� 4 � � �� 2 � cancellation 2 3 1 3 Pseudocode for computing approximation to � 6 1.732 3.732 .5175 3.105 12 1.932 3.932 .2609 3.130 � � 6 ; � � � 1 ; � � � 3 ; 24 1.982 3.982 .1307 3.136 do { 48 1.995 3.995 .06541 3.139 ? 4 � � �� ; � �� � � � / 2 � 96 1.998 3.998 .03272 3.141 192 1.999 3.999 .01636 3.141 � �� � �� �� ; � � 2� ; stop } while ( � �� � � � ) ⋮ ⋮ ⋮ ⋮ ⋮ 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

  8. • 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)

  9. • Try java double using � �� � � � / 2 � 4 � � �� ; • The stopping criteria while ( � �� � � � ) makes it stop, and the value is a good approximation of �

  10. 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: � � � � � � � � � � � � � � � � � � � � � �

  11. QUIZ ? 2000 2000 Catastrophic cancellation 3 30 2000 � � 30 � in the number system F(10, 4, − 99, 99) We want to compute 2000 � 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. �� �� �/� � 1 � �/� � 4. � 1 � 5. None of these 6. I don’t know

  12. 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.

  13. Computing Some algorithms are faster than others

  14. 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

  15. Buffon’s needle (Le Clerc, 1777) � • Throw some needles of length � on 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 � of a needle landing across a gap is � � � #needles #crossings • Experiment: ������ • � � ����� � 3.147663175 Need 10 �� iterations to compute � digits • Applet simulator at • To get 1 additional digit throw 100 times http://www.ventrella.com/Buffon/index.html as many needles (no guarantees – when using randomness)

  16. 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 � • Need � � iterations to compute � digits To get 1 additional digit make � 1 more • http://www.mathsisgoodforyou.com/applets/calculatingPi.htm iterations

  17. Inscribed/circumscribed polygon � � � 2 3 � � � 3 � ��� � 2� � � � � � � � � � ��� � � ��� � � Need � � iterations to compute � digits • To get 1 additional digit make � 1 more • iterations

  18. Arithmetic-geometric mean (Gauss-Legendre; Salamin-Brent, 1976) � � � 1 � � � 1 � � � 1 2 2 � � � � ��� � � ��� 2 � � � � ��� � ��� http://www.apfloat.org/apfloat_java/applet/pi.html � � � � �� � � � � � � � � ��� � 2 � � � � � � 2� �� � � � � � 1 3.187672642712109 � � lim �→� � � 2 3.141680293297657 • Need log � � iterations to compute � digits 3 3.14159265389546 To double the number of digits make 1 • more iteration

Recommend


More recommend