3 * Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman
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.
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.
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.
20 /* Author: G. Jungman */
23 #include <gsl/gsl_math.h>
24 #include <gsl/gsl_errno.h>
25 #include <gsl/gsl_sf_exp.h>
26 #include <gsl/gsl_sf_log.h>
27 #include <gsl/gsl_sf_psi.h>
28 #include <gsl/gsl_sf_gamma.h>
33 isnegint (const double x)
35 return (x < 0) && (x == floor(x));
39 gsl_sf_lnbeta_e(const double x, const double y, gsl_sf_result * result)
42 int status = gsl_sf_lnbeta_sgn_e(x,y,result,&sgn);
50 gsl_sf_lnbeta_sgn_e(const double x, const double y, gsl_sf_result * result, double * sgn)
52 /* CHECK_POINTER(result) */
54 if(x == 0.0 || y == 0.0) {
57 } else if (isnegint(x) || isnegint(y)) {
59 DOMAIN_ERROR(result); /* not defined for negative integers */
62 /* See if we can handle the postive case with min/max < 0.2 */
65 const double max = GSL_MAX(x,y);
66 const double min = GSL_MIN(x,y);
67 const double rat = min/max;
70 /* min << max, so be careful
71 * with the subtraction
79 gsl_sf_result gsx, gsy, gsxy;
80 gsl_sf_gammastar_e(x, &gsx);
81 gsl_sf_gammastar_e(y, &gsy);
82 gsl_sf_gammastar_e(x+y, &gsxy);
83 gsl_sf_log_1plusx_e(rat, &lnopr);
84 lnpre_val = log(gsx.val*gsy.val/gsxy.val * M_SQRT2*M_SQRTPI);
85 lnpre_err = gsx.err/gsx.val + gsy.err/gsy.val + gsxy.err/gsxy.val;
88 t3 = (x+y-0.5)*lnopr.val;
89 lnpow_val = t1 - t2 - t3;
90 lnpow_err = GSL_DBL_EPSILON * (fabs(t1) + fabs(t2) + fabs(t3));
91 lnpow_err += fabs(x+y-0.5) * lnopr.err;
92 result->val = lnpre_val + lnpow_val;
93 result->err = lnpre_err + lnpow_err;
94 result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
100 /* General case - Fallback */
102 gsl_sf_result lgx, lgy, lgxy;
103 double sgx, sgy, sgxy, xy = x+y;
104 int stat_gx = gsl_sf_lngamma_sgn_e(x, &lgx, &sgx);
105 int stat_gy = gsl_sf_lngamma_sgn_e(y, &lgy, &sgy);
106 int stat_gxy = gsl_sf_lngamma_sgn_e(xy, &lgxy, &sgxy);
107 *sgn = sgx * sgy * sgxy;
108 result->val = lgx.val + lgy.val - lgxy.val;
109 result->err = lgx.err + lgy.err + lgxy.err;
110 result->err += 2.0 * GSL_DBL_EPSILON * (fabs(lgx.val) + fabs(lgy.val) + fabs(lgxy.val));
111 result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
112 return GSL_ERROR_SELECT_3(stat_gx, stat_gy, stat_gxy);
118 gsl_sf_beta_e(const double x, const double y, gsl_sf_result * result)
120 if((x > 0 && y > 0) && x < 50.0 && y < 50.0) {
121 /* Handle the easy case */
122 gsl_sf_result gx, gy, gxy;
123 gsl_sf_gamma_e(x, &gx);
124 gsl_sf_gamma_e(y, &gy);
125 gsl_sf_gamma_e(x+y, &gxy);
126 result->val = (gx.val*gy.val)/gxy.val;
127 result->err = gx.err * fabs(gy.val/gxy.val);
128 result->err += gy.err * fabs(gx.val/gxy.val);
129 result->err += fabs((gx.val*gy.val)/(gxy.val*gxy.val)) * gxy.err;
130 result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
133 else if (isnegint(x) || isnegint(y)) {
134 DOMAIN_ERROR(result);
135 } else if (isnegint(x+y)) { /* infinity in the denominator */
142 int stat_lb = gsl_sf_lnbeta_sgn_e(x, y, &lb, &sgn);
143 if(stat_lb == GSL_SUCCESS) {
144 int status = gsl_sf_exp_err_e(lb.val, lb.err, result);
157 /*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/
161 double gsl_sf_lnbeta(const double x, const double y)
163 EVAL_RESULT(gsl_sf_lnbeta_e(x, y, &result));
166 double gsl_sf_beta(const double x, const double y)
168 EVAL_RESULT(gsl_sf_beta_e(x, y, &result));