13 #define min(a,b) ((a <= b)?a:b) 15 #define max(a,b) ((a > b)?a:b) 18 using atomic::tiny_ad::max_fabs;
34 # define R_ifDEBUG_printf(...) REprintf(__VA_ARGS__) 40 #define R_Log1_Exp(x) ((x) > -M_LN2 ? log(-rexpm1(x)) : log1p(-exp(x))) 43 template<
class Float>
static Float bfrac(Float, Float, Float, Float, Float, Float,
int log_p);
44 template<
class Float>
static void bgrat(Float, Float, Float, Float, Float *, Float,
int *,
bool log_w);
45 template<
class Float>
static Float grat_r(Float a, Float x, Float r, Float eps);
46 template<
class Float>
static Float apser(Float, Float, Float, Float);
47 template<
class Float>
static Float bpser(Float, Float, Float, Float,
int log_p);
48 template<
class Float>
static Float basym(Float, Float, Float, Float,
int log_p);
49 template<
class Float>
static Float fpser(Float, Float, Float, Float,
int log_p);
50 template<
class Float>
static Float bup(Float, Float, Float, Float,
int, Float,
int give_log);
51 static double exparg(
int);
52 template<
class Float>
static Float psi(Float);
53 template<
class Float>
static Float gam1(Float);
54 template<
class Float>
static Float gamln1(Float);
55 template<
class Float>
static Float betaln(Float, Float);
56 template<
class Float>
static Float algdiv(Float, Float);
57 template<
class Float>
static Float brcmp1(
int, Float, Float, Float, Float,
int give_log);
58 template<
class Float>
static Float brcomp(Float, Float, Float, Float,
int log_p);
59 template<
class Float>
static Float rlog1(Float);
60 template<
class Float>
static Float bcorr(Float, Float);
61 template<
class Float>
static Float gamln(Float);
62 template<
class Float>
static Float alnrel(Float);
63 template<
class Float>
static Float esum(
int, Float,
int give_log);
64 template<
class Float>
static Float erf__(Float);
65 template<
class Float>
static Float rexpm1(Float);
66 template<
class Float>
static Float erfc1(
int, Float);
67 template<
class Float>
static Float gsumln(Float, Float);
80 bratio(Float a, Float b, Float x, Float y, Float *w, Float *w1,
126 Float z, a0, b0, x0, y0, lambda;
131 Float eps = 2. * Rf_d1mach(3);
139 if (ISNAN(x) || ISNAN(y) ||
140 ISNAN(a) || ISNAN(b)) { *ierr = 9;
return; }
142 if (a < 0. || b < 0.) { *ierr = 1;
return; }
143 if (a == 0. && b == 0.) { *ierr = 2;
return; }
144 if (x < 0. || x > 1.) { *ierr = 3;
return; }
145 if (y < 0. || y > 1.) { *ierr = 4;
return; }
148 z = x + y - 0.5 - 0.5;
150 if (fabs(z) > eps * 3.) { *ierr = 5;
return; }
154 if (x == 0.)
goto L200;
155 if (y == 0.)
goto L210;
157 if (a == 0.)
goto L211;
158 if (b == 0.)
goto L201;
160 eps =
max(eps, 1e-15);
162 if ( (a_lt_b ? b : a) < eps * .001) {
167 *w = log1p(-a/(a+b));
168 *w1 = log ( a/(a+b));
171 *w1 = log1p(-b/(a+b));
182 #define SET_0_noswap \ 190 if (
min(a,b) <= 1.) {
202 if (b0 <
min(eps, eps * a0)) {
203 *w = fpser(a0, b0, x0, eps, log_p);
204 *w1 = log_p ? R_Log1_Exp(*w) : 0.5 - *w + 0.5;
209 if (a0 <
min(eps, eps * b0) && b0 * x0 <= 1.) {
210 *w1 = apser(a0, b0, x0, eps);
215 bool did_bup = FALSE;
216 if (
max(a0,b0) > 1.) {
218 if (b0 <= 1.)
goto L_w_bpser;
220 if (x0 >= 0.29)
goto L_w1_bpser;
222 if (x0 < 0.1 && pow(x0*b0, a0) <= 0.7)
goto L_w_bpser;
230 if (a0 >=
min(0.2, b0))
goto L_w_bpser;
232 if (pow(x0, a0) <= 0.9)
goto L_w_bpser;
234 if (x0 >= 0.3)
goto L_w1_bpser;
237 *w1 = bup(b0, a0, y0, x0, n, eps, FALSE); did_bup = TRUE;
242 bgrat<Float>(b0, a0, y0, x0, w1, 15*eps, &ierr1, FALSE);
244 REprintf(
" ==> new w1=%.15g", *w1);
245 if(ierr1) REprintf(
" ERROR(code=%d)\n", ierr1) ;
else REprintf(
"\n");
247 if(*w1 == 0 || (0 < *w1 && *w1 < DBL_MIN)) {
253 *w1 = bup<Float>(b0-n, a0, y0, x0, n, eps, TRUE);
255 else *w1 = ML_NEGINF;
256 bgrat<Float>(b0, a0, y0, x0, w1, 15*eps, &ierr1, TRUE);
257 if(ierr1) *ierr = 10 + ierr1;
259 REprintf(
" ==> new log(w1)=%.15g", *w1);
260 if(ierr1) REprintf(
" Error(code=%d)\n", ierr1) ;
else REprintf(
"\n");
262 goto L_end_from_w1_log;
265 if(ierr1) *ierr = 10 + ierr1;
267 MATHLIB_WARNING4(
"bratio(a=%g, b=%g, x=%g): bgrat() -> w1 = %g",
275 lambda = R_FINITE(a+b)
276 ? ((a > b) ? (a + b) * y - b : a - (a + b) * x)
278 do_swap = (lambda < 0.);
290 || (log_p && lambda > 650.))
297 if (b0 <= 100. || lambda > b0 * 0.03)
300 }
else if (a0 <= 100.) {
304 else if (lambda > a0 * 0.03) {
310 *w = basym<Float>(a0, b0, lambda, eps * 100., log_p);
311 *w1 = log_p ? R_Log1_Exp(*w) : 0.5 - *w + 0.5;
319 *w = bpser(a0, b0, x0, eps, log_p);
320 *w1 = log_p ? R_Log1_Exp(*w) : 0.5 - *w + 0.5;
325 *w1 = bpser(b0, a0, y0, eps, log_p);
326 *w = log_p ? R_Log1_Exp(*w1) : 0.5 - *w1 + 0.5;
331 *w = bfrac<Float>(a0, b0, x0, y0, lambda, eps * 15., log_p);
332 *w1 = log_p ? R_Log1_Exp(*w) : 0.5 - *w + 0.5;
344 *w = bup(b0, a0, y0, x0, n, eps, FALSE);
346 if(*w < DBL_MIN && log_p) {
355 *w += bpser(a0, b0, x0, eps, FALSE);
362 *w += bup(a0, b0, x0, y0, n, eps, FALSE);
367 bgrat<Float>(a0, b0, x0, y0, w, 15*eps, &ierr1, FALSE);
368 if(ierr1) *ierr = 10 + ierr1;
370 REprintf(
"==> new w=%.15g", *w);
371 if(ierr1) REprintf(
" Error(code=%d)\n", ierr1) ;
else REprintf(
"\n");
379 if (a == 0.) { *ierr = 6;
return; }
381 L201: *w = R_D__0; *w1 = R_D__1;
return;
384 if (b == 0.) { *ierr = 7;
return; }
386 L211: *w = R_D__1; *w1 = R_D__0;
return;
393 *w1 = 0.5 - *w + 0.5;
402 *w = 0.5 - *w1 + 0.5;
409 *w = R_Log1_Exp(*w1);
419 Float t = *w; *w = *w1; *w1 = t;
428 template<
class Float> Float fpser(Float a, Float b, Float x, Float eps,
int log_p)
439 Float ans, c, s, t, an, tol;
444 }
else if (a > eps * 0.001) {
456 ans += log(b) - log(a);
469 }
while (max_fabs(c) > tol);
478 template<
class Float>
static Float apser(Float a, Float b, Float x, Float eps)
486 static double const g = .577215664901533;
488 Float tol, c, j, s, t, aj;
493 c = log(x) + psi(b) + g + t;
497 tol = eps * 5. * fabs(c);
505 }
while (max_fabs(aj) > tol);
510 template<
class Float>
static Float bpser(Float a, Float b, Float x, Float eps,
int log_p)
519 Float ans, c, t, u, z, a0, b0, apb;
529 z = a * log(x) - betaln(a, b);
530 ans = log_p ? z - log(a) : exp(z) / a;
549 z = (gam1(u) + 1.) / apb;
553 c = (gam1(a) + 1.) * (gam1(b) + 1.) / z;
556 ans += log(c * (b / apb));
558 ans *= c * (b / apb);
563 m = (int)trunc(b0 - 1.);
566 for (i = 1; i <= m; ++i) {
578 t = (gam1(u) + 1.) / apb;
584 ans = z + log(a0 / a) + log1p(gam1(b0)) - log(t);
586 ans = exp(z) * (a0 / a) * (gam1(b0) + 1.) / t;
591 u = gamln1(a0) + algdiv(a0, b0);
595 ans = z + log(a0 / a);
597 ans = a0 / a * exp(z);
600 if (ans == R_D__0 || (!log_p && a <= eps * 0.1)) {
613 c *= (0.5 - b / n + 0.5) * x;
616 }
while (n < 1e7 && max_fabs(w) > tol);
619 if(( log_p && !(a*
sum > -1. && fabs(log1p(a *
sum)) < eps*fabs(ans))) ||
620 (!log_p && fabs(a*sum + 1.) != 1.))
622 " bpser(a=%g, b=%g, x=%g,...) did not converge (n=1e7, |w|/tol=%g > 1; A=%g)",
623 a,b,x, fabs(w)/tol, ans);
626 if (a*sum > -1.) ans += log1p(a * sum);
630 "pbeta(*, log.p=TRUE) -> bpser(a=%g, b=%g, x=%g,...) underflow to -Inf",
634 }
else if (a*sum > -1.)
635 ans *= (a * sum + 1.);
641 template<
class Float>
static Float bup(Float a, Float b, Float x, Float y,
int n, Float eps,
657 if (n > 1 && a >= 1. && apb >= ap1 * 1.1) {
658 mu = (int)fabs(exparg(1));
662 d = exp(-(
double) mu);
671 ? brcmp1(mu, a, b, x, y, TRUE) - log(a)
672 : brcmp1(mu, a, b, x, y, FALSE) / a;
674 (give_log && ret_val == ML_NEGINF) || (!give_log && ret_val == 0.))
685 Float r = (b - 1.) * x / y - a;
687 k = (r < nm1) ? (
int) trunc(r) : nm1;
693 for (i = 0; i < k; ++i) {
695 d *= (apb + l) / (ap1 + l) * x;
702 for (i = k; i < nm1; ++i) {
704 d *= (apb + l) / (ap1 + l) * x;
719 template<
class Float>
720 static Float bfrac(Float a, Float b, Float x, Float y, Float lambda,
721 Float eps,
int log_p)
728 Float c, e, n, p, r, s, t, w, c0, c1, r0, an, bn, yp1, anp1, bnp1,
731 if(!R_FINITE(lambda))
return ML_NAN;
732 brc = brcomp(a, b, x, y, log_p);
737 if (!log_p && brc == 0.) {
767 alpha = p * (p + c0) * e * e * (w * x);
768 e = (t + 1.) / (c1 + t + t);
769 beta = n + w / s + e * (c + n * yp1);
775 t = alpha * an + beta * anp1; an = anp1; anp1 = t;
776 t = alpha * bn + beta * bnp1; bn = bnp1; bnp1 = t;
780 #ifdef _not_normally_DEBUG_bfrac 782 if (fabs(r - r0) <= eps * r)
794 " bfrac(a=%g, b=%g, x=%g, y=%g, lambda=%g) did *not* converge (in 10000 steps)\n",
796 return (log_p ? brc + log(r) : brc * r);
799 template<
class Float>
static Float brcomp(Float a, Float b, Float x, Float y,
int log_p)
805 static double const__ = .398942280401433;
808 Float c, e, u, v, z, a0, b0, apb;
810 if (x == 0. || y == 0.) {
830 z = a * lnx + b * lny;
842 u = gamln1(a0) + algdiv(a0, b0);
844 return (log_p ? log(a0) + (z - u) : a0 * exp(z - u));
850 Float e_z = R_D_exp(z);
852 if (!log_p && e_z == 0.)
858 z = (gam1(u) + 1.) / apb;
863 c = (gam1(a) + 1.) * (gam1(b) + 1.) / z;
866 ? e_z + log(a0 * c) - log1p(a0/b0)
867 : e_z * (a0 * c) / (a0 / b0 + 1.));
873 n = (int)trunc(b0 - 1.);
876 for (i = 1; i <= n; ++i) {
888 t = (gam1(u) + 1.) / apb;
894 ? log(a0) + z + log1p(gam1(b0)) - log(t)
895 : a0 * exp(z) * (gam1(b0) + 1.) / t);
901 Float h, x0, y0, lambda;
906 lambda = a - (a + b) * x;
911 lambda = (a + b) * y - b;
926 z = log_p ? -(a * u + b * v) : exp(-(a * u + b * v));
929 ? -M_LN_SQRT_2PI + .5*log(b * x0) + z - bcorr(a,b)
930 : const__ * sqrt(b * x0) * z * exp(-bcorr(a, b)));
936 template<
class Float>
static Float brcmp1(
int mu, Float a, Float b, Float x, Float y,
int give_log)
942 static double const__ = .398942280401433;
946 Float c, t, u, v, z, a0, b0, apb;
954 }
else if (y > .375) {
964 z = a * lnx + b * lny;
967 return esum(mu, z, give_log);
977 u = gamln1(a0) + algdiv(a0, b0);
980 ? log(a0) + esum(mu, z - u, TRUE)
981 : a0 * esum(mu, z - u, FALSE);
983 }
else if (b0 <= 1.) {
985 Float ans = esum(mu, z, give_log);
986 if (ans == (give_log ? ML_NEGINF : 0.))
993 z = (gam1(u) + 1.) / apb;
999 ? log1p(gam1(a)) + log1p(gam1(b)) - log(z)
1000 : (gam1(a) + 1.) * (gam1(b) + 1.) / z;
1003 ? ans + log(a0) + c - log1p(a0 / b0)
1004 : ans * (a0 * c) / (a0 / b0 + 1.);
1009 int n = (int)trunc(b0 - 1.);
1012 for (
int i = 1; i <= n; ++i) {
1014 c *= b0 / (a0 + b0);
1025 t = (gam1(apb - 1.) + 1.) / apb;
1032 ? log(a0)+ esum(mu, z, TRUE) + log1p(gam1(b0)) - log(t)
1033 : a0 * esum(mu, z, FALSE) * (gam1(b0) + 1.) / t;
1041 Float h, x0, y0, lambda;
1047 lambda = (a + b) * y - b;
1052 lambda = a - (a + b) * x;
1054 Float lx0 = -log1p(b/a);
1057 Float e = -lambda / a;
1058 if (fabs(e) > 0.6) {
1060 u = e - log(x / x0);
1067 if (fabs(e) > 0.6) {
1069 v = e - log(y / y0);
1075 z = esum(mu, -(a * u + b * v), give_log);
1077 ? log(const__)+ (log(b) + lx0)/2. + z - bcorr(a, b)
1078 : const__ * sqrt(b * x0) * z * exp(-bcorr(a, b));
1083 template<
class Float>
static void bgrat(Float a, Float b, Float x, Float y, Float *w,
1084 Float eps,
int *ierr,
bool log_w)
1098 #define n_terms_bgrat 30 1099 Float c[n_terms_bgrat], d[n_terms_bgrat];
1100 Float bm1 = b - 0.5 - 0.5,
1103 lnx = (y > 0.375) ? log(x) : alnrel(-y),
1110 "bgrat(a=%g, b=%g, x=%g, y=%g): z=%g, b*z == 0 underflow, hence inaccurate pbeta()",
1124 log_r = log(b) + log1p(gam1(b)) + b * log(z) + nu * lnx,
1127 log_u = log_r - (algdiv(b, a) + b * log(nu)),
1133 if (log_u == ML_NEGINF) {
1137 bool u_0 = (u == 0.);
1140 ? ((*w == ML_NEGINF) ? 0. : exp( *w - log_u))
1141 : ((*w == 0.) ? 0. : exp(log(*w) - log_u));
1144 q_r = grat_r(b, z, log_r, eps),
1145 v = 0.25 / (nu * nu),
1146 t2 = lnx * 0.25 * lnx,
1149 t = 1., cn = 1., n2 = 0.;
1150 for (
int n = 1; n <= n_terms_bgrat; ++n) {
1151 Float bp2n = b + n2;
1152 j = (bp2n * (bp2n + 1.) * j + (z + bp2n + 1.) * t) * v;
1155 cn /= n2 * (n2 + 1.);
1161 for (
int i = 1; i <= nm1; ++i) {
1162 s += coef * c[i - 1] * d[nm1 - i];
1166 d[nm1] = bm1 * cn + s / n;
1167 Float dj = d[nm1] * j;
1173 if (fabs(dj) <= eps * (sum + l)) {
1176 }
else if(n == n_terms_bgrat) {
1179 "bgrat(a=%g, b=%g, x=%g) *no* convergence: NOTIFY R-core!\n dj=%g, rel.err=%g\n",
1180 a,b,x, dj, fabs(dj) /(sum + l));
1187 *w = logspace_add<Float>(*w, log_u + log(sum));
1189 *w += (u_0 ? exp(log_u + log(sum)) : u * sum);
1195 template<
class Float>
static Float grat_r(Float a, Float x, Float log_r, Float eps)
1214 else if (a == 0.5) {
1217 Float p = erf__(sqrt(x));
1218 return (0.5 - p + 0.5)*exp(-log_r);
1222 q_r = erfc1(1, sx)/sx * M_SQRT_PI;
1226 }
else if (x < 1.1) {
1231 tol = eps * 0.1 / (a + 1.), t;
1237 }
while (max_fabs(t) > tol);
1239 Float j = a * x * ((sum/6. - 0.5/(a + 2.)) * x + 1./(a + 1.)),
1244 if ((x >= 0.25 && (a < x / 2.59)) || (z > -0.13394)) {
1246 Float l = rexpm1(z),
1247 q = ((l + 0.5 + 0.5) * j - l) * g - h;
1253 return q * exp(-log_r);
1257 Float p = exp(z) * g * (0.5 - j + 0.5);
1259 return (0.5 - p + 0.5) * exp(-log_r);
1272 a2n_1 = x * a2n + c * a2n_1;
1273 b2n_1 = x * b2n + c * b2n_1;
1274 am0 = a2n_1 / b2n_1;
1277 a2n = a2n_1 + c_a * a2n;
1278 b2n = b2n_1 + c_a * b2n;
1280 }
while (max_fabs(an0 - am0) >= eps * max_fabs(an0));
1288 template<
class Float>
static Float basym(Float a, Float b, Float lambda, Float eps,
int log_p)
1304 static double const e0 = 1.12837916709551;
1305 static double const e1 = .353553390593274;
1306 static double const ln_e0 = 0.120782237635245;
1308 Float a0[num_IT + 1], b0[num_IT + 1], c[num_IT + 1], d[num_IT + 1];
1310 Float f = a * rlog1(-lambda/a) + b * rlog1(lambda/b), t;
1328 w0 = 1. / sqrt(a * (h + 1.));
1333 w0 = 1. / sqrt(b * (h + 1.));
1336 a0[0] = r1 * .66666666666666663;
1337 c[0] = a0[0] * -0.5;
1339 Float j0 = 0.5 / e0 * erfc1(1, z0),
1341 sum = j0 + d[0] * w0 * j1;
1349 for (
int n = 2; n <= num_IT; n += 2) {
1351 a0[n - 1] = r0 * 2. * (h * hn + 1.) / (n + 2.);
1354 a0[np1 - 1] = r1 * 2. * s / (n + 3.);
1356 for (
int i = n; i <= np1; ++i) {
1357 Float r = (i + 1.) * -0.5;
1359 for (
int m = 2; m <= i; ++m) {
1361 for (
int j = 1; j <= m-1; ++j) {
1363 bsum += (j * r - mmj) * a0[j - 1] * b0[mmj - 1];
1365 b0[m - 1] = r * a0[m - 1] + bsum / m;
1367 c[i - 1] = b0[i - 1] / (i + 1.);
1370 for (
int j = 1; j <= i-1; ++j) {
1371 dsum += d[i - j - 1] * c[j - 1];
1373 d[i - 1] = -(dsum + c[i - 1]);
1376 j0 = e1 * znm1 + (n - 1.) * j0;
1377 j1 = e1 * zn + n * j1;
1381 Float t0 = d[n - 1] * w * j0;
1383 Float t1 = d[np1 - 1] * w * j1;
1385 if (fabs(t0) + fabs(t1) <= eps *
sum) {
1391 return ln_e0 + t - bcorr(a, b) + log(sum);
1393 Float u = exp(-bcorr(a, b));
1394 return e0 * t * u *
sum;
1400 static inline double exparg(
int l)
1413 static double const lnb = .69314718055995;
1414 int m = (l == 0) ? Rf_i1mach(16) : Rf_i1mach(15) - 1;
1416 return m * lnb * .99999;
1419 template<
class Float>
static Float esum(
int mu, Float x,
int give_log)
1426 return x + (double) mu;
1431 if (mu > 0)
return exp((
double) mu) * exp(x);
1433 if (w < 0.)
return exp((
double) mu) * exp(x);
1436 if (mu < 0)
return exp((
double) mu) * exp(x);
1438 if (w > 0.)
return exp((
double) mu) * exp(x);
1444 template<
class Float> Float rexpm1(Float x)
1450 static double p1 = 9.14041914819518e-10;
1451 static double p2 = .0238082361044469;
1452 static double q1 = -.499999999085958;
1453 static double q2 = .107141568980644;
1454 static double q3 = -.0119041179760821;
1455 static double q4 = 5.95130811860248e-4;
1457 if (fabs(x) <= 0.15) {
1458 return x * (((p2 * x + p1) * x + 1.) /
1459 ((((q4 * x + q3) * x + q2) * x + q1) * x + 1.));
1464 return w * (0.5 - 1. / w + 0.5);
1466 return w - 0.5 - 0.5;
1471 template<
class Float>
static Float alnrel(Float a)
1477 if (fabs(a) > 0.375)
1481 p1 = -1.29418923021993,
1482 p2 = .405303492862024,
1483 p3 = -.0178874546012214,
1484 q1 = -1.62752256355323,
1485 q2 = .747811014037616,
1486 q3 = -.0845104217945565;
1490 w = (((p3 * t2 + p2) * t2 + p1) * t2 + 1.) /
1491 (((q3 * t2 + q2) * t2 + q1) * t2 + 1.);
1496 template<
class Float>
static Float rlog1(Float x)
1502 static double a = .0566749439387324;
1503 static double b = .0456512608815524;
1504 static double p0 = .333333333333333;
1505 static double p1 = -.224696413112536;
1506 static double p2 = .00620886815375787;
1507 static double q1 = -1.27408923933623;
1508 static double q2 = .354508718369557;
1510 Float h, r, t, w, w1;
1511 if (x < -0.39 || x > 0.57) {
1521 else if (x > 0.18) {
1534 w = ((p2 * t + p1) * t + p0) / ((q2 * t + q1) * t + 1.);
1535 return t * 2. * (1. / (1. - r) - r * w) + w1;
1539 template<
class Float>
static Float erf__(Float x)
1547 static double c = .564189583547756;
1548 static double a[5] = { 7.7105849500132e-5,-.00133733772997339,
1549 .0323076579225834,.0479137145607681,.128379167095513 };
1550 static double b[3] = { .00301048631703895,.0538971687740286,
1552 static double p[8] = { -1.36864857382717e-7,.564195517478974,
1553 7.21175825088309,43.1622272220567,152.98928504694,
1554 339.320816734344,451.918953711873,300.459261020162 };
1555 static double q[8] = { 1.,12.7827273196294,77.0001529352295,
1556 277.585444743988,638.980264465631,931.35409485061,
1557 790.950925327898,300.459260956983 };
1558 static double r[5] = { 2.10144126479064,26.2370141675169,
1559 21.3688200555087,4.6580782871847,.282094791773523 };
1560 static double s[4] = { 94.153775055546,187.11481179959,
1561 99.0191814623914,18.0124575948747 };
1564 Float t, x2, ax, bot, top;
1569 top = (((a[0] * t + a[1]) * t + a[2]) * t + a[3]) * t + a[4] + 1.;
1570 bot = ((b[0] * t + b[1]) * t + b[2]) * t + 1.;
1572 return x * (top / bot);
1578 top = ((((((p[0] * ax + p[1]) * ax + p[2]) * ax + p[3]) * ax + p[4]) * ax
1579 + p[5]) * ax + p[6]) * ax + p[7];
1580 bot = ((((((q[0] * ax + q[1]) * ax + q[2]) * ax + q[3]) * ax + q[4]) * ax
1581 + q[5]) * ax + q[6]) * ax + q[7];
1582 Float R = 0.5 - exp(-x * x) * top / bot + 0.5;
1583 return (x < 0) ? -R : R;
1589 return x > 0 ? 1 : -1;
1595 top = (((r[0] * t + r[1]) * t + r[2]) * t + r[3]) * t + r[4];
1596 bot = (((s[0] * t + s[1]) * t + s[2]) * t + s[3]) * t + 1.;
1597 t = (c - top / (x2 * bot)) / ax;
1598 Float R = 0.5 - exp(-x2) * t + 0.5;
1599 return (x < 0) ? -R : R;
1602 template<
class Float>
static Float erfc1(
int ind, Float x)
1613 static double c = .564189583547756;
1614 static double a[5] = { 7.7105849500132e-5,-.00133733772997339,
1615 .0323076579225834,.0479137145607681,.128379167095513 };
1616 static double b[3] = { .00301048631703895,.0538971687740286,
1618 static double p[8] = { -1.36864857382717e-7,.564195517478974,
1619 7.21175825088309,43.1622272220567,152.98928504694,
1620 339.320816734344,451.918953711873,300.459261020162 };
1621 static double q[8] = { 1.,12.7827273196294,77.0001529352295,
1622 277.585444743988,638.980264465631,931.35409485061,
1623 790.950925327898,300.459260956983 };
1624 static double r[5] = { 2.10144126479064,26.2370141675169,
1625 21.3688200555087,4.6580782871847,.282094791773523 };
1626 static double s[4] = { 94.153775055546,187.11481179959,
1627 99.0191814623914,18.0124575948747 };
1630 Float e, t, w, bot, top;
1636 top = (((a[0] * t + a[1]) * t + a[2]) * t + a[3]) * t + a[4] + 1.,
1637 bot = ((b[0] * t + b[1]) * t + b[2]) * t + 1.;
1638 ret_val = 0.5 - x * (top / bot) + 0.5;
1640 ret_val = exp(t) * ret_val;
1646 top = ((((((p[0] * ax + p[1]) * ax + p[2]) * ax + p[3]) * ax + p[4]) * ax
1647 + p[5]) * ax + p[6]) * ax + p[7];
1648 bot = ((((((q[0] * ax + q[1]) * ax + q[2]) * ax + q[3]) * ax + q[4]) * ax
1649 + q[5]) * ax + q[6]) * ax + q[7];
1650 ret_val = top / bot;
1658 ret_val = exp(x * x) * 2.;
1662 if (ind == 0 && (x > 100. || x * x > -exparg(1))) {
1670 top = (((r[0] * t + r[1]) * t + r[2]) * t + r[3]) * t + r[4];
1671 bot = (((s[0] * t + s[1]) * t + s[2]) * t + s[3]) * t + 1.;
1672 ret_val = (c - t * top / bot) / ax;
1678 ret_val = exp(x * x) * 2. - ret_val;
1684 ret_val = (0.5 - e + 0.5) * exp(-t) * ret_val;
1686 ret_val = 2. - ret_val;
1692 template<
class Float>
static Float gam1(Float a)
1698 Float d, t, w, bot, top;
1707 r[9] = { -.422784335098468,-.771330383816272,
1708 -.244757765222226,.118378989872749,9.30357293360349e-4,
1709 -.0118290993445146,.00223047661158249,2.66505979058923e-4,
1710 -1.32674909766242e-4 },
1711 s1 = .273076135303957,
1712 s2 = .0559398236957378;
1714 top = (((((((r[8] * t + r[7]) * t + r[6]) * t + r[5]) * t + r[4]
1715 ) * t + r[3]) * t + r[2]) * t + r[1]) * t + r[0];
1716 bot = (s2 * t + s1) * t + 1.;
1722 return a * (w + 0.5 + 0.5);
1730 p[7] = { .577215664901533,-.409078193005776,
1731 -.230975380857675,.0597275330452234,.0076696818164949,
1732 -.00514889771323592,5.89597428611429e-4 },
1733 q[5] = { 1.,.427569613095214,.158451672430138,
1734 .0261132021441447,.00423244297896961 };
1736 top = (((((p[6] * t + p[5]) * t + p[4]) * t + p[3]) * t + p[2]
1737 ) * t + p[1]) * t + p[0];
1738 bot = (((q[4] * t + q[3]) * t + q[2]) * t + q[1]) * t + 1.;
1741 return t / a * (w - 0.5 - 0.5);
1747 template<
class Float>
static Float gamln1(Float a)
1755 static double p0 = .577215664901533;
1756 static double p1 = .844203922187225;
1757 static double p2 = -.168860593646662;
1758 static double p3 = -.780427615533591;
1759 static double p4 = -.402055799310489;
1760 static double p5 = -.0673562214325671;
1761 static double p6 = -.00271935708322958;
1762 static double q1 = 2.88743195473681;
1763 static double q2 = 3.12755088914843;
1764 static double q3 = 1.56875193295039;
1765 static double q4 = .361951990101499;
1766 static double q5 = .0325038868253937;
1767 static double q6 = 6.67465618796164e-4;
1768 w = ((((((p6 * a + p5)* a + p4)* a + p3)* a + p2)* a + p1)* a + p0) /
1769 ((((((q6 * a + q5)* a + q4)* a + q3)* a + q2)* a + q1)* a + 1.);
1773 static double r0 = .422784335098467;
1774 static double r1 = .848044614534529;
1775 static double r2 = .565221050691933;
1776 static double r3 = .156513060486551;
1777 static double r4 = .017050248402265;
1778 static double r5 = 4.97958207639485e-4;
1779 static double s1 = 1.24313399877507;
1780 static double s2 = .548042109832463;
1781 static double s3 = .10155218743983;
1782 static double s4 = .00713309612391;
1783 static double s5 = 1.16165475989616e-4;
1784 Float x = a - 0.5 - 0.5;
1785 w = (((((r5 * x + r4) * x + r3) * x + r2) * x + r1) * x + r0) /
1786 (((((s5 * x + s4) * x + s3) * x + s2) * x + s1) * x + 1.);
1791 template<
class Float>
static Float psi(Float x)
1812 static double piov4 = .785398163397448;
1814 static double dx0 = 1.461632144968362341262659542325721325;
1819 static double p1[7] = { .0089538502298197,4.77762828042627,
1820 142.441585084029,1186.45200713425,3633.51846806499,
1821 4138.10161269013,1305.60269827897 };
1822 static double q1[6] = { 44.8452573429826,520.752771467162,
1823 2210.0079924783,3641.27349079381,1908.310765963,
1824 6.91091682714533e-6 };
1832 static double p2[4] = { -2.12940445131011,-7.01677227766759,
1833 -4.48616543918019,-.648157123766197 };
1834 static double q2[4] = { 32.2703493791143,89.2920700481861,
1835 54.6117738103215,7.77788548522962 };
1841 Float den, aug, sgn, xmx0, xmax1, upper, xsmall;
1855 xmax1 = (double) INT_MAX;
1856 d2 = 0.5 / Rf_d1mach(3);
1857 if(xmax1 > d2) xmax1 = d2;
1870 if (fabs(x) <= xsmall) {
1897 nq = (int) trunc(w);
1899 nq = (int) trunc(w * 4.);
1900 w = (w - (double) nq * 0.25) * 4.;
1932 aug = sgn * (cos(z) / sin(z) * 4.);
1935 aug = sgn * (sin(z) / cos(z) * 4.);
1950 for (i = 1; i <= 5; ++i) {
1951 den = (den + q1[i - 1]) * x;
1952 upper = (upper + p1[i]) * x;
1955 den = (upper + p1[6]) / (den + q1[5]);
1957 return den * xmx0 + aug;
1971 for (i = 1; i <= 3; ++i) {
1972 den = (den + q2[i - 1]) * w;
1973 upper = (upper + p2[i]) * w;
1976 aug = upper / (den + q2[3]) - 0.5 / x + aug;
1978 return aug + log(x);
1987 template<
class Float>
static Float betaln(Float a0, Float b0)
1993 static double e = .918938533204673;
2005 return gamln(a) + (gamln(b) - gamln(a+b));
2007 return gamln(a) + algdiv(a, b);
2017 return gamln(a) + gamln(b) - gsumln(a, b);
2025 return gamln(a) + algdiv(a, b);
2030 n = (int)trunc(a - 1.);
2032 for (
int i = 1; i <= n; ++i) {
2040 return w + gamln(a) + algdiv(a, b);
2045 n = (int)trunc(b - 1.);
2047 for (
int i = 1; i <= n; ++i) {
2051 return w + log(z) + (gamln(a) + (gamln(b) - gsumln(a, b)));
2054 int n = (int)trunc(a - 1.);
2056 for (
int i = 1; i <= n; ++i) {
2058 w *= a / (a / b + 1.);
2060 return log(w) - n * log(b) + (gamln(a) + algdiv(a, b));
2071 u = -(a - 0.5) * log(h / (h + 1.)),
2074 return log(b) * -0.5 + e + w - v - u;
2076 return log(b) * -0.5 + e + w - u - v;
2081 template<
class Float>
static Float gsumln(Float a, Float b)
2088 Float x = a + b - 2.;
2091 return gamln1(x + 1.);
2095 return gamln1(x) + alnrel(x);
2097 return gamln1(x - 1.) + log(x * (x + 1.));
2101 template<
class Float>
static Float bcorr(Float a0, Float b0)
2112 static double c0 = .0833333333333333;
2113 static double c1 = -.00277777777760991;
2114 static double c2 = 7.9365066682539e-4;
2115 static double c3 = -5.9520293135187e-4;
2116 static double c4 = 8.37308034031215e-4;
2117 static double c5 = -.00165322962780713;
2123 Float a, b, c, h, t, w, x, s3, s5, x2, s7, s9, s11;
2136 s5 = x + x2 * s3 + 1.;
2137 s7 = x + x2 * s5 + 1.;
2138 s9 = x + x2 * s7 + 1.;
2139 s11 = x + x2 * s9 + 1.;
2146 w = ((((c5 * s11 * t + c4 * s9) * t + c3 * s7) * t + c2 * s5) * t + c1 *
2155 ret_val = (((((c5 * t + c4) * t + c3) * t + c2) * t + c1) * t + c0) / a +
2160 template<
class Float>
static Float algdiv(Float a, Float b)
2175 static double c0 = .0833333333333333;
2176 static double c1 = -.00277777777760991;
2177 static double c2 = 7.9365066682539e-4;
2178 static double c3 = -5.9520293135187e-4;
2179 static double c4 = 8.37308034031215e-4;
2180 static double c5 = -.00165322962780713;
2182 Float c, d, h, t, u, v, w, x, s3, s5, x2, s7, s9, s11;
2202 s5 = x + x2 * s3 + 1.;
2203 s7 = x + x2 * s5 + 1.;
2204 s9 = x + x2 * s7 + 1.;
2205 s11 = x + x2 * s9 + 1.;
2210 w = ((((c5 * s11 * t + c4 * s9) * t + c3 * s7) * t + c2 * s5) * t + c1 *
2216 u = d * alnrel(a / b);
2217 v = a * (log(b) - 1.);
2224 template<
class Float>
static Float gamln(Float a)
2234 static double d = .418938533204673;
2236 static double c0 = .0833333333333333;
2237 static double c1 = -.00277777777760991;
2238 static double c2 = 7.9365066682539e-4;
2239 static double c3 = -5.9520293135187e-4;
2240 static double c4 = 8.37308034031215e-4;
2241 static double c5 = -.00165322962780713;
2244 return gamln1(a) - log(a);
2246 return gamln1(a - 0.5 - 0.5);
2249 int i, n = (int)trunc(a - 1.25);
2252 for (i = 1; i <= n; ++i) {
2256 return gamln1(t - 1.) + log(w);
2259 Float t = 1. / (a * a);
2260 Float w = (((((c5 * t + c4) * t + c3) * t + c2) * t + c1) * t + c0) / a;
2261 return d + w + (a - 0.5) * (log(a) - 1.);
Type min(const vector< Type > &x)
Type sum(Vector< Type > x)
Type max(const vector< Type > &x)