Stan Math Library  2.20.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/meta.hpp>
5 #include <stan/math/rev/core.hpp>
9 #include <limits>
10 
11 namespace stan {
12 namespace math {
13 
14 namespace internal {
16  public:
18  : op_vv_vari(multiply_log(avi->val_, bvi->val_), avi, bvi) {}
19  void chain() {
20  using std::log;
21  if (unlikely(is_any_nan(avi_->val_, 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 };
34  public:
35  multiply_log_vd_vari(vari* avi, double b)
36  : op_vd_vari(multiply_log(avi->val_, b), avi, b) {}
37  void chain() {
38  using std::log;
39  if (unlikely(is_any_nan(avi_->val_, bd_)))
40  avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
41  else
42  avi_->adj_ += adj_ * log(bd_);
43  }
44 };
46  public:
47  multiply_log_dv_vari(double a, vari* bvi)
48  : op_dv_vari(multiply_log(a, bvi->val_), a, bvi) {}
49  void chain() {
50  if (bvi_->val_ == 0.0 && ad_ == 0.0)
51  bvi_->adj_ += adj_ * std::numeric_limits<double>::infinity();
52  else
53  bvi_->adj_ += adj_ * ad_ / bvi_->val_;
54  }
55 };
56 } // namespace internal
57 
70 inline var multiply_log(const var& a, const var& b) {
71  return var(new internal::multiply_log_vv_vari(a.vi_, b.vi_));
72 }
83 inline var multiply_log(const var& a, double b) {
84  return var(new internal::multiply_log_vd_vari(a.vi_, b));
85 }
97 inline var multiply_log(double a, const var& b) {
98  if (a == 1.0)
99  return log(b);
100  return var(new internal::multiply_log_dv_vari(a, b.vi_));
101 }
102 
103 } // namespace math
104 } // namespace stan
105 #endif
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:12
The variable implementation base class.
Definition: vari.hpp:30
bool is_any_nan(const T &x)
Returns true if the input is NaN and false otherwise.
Definition: is_any_nan.hpp:21
void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:33
friend class var
Definition: vari.hpp:32
const double val_
The value of this variable.
Definition: vari.hpp:38
#define unlikely(x)
Definition: likely.hpp:9
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:45
void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
fvar< T > multiply_log(const fvar< T > &x1, const fvar< T > &x2)
double adj_
The adjoint of this variable, which is the partial derivative of this variable with respect to the ro...
Definition: vari.hpp:44
void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...

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