1 #ifndef STAN_MATH_PRIM_FUN_TGAMMA_HPP
2 #define STAN_MATH_PRIM_FUN_TGAMMA_HPP
3 
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/err.hpp>
6 #include <stan/math/prim/fun/is_nonpositive_integer.hpp>
7 #include <stan/math/prim/functor/apply_scalar_unary.hpp>
8 #include <cmath>
9 
10 namespace stan {
11 namespace math {
12 
13 /**
14  * Return the gamma function applied to the specified argument.
15  *
16  * @param x Argument.
17  * @return The gamma function applied to argument.
18  */
tgamma(double x)19 inline double tgamma(double x) {
20   if (x == 0.0 || is_nonpositive_integer(x)) {
21     throw_domain_error("tgamma", "x", x, "x == 0 or negative integer");
22   }
23   return std::tgamma(x);
24 }
25 
26 /**
27  * Structure to wrap tgamma() so that it can be vectorized.
28  *
29  * @tparam T type of variable
30  * @param x variable
31  * @return Gamma function applied to x.
32  * @throw std::domain_error if x is 0 or a negative integer
33  */
34 struct tgamma_fun {
35   template <typename T>
funstan::math::tgamma_fun36   static inline T fun(const T& x) {
37     return tgamma(x);
38   }
39 };
40 
41 /**
42  * Vectorized version of tgamma().
43  *
44  * @tparam T type of container
45  * @param x container
46  * @return Gamma function applied to each value in x.
47  * @throw std::domain_error if any value is 0 or a negative integer
48  */
49 template <
50     typename T,
51     require_all_not_nonscalar_prim_or_rev_kernel_expression_t<T>* = nullptr,
52     require_not_var_matrix_t<T>* = nullptr>
tgamma(const T & x)53 inline auto tgamma(const T& x) {
54   return apply_scalar_unary<tgamma_fun, T>::apply(x);
55 }
56 
57 }  // namespace math
58 }  // namespace stan
59 
60 #endif
61