File size: 6,981 Bytes
7885a28 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
/* Translated into C++ by SciPy developers in 2024.
* Original header with Copyright information appears below.
*/
/* beta.c
*
* Beta function
*
*
*
* SYNOPSIS:
*
* double a, b, y, beta();
*
* y = beta( a, b );
*
*
*
* DESCRIPTION:
*
* - -
* | (a) | (b)
* beta( a, b ) = -----------.
* -
* | (a+b)
*
* For large arguments the logarithm of the function is
* evaluated using lgam(), then exponentiated.
*
*
*
* ACCURACY:
*
* Relative error:
* arithmetic domain # trials peak rms
* IEEE 0,30 30000 8.1e-14 1.1e-14
*
* ERROR MESSAGES:
*
* message condition value returned
* beta overflow log(beta) > MAXLOG 0.0
* a or b <0 integer 0.0
*
*/
/*
* Cephes Math Library Release 2.0: April, 1987
* Copyright 1984, 1987 by Stephen L. Moshier
* Direct inquiries to 30 Frost Street, Cambridge, MA 02140
*/
#pragma once
#include "../config.h"
#include "const.h"
#include "gamma.h"
#include "rgamma.h"
namespace xsf {
namespace cephes {
XSF_HOST_DEVICE double beta(double, double);
XSF_HOST_DEVICE double lbeta(double, double);
namespace detail {
constexpr double beta_ASYMP_FACTOR = 1e6;
/*
* Asymptotic expansion for ln(|B(a, b)|) for a > ASYMP_FACTOR*max(|b|, 1).
*/
XSF_HOST_DEVICE inline double lbeta_asymp(double a, double b, int *sgn) {
double r = lgam_sgn(b, sgn);
r -= b * std::log(a);
r += b * (1 - b) / (2 * a);
r += b * (1 - b) * (1 - 2 * b) / (12 * a * a);
r += -b * b * (1 - b) * (1 - b) / (12 * a * a * a);
return r;
}
/*
* Special case for a negative integer argument
*/
XSF_HOST_DEVICE inline double beta_negint(int a, double b) {
int sgn;
if (b == static_cast<int>(b) && 1 - a - b > 0) {
sgn = (static_cast<int>(b) % 2 == 0) ? 1 : -1;
return sgn * xsf::cephes::beta(1 - a - b, b);
} else {
set_error("lbeta", SF_ERROR_OVERFLOW, NULL);
return std::numeric_limits<double>::infinity();
}
}
XSF_HOST_DEVICE inline double lbeta_negint(int a, double b) {
double r;
if (b == static_cast<int>(b) && 1 - a - b > 0) {
r = xsf::cephes::lbeta(1 - a - b, b);
return r;
} else {
set_error("lbeta", SF_ERROR_OVERFLOW, NULL);
return std::numeric_limits<double>::infinity();
}
}
} // namespace detail
XSF_HOST_DEVICE inline double beta(double a, double b) {
double y;
int sign = 1;
if (a <= 0.0) {
if (a == std::floor(a)) {
if (a == static_cast<int>(a)) {
return detail::beta_negint(static_cast<int>(a), b);
} else {
goto overflow;
}
}
}
if (b <= 0.0) {
if (b == std::floor(b)) {
if (b == static_cast<int>(b)) {
return detail::beta_negint(static_cast<int>(b), a);
} else {
goto overflow;
}
}
}
if (std::abs(a) < std::abs(b)) {
y = a;
a = b;
b = y;
}
if (std::abs(a) > detail::beta_ASYMP_FACTOR * std::abs(b) && a > detail::beta_ASYMP_FACTOR) {
/* Avoid loss of precision in lgam(a + b) - lgam(a) */
y = detail::lbeta_asymp(a, b, &sign);
return sign * std::exp(y);
}
y = a + b;
if (std::abs(y) > detail::MAXGAM || std::abs(a) > detail::MAXGAM || std::abs(b) > detail::MAXGAM) {
int sgngam;
y = detail::lgam_sgn(y, &sgngam);
sign *= sgngam; /* keep track of the sign */
y = detail::lgam_sgn(b, &sgngam) - y;
sign *= sgngam;
y = detail::lgam_sgn(a, &sgngam) + y;
sign *= sgngam;
if (y > detail::MAXLOG) {
goto overflow;
}
return (sign * std::exp(y));
}
y = rgamma(y);
a = Gamma(a);
b = Gamma(b);
if (std::isinf(y)) {
goto overflow;
}
if (std::abs(std::abs(a*y) - 1.0) > std::abs(std::abs(b*y) - 1.0)) {
y = b * y;
y *= a;
} else {
y = a * y;
y *= b;
}
return (y);
overflow:
set_error("beta", SF_ERROR_OVERFLOW, NULL);
return (sign * std::numeric_limits<double>::infinity());
}
/* Natural log of |beta|. */
XSF_HOST_DEVICE inline double lbeta(double a, double b) {
double y;
int sign;
sign = 1;
if (a <= 0.0) {
if (a == std::floor(a)) {
if (a == static_cast<int>(a)) {
return detail::lbeta_negint(static_cast<int>(a), b);
} else {
goto over;
}
}
}
if (b <= 0.0) {
if (b == std::floor(b)) {
if (b == static_cast<int>(b)) {
return detail::lbeta_negint(static_cast<int>(b), a);
} else {
goto over;
}
}
}
if (std::abs(a) < std::abs(b)) {
y = a;
a = b;
b = y;
}
if (std::abs(a) > detail::beta_ASYMP_FACTOR * std::abs(b) && a > detail::beta_ASYMP_FACTOR) {
/* Avoid loss of precision in lgam(a + b) - lgam(a) */
y = detail::lbeta_asymp(a, b, &sign);
return y;
}
y = a + b;
if (std::abs(y) > detail::MAXGAM || std::abs(a) > detail::MAXGAM || std::abs(b) > detail::MAXGAM) {
int sgngam;
y = detail::lgam_sgn(y, &sgngam);
sign *= sgngam; /* keep track of the sign */
y = detail::lgam_sgn(b, &sgngam) - y;
sign *= sgngam;
y = detail::lgam_sgn(a, &sgngam) + y;
sign *= sgngam;
return (y);
}
y = rgamma(y);
a = Gamma(a);
b = Gamma(b);
if (std::isinf(y)) {
over:
set_error("lbeta", SF_ERROR_OVERFLOW, NULL);
return (sign * std::numeric_limits<double>::infinity());
}
if (std::abs(std::abs(a*y) - 1.0) > std::abs(std::abs(b*y) - 1.0)) {
y = b * y;
y *= a;
} else {
y = a * y;
y *= b;
}
if (y < 0) {
y = -y;
}
return (std::log(y));
}
} // namespace cephes
} // namespace xsf
|