Debian Codegraph @

external/gsl/gsl_specfunc__lambert.c

raw
1 /* specfunc/lambert.c
2 *
3 * Copyright (C) 2007 Brian Gough
4 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001 Gerard Jungman
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or (at
9 * your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20
21 /* Author: G. Jungman */
22
23 #include "gsl__config.h"
24 #include <math.h>
25 #include "gsl_math.h"
26 #include "gsl_errno.h"
27 #include "gsl_sf_lambert.h"
28
29 /* Started with code donated by K. Briggs; added
30 * error estimates, GSL foo, and minor tweaks.
31 * Some Lambert-ology from
32 * [Corless, Gonnet, Hare, and Jeffrey, "On Lambert's W Function".]
33 */
34
35
36 /* Halley iteration (eqn. 5.12, Corless et al) */
37 static int
38 halley_iteration(
39 double x,
40 double w_initial,
41 unsigned int max_iters,
42 gsl_sf_result * result
43 )
44 {
45 double w = w_initial;
46 unsigned int i;
47
48 for(i=0; i<max_iters; i++) {
49 double tol;
50 const double e = exp(w);
51 const double p = w + 1.0;
52 double t = w*e - x;
53 /* printf("FOO: %20.16g %20.16g\n", w, t); */
54
55 if (w > 0) {
56 t = (t/p)/e; /* Newton iteration */
57 } else {
58 t /= e*p - 0.5*(p + 1.0)*t/p; /* Halley iteration */
59 };
60
61 w -= t;
62
63 tol = 10 * GSL_DBL_EPSILON * GSL_MAX_DBL(fabs(w), 1.0/(fabs(p)*e));
64
65 if(fabs(t) < tol)
66 {
67 result->val = w;
68 result->err = 2.0*tol;
69 return GSL_SUCCESS;
70 }
71 }
72
73 /* should never get here */
74 result->val = w;
75 result->err = fabs(w);
76 return GSL_EMAXITER;
77 }
78
79
80 /* series which appears for q near zero;
81 * only the argument is different for the different branches
82 */
83 static double
84 series_eval(double r)
85 {
86 static const double c[12] = {
87 -1.0,
88 2.331643981597124203363536062168,
89 -1.812187885639363490240191647568,
90 1.936631114492359755363277457668,
91 -2.353551201881614516821543561516,
92 3.066858901050631912893148922704,
93 -4.175335600258177138854984177460,
94 5.858023729874774148815053846119,
95 -8.401032217523977370984161688514,
96 12.250753501314460424,
97 -18.100697012472442755,
98 27.029044799010561650
99 };
100 const double t_8 = c[8] + r*(c[9] + r*(c[10] + r*c[11]));
101 const double t_5 = c[5] + r*(c[6] + r*(c[7] + r*t_8));
102 const double t_1 = c[1] + r*(c[2] + r*(c[3] + r*(c[4] + r*t_5)));
103 return c[0] + r*t_1;
104 }
105
106
107 /*-*-*-*-*-*-*-*-*-*-*-* Functions with Error Codes *-*-*-*-*-*-*-*-*-*-*-*/
108
109 int
110 gsl_sf_lambert_W0_e(double x, gsl_sf_result * result)
111 {
112 const double one_over_E = 1.0/M_E;
113 const double q = x + one_over_E;
114
115 if(x == 0.0) {
116 result->val = 0.0;
117 result->err = 0.0;
118 return GSL_SUCCESS;
119 }
120 else if(q < 0.0) {
121 /* Strictly speaking this is an error. But because of the
122 * arithmetic operation connecting x and q, I am a little
123 * lenient in case of some epsilon overshoot. The following
124 * answer is quite accurate in that case. Anyway, we have
125 * to return GSL_EDOM.
126 */
127 result->val = -1.0;
128 result->err = sqrt(-q);
129 return GSL_EDOM;
130 }
131 else if(q == 0.0) {
132 result->val = -1.0;
133 result->err = GSL_DBL_EPSILON; /* cannot error is zero, maybe q == 0 by "accident" */
134 return GSL_SUCCESS;
135 }
136 else if(q < 1.0e-03) {
137 /* series near -1/E in sqrt(q) */
138 const double r = sqrt(q);
139 result->val = series_eval(r);
140 result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
141 return GSL_SUCCESS;
142 }
143 else {
144 static const unsigned int MAX_ITERS = 10;
145 double w;
146
147 if (x < 1.0) {
148 /* obtain initial approximation from series near x=0;
149 * no need for extra care, since the Halley iteration
150 * converges nicely on this branch
151 */
152 const double p = sqrt(2.0 * M_E * q);
153 w = -1.0 + p*(1.0 + p*(-1.0/3.0 + p*11.0/72.0));
154 }
155 else {
156 /* obtain initial approximation from rough asymptotic */
157 w = log(x);
158 if(x > 3.0) w -= log(w);
159 }
160
161 return halley_iteration(x, w, MAX_ITERS, result);
162 }
163 }
164
165
166 int
167 gsl_sf_lambert_Wm1_e(double x, gsl_sf_result * result)
168 {
169 if(x > 0.0) {
170 return gsl_sf_lambert_W0_e(x, result);
171 }
172 else if(x == 0.0) {
173 result->val = 0.0;
174 result->err = 0.0;
175 return GSL_SUCCESS;
176 }
177 else {
178 static const unsigned int MAX_ITERS = 32;
179 const double one_over_E = 1.0/M_E;
180 const double q = x + one_over_E;
181 double w;
182
183 if (q < 0.0) {
184 /* As in the W0 branch above, return some reasonable answer anyway. */
185 result->val = -1.0;
186 result->err = sqrt(-q);
187 return GSL_EDOM;
188 }
189
190 if(x < -1.0e-6) {
191 /* Obtain initial approximation from series about q = 0,
192 * as long as we're not very close to x = 0.
193 * Use full series and try to bail out if q is too small,
194 * since the Halley iteration has bad convergence properties
195 * in finite arithmetic for q very small, because the
196 * increment alternates and p is near zero.
197 */
198 const double r = -sqrt(q);
199 w = series_eval(r);
200 if(q < 3.0e-3) {
201 /* this approximation is good enough */
202 result->val = w;
203 result->err = 5.0 * GSL_DBL_EPSILON * fabs(w);
204 return GSL_SUCCESS;
205 }
206 }
207 else {
208 /* Obtain initial approximation from asymptotic near zero. */
209 const double L_1 = log(-x);
210 const double L_2 = log(-L_1);
211 w = L_1 - L_2 + L_2/L_1;
212 }
213
214 return halley_iteration(x, w, MAX_ITERS, result);
215 }
216 }
217
218
219 /*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/
220
221 #include "gsl_specfunc__eval.h"
222
223 double gsl_sf_lambert_W0(double x)
224 {
225 EVAL_RESULT(gsl_sf_lambert_W0_e(x, &result));
226 }
227
228 double gsl_sf_lambert_Wm1(double x)
229 {
230 EVAL_RESULT(gsl_sf_lambert_Wm1_e(x, &result));
231 }