]> www.ginac.de Git - cln.git/blob - src/float/transcendental/cl_F_expx.cc
* All Files have been modified for inclusion of namespace cln;
[cln.git] / src / float / transcendental / cl_F_expx.cc
1 // expx().
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 "cln/float.h"
13 #include "cl_low.h"
14 #include "cl_F.h"
15 #include "cln/lfloat.h"
16 #include "cl_LF.h"
17 #include "cln/integer.h"
18
19 #undef MAYBE_INLINE
20 #define MAYBE_INLINE inline
21 #include "cl_LF_zerop.cc"
22 #include "cl_LF_exponent.cc"
23
24 namespace cln {
25
26 // cl_F expx_naive (const cl_F& x)
27 // cl_LF expx_naive (const cl_LF& x)
28 //
29 // Methode:
30 // e := Exponent aus (decode-float x), d := (float-digits x)
31 // Bei x=0.0 oder e<-d liefere 1.0
32 //   (denn bei e<=-d-1 ist abs(exp(x)-1) = abs(x)+O(x^2) < 2^(-d-1),
33 //    also ist exp(x), auf d Bits gerundet, gleich 1.0).
34 // Bei e<=-sqrt(d) verwende die Potenzreihe
35 //   exp(x) = sum(j=0..inf,x^j/j!):
36 //   b:=1, i:=0, sum:=0,
37 //   while (/= sum (setq sum (+ sum b))) do b:=b*x/(i+1), i:=i+1.
38 //   Ergebnis sum.
39 // Sonst setze y := x/2 = (scale-float x -1),
40 //   berechne rekursiv z:=exp(y) und liefere z^2.
41 // Aufwand: asymptotisch d^2.5 .
42
43 const cl_LF expx_naive (const cl_LF& x)
44 {
45 // Methode:
46 // wie oben, mit adaptiver Genauigkeit während der Potenzreihen-Summation.
47         if (zerop(x))
48                 return cl_float(1,x);
49         var uintL actuallen = TheLfloat(x)->len;
50         var uintL d = float_digits(x);
51         var sintL e = float_exponent(x);
52         if (e < -(sintL)d) // e < -d ?
53                 return cl_float(1,x); // ja -> 1.0 als Ergebnis
54  {      Mutable(cl_LF,x);
55         var uintL k = 0; // Rekursionszähler k:=0
56         // Bei e <= -1-limit_slope*floor(sqrt(d)) kann die Potenzreihe
57         // angewandt werden. limit_slope = 1.0 ist nicht schlecht,
58         // auch im Bereich d = ca. 800.
59         var sintL e_limit = -1-isqrt(d); // -1-floor(sqrt(d))
60         if (e > e_limit) {
61                 // e > -1-floor(sqrt(d)) -> muß |x| verkleinern.
62                 k = e - e_limit;
63                 x = scale_float(x,-(sintL)k); // x := x/2^k
64                 // Neuer Exponent = e-k = e_limit.
65         }
66         // Potenzreihe anwenden:
67         var int i = 0;
68         var cl_LF b = cl_float(1,x); // b := (float 1 x)
69         var cl_LF eps = scale_float(b,-(sintL)d-10);
70         var cl_LF sum = cl_float(0,x); // sum := (float 0 x)
71         loop {
72                 var cl_LF new_sum = sum + LF_to_LF(b,actuallen);
73                 if (new_sum == sum) // = sum ?
74                         break; // ja -> Potenzreihe abbrechen
75                 sum = new_sum;
76                 i = i+1;
77                 b = cl_LF_shortenwith(b,eps);
78                 b = (b*x)/(cl_I)i; // b := b*x/i
79         }
80         var cl_LF& result = sum; // sum als Ergebnis
81         // Wegen Rekursion noch k mal quadrieren:
82         for ( ; k > 0; k--)
83                 result = square(result);
84         return result;
85 }}
86 // Bit complexity (N = length(x)): O(N^(1/2)*M(N)).
87
88 const cl_F expx_naive (const cl_F& x)
89 {
90         if (longfloatp(x)) {
91                 DeclareType(cl_LF,x);
92                 return expx_naive(x);
93         }
94         if (zerop(x))
95                 return cl_float(1,x);
96         var uintL d = float_digits(x);
97         var sintL e = float_exponent(x);
98         if (e < -(sintL)d) // e < -d ?
99                 return cl_float(1,x); // ja -> 1.0 als Ergebnis
100  {      Mutable(cl_F,x);
101         var uintL k = 0; // Rekursionszähler k:=0
102         // Bei e <= -1-limit_slope*floor(sqrt(d)) kann die Potenzreihe
103         // angewandt werden. limit_slope = 1.0 ist nicht schlecht. Für
104         // d > 1600 scheint der Bereich 2.0 <= limit_slope <= 2.6 am besten
105         // zu sein (mit bis zu 15% Beschleunigung gegenüber limit_slope = 1.0),
106         // aber in diesem Bereich rechnen wir gar nicht.
107         // Wir wählen limit_slope = 1.5.
108         var sintL e_limit = -1-floor(isqrt(d)*3,2); // -1-floor(sqrt(d))
109         if (e > e_limit) {
110                 // e > -1-floor(sqrt(d)) -> muß |x| verkleinern.
111                 k = e - e_limit;
112                 x = scale_float(x,-(sintL)k); // x := x/2^k
113                 // Neuer Exponent = e-k = e_limit.
114         }
115         // Potenzreihe anwenden:
116         var int i = 0;
117         var cl_F b = cl_float(1,x); // b := (float 1 x)
118         var cl_F sum = cl_float(0,x); // sum := (float 0 x)
119         loop {
120                 var cl_F new_sum = sum + b;
121                 if (new_sum == sum) // = sum ?
122                         break; // ja -> Potenzreihe abbrechen
123                 sum = new_sum;
124                 i = i+1;
125                 b = (b*x)/(cl_I)i; // b := b*x/i
126         }
127         var cl_F& result = sum; // sum als Ergebnis
128         // Wegen Rekursion noch k mal quadrieren:
129         for ( ; k > 0; k--)
130                 result = square(result);
131         return result;
132 }}
133 // Bit complexity (N = length(x)): O(N^(1/2)*M(N)).
134
135 const cl_LF expx_ratseries (const cl_LF& x)
136 {
137         // [Jonathan M. Borwein, Peter B. Borwein: Pi and the AGM.
138         //  Wiley 1987. Section 10.2.3]
139         var uintC len = TheLfloat(x)->len;
140         var cl_idecoded_float x_ = integer_decode_float(x);
141         // x = (-1)^sign * 2^exponent * mantissa
142         var uintL lq = cl_I_to_UL(- x_.exponent);
143         var const cl_I& p = x_.mantissa;
144         // Compute exp(p/2^lq) by splitting into pieces.
145         // Each piece gives rise to a factor exp(pk/2^lqk).
146         // Instead of the standard choice lqk = 2^k, we choose
147         // lqk = c^k + O(1), where c > 1 is real.
148         // Running time on Linux i486, 33 Mhz, computing exp(sqrt(2)-1):
149         //   c  2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5
150         //  (a) 400 393 390 377 371 360 363 367 367 358 362 362 363 362 376 372
151         //  (b) 311 317 305 312 295 291 286 293 291 284 295 284 293 287 288 305
152         // (a): N=300, time in 0.01 sec. (b): N=1000, time in 0.1 sec.
153         // Values 2.5 <= c <= 3.2 seem best. Let's choose c = 2.875.
154         var cl_boolean first_factor = cl_true;
155         var cl_LF product;
156         var uintL b1;
157         var uintL b2;
158         for (b1 = 0, b2 = 1; b1 < lq; b1 = b2, b2 = ceiling(b2*23,8)) {
159                 // Piece containing bits b1+1..b2 after "decimal point"
160                 // in the binary representation of (p/2^lq).
161                 var uintL lqk = (lq >= b2 ? b2 : lq);
162                 var cl_I pk = ldb(p,cl_byte(lqk-b1,lq-lqk));
163                 // Compute exp(pk/2^lqk).
164                 if (!zerop(pk)) {
165                         if (minusp(x_.sign)) { pk = -pk; }
166                         var cl_LF factor = cl_exp_aux(pk,lqk,len);
167                         if (first_factor) {
168                                 product = factor;
169                                 first_factor = cl_false;
170                         } else
171                                 product = product * factor;
172                 }
173         }
174         if (first_factor)
175                 return cl_I_to_LF(1,len);
176         else
177                 return product;
178 }
179 // Bit complexity (N = length(x)): O(log(N)^2*M(N)).
180
181 // Timings of the above algorithms, on an i486 33 MHz, running Linux,
182 // applied to x = sqrt(2)-1 = 0.414...
183 // ("naive" with adaptive limit_slope, about sqrt(ln(len)).)
184 //   N      naive  ratseries
185 //   10     0.010   0.027
186 //   25     0.039   0.072
187 //   50     0.15    0.19
188 //  100     0.60    0.55
189 //  250     3.9     2.6
190 //  500    16.3     9.3
191 // 1000    68      29
192 // ==> ratseries faster for N >= 84.
193
194 }  // namespace cln