1 #ifndef STAN_MATH_FWD_FUN_FALLING_FACTORIAL_HPP
2 #define STAN_MATH_FWD_FUN_FALLING_FACTORIAL_HPP
3 
4 #include <stan/math/fwd/meta.hpp>
5 #include <stan/math/fwd/core.hpp>
6 
7 #include <stan/math/prim/fun/falling_factorial.hpp>
8 #include <stan/math/prim/fun/digamma.hpp>
9 
10 namespace stan {
11 namespace math {
12 
13 /**
14  * Return autodiff variable with the gradient and
15  * result of the falling factorial function applied
16  * to the inputs.
17  * Will throw for NaN x and for negative n, as
18  * implemented in primitive function.
19  *
20  * @tparam T inner type of the fvar
21  * @param x Argument.
22  * @param n Argument
23  * @return tangent of falling factorial at arguments.
24  */
25 template <typename T>
falling_factorial(const fvar<T> & x,int n)26 inline fvar<T> falling_factorial(const fvar<T>& x, int n) {
27   T falling_fact(falling_factorial(x.val_, n));
28   return fvar<T>(
29       falling_fact,
30       falling_fact * (digamma(x.val_ + 1) - digamma(x.val_ - n + 1)) * x.d_);
31 }
32 
33 }  // namespace math
34 }  // namespace stan
35 #endif
36