1 #ifndef STAN_MATH_REV_FUN_LGAMMA_HPP 2 #define STAN_MATH_REV_FUN_LGAMMA_HPP 3 4 #include <stan/math/rev/meta.hpp> 5 #include <stan/math/rev/core.hpp> 6 #include <stan/math/prim/fun/digamma.hpp> 7 #include <stan/math/prim/fun/lgamma.hpp> 8 9 namespace stan { 10 namespace math { 11 12 /** 13 * The log gamma function for variables (C99). 14 * 15 * The derivative is the digamma function, 16 * 17 * \f$\frac{d}{dx} \Gamma(x) = \psi^{(0)}(x)\f$. 18 * 19 * @tparam T Arithmetic or a type inheriting from `EigenBase`. 20 * @param a The variable. 21 * @return Log gamma of the variable. 22 */ 23 template <typename T, require_stan_scalar_or_eigen_t<T>* = nullptr> lgamma(const var_value<T> & a)24inline auto lgamma(const var_value<T>& a) { 25 return make_callback_var(lgamma(a.val()), [a](auto& vi) mutable { 26 as_array_or_scalar(a.adj()) 27 += as_array_or_scalar(vi.adj()) * as_array_or_scalar(digamma(a.val())); 28 }); 29 } 30 31 } // namespace math 32 } // namespace stan 33 #endif 34