1#ifndef STAN_MATH_OPENCL_KERNELS_DEVICE_FUNCTIONS_LGAMMA_STIRLING_DIFF_HPP
2#define STAN_MATH_OPENCL_KERNELS_DEVICE_FUNCTIONS_LGAMMA_STIRLING_DIFF_HPP
10namespace opencl_kernels {
13static constexpr const char* lgamma_stirling_diff_device_function
15 "#ifndef STAN_MATH_OPENCL_KERNELS_DEVICE_FUNCTIONS_LGAMMA_STIRLING_DIFF\n"
16 "#define STAN_MATH_OPENCL_KERNELS_DEVICE_FUNCTIONS_LGAMMA_STIRLING_DIFF\n"
17 "#define LGAMMA_STIRLING_DIFF_USEFUL 10\n" STRINGIFY(
52 if (x < LGAMMA_STIRLING_DIFF_USEFUL) {
57 const double stirling_series[] = {
58 0.0833333333333333333333333, -0.00277777777777777777777778,
59 0.000793650793650793650793651, -0.000595238095238095238095238,
60 0.000841750841750841750841751, -0.00191752691752691752691753,
61 0.00641025641025641025641026, -0.0295506535947712418300654};
62 double multiplier = 1 / x;
63 double inv_x_squared = multiplier * multiplier;
64 double result = stirling_series[0] * multiplier;
65 for (
int n = 1; n < 6; n++) {
66 multiplier *= inv_x_squared;
67 result += stirling_series[n] * multiplier;
double lgamma_stirling_diff(double x)
Return the difference between log of the gamma function and its Stirling approximation.
double lgamma_stirling(double x)
Return the Stirling approximation to the lgamma function.
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...