Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
log_falling_factorial.hpp
Go to the documentation of this file.
1 #ifndef STAN__AGRAD__REV__FUNCTIONS__LOG_FALLING_FACTORIAL_HPP
2 #define STAN__AGRAD__REV__FUNCTIONS__LOG_FALLING_FACTORIAL_HPP
3 
4 #include <stan/agrad/rev/var.hpp>
9 #include <boost/math/special_functions/digamma.hpp>
10 #include <boost/math/special_functions/fpclassify.hpp>
11 
12 namespace stan {
13  namespace agrad {
14 
15  namespace {
16 
17  class log_falling_factorial_vv_vari : public op_vv_vari {
18  public:
19  log_falling_factorial_vv_vari(vari* avi, vari* bvi) :
20  op_vv_vari(stan::math::log_falling_factorial(avi->val_, bvi->val_), avi, bvi) {
21  }
22  void chain() {
23  if (unlikely(boost::math::isnan(avi_->val_)
24  || boost::math::isnan(bvi_->val_))) {
25  avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
26  bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
27  } else {
28  avi_->adj_ += adj_ * boost::math::digamma(avi_->val_ + 1);
29  bvi_->adj_ += adj_ * -boost::math::digamma(bvi_->val_ + 1);
30  }
31  }
32  };
33 
34  class log_falling_factorial_vd_vari : public op_vd_vari {
35  public:
36  log_falling_factorial_vd_vari(vari* avi, double b) :
37  op_vd_vari(stan::math::log_falling_factorial(avi->val_, b), avi, b) {
38  }
39  void chain() {
40  if (unlikely(boost::math::isnan(avi_->val_)
41  || boost::math::isnan(bd_)))
42  avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
43  else
44  avi_->adj_ += adj_ * boost::math::digamma(avi_->val_ + 1);
45  }
46  };
47 
48  class log_falling_factorial_dv_vari : public op_dv_vari {
49  public:
50  log_falling_factorial_dv_vari(double a, vari* bvi) :
51  op_dv_vari(stan::math::log_falling_factorial(a, bvi->val_), a, bvi) {
52  }
53  void chain() {
55  || boost::math::isnan(bvi_->val_)))
56  bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
57  else
58  bvi_->adj_ += adj_ * -boost::math::digamma(bvi_->val_ + 1);
59  }
60  };
61  }
62 
63  inline var log_falling_factorial(const var& a,
64  const double& b) {
65  return var(new log_falling_factorial_vd_vari(a.vi_, b));
66  }
67 
68  inline var log_falling_factorial(const var& a,
69  const var& b) {
70  return var(new log_falling_factorial_vv_vari(a.vi_, b.vi_));
71  }
72 
73  inline var log_falling_factorial(const double& a,
74  const var& b) {
75  return var(new log_falling_factorial_dv_vari(a, b.vi_));
76  }
77  }
78 }
79 #endif
bool isnan(const stan::agrad::var &v)
Checks if the given number is NaN.
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:40
fvar< T > log_falling_factorial(const fvar< T > &x, const fvar< T > &n)
#define unlikely(x)
Definition: likely.hpp:9
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:27
fvar< T > digamma(const fvar< T > &x)
Definition: digamma.hpp:16

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