]> www.ginac.de Git - cln.git/blob - src/float/transcendental/cl_LF_pi.cc
ef22cb73b8632c2e9bd25f13ef0e9a09d70d8792
[cln.git] / src / float / transcendental / cl_LF_pi.cc
1 // cl_pi().
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 // Specification.
7 #include "cl_F_tran.h"
8
9
10 // Implementation.
11
12 #include "cl_lfloat.h"
13 #include "cl_LF_tran.h"
14 #include "cl_LF.h"
15 #include "cl_integer.h"
16 #include "cl_alloca.h"
17
18 ALL_cl_LF_OPERATIONS_SAME_PRECISION()
19
20 // For the next algorithms, I warmly recommend
21 // [Jörg Arndt: hfloat documentation, august 1996,
22 //  http://www.tu-chemnitz.de/~arndt/ hfdoc.dvi
23 //  But beware of the typos in his formulas! ]
24
25 const cl_LF compute_pi_brent_salamin (uintC len)
26 {
27         // Methode:
28         // [Richard P. Brent: Fast multiple-precision evaluation of elementary
29         //  functions. J. ACM 23(1976), 242-251.]
30         // [Jonathan M. Borwein, Peter B. Borwein: Pi and the AGM.
31         //  Wiley 1987. Algorithm 2.2, p. 48.]
32         // [Jörg Arndt, formula (4.51)-(4.52).]
33         //    pi = AGM(1,1/sqrt(2))^2 * 2/(1 - sum(k=0..infty, 2^k c_k^2)).
34         // where the AGM iteration reads
35         //    a_0 := 1, b_0 := 1/sqrt(2).
36         //    a_(k+1) := (a_k + b_k)/2, b_(k+1) := sqrt(a_k*b_k).
37         // and we set
38         //    c_k^2 := a_k^2 - b_k^2,
39         // i.e. c_0^2 = 1/2,
40         //      c_(k+1)^2 = a_(k+1)^2 - b_(k+1)^2 = (a_k - b_k)^2/4
41         //                = (a_k - a_(k+1))^2.
42         // (Actually c_(k+1) is _defined_ as  = a_k - a_(k+1) = (a_k - b_k)/2.)
43         // d=len, n:=intDsize*d. Verwende Long-Floats mit intDsize*(d+1)
44         // Mantissenbits.
45         // (let* ((a (coerce 1 'long-float)) ; 1
46         //        (b (sqrt (scale-float a -1))) ; 2^-(1/2)
47         //        (eps (scale-float a (- n))) ; 2^-n
48         //        (t (scale-float a -2)) ; 1/4
49         //        (k 0)
50         //       )
51         //   (loop
52         //     (when (< (- a b) eps) (return))
53         //     (let ((y a))
54         //       (setq a (scale-float (+ a b) -1))
55         //       (setq b (sqrt (* b y)))
56         //       (setq t (- t (scale-float (expt (- a y) 2) k)))
57         //     )
58         //     (incf k)
59         //   )
60         //   (/ (expt a 2) t)
61         // )
62         var uintC actuallen = len + 1; // 1 Schutz-Digit
63         var uintL uexp_limit = LF_exp_mid - intDsize*(uintL)len;
64         // Ein Long-Float ist genau dann betragsmäßig <2^-n, wenn
65         // sein Exponent < LF_exp_mid-n = uexp_limit ist.
66         var cl_LF a = cl_I_to_LF(1,actuallen);
67         var cl_LF b = sqrt(scale_float(a,-1));
68         var uintL k = 0;
69         var cl_LF t = scale_float(a,-2);
70         until (TheLfloat(a-b)->expo < uexp_limit) {
71                 // |a-b| < 2^-n -> fertig
72                 var cl_LF new_a = scale_float(a+b,-1); // (a+b)/2
73                 b = sqrt(a*b);
74                 var cl_LF a_diff = new_a - a;
75                 t = t - scale_float(square(a_diff),k);
76                 a = new_a;
77                 k++;
78         }
79         var cl_LF pi = square(a)/t; // a^2/t
80         return shorten(pi,len); // verkürzen und fertig
81 }
82 // Bit complexity (N := len): O(log(N)*M(N)).
83
84 const cl_LF compute_pi_brent_salamin_quartic (uintC len)
85 {
86         // See [Borwein, Borwein, section 1.4, exercise 3, p. 17].
87         // See also [Jörg Arndt], formulas (4.52) and (3.30)[wrong!].
88         // As above, we are using the formula
89         //    pi = AGM(1,1/sqrt(2))^2 * 2/(1 - sum(k=0..infty, 2^k c_k^2)).
90         // where the AGM iteration reads
91         //    a_0 := 1, b_0 := 1/sqrt(2).
92         //    a_(k+1) := (a_k + b_k)/2, b_(k+1) := sqrt(a_k*b_k).
93         // But we keep computing with
94         //    wa_k := sqrt(a_k)  and  wb_k := sqrt(b_k)
95         // at the same time and do two iteration steps at once.
96         // The iteration takes now takes the form
97         //    wa_0 := 1, wb_0 := 2^-1/4,
98         //    (wa_k, wb_k, a_k, b_k)
99         //    -->         ((wa_k^2 + wb_k^2)/2, wa_k*wb_k)
100         //    -->         (((wa_k + wb_k)/2)^2, sqrt(wa_k*wb_k*(wa_k^2 + wb_k^2)/2)),
101         //    i.e. wa_(k+2) = (wa_k + wb_k)/2 and
102         //         wb_(k+2) = sqrt(sqrt(wa_k*wb_k*(wa_k^2 + wb_k^2)/2)).
103         // For the sum, we can group two successive items together:
104         //   2^k * c_k^2 + 2^(k+1) * c_(k+1)^2
105         //   = 2^k * [(a_k^2 - b_k^2) + 2*((a_k - b_k)/2)^2]
106         //   = 2^k * [3/2*a_k^2 - a_k*b_k - 1/2*b_k^2]
107         //   = 2^k * [2*a_k^2 - 1/2*(a_k+b_k)^2]
108         //   = 2^(k+1) * [a_k^2 - ((a_k+b_k)/2)^2]
109         //   = 2^(k+1) * [wa_k^4 - ((wa_k^2+wb_k^2)/2)^2].
110         // Hence,
111         //   pi = AGM(1,1/sqrt(2))^2 * 1/(1/2 - sum(k even, 2^k*[....])).
112         var uintC actuallen = len + 1; // 1 Schutz-Digit
113         var uintL uexp_limit = LF_exp_mid - intDsize*(uintL)len;
114         var cl_LF one = cl_I_to_LF(1,actuallen);
115         var cl_LF a = one;
116         var cl_LF wa = one;
117         var cl_LF b = sqrt(scale_float(one,-1));
118         var cl_LF wb = sqrt(b);
119         // We keep a = wa^2, b = wb^2.
120         var uintL k = 0;
121         var cl_LF t = scale_float(one,-1);
122         until (TheLfloat(wa-wb)->expo < uexp_limit) {
123                 // |wa-wb| < 2^-n -> fertig
124                 var cl_LF wawb = wa*wb;
125                 var cl_LF new_wa = scale_float(wa+wb,-1);
126                 var cl_LF a_b = scale_float(a+b,-1);
127                 var cl_LF new_a = scale_float(a_b+wawb,-1);
128                 var cl_LF new_b = sqrt(wawb*a_b);
129                 var cl_LF new_wb = sqrt(new_b);
130                 t = t - scale_float((a - a_b)*(a + a_b),k);
131                 a = new_a; wa = new_wa;
132                 b = new_b; wb = new_wb;
133                 k += 2;
134         }
135         var cl_LF pi = square(a)/t;
136         return shorten(pi,len); // verkürzen und fertig
137 }
138 // Bit complexity (N := len): O(log(N)*M(N)).
139
140 const cl_LF compute_pi_ramanujan_163 (uintC len)
141 {
142         // 1/pi = 1/sqrt(-1728 J)
143         //        * sum(n=0..infty, (6n)! (A+nB) / 12^(3n) (3n)! n!^3 J^n)
144         // mit J = -53360^3 = - (2^4 5 23 29)^3
145         //     A = 163096908 = 2^2 3 13 1045493
146         //     B = 6541681608 = 2^3 3^3 7 11 19 127 163
147         // See [Jörg Arndt], formulas (4.27)-(4.30).
148         // This is also the formula used in Pari.
149         // The absolute value of the n-th summand is approximately
150         //   |J|^-n * n^(-1/2) * B/(2*pi^(3/2)),
151         // hence every summand gives more than 14 new decimal digits
152         // in precision.
153         // The sum is best evaluated using fixed-point arithmetic,
154         // so that the precision is reduced for the later summands.
155         var uintL actuallen = len + 4; // 4 Schutz-Digits
156         var sintL scale = intDsize*actuallen;
157         static const cl_I A = "163096908";
158         static const cl_I B = "6541681608";
159         //static const cl_I J1 = "10939058860032000"; // 72*abs(J)
160         static const cl_I J2 = "333833583375"; // odd part of J1
161         var cl_I sum = 0;
162         var cl_I n = 0;
163         var cl_I factor = ash(1,scale);
164         while (!zerop(factor)) {
165                 sum = sum + factor * (A+n*B);
166                 factor = factor * ((6*n+1)*(2*n+1)*(6*n+5));
167                 n = n+1;
168                 factor = truncate1(factor,n*n*n*J2);
169                 // Finally divide by 2^15 and change sign.
170                 if (minusp(factor))
171                         factor = (-factor) >> 15;
172                 else
173                         factor = -(factor >> 15);
174         }
175         var cl_LF fsum = scale_float(cl_I_to_LF(sum,actuallen),-scale);
176         static const cl_I J3 = "262537412640768000"; // -1728*J
177         var cl_LF pi = sqrt(cl_I_to_LF(J3,actuallen)) / fsum;
178         return shorten(pi,len); // verkürzen und fertig
179 }
180 // Bit complexity (N := len): O(N^2).
181
182 #if defined(__mips__) && !defined(__GNUC__) // workaround SGI CC bug
183 #define A A_fast
184 #define B B_fast
185 #define J3 J3_fast
186 #endif
187
188 const cl_LF compute_pi_ramanujan_163_fast (uintC len)
189 {
190         // Same formula as above, using a binary splitting evaluation.
191         // See [Borwein, Borwein, section 10.2.3].
192         var uintL actuallen = len + 4; // 4 Schutz-Digits
193         static const cl_I A = "163096908";
194         static const cl_I B = "6541681608";
195         static const cl_I J1 = "10939058860032000"; // 72*abs(J)
196         // Evaluate a sum(0 <= n < N, a(n)/b(n) * (p(0)...p(n))/(q(0)...q(n)))
197         // with appropriate N, and
198         //   a(n) = A+n*B, b(n) = 1,
199         //   p(n) = -(6n-5)(2n-1)(6n-1) for n>0,
200         //   q(n) = 72*|J|*n^3 for n>0.
201         var const uintL n_slope = (uintL)(intDsize*32*0.02122673)+1;
202         // n_slope >= 32*intDsize*log(2)/log(|J|), normally n_slope = 22.
203         var uintL N = (n_slope*actuallen)/32 + 1;
204         // N > intDsize*log(2)/log(|J|) * actuallen, hence
205         // |J|^-N < 2^(-intDsize*actuallen).
206         CL_ALLOCA_STACK;
207         var cl_I* av = (cl_I*) cl_alloca(N*sizeof(cl_I));
208         var cl_I* pv = (cl_I*) cl_alloca(N*sizeof(cl_I));
209         var cl_I* qv = (cl_I*) cl_alloca(N*sizeof(cl_I));
210         var uintL* qsv = (uintL*) cl_alloca(N*sizeof(uintL));
211         var uintL n;
212         for (n = 0; n < N; n++) {
213                 init1(cl_I, av[n]) (A+n*B);
214                 if (n==0) {
215                         init1(cl_I, pv[n]) (1);
216                         init1(cl_I, qv[n]) (1);
217                 } else {
218                         init1(cl_I, pv[n]) (-((cl_I)(6*n-5)*(cl_I)(2*n-1)*(cl_I)(6*n-1)));
219                         init1(cl_I, qv[n]) ((cl_I)n*(cl_I)n*(cl_I)n*J1);
220                 }
221         }
222         var cl_pqa_series series;
223         series.av = av;
224         series.pv = pv; series.qv = qv;
225         series.qsv = (len >= 35 ? qsv : 0); // 5% speedup for large len's
226         var cl_LF fsum = eval_rational_series(N,series,actuallen);
227         for (n = 0; n < N; n++) {
228                 av[n].~cl_I();
229                 pv[n].~cl_I();
230                 qv[n].~cl_I();
231         }
232         static const cl_I J3 = "262537412640768000"; // -1728*J
233         var cl_LF pi = sqrt(cl_I_to_LF(J3,actuallen)) / fsum;
234         return shorten(pi,len); // verkürzen und fertig
235 }
236 // Bit complexity (N := len): O(log(N)^2*M(N)).
237
238 // Timings of the above algorithms, on an i486 33 MHz, running Linux.
239 //    N      Brent   Brent4  R 163   R 163 fast
240 //    10     0.0079  0.0079  0.0052  0.0042
241 //    25     0.026   0.026   0.014   0.012
242 //    50     0.085   0.090   0.037   0.033
243 //   100     0.29    0.29    0.113   0.098
244 //   250     1.55    1.63    0.60    0.49
245 //   500     5.7     5.7     2.24    1.71
246 //  1000    21.6    22.9     8.5     5.5
247 //  2500    89      95      49      19.6
248 //  5000   217     218     188      49
249 // 10000   509     540     747     117
250 // 25000  1304    1310    4912     343
251 // We see that
252 //   - "Brent4" isn't worth it: No speed improvement over "Brent".
253 //   - "R 163" is pretty fast at the beginning, but it is an O(N^2)
254 //     algorithm, hence it loses in the end,
255 //   - "R 163 fast", which uses the same formula as "R 163", but evaluates
256 //     it using binary splitting, is an O(log N * M(N)) algorithm, and
257 //     outperforms all of the others.
258
259 const cl_LF cl_pi (uintC len)
260 {
261         var uintC oldlen = TheLfloat(cl_LF_pi)->len; // vorhandene Länge
262         if (len < oldlen)
263                 return shorten(cl_LF_pi,len);
264         if (len == oldlen)
265                 return cl_LF_pi;
266
267         // TheLfloat(cl_LF_pi)->len um mindestens einen konstanten Faktor
268         // > 1 wachsen lassen, damit es nicht zu häufig nachberechnet wird:
269         var uintC newlen = len;
270         oldlen += floor(oldlen,2); // oldlen * 3/2
271         if (newlen < oldlen)
272                 newlen = oldlen;
273
274         // gewünschte > vorhandene Länge -> muß nachberechnen:
275         cl_LF_pi = compute_pi_ramanujan_163_fast(newlen);
276         return (len < newlen ? shorten(cl_LF_pi,len) : cl_LF_pi);
277 }