1#ifndef STAN_MATH_PRIM_FUN_LGAMMA_STIRLING_DIFF_HPP
2#define STAN_MATH_PRIM_FUN_LGAMMA_STIRLING_DIFF_HPP
60 constexpr double stirling_series[]{
61 0.0833333333333333333333333, -0.00277777777777777777777778,
62 0.000793650793650793650793651, -0.000595238095238095238095238,
63 0.000841750841750841750841751, -0.00191752691752691752691753,
64 0.00641025641025641025641026, -0.0295506535947712418300654};
66 constexpr int n_stirling_terms = 6;
68 T_ret multiplier =
inv(x);
69 T_ret inv_x_squared =
square(multiplier);
70 for (
int n = 0; n < n_stirling_terms; n++) {
72 multiplier *= inv_x_squared;
74 result += stirling_series[n] * multiplier;
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
double value_of_rec(const fvar< T > &v)
Return the value of the specified variable.
static constexpr double NOT_A_NUMBER
(Quiet) not-a-number value.
bool is_nan(T &&x)
Returns 1 if the input's value is NaN and 0 otherwise.
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
return_type_t< T > lgamma_stirling(const T 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.
return_type_t< T > lgamma_stirling_diff(const T x)
Return the difference between log of the gamma function and its Stirling approximation.
constexpr double lgamma_stirling_diff_useful
fvar< T > inv(const fvar< T > &x)
static constexpr double INFTY
Positive infinity.
fvar< T > square(const fvar< T > &x)
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...