| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
package dr.evomodel.epidemiology; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 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 |
|
| 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 |
|
| 71 |
|
| 72 |
if (w > 0) { |
| 73 |
t = (t/p)/e; |
| 74 |
} else { |
| 75 |
t /= e*p - 0.5*(p + 1.0)*t/p; |
| 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 |
|
| 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 |
|
| 112 |
|
| 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 |
|
| 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 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 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; |
| 150 |
return GSL_RETURN.GSL_SUCCESS; |
| 151 |
} |
| 152 |
else if(q < 1.0e-03) { |
| 153 |
|
| 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 |
|
| 165 |
|
| 166 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
final double r = -Math.sqrt(q); |
| 215 |
w = series_eval(r); |
| 216 |
if(q < 3.0e-3) { |
| 217 |
|
| 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 |
|
| 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 |
|
| 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 |
} |