igami.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 <math.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 )
  63. double a, y0;
  64. {
  65. double x0, x1, x, yl, yh, y, d, lgm, dithresh;
  66. int i, dir;
  67. /* bound the solution */
  68. x0 = MAXNUM;
  69. yl = 0;
  70. x1 = 0;
  71. yh = 1.0;
  72. dithresh = 5.0 * MACHEP;
  73. /* approximation to inverse function */
  74. d = 1.0/(9.0*a);
  75. y = ( 1.0 - d - ndtri(y0) * sqrt(d) );
  76. x = a * y * y * y;
  77. lgm = lgam(a);
  78. for( i=0; i<10; i++ )
  79. {
  80. if( x > x0 || x < x1 )
  81. goto ihalve;
  82. y = igamc(a,x);
  83. if( y < yl || y > yh )
  84. goto ihalve;
  85. if( y < y0 )
  86. {
  87. x0 = x;
  88. yl = y;
  89. }
  90. else
  91. {
  92. x1 = x;
  93. yh = y;
  94. }
  95. /* compute the derivative of the function at this point */
  96. d = (a - 1.0) * log(x) - x - lgm;
  97. if( d < -MAXLOG )
  98. goto ihalve;
  99. d = -exp(d);
  100. /* compute the step to the next approximation of x */
  101. d = (y - y0)/d;
  102. if( fabs(d/x) < MACHEP )
  103. goto done;
  104. x = x - d;
  105. }
  106. /* Resort to interval halving if Newton iteration did not converge. */
  107. ihalve:
  108. d = 0.0625;
  109. if( x0 == MAXNUM )
  110. {
  111. if( x <= 0.0 )
  112. x = 1.0;
  113. while( x0 == MAXNUM )
  114. {
  115. x = (1.0 + d) * x;
  116. y = igamc( a, x );
  117. if( y < y0 )
  118. {
  119. x0 = x;
  120. yl = y;
  121. break;
  122. }
  123. d = d + d;
  124. }
  125. }
  126. d = 0.5;
  127. dir = 0;
  128. for( i=0; i<400; i++ )
  129. {
  130. x = x1 + d * (x0 - x1);
  131. y = igamc( a, x );
  132. lgm = (x0 - x1)/(x1 + x0);
  133. if( fabs(lgm) < dithresh )
  134. break;
  135. lgm = (y - y0)/y0;
  136. if( fabs(lgm) < dithresh )
  137. break;
  138. if( x <= 0.0 )
  139. break;
  140. if( y >= y0 )
  141. {
  142. x1 = x;
  143. yh = y;
  144. if( dir < 0 )
  145. {
  146. dir = 0;
  147. d = 0.5;
  148. }
  149. else if( dir > 1 )
  150. d = 0.5 * d + 0.5;
  151. else
  152. d = (y0 - yl)/(yh - yl);
  153. dir += 1;
  154. }
  155. else
  156. {
  157. x0 = x;
  158. yl = y;
  159. if( dir > 0 )
  160. {
  161. dir = 0;
  162. d = 0.5;
  163. }
  164. else if( dir < -1 )
  165. d = 0.5 * d;
  166. else
  167. d = (y0 - yl)/(yh - yl);
  168. dir -= 1;
  169. }
  170. }
  171. if( x == 0.0 )
  172. mtherr( "igami", UNDERFLOW );
  173. done:
  174. return( x );
  175. }