Automatic Differentiation
 
Loading...
Searching...
No Matches
log_falling_factorial.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_LOG_FALLING_FACTORIAL_HPP
2#define STAN_MATH_REV_FUN_LOG_FALLING_FACTORIAL_HPP
3
10
11namespace stan {
12namespace math {
13
14namespace internal {
15
17 public:
19 : op_vv_vari(log_falling_factorial(avi->val_, bvi->val_), avi, bvi) {}
20 void chain() {
21 if (unlikely(is_any_nan(avi_->val_, bvi_->val_))) {
22 avi_->adj_ = NOT_A_NUMBER;
23 bvi_->adj_ = NOT_A_NUMBER;
24 } else {
25 avi_->adj_
26 += adj_
27 * (digamma(avi_->val_ + 1) - digamma(avi_->val_ - bvi_->val_ + 1));
28 bvi_->adj_ += adj_ * digamma(avi_->val_ - bvi_->val_ + 1);
29 }
30 }
31};
32
34 public:
36 : op_vd_vari(log_falling_factorial(avi->val_, b), avi, b) {}
37 void chain() {
38 if (unlikely(is_any_nan(avi_->val_, bd_))) {
39 avi_->adj_ = NOT_A_NUMBER;
40 } else {
41 avi_->adj_
42 += adj_ * (digamma(avi_->val_ + 1) - digamma(avi_->val_ - bd_ + 1));
43 }
44 }
45};
46
48 public:
50 : op_dv_vari(log_falling_factorial(a, bvi->val_), a, bvi) {}
51 void chain() {
52 if (unlikely(is_any_nan(ad_, bvi_->val_))) {
53 bvi_->adj_ = NOT_A_NUMBER;
54 } else {
55 bvi_->adj_ += adj_ * digamma(ad_ - bvi_->val_ + 1);
56 }
57 }
58};
59} // namespace internal
60
61inline var log_falling_factorial(const var& a, double b) {
62 return var(new internal::log_falling_factorial_vd_vari(a.vi_, b));
63}
64
65inline var log_falling_factorial(const var& a, const var& b) {
66 return var(new internal::log_falling_factorial_vv_vari(a.vi_, b.vi_));
67}
68
69inline var log_falling_factorial(double a, const var& b) {
70 return var(new internal::log_falling_factorial_dv_vari(a, b.vi_));
71}
72
73} // namespace math
74} // namespace stan
75#endif
#define unlikely(x)
static constexpr double NOT_A_NUMBER
(Quiet) not-a-number value.
Definition constants.hpp:56
var_value< double > var
Definition var.hpp:1187
fvar< T > log_falling_factorial(const fvar< T > &x, const fvar< T > &n)
bool is_any_nan(const T &x)
Returns true if the input is NaN and false otherwise.
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 ...
Definition fvar.hpp:9