Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CLEVE’S CORNER Computing π By Cleve Moler Computing hundreds, or trillions, of digits of π has long been used to stress hardware, validate software, and establish bragging rights. MATLAB® implementations of the most widely used algorithms for computing π illustrate two different styles of arithmetic available in Symbolic Math Toolbox™: exact rational arithmetic and variable-precision floating-point arithmetic. L ast August, two computer hobbyists, Alexander Yee and Shigeru Kondo, announced that they had set a world record by computing 5 trillion digits of π (Figure 1). Their computation took 90 days on a “homebrew” PC. Yee is now a graduate student at the University of Illinois. His computing software, “y-cruncher,” began as a class project at Palo Alto High School. Kondo is a systems engineer who assembles personal computers at his home in Japan. The machine that Cleve's Corner: Computing π he built for this project (Figure 2) has two Intel® Xeon® processors Computing hundreds, or trillions, of digits of π has long been used to stress hardware, validate software, and establish braggingof rights. of the most used algorithms with a total 12 MATLAB cores,implementations 96 gigabytes ofwidely RAM, and 20for external hard computing π illustrate two different styles of arithmetic available in Symbolic Math Toolbox: exact rational variable-precisioncapacity floating-point of arithmetic. disksarithmetic with aandcombined 32 terabytes. Last August, two computer hobbyists, Alexander Yee and Shigeru Kondo, announced that they had The computation of 5 trillion digits is a huge processing task set a world record by computing five trillion digits of π. Their computation took 90 days on a homebrew PC. Yee is now a graduate student at the University of Illinois. His computing software, “y-cruncher”, where the time required for the data movement is just as important began as a class project at Palo Alto High School. Kondo is a systems engineer who assembles personal computers at his home in Japan. The machine that he built for this project (Figure 1) has two Intel Xeons as the time required for the arithmetic. with a total of 12 cores, 96 gigabytes of RAM, and 20 external hard disks with a combined capacity of 32 terabytes. y-cruncher uses several different formulas for computation and The computation of five trillion digits is a huge processing task where the time required for the data verification. The primary toolforisthea arithmetic. formidable formula discovered by movement is just as important as the time required y-cruncher usesGregory several different formulas for computation and verification. The primary tool is a David and Chudnovsky in 1987: formidable formula discovered by David and Gregory Chudnovsky in 1987. The Chudnovsky brothers, who were the subject of two fascinating articles in the New Yorker and a NOVA documentary, have also built “home brew” computers in their apartment in Manhattan. The massively parallel machine that they assembled from commodity parts in the 1990s was considered a supercomputer at the time. The Chudnovsky brothers, who were the subject of two fascinating articles in The New Yorker and a NOVA documentary, have also High-Precision Arithmetic in Symbolic Math Toolbox built homebrew computers in their apartment in Manhattan. The Symbolic Math Toolbox provides two styles of high-precision arithmetic, sym and vpa. massively parallel machine that they assembled from commodity The sym function initiates exact rational arithmetic: Arithmetic quantities are represented by quotients large integers. length increasesaassupercomputer necessary, limited only by computer parts and in roots theof1990s wasInteger considered at the time. time and storage requirements. Quotients and roots are not computed unless the result is an exact integer. High-Precision Arithmetic in Symbolic Math Toolbox Symbolic Math Toolbox provides tworoundoff styles ofat the high-precision arithArithmetic operations, including divisions and roots, can involve errors level of the specified accuracy.For example, the MATLAB statement metic: sym and vpa. p = rat(pi) The sym function initiates exact rational arithmetic: Arithmetic quantities are represented by quotients and roots of large integers. Integer length increases as necessary, limited only by computer time and storage requirements. Quotients and roots are not computed unless the result is an exact integer. Figure 1. A log plot of the data from the Wikipedia article “Chronology of computation of pi” showing how the world record for number of digits has increased over the last 60 years. A linear fit to the logarithm reveals a Moore’s Law phenomenon. The number of digits doubles about every 22.5 months. The vpa function initiates variable-precision floating-point arithmetic: Arithmetic quantities are represented by decimal fractions of a specified length together with a power-of-10 exponent. Arithmetic operations, including divisions and roots, can involve roundoff errors at the level of the specified accuracy. For example, the MATLAB statement The vpa function initiates variable-precision floating-point arithmetic: Arithmetic quantities are represented by decimal fractions of a specified length together with a power-of-ten exponent. 1 p = rat(pi) produces three terms in the continued fraction approximation of π p = 3 + 1/(7 + 1/16) Then the statement sym(p) yields the rational expression 355/113 which is an attractive alternative to the familiar 22/7. On the other hand, the statement vpa(p,8) produces the 8-digit floating-point value produces three terms in the continued fraction approximation of π p = 3 + 1/(7 + 1/16) 3.1415929 Then the statement sym(p) The Chudnovsky Algorithm Our MATLAB programs implement three of the hundreds of possible 355/113 algorithms for computing π. OnThe Chudnovsky which is an attractive alternative to the familiar 22/7. the other hand, the statementalgorithm (Figure 3) vpa(p,8) uses exact rational arithmetic until the last step. Each term in the produces the 8-digit floating-point value Chudnovsky series provides about 14 decimal digits, so to obtain d 3.1415929 digits requires [d/14] terms. For example, the statement yields the rational expression Figure 2. Shigeru Kondo’s homebrew PC, which currently holds the world record for computing digits of π. function P = chud _ pi(d) % CHUD _ PI Chudnovsky algorithm for pi. The Chudnovsky Algorithm % chud _ pi(d) produces d decimal digits. Our MATLAB programs implement three of the hundreds of possible algorithms for computing π. The Chudnovsky algorithm (Figure 2) uses exact rational arithmetic until the last step. Each term in the chud_pi(42) Chudnovsky series provides about 14 decimal digits, so to obtain d digits requires terms. For example, the statement k = sym(0); chud_pi(42) uses exact rational computation to produce uses exact rational computation to produce s = sym(0); sig = sym(1); n = ceil(d/14); for j = 1:n s = s + sig * prod(3*k+1:6*k)/prod(1:k)^3 * ... and then the final step uses variable-precision floating-point arithmetic with 42 digits to produce and then the final step uses variable-precision floating-point arithmetic 3.14159265358979323846264338327950288419717 with 42 digits to the produce One interesting fact about digits of π is revealed by chud_pi(775) or 3.14159265358979323846264338327950288419717 vpa(pi,775) One interesting fact about the digits of π is revealed by chud_pi(775) (13591409+545140134*k) / 640320^(3*k+3/2); k = k+1; sig = -sig; end S = 1/(12*s); P = vpa(S,d); Figure 3. MATLAB implementation of the Chudnovsky algorithm, using sym to initiate exact rational arithmetic. or vpa(pi,775) These both produce 775 digits, beginning and ending with 3.141592653589793238 ...... 707211349999998372978 We see not only that chud_pi is working correctly but also that there are six 9s in a row around position 765 in the decimal expansion of π (Figure 4). Computing 5000 digits with chud_pi requires 358 terms and takes about 20 seconds on my laptop. The final symbolic expression contains 12,425 characters. 2 = = bn+1 = �𝑎𝑎𝑛𝑛 𝑏𝑏𝑛𝑛 = until a== n and bn agree to a desired accuracy. For example, arithmetic mean ofaccuracy. 1 and 9 isFor 5, the until andthe bnagree agree to aa desired desired example, the arithm until aannand and bmean to accuracy. Forexample, example, the arithm nagree geometric is 3, and the agm is 3.9362. until a b to a desired accuracy. For the arithme n n geometric mean is 3, and the agm is 3.9362. geometric mean is 3, and the agm is 3.9362. geometric mean isproperties 3, and theof agm 3.9362. The powerful theisagm lie not just in The powerful properties ofresults the agm lie not just in the final value theThe final value but also in the generated powerfulproperties propertiesofofthe theagm agmlielienot notjust justininthe the final valu The powerful final value along the way. Computing isisjust one compu along theway. way.Computing Computingπππis justone oneexample. example.ItItItinvolves along the just example. involves comp along the way. Computing π is just one example. It involves comput involves computing ( ) (( )) M = agm(1,1/√2) During the iteration, keep track of the changes, During the iteration, keep track ofthe thechanges, changes, During the iteration, keep track During the iteration, keep track of track theof changes, During the iteration, keep of the changes, Duringthe theiteration, iteration,keep keeptrack trackofofthe thechanges, changes, During cn = an+1 - an an+1 - an c ncn==an+1 Finally, letlet- an Finally, Finally, let Finally, let Finally, Finally,1let let Finally, let 𝑛𝑛 2 S = 1 – ∑∞ 𝑛𝑛=0 2 𝑐𝑐𝑛𝑛 14 ∞ 𝑛𝑛 𝑛𝑛 2 2 ∞ ∑ S = – 𝑐𝑐𝑛𝑛 2 S = 4– ∑𝑛𝑛=0 𝑛𝑛=02 𝑐𝑐𝑛𝑛 Then4 Then Then Then Then Then Then 𝑀𝑀2 2 𝜋𝜋 = 𝑀𝑀𝑀𝑀2 These both produce 775 digits, beginning and ending with 𝜋𝜋𝜋𝜋== 𝑆𝑆𝑆𝑆 Our MATLAB 𝑆𝑆 function for the agm algorithm (Figure 3) uses variable- precision floating-point arithmetic from the very beginning. If we used symbolic rational arithmetic, we would end up with a Our MATLAB function the agm algorithm (Figure uses variableprecision floating-point 3.141592653589793238 ...... 707211349999998372978 Our MATLAB function forfor the agm algorithm (Figure 3) 3) uses precision floating-point nested sequence of square roots. After just two iterations wevariablewould have arithmetic from very beginning. If we used symbolic rational arithmetic, would end with arithmetic from thethe very beginning. If we used symbolic rational arithmetic, wewe would end upup with a a nested sequence square roots. After two iterations would have nested sequence of of square roots. After justjust two iterations wewe would have 2 Our MATLAB function for the agm algorithm (Figure 5) uses variable-precision floating-point arithmetic from the very beginning. We see not only that chud_pi is working correctly but also that there are six 9s�in around √2a row 22 1 If wefunction usedforsymbolic rational we would end up with a nest√2 2 3)arithmetic, Our MATLAB the agm algorithm (Figure uses√2 variable- precision floating-point √2 position 765 in the decimal expansion of π (Figure 4). arithmetic ⎛ �� + +1 ⎞ from the very beginning. If we used symbolic end up with a Figure 4. 10,000 digits of π, visualized in MATLAB. Can you see the six √2 8arithmetic, 4⎞we would 1⎞ √2 222+rational ⎛roots. ed sequence of After square After just two iterations we would have ⎛two + + nested sequence of square roots. just iterations we would have + 22 consecutive 9s in the eighth row? 88 44 ⎝ 20 seconds on ⎠ my Computing five thousand digits with chud_pi requires 358 terms and takes about ⎝⎝ ⎠⎠ laptop. The final symbolic expression contains 12,425 characters. 2 �√2 The Algebraic-Geometric Mean Algorithm TheAlgebraic-Geometric Chudnovsky formula is a power series: Each new term in the partial The Mean Algorithm 2 22 1 √2 √2 �� √2 1 2 + 1⎞ √2 − �√2 −112�2 − 2 ⎛√2 − 11 4⎞ 4− � √28 − 222 +11 √24 − 2� − 2⎛⎛ ⎞ − � 4− 2 � −2 8− 22 + 44 8 44 4 2 ⎝ ⎠ ⎝⎝ ⎠⎠ to 𝜋𝜋. The The decimal value of this monstrosity is 3.14168, so it is not yet a very good approximation These both produce 775 digits, beginning endingof withaccuracy. The algebraic-geometric sum adds a fixed number ofand digits 3.141592653589793238 ...... 707211349999998372978 mean algorithm is completely different; it is quadratically convergent: The Chudnovsky formula is a power series: Each new term in the partial sumis 3.14168, adds ait isfixed number of to 𝜋𝜋. The complexity doubles with each iteration. The decimal value monstrosity a very good approximation The decimal value of of thisthis monstrosity is 3.14168, so so it is notnot yetyet a very good approximation to 𝜋𝜋. The We see not only that chud_pi is working correctly but also that there are six 9s in a row around complexity doubles with each iteration. Each of new iteration doubles the number of correct digits. Thealgorithm agm al- complexity digits accuracy. The algebraic-geometric mean is completely different; it is quadratically doubles with each iteration. Such exact symbolic expressions are computationally unwieldy and inefficient. With vpa, however, the position 765 in the decimal expansion of π (Figure 4). quadratic convergence can be seen in the successive unwieldy iterates: and inefficient. With vpa, however, the Such exact symbolic expressions computationally gorithm has a Each long history dating backdoubles to Gauss and Such exact symbolic expressions areare computationally unwieldy and inefficient. convergent: new iteration theLegendre. numberItsofabilcorrect digits. The agm algorithm has a longWith vpa, however, the Computing five thousand digits with chud_pi requires 358 terms and takes about 20 seconds on my quadratic convergence seen in the successive iterates: quadratic convergence cancan bebe seen in the successive iterates: 3.2 laptop. The final symbolic expression contains 12,425independently characters. ity to compute π was discovered by Richard Brent and history dating back to Gauss and Legendre. Its ability toThecompute π was discovered independently by 3.142 3.2 decimal3.2 value of this monstrosity is 3.14168, so it is not yet a very good approximation to . The The Algebraic-Geometric Mean Algorithm 3.142 Eugene Salamin in 1975. complexity doubles each iteration. Thewith decimal value of this monstrosity is 3.14168, so it is not yet a very 3.142 Richard Brent and Eugene Salamin in 1975. The Chudnovsky formula is a power of series: Eachnumbers, new term in the(a+b)/2, partial sum adds fixed number of Such exactgood symbolicapproximation expressions are computationally unwieldy and inefficient.doubles With vpa,with however, the iteration. The arithmetic mean two is aalways greater to π. The complexity each digits of accuracy. The algebraic-geometric mean algorithm is completely different; it is quadratically quadratic convergence can be seen in the successive iterates: convergent: Eachgeometric new iteration doubles the number ofTheir correct digits. The agm algorithm has a long mean, than their mean, √ab. arithmetic-geometric Such exact symbolic expressions are computationally unwieldy The arithmetic meanItsof two numbers, (a+b)/2, greater than their geometric mean, . 3.2 history dating back to Gauss and Legendre. ability to compute π was discovered independentlyis byalways or agm(a,b), is computed 3.142 and inefficient. With vpa, however, the quadratic convergence can Richard Brent and Eugene Salamin in 1975.by repeatedly taking arithmetic and geoTheir arithmetic-geometric mean, or agm(a,b), is computed by repeatedly taking arithmetic and 3.1415927 metric means. with a = a and b = b, iterate be seen in the successive iterates: 3.141592653589793 The arithmetic meanStarting of two numbers, (a+b)/2, is always greater than their geometric mean, √𝑎𝑎𝑎𝑎. 0 0 geometric means. Starting with a = a and b = b, iterate 0 Their arithmetic-geometric mean, or agm(a,b), is computed by 0repeatedly taking arithmetic and geometric means. Starting with a0 = a and b0 = b, iterate 3.1415926535897932384626433832795 3.141592653589793238462643383279502884197169399375105820974944592 3.2 3.142 The quadratic convergence makes agm very fast. To compute 100,000 digits requires only 17 steps 2 and about 2.5 seconds on my laptop. 3.1415927 bn+1 == �𝑎𝑎𝑛𝑛 𝑏𝑏𝑛𝑛 The BBP Formula 3.141592653589793 Yee spot-checked his computation with the BBP formula. This formula, named after David Bailey, 3.1415926535897932384626433832795 Jonathan Borwein, and Simon Plouffe and discovered by Plouffe in 1995, is a power series involving until an and bn agree to a desired accuracy. For inverse powers of 16. until a b agree to a desired accuracy. For example, the arithmetic mean of 1 and 9 is 5, the until a and b agree to a desired accuracy. For example, the arith3.1415926535897932…79502884197169399375105820974944592 n nand n n arithmetic mean of 1 and 9 is 5, the example, the geometric mean is 3, and the agm is 3.9362. metic mean of 1 andis9 3, is 5, thethe geometric is 3, and the agm geometric mean and agm ismean 3.9362. is 3.9362. The sixth entry in this output has 64 correct digits, and the tenth Thepowerful properties ofagm thelieagm agm lie not not just invalue powerful properties the lie just in the finalhasvalue 1024.but also in the results generated The properties of theof not just in the final thealso final value butgenerated also inπthe results generated along theinway. Computing is just one example. but the results along the way. ComputingItπinvolves is just computing The quadratic convergence makes agm very fast. To compute 100,000 along the way. Computing π is just one example. It digits requires only 17 steps and about 2.5 seconds on my laptop. one example. It involves computing an+1 == 𝑎𝑎𝑛𝑛 + 𝑏𝑏𝑛𝑛 involves computing ( ) M =the agm(1,1/√2) During iteration, keep track of the changes, 3 Finally, let The tenth entry is this output would have 1024 correct digits. function P = agm _ pi(d) % AGM _ PI Arithmetic-geometric mean for pi. % agm _ pi(d) produces d decimal digits. Our MATLAB function for the agm algorithm (Figure 3) uses variable- precision floating-point arithmetic from the very beginning. If we used symbolic rational arithmetic, we would end up with a digits(d) nested sequence of square roots. After just two iterations we would have a = vpa(1,d); b = 1/sqrt(vpa(2,d)); s = 1/vpa(4,d); p = 1; n = ceil(log2(d)); Richard Preston, “The Mountains of Pi,” The New Yorker, March 2, 1992. www.newyorker.com/archive/1992/03/02/1992_03_02_036_TNY_ CARDS_000362534 Richard Preston, “Capturing the Unicorn,” The New Yorker, April 11, 2005. www.newyorker.com/archive/2005/04/11/050411fa_fact for k = 1:n c = (a+b)/2; b = sqrt(a*b); s = s - p*(c-a)^2; p = 2*p; The decimal of this monstrosity is 3.14168, so it is not yet a very good approximation to . The a value = c; complexity doubles with each iteration. end Such exact symbolic expressions are computationally unwieldy and inefficient. With vpa, however, the P = a^2/s; quadratic convergence can be seen in the successive iterates: 3.2 3.142 5. MATLAB implementation of the arithmetic-geometric mean Figure 3.1415927 algorithm, using digits and vpa to initiate variable-precision floating3.141592653589793 3.1415926535897932384626433832795 point arithmetic. 3.141592653589793238462643383279502884197169399375105820974944592 The tenth entry is this output would have 1024 correct digits. The BBP Formula The quadratic convergence makes agm very fast. To compute 100,000 digits requires only 17 steps and about 2.5 seconds on myhis laptop. Yee spot-checked computation with the BBP formula. This forThe BBP Formula mula, named after David Bailey, Jonathan Borwein, and Simon Plouffe and discovered bywith Plouffe 1995,This is aformula, power series Yee spot-checked his computation the BBPin formula. named afterinvolving David Bailey, Jonathan Borwein, and Simon Plouffe and discovered by Plouffe in 1995, is a power series involving inverse powers of 16: inverse powers of 16. The remarkable property of the BBP formula is that it allows the computation of several consecutive hexadecimal digits in the base 16 expansion of π without computing the earlier digits and without using extra-precision arithmetic. A formula for the hex digits starting at position d is obtained by simply multiplying this formula by 16d and then taking the fractional part. I can’t resist concluding that BBP can be used to compute just a piece of π. Our BBP program is too long to be included here, but all three programs, chudovsky_pi, agm_pi, and bbp_pi, are available for download on MATLAB Central. ■ Published 2011 91954v00 References Alexander J. Yee and Shigeru Kondo, “5 Trillion Digits of Pi,” August 2, 2010. www.numberworld.org/misc_runs/pi-5t/details.html “Brothers Chudnovsky,” PBS, NOVA, July 26, 2005. www.pbs.org/wgbh/nova/physics/chudnovsky-math.html David Bailey, Jonathan Borwein, Peter Borwein, and Simon Plouffe, “The Quest for Pi,” Mathematical Intelligencer 19, pp. 50-57, 1997. http://crd.lbl.gov/~dhbailey/dhbpapers/pi-quest.pdf Jonathan M. Borwein and David H. Bailey, Mathematics by Experiment: Plausible Reasoning in the 21st Century, AK Peters, Natick, MA, 2008. “Chronology of computation of pi,” Wikipedia, March 9, 2011. http://en.wikipedia.org/wiki/Chronology_of_computation_of_π Learn More Download: MATLAB Implementations of the Chudnovsky, agm, and BBP Algorithms mathworks.com/computing-pi Cleve's Corner Collection mathworks.com/clevescorner mathworks.com © 2011 The MathWorks, Inc. MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand names may be trademarks or registered trademarks of their respective holders. 4