gsl_specfunc__beta_inc.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* specfunc/beta_inc.c
  2. *
  3. * Copyright (C) 2007 Brian Gough
  4. * Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. /* Author: G. Jungman */
  21. #include "gsl__config.h"
  22. #include "gsl_math.h"
  23. #include "gsl_errno.h"
  24. #include "gsl_sf_log.h"
  25. #include "gsl_sf_exp.h"
  26. #include "gsl_sf_gamma.h"
  27. #include "gsl_sf_hyperg.h"
  28. #include "gsl_specfunc__error.h"
  29. #include "gsl_specfunc__check.h"
  30. static double
  31. isnegint (const double x)
  32. {
  33. return (x < 0) && (x == floor(x));
  34. }
  35. static
  36. int
  37. beta_cont_frac(
  38. const double a,
  39. const double b,
  40. const double x,
  41. gsl_sf_result * result
  42. )
  43. {
  44. const unsigned int max_iter = 512; /* control iterations */
  45. const double cutoff = 2.0 * GSL_DBL_MIN; /* control the zero cutoff */
  46. unsigned int iter_count = 0;
  47. double cf;
  48. /* standard initialization for continued fraction */
  49. double num_term = 1.0;
  50. double den_term = 1.0 - (a+b)*x/(a+1.0);
  51. if (fabs(den_term) < cutoff) den_term = cutoff;
  52. den_term = 1.0/den_term;
  53. cf = den_term;
  54. while(iter_count < max_iter) {
  55. const int k = iter_count + 1;
  56. double coeff = k*(b-k)*x/(((a-1.0)+2*k)*(a+2*k));
  57. double delta_frac;
  58. /* first step */
  59. den_term = 1.0 + coeff*den_term;
  60. num_term = 1.0 + coeff/num_term;
  61. if(fabs(den_term) < cutoff) den_term = cutoff;
  62. if(fabs(num_term) < cutoff) num_term = cutoff;
  63. den_term = 1.0/den_term;
  64. delta_frac = den_term * num_term;
  65. cf *= delta_frac;
  66. coeff = -(a+k)*(a+b+k)*x/((a+2*k)*(a+2*k+1.0));
  67. /* second step */
  68. den_term = 1.0 + coeff*den_term;
  69. num_term = 1.0 + coeff/num_term;
  70. if(fabs(den_term) < cutoff) den_term = cutoff;
  71. if(fabs(num_term) < cutoff) num_term = cutoff;
  72. den_term = 1.0/den_term;
  73. delta_frac = den_term*num_term;
  74. cf *= delta_frac;
  75. if(fabs(delta_frac-1.0) < 2.0*GSL_DBL_EPSILON) break;
  76. ++iter_count;
  77. }
  78. result->val = cf;
  79. result->err = iter_count * 4.0 * GSL_DBL_EPSILON * fabs(cf);
  80. if(iter_count >= max_iter)
  81. GSL_ERROR ("error", GSL_EMAXITER);
  82. else
  83. return GSL_SUCCESS;
  84. }
  85. /*-*-*-*-*-*-*-*-*-*-*-* Functions with Error Codes *-*-*-*-*-*-*-*-*-*-*-*/
  86. int
  87. gsl_sf_beta_inc_e(
  88. const double a,
  89. const double b,
  90. const double x,
  91. gsl_sf_result * result
  92. )
  93. {
  94. if(x < 0.0 || x > 1.0) {
  95. DOMAIN_ERROR(result);
  96. } else if (isnegint(a) || isnegint(b)) {
  97. DOMAIN_ERROR(result);
  98. } else if (isnegint(a+b)) {
  99. DOMAIN_ERROR(result);
  100. } else if(x == 0.0) {
  101. result->val = 0.0;
  102. result->err = 0.0;
  103. return GSL_SUCCESS;
  104. }
  105. else if(x == 1.0) {
  106. result->val = 1.0;
  107. result->err = 0.0;
  108. return GSL_SUCCESS;
  109. } else if (a <= 0 || b <= 0) {
  110. gsl_sf_result f, beta;
  111. int stat;
  112. const int stat_f = gsl_sf_hyperg_2F1_e(a, 1-b, a+1, x, &f);
  113. const int stat_beta = gsl_sf_beta_e(a, b, &beta);
  114. double prefactor = (pow(x, a) / a);
  115. result->val = prefactor * f.val / beta.val;
  116. result->err = fabs(prefactor) * f.err/ fabs(beta.val) + fabs(result->val/beta.val) * beta.err;
  117. stat = GSL_ERROR_SELECT_2(stat_f, stat_beta);
  118. if(stat == GSL_SUCCESS) {
  119. CHECK_UNDERFLOW(result);
  120. }
  121. return stat;
  122. } else {
  123. gsl_sf_result ln_beta;
  124. gsl_sf_result ln_x;
  125. gsl_sf_result ln_1mx;
  126. gsl_sf_result prefactor;
  127. const int stat_ln_beta = gsl_sf_lnbeta_e(a, b, &ln_beta);
  128. const int stat_ln_1mx = gsl_sf_log_1plusx_e(-x, &ln_1mx);
  129. const int stat_ln_x = gsl_sf_log_e(x, &ln_x);
  130. const int stat_ln = GSL_ERROR_SELECT_3(stat_ln_beta, stat_ln_1mx, stat_ln_x);
  131. const double ln_pre_val = -ln_beta.val + a * ln_x.val + b * ln_1mx.val;
  132. const double ln_pre_err = ln_beta.err + fabs(a*ln_x.err) + fabs(b*ln_1mx.err);
  133. const int stat_exp = gsl_sf_exp_err_e(ln_pre_val, ln_pre_err, &prefactor);
  134. if(stat_ln != GSL_SUCCESS) {
  135. result->val = 0.0;
  136. result->err = 0.0;
  137. GSL_ERROR ("error", GSL_ESANITY);
  138. }
  139. if(x < (a + 1.0)/(a+b+2.0)) {
  140. /* Apply continued fraction directly. */
  141. gsl_sf_result cf;
  142. const int stat_cf = beta_cont_frac(a, b, x, &cf);
  143. int stat;
  144. result->val = prefactor.val * cf.val / a;
  145. result->err = (fabs(prefactor.err * cf.val) + fabs(prefactor.val * cf.err))/a;
  146. stat = GSL_ERROR_SELECT_2(stat_exp, stat_cf);
  147. if(stat == GSL_SUCCESS) {
  148. CHECK_UNDERFLOW(result);
  149. }
  150. return stat;
  151. }
  152. else {
  153. /* Apply continued fraction after hypergeometric transformation. */
  154. gsl_sf_result cf;
  155. const int stat_cf = beta_cont_frac(b, a, 1.0-x, &cf);
  156. int stat;
  157. const double term = prefactor.val * cf.val / b;
  158. result->val = 1.0 - term;
  159. result->err = fabs(prefactor.err * cf.val)/b;
  160. result->err += fabs(prefactor.val * cf.err)/b;
  161. result->err += 2.0 * GSL_DBL_EPSILON * (1.0 + fabs(term));
  162. stat = GSL_ERROR_SELECT_2(stat_exp, stat_cf);
  163. if(stat == GSL_SUCCESS) {
  164. CHECK_UNDERFLOW(result);
  165. }
  166. return stat;
  167. }
  168. }
  169. }
  170. /*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/
  171. #include "gsl_specfunc__eval.h"
  172. double gsl_sf_beta_inc(const double a, const double b, const double x)
  173. {
  174. EVAL_RESULT(gsl_sf_beta_inc_e(a, b, x, &result));
  175. }