igamif.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* igamif()
  2. *
  3. * Inverse of complemented imcomplete gamma integral
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * float a, x, y, igamif();
  10. *
  11. * x = igamif( a, y );
  12. *
  13. *
  14. *
  15. * DESCRIPTION:
  16. *
  17. * Given y, the function finds x such that
  18. *
  19. * igamc( a, x ) = y.
  20. *
  21. * Starting with the approximate value
  22. *
  23. * 3
  24. * x = a t
  25. *
  26. * where
  27. *
  28. * t = 1 - d - ndtri(y) sqrt(d)
  29. *
  30. * and
  31. *
  32. * d = 1/9a,
  33. *
  34. * the routine performs up to 10 Newton iterations to find the
  35. * root of igamc(a,x) - y = 0.
  36. *
  37. *
  38. * ACCURACY:
  39. *
  40. * Tested for a ranging from 0 to 100 and x from 0 to 1.
  41. *
  42. * Relative error:
  43. * arithmetic domain # trials peak rms
  44. * IEEE 0,100 5000 1.0e-5 1.5e-6
  45. *
  46. */
  47. /*
  48. Cephes Math Library Release 2.2: July, 1992
  49. Copyright 1984, 1987, 1992 by Stephen L. Moshier
  50. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  51. */
  52. #include <math.h>
  53. extern float MACHEPF, MAXLOGF;
  54. #define fabsf(x) ( (x) < 0 ? -(x) : (x) )
  55. #ifdef ANSIC
  56. float igamcf(float, float);
  57. float ndtrif(float), expf(float), logf(float), sqrtf(float), lgamf(float);
  58. #else
  59. float igamcf();
  60. float ndtrif(), expf(), logf(), sqrtf(), lgamf();
  61. #endif
  62. float igamif( float aa, float yy0 )
  63. {
  64. float a, y0, d, y, x0, lgm;
  65. int i;
  66. a = aa;
  67. y0 = yy0;
  68. /* approximation to inverse function */
  69. d = 1.0/(9.0*a);
  70. y = ( 1.0 - d - ndtrif(y0) * sqrtf(d) );
  71. x0 = a * y * y * y;
  72. lgm = lgamf(a);
  73. for( i=0; i<10; i++ )
  74. {
  75. if( x0 <= 0.0 )
  76. {
  77. mtherr( "igamif", UNDERFLOW );
  78. return(0.0);
  79. }
  80. y = igamcf(a,x0);
  81. /* compute the derivative of the function at this point */
  82. d = (a - 1.0) * logf(x0) - x0 - lgm;
  83. if( d < -MAXLOGF )
  84. {
  85. mtherr( "igamif", UNDERFLOW );
  86. goto done;
  87. }
  88. d = -expf(d);
  89. /* compute the step to the next approximation of x */
  90. if( d == 0.0 )
  91. goto done;
  92. d = (y - y0)/d;
  93. x0 = x0 - d;
  94. if( i < 3 )
  95. continue;
  96. if( fabsf(d/x0) < (2.0 * MACHEPF) )
  97. goto done;
  98. }
  99. done:
  100. return( x0 );
  101. }