Stan Math Library  2.10.0
reverse mode automatic differentiation
multiply_log.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_SCAL_FUN_MULTIPLY_LOG_HPP
2 #define STAN_MATH_REV_SCAL_FUN_MULTIPLY_LOG_HPP
3 
4 #include <stan/math/rev/core.hpp>
7 #include <boost/math/special_functions/fpclassify.hpp>
8 #include <limits>
9 
10 namespace stan {
11  namespace math {
12 
13  namespace {
14  class multiply_log_vv_vari : public op_vv_vari {
15  public:
16  multiply_log_vv_vari(vari* avi, vari* bvi) :
17  op_vv_vari(stan::math::multiply_log(avi->val_, bvi->val_), avi, bvi) {
18  }
19  void chain() {
20  using std::log;
21  if (unlikely(boost::math::isnan(avi_->val_)
22  || boost::math::isnan(bvi_->val_))) {
23  avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
24  bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
25  } else {
26  avi_->adj_ += adj_ * log(bvi_->val_);
27  if (bvi_->val_ == 0.0 && avi_->val_ == 0)
28  bvi_->adj_ += adj_ * std::numeric_limits<double>::infinity();
29  else
30  bvi_->adj_ += adj_ * avi_->val_ / bvi_->val_;
31  }
32  }
33  };
34  class multiply_log_vd_vari : public op_vd_vari {
35  public:
36  multiply_log_vd_vari(vari* avi, double b) :
37  op_vd_vari(stan::math::multiply_log(avi->val_, b), avi, b) {
38  }
39  void chain() {
40  using std::log;
41  if (unlikely(boost::math::isnan(avi_->val_)
42  || boost::math::isnan(bd_)))
43  avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
44  else
45  avi_->adj_ += adj_ * log(bd_);
46  }
47  };
48  class multiply_log_dv_vari : public op_dv_vari {
49  public:
50  multiply_log_dv_vari(double a, vari* bvi) :
51  op_dv_vari(stan::math::multiply_log(a, bvi->val_), a, bvi) {
52  }
53  void chain() {
54  if (bvi_->val_ == 0.0 && ad_ == 0.0)
55  bvi_->adj_ += adj_ * std::numeric_limits<double>::infinity();
56  else
57  bvi_->adj_ += adj_ * ad_ / bvi_->val_;
58  }
59  };
60  }
61 
74  inline var multiply_log(const var& a, const var& b) {
75  return var(new multiply_log_vv_vari(a.vi_, b.vi_));
76  }
87  inline var multiply_log(const var& a, const double b) {
88  return var(new multiply_log_vd_vari(a.vi_, b));
89  }
101  inline var multiply_log(const double a, const var& b) {
102  if (a == 1.0)
103  return log(b);
104  return var(new multiply_log_dv_vari(a, b.vi_));
105  }
106 
107  }
108 }
109 #endif
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
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 > multiply_log(const fvar< T > &x1, const fvar< T > &x2)

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