gsl_specfunc__beta.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* specfunc/beta.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. /* Author: G. Jungman */
  20. #include "gsl__config.h"
  21. #include "gsl_math.h"
  22. #include "gsl_errno.h"
  23. #include "gsl_sf_exp.h"
  24. #include "gsl_sf_log.h"
  25. #include "gsl_sf_psi.h"
  26. #include "gsl_sf_gamma.h"
  27. #include "gsl_specfunc__error.h"
  28. static double
  29. isnegint (const double x)
  30. {
  31. return (x < 0) && (x == floor(x));
  32. }
  33. int
  34. gsl_sf_lnbeta_e(const double x, const double y, gsl_sf_result * result)
  35. {
  36. double sgn;
  37. int status = gsl_sf_lnbeta_sgn_e(x,y,result,&sgn);
  38. if (sgn == -1) {
  39. DOMAIN_ERROR(result);
  40. }
  41. return status;
  42. }
  43. int
  44. gsl_sf_lnbeta_sgn_e(const double x, const double y, gsl_sf_result * result, double * sgn)
  45. {
  46. /* CHECK_POINTER(result) */
  47. if(x == 0.0 || y == 0.0) {
  48. *sgn = 0.0;
  49. DOMAIN_ERROR(result);
  50. } else if (isnegint(x) || isnegint(y)) {
  51. *sgn = 0.0;
  52. DOMAIN_ERROR(result); /* not defined for negative integers */
  53. }
  54. /* See if we can handle the postive case with min/max < 0.2 */
  55. if (x > 0 && y > 0) {
  56. const double max = GSL_MAX(x,y);
  57. const double min = GSL_MIN(x,y);
  58. const double rat = min/max;
  59. if(rat < 0.2) {
  60. /* min << max, so be careful
  61. * with the subtraction
  62. */
  63. double lnpre_val;
  64. double lnpre_err;
  65. double lnpow_val;
  66. double lnpow_err;
  67. double t1, t2, t3;
  68. gsl_sf_result lnopr;
  69. gsl_sf_result gsx, gsy, gsxy;
  70. gsl_sf_gammastar_e(x, &gsx);
  71. gsl_sf_gammastar_e(y, &gsy);
  72. gsl_sf_gammastar_e(x+y, &gsxy);
  73. gsl_sf_log_1plusx_e(rat, &lnopr);
  74. lnpre_val = log(gsx.val*gsy.val/gsxy.val * M_SQRT2*M_SQRTPI);
  75. lnpre_err = gsx.err/gsx.val + gsy.err/gsy.val + gsxy.err/gsxy.val;
  76. t1 = min*log(rat);
  77. t2 = 0.5*log(min);
  78. t3 = (x+y-0.5)*lnopr.val;
  79. lnpow_val = t1 - t2 - t3;
  80. lnpow_err = GSL_DBL_EPSILON * (fabs(t1) + fabs(t2) + fabs(t3));
  81. lnpow_err += fabs(x+y-0.5) * lnopr.err;
  82. result->val = lnpre_val + lnpow_val;
  83. result->err = lnpre_err + lnpow_err;
  84. result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  85. *sgn = 1.0;
  86. return GSL_SUCCESS;
  87. }
  88. }
  89. /* General case - Fallback */
  90. {
  91. gsl_sf_result lgx, lgy, lgxy;
  92. double sgx, sgy, sgxy, xy = x+y;
  93. int stat_gx = gsl_sf_lngamma_sgn_e(x, &lgx, &sgx);
  94. int stat_gy = gsl_sf_lngamma_sgn_e(y, &lgy, &sgy);
  95. int stat_gxy = gsl_sf_lngamma_sgn_e(xy, &lgxy, &sgxy);
  96. *sgn = sgx * sgy * sgxy;
  97. result->val = lgx.val + lgy.val - lgxy.val;
  98. result->err = lgx.err + lgy.err + lgxy.err;
  99. result->err += 2.0 * GSL_DBL_EPSILON * (fabs(lgx.val) + fabs(lgy.val) + fabs(lgxy.val));
  100. result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  101. return GSL_ERROR_SELECT_3(stat_gx, stat_gy, stat_gxy);
  102. }
  103. }
  104. int
  105. gsl_sf_beta_e(const double x, const double y, gsl_sf_result * result)
  106. {
  107. if((x > 0 && y > 0) && x < 50.0 && y < 50.0) {
  108. /* Handle the easy case */
  109. gsl_sf_result gx, gy, gxy;
  110. gsl_sf_gamma_e(x, &gx);
  111. gsl_sf_gamma_e(y, &gy);
  112. gsl_sf_gamma_e(x+y, &gxy);
  113. result->val = (gx.val*gy.val)/gxy.val;
  114. result->err = gx.err * fabs(gy.val/gxy.val);
  115. result->err += gy.err * fabs(gx.val/gxy.val);
  116. result->err += fabs((gx.val*gy.val)/(gxy.val*gxy.val)) * gxy.err;
  117. result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  118. return GSL_SUCCESS;
  119. }
  120. else if (isnegint(x) || isnegint(y)) {
  121. DOMAIN_ERROR(result);
  122. } else if (isnegint(x+y)) { /* infinity in the denominator */
  123. result->val = 0.0;
  124. result->err = 0.0;
  125. return GSL_SUCCESS;
  126. } else {
  127. gsl_sf_result lb;
  128. double sgn;
  129. int stat_lb = gsl_sf_lnbeta_sgn_e(x, y, &lb, &sgn);
  130. if(stat_lb == GSL_SUCCESS) {
  131. int status = gsl_sf_exp_err_e(lb.val, lb.err, result);
  132. result->val *= sgn;
  133. return status;
  134. }
  135. else {
  136. result->val = 0.0;
  137. result->err = 0.0;
  138. return stat_lb;
  139. }
  140. }
  141. }
  142. /*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/
  143. #include "gsl_specfunc__eval.h"
  144. double gsl_sf_lnbeta(const double x, const double y)
  145. {
  146. EVAL_RESULT(gsl_sf_lnbeta_e(x, y, &result));
  147. }
  148. double gsl_sf_beta(const double x, const double y)
  149. {
  150. EVAL_RESULT(gsl_sf_beta_e(x, y, &result));
  151. }