Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
multiply_log.hpp
Go to the documentation of this file.
1 #ifndef STAN__AGRAD__REV__FUNCTIONS__MULTIPLY_LOG_HPP
2 #define STAN__AGRAD__REV__FUNCTIONS__MULTIPLY_LOG_HPP
3 
4 #include <limits>
5 #include <stan/agrad/rev/var.hpp>
11 #include <boost/math/special_functions/fpclassify.hpp>
12 
13 namespace stan {
14  namespace agrad {
15 
16  namespace {
17  class multiply_log_vv_vari : public op_vv_vari {
18  public:
19  multiply_log_vv_vari(vari* avi, vari* bvi) :
20  op_vv_vari(stan::math::multiply_log(avi->val_,bvi->val_),avi,bvi) {
21  }
22  void chain() {
23  using std::log;
24  if (unlikely(boost::math::isnan(avi_->val_)
25  || boost::math::isnan(bvi_->val_))) {
26  avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
27  bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
28  } else {
29  avi_->adj_ += adj_ * log(bvi_->val_);
30  if (bvi_->val_ == 0.0 && avi_->val_ == 0)
31  bvi_->adj_ += adj_ * std::numeric_limits<double>::infinity();
32  else
33  bvi_->adj_ += adj_ * avi_->val_ / bvi_->val_;
34  }
35  }
36  };
37  class multiply_log_vd_vari : public op_vd_vari {
38  public:
39  multiply_log_vd_vari(vari* avi, double b) :
40  op_vd_vari(stan::math::multiply_log(avi->val_,b),avi,b) {
41  }
42  void chain() {
43  using std::log;
44  if (unlikely(boost::math::isnan(avi_->val_)
45  || boost::math::isnan(bd_)))
46  avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
47  else
48  avi_->adj_ += adj_ * log(bd_);
49  }
50  };
51  class multiply_log_dv_vari : public op_dv_vari {
52  public:
53  multiply_log_dv_vari(double a, vari* bvi) :
54  op_dv_vari(stan::math::multiply_log(a,bvi->val_),a,bvi) {
55  }
56  void chain() {
57  if (bvi_->val_ == 0.0 && ad_ == 0.0)
58  bvi_->adj_ += adj_ * std::numeric_limits<double>::infinity();
59  else
60  bvi_->adj_ += adj_ * ad_ / bvi_->val_;
61  }
62  };
63  }
64 
77  inline var multiply_log(const var& a, const var& b) {
78  return var(new multiply_log_vv_vari(a.vi_,b.vi_));
79  }
90  inline var multiply_log(const var& a, const double b) {
91  return var(new multiply_log_vd_vari(a.vi_,b));
92  }
104  inline var multiply_log(const double a, const var& b) {
105  if (a == 1.0)
106  return log(b);
107  return var(new multiply_log_dv_vari(a,b.vi_));
108  }
109 
110  }
111 }
112 #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
#define unlikely(x)
Definition: likely.hpp:9
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:27
fvar< T > multiply_log(const fvar< T > &x1, const fvar< T > &x2)
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15

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