Stan  1.3
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
multiply_log.hpp
Go to the documentation of this file.
1 #ifndef __STAN__AGRAD__REV__MULTIPLY_LOG_HPP__
2 #define __STAN__AGRAD__REV__MULTIPLY_LOG_HPP__
3 
4 #include <limits>
5 #include <stan/agrad/rev/var.hpp>
9 #include <stan/agrad/rev/log.hpp>
11 
12 namespace stan {
13  namespace agrad {
14 
15  namespace {
16  class multiply_log_vv_vari : public op_vv_vari {
17  public:
18  multiply_log_vv_vari(vari* avi, vari* bvi) :
19  op_vv_vari(stan::math::multiply_log(avi->val_,bvi->val_),avi,bvi) {
20  }
21  void chain() {
22  using std::log;
23  avi_->adj_ += adj_ * log(bvi_->val_);
24  if (bvi_->val_ == 0.0 && avi_->val_ == 0)
25  bvi_->adj_ += adj_ * std::numeric_limits<double>::infinity();
26  else
27  bvi_->adj_ += adj_ * avi_->val_ / bvi_->val_;
28  }
29  };
30  class multiply_log_vd_vari : public op_vd_vari {
31  public:
32  multiply_log_vd_vari(vari* avi, double b) :
33  op_vd_vari(stan::math::multiply_log(avi->val_,b),avi,b) {
34  }
35  void chain() {
36  using std::log;
37  avi_->adj_ += adj_ * log(bd_);
38  }
39  };
40  class multiply_log_dv_vari : public op_dv_vari {
41  public:
42  multiply_log_dv_vari(double a, vari* bvi) :
43  op_dv_vari(stan::math::multiply_log(a,bvi->val_),a,bvi) {
44  }
45  void chain() {
46  if (bvi_->val_ == 0.0 && ad_ == 0.0)
47  bvi_->adj_ += adj_ * std::numeric_limits<double>::infinity();
48  else
49  bvi_->adj_ += adj_ * ad_ / bvi_->val_;
50  }
51  };
52  }
53 
66  inline var multiply_log(const var& a, const var& b) {
67  return var(new multiply_log_vv_vari(a.vi_,b.vi_));
68  }
79  inline var multiply_log(const var& a, const double b) {
80  return var(new multiply_log_vd_vari(a.vi_,b));
81  }
93  inline var multiply_log(const double a, const var& b) {
94  if (a == 1.0)
95  return log(b);
96  return var(new multiply_log_dv_vari(a,b.vi_));
97  }
98 
99  }
100 }
101 #endif

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