| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

log_falling_factorial.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_SCAL_FUN_LOG_FALLING_FACTORIAL_HPP
2 #define STAN_MATH_REV_SCAL_FUN_LOG_FALLING_FACTORIAL_HPP
3
4 #include <stan/math/rev/core.hpp>
5 #include <stan/math/prim/scal/fun/log_falling_factorial.hpp>
6 #include <stan/math/prim/scal/fun/is_nan.hpp>
7 #include <boost/math/special_functions/digamma.hpp>
8 #include <limits>
9
10 namespace stan {
11 namespace math {
12
13 namespace {
14
15 class log_falling_factorial_vv_vari : public op_vv_vari {
16 public:
17 log_falling_factorial_vv_vari(vari* avi, vari* bvi) :
18 op_vv_vari(log_falling_factorial(avi->val_, bvi->val_),
19 avi, bvi) {
20 }
21 void chain() {
22 if (unlikely(is_nan(avi_->val_)
23 || is_nan(bvi_->val_))) {
24 avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
25 bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
26 } else {
27 avi_->adj_ += adj_
28 * (boost::math::digamma(avi_->val_ + 1)
29 - boost::math::digamma(avi_->val_ - bvi_->val_ + 1));
30 bvi_->adj_ += adj_
31 * boost::math::digamma(avi_->val_ - bvi_->val_ + 1);
32 }
33 }
34 };
35
36 class log_falling_factorial_vd_vari : public op_vd_vari {
37 public:
38 log_falling_factorial_vd_vari(vari* avi, double b) :
39 op_vd_vari(log_falling_factorial(avi->val_, b), avi, b) {
40 }
41 void chain() {
42 if (unlikely(is_nan(avi_->val_)
43 || is_nan(bd_)))
44 avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
45 else
46 avi_->adj_ += adj_
47 * (boost::math::digamma(avi_->val_ + 1)
48 - boost::math::digamma(avi_->val_ - bd_ + 1));
49 }
50 };
51
52 class log_falling_factorial_dv_vari : public op_dv_vari {
53 public:
54 log_falling_factorial_dv_vari(double a, vari* bvi) :
55 op_dv_vari(log_falling_factorial(a, bvi->val_), a, bvi) {
56 }
57 void chain() {
58 if (unlikely(is_nan(ad_)
59 || is_nan(bvi_->val_)))
60 bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
61 else
62 bvi_->adj_ += adj_
63 * boost::math::digamma(ad_ - bvi_->val_ + 1);
64 }
65 };
66 }
67
68 inline var log_falling_factorial(const var& a,
69 double b) {
70 return var(new log_falling_factorial_vd_vari(a.vi_, b));
71 }
72
73 inline var log_falling_factorial(const var& a,
74 const var& b) {
75 return var(new log_falling_factorial_vv_vari(a.vi_, b.vi_));
76 }
77
78 inline var log_falling_factorial(double a,
79 const var& b) {
80 return var(new log_falling_factorial_dv_vari(a, b.vi_));
81 }
82
83 }
84 }
85 #endif
core.hpp
stan::math::log_falling_factorial
fvar< T > log_falling_factorial(const fvar< T > &x, const fvar< T > &n)
Definition: log_falling_factorial.hpp:14
is_nan.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:30
unlikely
#define unlikely(x)
Definition: likely.hpp:9
stan::math::var::vi_
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:42
stan::math::is_nan
int is_nan(const fvar< T > &x)
Returns 1 if the input's value is NaN and 0 otherwise.
Definition: is_nan.hpp:21
log_falling_factorial.hpp
stan::math::digamma
fvar< T > digamma(const fvar< T > &x)
Definition: digamma.hpp:15

[ Stan Home Page ] © 2011–2016, Stan Development Team.