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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
/* igami()
*
* Inverse of complemented imcomplete Gamma integral
*
*
*
* SYNOPSIS:
*
* double a, x, p, igami();
*
* x = igami( a, p );
*
* DESCRIPTION:
*
* Given p, the function finds x such that
*
* igamc( a, x ) = p.
*
* Starting with the approximate value
*
* 3
* x = a t
*
* where
*
* t = 1 - d - ndtri(p) sqrt(d)
*
* and
*
* d = 1/9a,
*
* the routine performs up to 10 Newton iterations to find the
* root of igamc(a,x) - p = 0.
*
* ACCURACY:
*
* Tested at random a, p in the intervals indicated.
*
* a p Relative error:
* arithmetic domain domain # trials peak rms
* IEEE 0.5,100 0,0.5 100000 1.0e-14 1.7e-15
* IEEE 0.01,0.5 0,0.5 100000 9.0e-14 3.4e-15
* IEEE 0.5,10000 0,0.5 20000 2.3e-13 3.8e-14
*/
/*
* Cephes Math Library Release 2.3: March, 1995
* Copyright 1984, 1987, 1995 by Stephen L. Moshier
*/
#include "mconf.h"
#include <stdio.h>
extern double MACHEP, MAXLOG, MINLOG;
double igami(a, y0)
double a, y0;
{
double x0, x1, x, yl, yh, y, d, lgm, dithresh;
int i, dir;
/* bound the solution */
x0 = NPY_INFINITY;
yl = 0;
x1 = 0;
yh = 1.0;
dithresh = 5.0 * MACHEP;
if ((y0 < 0.0) || (y0 > 1.0) || (a <= 0)) {
mtherr("igami", DOMAIN);
return (NPY_NAN);
}
if (y0 == 0.0) {
return (NPY_INFINITY);
}
if (y0 == 1.0) {
return 0.0;
}
/* approximation to inverse function */
d = 1.0 / (9.0 * a);
y = (1.0 - d - ndtri(y0) * sqrt(d));
x = a * y * y * y;
lgm = lgam(a);
for (i = 0; i < 10; i++) {
if (x > x0 || x < x1)
goto ihalve;
y = igamc(a, x);
if (y < yl || y > yh)
goto ihalve;
if (y < y0) {
x0 = x;
yl = y;
}
else {
x1 = x;
yh = y;
}
/* compute the derivative of the function at this point */
d = (a - 1.0) * log(x) - x - lgm;
if (d < -MAXLOG)
goto ihalve;
d = -exp(d);
/* compute the step to the next approximation of x */
d = (y - y0) / d;
if (fabs(d / x) < MACHEP)
goto done;
x = x - d;
}
/* Resort to interval halving if Newton iteration did not converge. */
ihalve:
d = 0.0625;
if (x0 == NPY_INFINITY) {
if (x <= 0.0)
x = 1.0;
while (x0 == NPY_INFINITY) {
x = (1.0 + d) * x;
y = igamc(a, x);
if (y < y0) {
x0 = x;
yl = y;
break;
}
d = d + d;
}
}
d = 0.5;
dir = 0;
for (i = 0; i < 400; i++) {
x = x1 + d * (x0 - x1);
y = igamc(a, x);
lgm = (x0 - x1) / (x1 + x0);
if (fabs(lgm) < dithresh)
break;
lgm = (y - y0) / y0;
if (fabs(lgm) < dithresh)
break;
if (x <= 0.0)
break;
if (y >= y0) {
x1 = x;
yh = y;
if (dir < 0) {
dir = 0;
d = 0.5;
}
else if (dir > 1)
d = 0.5 * d + 0.5;
else
d = (y0 - yl) / (yh - yl);
dir += 1;
}
else {
x0 = x;
yl = y;
if (dir > 0) {
dir = 0;
d = 0.5;
}
else if (dir < -1)
d = 0.5 * d;
else
d = (y0 - yl) / (yh - yl);
dir -= 1;
}
}
if (x == 0.0)
mtherr("igami", UNDERFLOW);
done:
return (x);
}
|