igami.c 3.3 KB

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