3 * Copyright (C) 2007 Brian Gough
4 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001 Gerard Jungman
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.
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.
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.
21 /* Author: G. Jungman */
25 #include <gsl/gsl_math.h>
26 #include <gsl/gsl_errno.h>
27 #include <gsl/gsl_sf_lambert.h>
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".]
36 /* Halley iteration (eqn. 5.12, Corless et al) */
41 unsigned int max_iters,
42 gsl_sf_result * result
48 for(i=0; i<max_iters; i++) {
50 const double e = exp(w);
51 const double p = w + 1.0;
53 /* printf("FOO: %20.16g %20.16g\n", w, t); */
56 t = (t/p)/e; /* Newton iteration */
58 t /= e*p - 0.5*(p + 1.0)*t/p; /* Halley iteration */
63 tol = 10 * GSL_DBL_EPSILON * GSL_MAX_DBL(fabs(w), 1.0/(fabs(p)*e));
68 result->err = 2.0*tol;
73 /* should never get here */
75 result->err = fabs(w);
80 /* series which appears for q near zero;
81 * only the argument is different for the different branches
86 static const double c[12] = {
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,
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)));
107 /*-*-*-*-*-*-*-*-*-*-*-* Functions with Error Codes *-*-*-*-*-*-*-*-*-*-*-*/
110 gsl_sf_lambert_W0_e(double x, gsl_sf_result * result)
112 const double one_over_E = 1.0/M_E;
113 const double q = x + one_over_E;
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.
128 result->err = sqrt(-q);
133 result->err = GSL_DBL_EPSILON; /* cannot error is zero, maybe q == 0 by "accident" */
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);
144 static const unsigned int MAX_ITERS = 10;
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
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));
156 /* obtain initial approximation from rough asymptotic */
158 if(x > 3.0) w -= log(w);
161 return halley_iteration(x, w, MAX_ITERS, result);
167 gsl_sf_lambert_Wm1_e(double x, gsl_sf_result * result)
170 return gsl_sf_lambert_W0_e(x, result);
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;
184 /* As in the W0 branch above, return some reasonable answer anyway. */
186 result->err = sqrt(-q);
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.
198 const double r = -sqrt(q);
201 /* this approximation is good enough */
203 result->err = 5.0 * GSL_DBL_EPSILON * fabs(w);
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;
214 return halley_iteration(x, w, MAX_ITERS, result);
219 /*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/
223 double gsl_sf_lambert_W0(double x)
225 EVAL_RESULT(gsl_sf_lambert_W0_e(x, &result));
228 double gsl_sf_lambert_Wm1(double x)
230 EVAL_RESULT(gsl_sf_lambert_Wm1_e(x, &result));