Stan Math Library  2.15.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 <limits>
8 
9 namespace stan {
10  namespace math {
11 
12  namespace {
13  class multiply_log_vv_vari : public op_vv_vari {
14  public:
15  multiply_log_vv_vari(vari* avi, vari* bvi) :
16  op_vv_vari(multiply_log(avi->val_, bvi->val_), avi, bvi) {
17  }
18  void chain() {
19  using std::log;
20  if (unlikely(is_nan(avi_->val_)
21  || is_nan(bvi_->val_))) {
22  avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
23  bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
24  } else {
25  avi_->adj_ += adj_ * log(bvi_->val_);
26  if (bvi_->val_ == 0.0 && avi_->val_ == 0)
27  bvi_->adj_ += adj_ * std::numeric_limits<double>::infinity();
28  else
29  bvi_->adj_ += adj_ * avi_->val_ / bvi_->val_;
30  }
31  }
32  };
33  class multiply_log_vd_vari : public op_vd_vari {
34  public:
35  multiply_log_vd_vari(vari* avi, double b) :
36  op_vd_vari(multiply_log(avi->val_, b), avi, b) {
37  }
38  void chain() {
39  using std::log;
40  if (unlikely(is_nan(avi_->val_)
41  || is_nan(bd_)))
42  avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
43  else
44  avi_->adj_ += adj_ * log(bd_);
45  }
46  };
47  class multiply_log_dv_vari : public op_dv_vari {
48  public:
49  multiply_log_dv_vari(double a, vari* bvi) :
50  op_dv_vari(multiply_log(a, bvi->val_), a, bvi) {
51  }
52  void chain() {
53  if (bvi_->val_ == 0.0 && ad_ == 0.0)
54  bvi_->adj_ += adj_ * std::numeric_limits<double>::infinity();
55  else
56  bvi_->adj_ += adj_ * ad_ / bvi_->val_;
57  }
58  };
59  }
60 
73  inline var multiply_log(const var& a, const var& b) {
74  return var(new multiply_log_vv_vari(a.vi_, b.vi_));
75  }
86  inline var multiply_log(const var& a, double b) {
87  return var(new multiply_log_vd_vari(a.vi_, b));
88  }
100  inline var multiply_log(double a, const var& b) {
101  if (a == 1.0)
102  return log(b);
103  return var(new multiply_log_dv_vari(a, b.vi_));
104  }
105 
106  }
107 }
108 #endif
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:14
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:30
#define unlikely(x)
Definition: likely.hpp:9
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:42
fvar< T > multiply_log(const fvar< T > &x1, const fvar< T > &x2)
int is_nan(const fvar< T > &x)
Returns 1 if the input&#39;s value is NaN and 0 otherwise.
Definition: is_nan.hpp:21

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