Viète à grande Vitesse or Accelerated Viète (kind of)
|
06-23-2024, 06:00 PM
Post: #21
|
|||
|
|||
RE: Viète à grande Vitesse or Accelerated Viète (kind of)
(06-23-2024 03:00 AM)Gerson W. Barbosa Wrote: \(\pi \approx \pi{'} \left[1+\frac{1}{6}\left(\frac{\pi{'}}{2^{n+1} }\right)^2\right]\left[1+\frac{3}{40}\left(\frac{\pi{'}}{2^{n+1} }\right)^4\right]\left[1+\frac{9}{280}\left(\frac{\pi{'}}{2^{n+1} }\right)^6\right] \) Correction factor had error of O(z^8), where z=pi'/2^(n+1) If we use Horner's scheme, we might as well use correct z^8 coefficient. It made code shorter (less terms), also more accurate, error = O(z^10) XCas> expand( (1+1/6*z^2) * (1+3/40*z^4) * (1+9/280*z^6) ) 1 + 1/6*z^2 + 3/40*z^4 + 5/112*z^6 + 3/560*z^8 + 27/11200*z^10 + 9/22400*z^12 Replaced this with asin(z)/z, upto z^8 only XCas> expand( 1+1/6*z^2 * (1+3²/(4*5)*z^2 * (1+5²/(6*7)*z^2 * (1+7²/(8*9)*z^2))) ) 1 +1/6*z^2 + 3/40*z^4 + 5/112*z^6 + 35/1152*z^8 To make code stack-only, we can't store n, and had to recover 2^(n+1) factor. Code was using 2^(n+1) = IP((355/113)/z), but this will fail if n is too big. Instead, I use log to recover n from z (Yes, this is stupid, all we need is to store n) z = pi' / 2^(n+1) log2(z) = log2(pi') - (n+1) -(n+1) ≈ log2(z) - 1.6515 -(n+1) = IP(log2(z)) - 2 n=1 --> IP(log2(z)) - 2 = IP(-0.50) - 2 = -2 n=9 --> IP(log2(z)) - 2 = IP(-8.35) - 2 = -10 n=99 --> IP(log2(z)) - 2 = IP(-98.35) - 2 = -100 n=999 --> IP(log2(z)) - 2 = IP(-998.35) - 2 = -1000 ... Code: 00 { 79-Byte Prgm } 1 XEQ "Vh" --> 3.138316846974900201597524772067293 10 XEQ "Vh" --> 3.141592653589793238462643383274432 11 XEQ "Vh" --> 3.141592653589793238462643383279499 12 XEQ "Vh" --> 3.141592653589793238462643383279504 |
|||
06-24-2024, 09:42 PM
Post: #22
|
|||
|
|||
RE: Viète à grande Vitesse or Accelerated Viète (kind of)
(06-23-2024 06:00 PM)Albert Chan Wrote:(06-23-2024 03:00 AM)Gerson W. Barbosa Wrote: \(\pi \approx \pi{'} \left[1+\frac{1}{6}\left(\frac{\pi{'}}{2^{n+1} }\right)^2\right]\left[1+\frac{3}{40}\left(\frac{\pi{'}}{2^{n+1} }\right)^4\right]\left[1+\frac{9}{280}\left(\frac{\pi{'}}{2^{n+1} }\right)^6\right] \) I could have used up to four correction terms in the format I’ve been using, but I have used only three as I wanted to compare the results with those of another program using COMB, as that one is limited to only three factors. But my program would be even longer, so it’s great to know there’s a way to do it more compactly. (06-23-2024 06:00 PM)Albert Chan Wrote: To make code stack-only, we can't store n, and had to recover 2^(n+1) factor. IP((355/113)/z) works for the small n we would need for maximum accuracy, 14 or 15 in my example with three factors. This starts to fail at n=23. Also, if more factors are used even less iterations are required (11 or 12 in your example). (06-23-2024 06:00 PM)Albert Chan Wrote: I don’t understand why one would want to run this for n=10000+ on Free 42. You code would be even more compact if is limited to 20 or so. Anyway, if that is desired you can save at least one more step. They say engineers round \(\pi\) to 4 just in case. Being an engineer, that’s exactly what I have done at step 42 :-) Code:
|
|||
06-25-2024, 12:41 AM
Post: #23
|
|||
|
|||
RE: Viète à grande Vitesse or Accelerated Viète (kind of)
(06-24-2024 09:42 PM)Gerson W. Barbosa Wrote: They say engineers round \(\pi\) to 4 just in case. pi = 4 ... and it actually work! Turns out I don't need to recover 2^(n+1). It was already there! This version is going for simplicity, which turns out pretty good. ε = asin(z)/z - 1 ≈ z^2/6 / (1 - 9/20*z^2) = 1 / (6/z^2 - 2.7) Code: 00 { 38-Byte Prgm } 1 --> 3.132559073643629893044600829969869 17 --> 3.141592653589793238462643383279402 18 --> 3.141592653589793238462643383279501 19 --> 3.141592653589793238462643383279502 |
|||
06-25-2024, 01:19 PM
Post: #24
|
|||
|
|||
RE: Viète à grande Vitesse or Accelerated Viète (kind of)
(06-25-2024 12:41 AM)Albert Chan Wrote: [quote='Gerson W. Barbosa' pid='188722' dateline='1719265376'] Yes, except that it is difficult to use it when we need all the stack for something else afterwards. Unless, of course, we use a numbered register, as in Vh and Vf below. The latter uses a formula to generate the coefficients in the format (1 + n/d), but this works only for the three first coefficients. Code:
Code:
|
|||
06-25-2024, 02:46 PM
Post: #25
|
|||
|
|||
RE: Viète à grande Vitesse or Accelerated Viète (kind of)
(06-25-2024 12:41 AM)Albert Chan Wrote: This version is going for simplicity, which turns out pretty good. Here is for next pade approximation ε = asin(z)/z - 1 ≈ z^2/6 * (1 + 9/20*z^2 / (1 - 25/42*z^2)) ε ≈ (1 + 2.7/(x-25/7)) / x , where x = 6/z^2 Code: 00 { 50-Byte Prgm } 13 --> 3.141592653589793238462643383279481 14 --> 3.141592653589793238462643383279502 |
|||
07-01-2024, 05:49 AM
(This post was last modified: 07-01-2024 06:00 AM by Gerson W. Barbosa.)
Post: #26
|
|||
|
|||
RE: Viète à grande Vitesse or Accelerated Viète
(06-23-2024 06:00 PM)Albert Chan Wrote: ... Same results: 1 4 XEQ "VCW" --> 3.138316846974900201597524772067292 10 4 XEQ "VCW" --> 3.141592653589793238462643383274435 11 4 XEQ "VCW" --> 3.141592653589793238462643383279500 12 4 XEQ "VCW" --> 3.141592653589793238462643383279504 Now with eight coefficients: 1 8 XEQ "VCW" --> 3.141497389177767990537566794077536 2 8 XEQ "VCW" --> 3.141592652530259569168830657515667 6 8 XEQ "VCW" --> 3.141592653589793238462643383279186 7 8 XEQ "VCW" --> 3.141592653589793238462643383279508 8 8 XEQ "VCW" --> 3.141592653589793238462643383279503 No optimization attempt at this stage. No coin flipping for program label – it looks better this way:-) Formulae later, if I find a suitable LaTeX editor (tired of editing these by hand). Code:
Edit to change Subject – this looks indeed accelerated although further tests are still needed. |
|||
07-01-2024, 07:53 PM
(This post was last modified: 07-03-2024 12:37 PM by Gerson W. Barbosa.)
Post: #27
|
|||
|
|||
RE: Viète à grande Vitesse or Accelerated Viète
DECIMAL BASIC PROGRAM
Code:
Code:
The last three digits should be 989, but we are here at the limit of DECIMAL BASIC precision. Like number of iterations for the Viète's Product and the correction polynomial, not sure whether this gives optimal efficiency. Anyway, sublinear convergence rate (the number of iterations depends on the square root of the number of digits). Edited to fix a typo ---------- For one thousand digits, only 11 iterations of the first loop and 165 of the second loop would suffice. I have added six iterations to the latter as a safety margin for this range. Almost 2.5 times faster. Perhaps not too far from the ideal m/n ratio. The last two digits should be 89. Code:
Code:
---------- Using a recurrent formula also for the denominator might be better... Code:
...and faster! Code:
|
|||
07-01-2024, 08:29 PM
Post: #28
|
|||
|
|||
RE: Viète à grande Vitesse or Accelerated Viète (kind of)
I just realized I had done this before, in Arc SOHCAHTOA method
(03-31-2022 11:07 PM)Albert Chan Wrote: asinq(x) = 2 * asinq(x/2/(1+sqrt(1-x))) lua> k, x = 4, .5 -- 4 * asinq(.5) = 4 * pi/4 = pi lua> FMT = '%g * asinq(%.17g)\n' lua> for i=1,5 do printf(FMT,k,x); k=k+k; x=0.5*x/(1+sqrt(1-x)) end 4 * asinq(0.5) 8 * asinq(0.14644660940672624) 16 * asinq(0.038060233744356624) 32 * asinq(0.009607359798384776) 64 * asinq(0.0024076366639015569) Argument inside asinq, x = z^2 in Free42 code. Undo asinq, we have pi lua> k*sqrt(x) -- = pi', with n=6 3.141277250932773 lua> _ * (1 + x/6*(1 + 9/20*x*(1 + 25/42*x*(1 + 49/72*x)))) 3.141592653589793 |
|||
07-01-2024, 09:05 PM
(This post was last modified: 07-01-2024 09:06 PM by Gerson W. Barbosa.)
Post: #29
|
|||
|
|||
RE: Viète à grande Vitesse or Accelerated Viète
(07-01-2024 08:29 PM)Albert Chan Wrote: I just realized I had done this before, in Arc SOHCAHTOA method Pi day celebrations appear to give these kind of interesting byproducts. In this thread from 2009 started by Egan Ford there’s a link about Vieta Acceleration. It looks quite complicated and his program for 5000 digits requires a lot of memory. I would like to check how the above compares to it. A good think about the correction polynomial it that its coefficients can be easily determined, what was something I was counting on in order to make it a valid method for computing \(\pi\). Wolfram Alpha returned a formula including COMB(2n, n) for the coefficients, which of course would not be practical for that purpose. I had already quit, when I evaluated the first six or seven such COMBs on the 42S and noticed this pattern: 1 2 3 4 5 6 7 8 9 10 3 6 10 15 21 10 20 35 56 35 70 126 126 252 462 Problem solved! |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 4 Guest(s)