Stan Math Library  2.11.0
reverse mode automatic differentiation
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>
6 #include <boost/math/special_functions/digamma.hpp>
7 #include <boost/math/special_functions/fpclassify.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(stan::math::log_falling_factorial(avi->val_, bvi->val_),
19  avi, bvi) {
20  }
21  void chain() {
22  if (unlikely(boost::math::isnan(avi_->val_)
23  || boost::math::isnan(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(stan::math::log_falling_factorial(avi->val_, b), avi, b) {
40  }
41  void chain() {
42  if (unlikely(boost::math::isnan(avi_->val_)
43  || boost::math::isnan(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(stan::math::log_falling_factorial(a, bvi->val_), a, bvi) {
56  }
57  void chain() {
59  || boost::math::isnan(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  const 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(const double& a,
79  const var& b) {
80  return var(new log_falling_factorial_dv_vari(a, b.vi_));
81  }
82  }
83 }
84 #endif
fvar< T > log_falling_factorial(const fvar< T > &x, const fvar< T > &n)
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:31
bool isnan(const stan::math::var &v)
Checks if the given number is NaN.
Definition: boost_isnan.hpp:22
#define unlikely(x)
Definition: likely.hpp:9
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:43
fvar< T > digamma(const fvar< T > &x)
Definition: digamma.hpp:16

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