Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
operator_multiplication.hpp
Go to the documentation of this file.
1 #ifndef STAN__AGRAD__REV__OPERATORS__OPERATOR_MULTIPLICATION_HPP
2 #define STAN__AGRAD__REV__OPERATORS__OPERATOR_MULTIPLICATION_HPP
3 
4 #include <stan/agrad/rev/var.hpp>
7 #include <boost/math/special_functions/fpclassify.hpp>
8 
9 namespace stan {
10  namespace agrad {
11 
12  namespace {
13  class multiply_vv_vari : public op_vv_vari {
14  public:
15  multiply_vv_vari(vari* avi, vari* bvi) :
16  op_vv_vari(avi->val_ * bvi->val_, avi, bvi) {
17  }
18  void chain() {
19  if (unlikely(boost::math::isnan(avi_->val_)
20  || boost::math::isnan(bvi_->val_))) {
21  avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
22  bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
23  } else {
24  avi_->adj_ += bvi_->val_ * adj_;
25  bvi_->adj_ += avi_->val_ * adj_;
26  }
27  }
28  };
29 
30  class multiply_vd_vari : public op_vd_vari {
31  public:
32  multiply_vd_vari(vari* avi, double b) :
33  op_vd_vari(avi->val_ * b, avi, b) {
34  }
35  void chain() {
36  if (unlikely(boost::math::isnan(avi_->val_)
37  || boost::math::isnan(bd_)))
38  avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
39  else
40  avi_->adj_ += adj_ * bd_;
41  }
42  };
43  }
44 
82  inline var operator*(const var& a, const var& b) {
83  return var(new multiply_vv_vari(a.vi_,b.vi_));
84  }
85 
97  inline var operator*(const var& a, const double b) {
98  if (b == 1.0)
99  return a;
100  return var(new multiply_vd_vari(a.vi_,b));
101  }
102 
114  inline var operator*(const double a, const var& b) {
115  if (a == 1.0)
116  return b;
117  return var(new multiply_vd_vari(b.vi_,a)); // by symmetry
118  }
119 
120  }
121 }
122 #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
fvar< T > operator*(const fvar< T > &x1, const fvar< T > &x2)
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:27

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