6namespace uni20::detail::native_math
9template <NativeReal R> R portable_beta(R a, R b)
11 using L = numeric_limits<R>;
12 if (!(a > 0) || !(b > 0))
return L::quiet_NaN();
13 if (std::isinf(a) || std::isinf(b))
return R(0);
14 if (a > b) std::swap(a, b);
15 if (a == 1)
return R(1) / b;
16 if (b == 1)
return R(1) / a;
23 R qa = R(.5) / a, qb = R(.5) / b,
sum = qa + qb;
24 if (std::isinf(
sum))
return L::infinity();
25 R low = qb - (
sum - qa);
26 low += std::fma(-qa, a, R(.5)) / a + std::fma(-qb, b, R(.5)) / b;
27 return R(2) * (
sum + low);
29 constexpr int bits = L::digits();
30 constexpr int cutoff = bits <= 24 ? 8 : bits <= 64 ? 16 : 32;
39 factor /= R(1) + a / b;
44 factor /= R(1) + b / a;
46 R
ratio = (std::tgamma(R(1) + a) / std::tgamma(R(1) + (a + b))) * std::tgamma(R(1) + b);
47 return (R(1) / a + R(1) / b) * (ratio * factor);
50 R r = a / b, l = std::log1p(r), invb = R(1) / b, invsum = invb / (R(1) + r);
54 R
lgamma = std::log(std::tgamma(R(1) + a));
57 logbeta = std::fma(-a, std::log(b), lgamma) +
remainder - (a - R(.5)) * l +
58 native_math::stirling_correction(invb) - native_math::stirling_correction(invsum);
61 if (a < 1)
return std::exp(logbeta) / a;
62 logbeta -= std::log(a);
67 logbeta = a * std::log(r) - a * l - b * l + (std::log(R(2) * pi<R>) - std::log(a) + l) / 2 +
68 native_math::stirling_correction(R(1) / a) + native_math::stirling_correction(invb) -
69 native_math::stirling_correction(invsum);
71 return std::exp(logbeta);
R ratio(DiagnosticMagnitude< R > const &a, DiagnosticMagnitude< R > const &b)
Definition gmres_diagnostics.hpp:125
constexpr detail::scalar_math::lgamma_fn lgamma
Logarithm of the absolute Gamma function; native providers are reentrant.
Definition math.hpp:569
void sum(BackendSelector selector, async::Async< OutputTensor > &output, async::Async< InputTensor > const &input)
Schedule a full sum into an explicit async scalar tensor.
Definition linalg/async/reductions.hpp:309
mpreal fma(mpreal const &a, mpreal const &b, mpreal const &c)
Compute a*b+c with one rounding, including mixed exact rational operands.
Definition mpreal_functions.hpp:192
mpreal remainder(mpreal const &a, mpreal const &b)
Nearest-even-quotient remainder; exact operands retain exact rational results.
Definition mpreal_functions.hpp:534