Stan Math Library  2.10.0
reverse mode automatic differentiation
operator_multiplication.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_CORE_OPERATOR_MULTIPLICATION_HPP
2 #define STAN_MATH_REV_CORE_OPERATOR_MULTIPLICATION_HPP
3 
7 #include <boost/math/special_functions/fpclassify.hpp>
8 #include <limits>
9 
10 namespace stan {
11  namespace math {
12 
13  namespace {
14  class multiply_vv_vari : public op_vv_vari {
15  public:
16  multiply_vv_vari(vari* avi, vari* bvi) :
17  op_vv_vari(avi->val_ * bvi->val_, avi, bvi) {
18  }
19  void chain() {
20  if (unlikely(boost::math::isnan(avi_->val_)
21  || boost::math::isnan(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_ += bvi_->val_ * adj_;
26  bvi_->adj_ += avi_->val_ * adj_;
27  }
28  }
29  };
30 
31  class multiply_vd_vari : public op_vd_vari {
32  public:
33  multiply_vd_vari(vari* avi, double b) :
34  op_vd_vari(avi->val_ * b, avi, b) {
35  }
36  void chain() {
37  if (unlikely(boost::math::isnan(avi_->val_)
38  || boost::math::isnan(bd_)))
39  avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
40  else
41  avi_->adj_ += adj_ * bd_;
42  }
43  };
44  }
45 
83  inline var operator*(const var& a, const var& b) {
84  return var(new multiply_vv_vari(a.vi_, b.vi_));
85  }
86 
98  inline var operator*(const var& a, const double b) {
99  if (b == 1.0)
100  return a;
101  return var(new multiply_vd_vari(a.vi_, b));
102  }
103 
115  inline var operator*(const double a, const var& b) {
116  if (a == 1.0)
117  return b;
118  return var(new multiply_vd_vari(b.vi_, a)); // by symmetry
119  }
120 
121  }
122 }
123 #endif
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 > operator*(const fvar< T > &x1, const fvar< T > &x2)

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