Automatic Differentiation
 
Loading...
Searching...
No Matches
falling_factorial.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_FALLING_FACTORIAL_HPP
2#define STAN_MATH_REV_FUN_FALLING_FACTORIAL_HPP
3
8
9namespace stan {
10namespace math {
11
12inline var falling_factorial(const var& a, int b) {
13 auto digamma_ab = digamma(a.val() + 1) - digamma(a.val() - b + 1);
14 return make_callback_var(falling_factorial(a.val(), b),
15 [a, digamma_ab](auto& vi) mutable {
16 a.adj() += vi.adj() * vi.val() * digamma_ab;
17 });
18}
19
20template <typename T1, typename T2, require_eigen_t<T1>* = nullptr,
21 require_st_integral<T2>* = nullptr>
22inline auto falling_factorial(const var_value<T1>& a, const T2& b) {
23 auto b_map = as_array_or_scalar(b);
24 auto digamma_ab = to_arena(digamma(a.val().array() + 1)
25 - digamma(a.val().array() - b_map + 1));
26 return make_callback_var(
27 falling_factorial(a.val(), b), [a, digamma_ab](auto& vi) mutable {
28 a.adj().array() += vi.adj().array() * vi.val().array() * digamma_ab;
29 });
30}
31
32} // namespace math
33} // namespace stan
34#endif
T as_array_or_scalar(T &&v)
Returns specified input value.
var_value< plain_type_t< T > > make_callback_var(T &&value, F &&functor)
Creates a new var initialized with a callback_vari with a given value and reverse-pass callback funct...
arena_t< T > to_arena(const T &a)
Converts given argument into a type that either has any dynamic allocation on AD stack or schedules i...
Definition to_arena.hpp:25
fvar< T > falling_factorial(const fvar< T > &x, int n)
Return autodiff variable with the gradient and result of the falling factorial function applied to th...
fvar< T > digamma(const fvar< T > &x)
Return the derivative of the log gamma function at the specified argument.
Definition digamma.hpp:23
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...