Debian Codegraph @

src/dr/evomodel/epidemiology/LambertW.java

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