Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
operator_addition.hpp
Go to the documentation of this file.
1 #ifndef STAN__AGRAD__REV__OPERATORS__OPERATOR_ADDITION_HPP
2 #define STAN__AGRAD__REV__OPERATORS__OPERATOR_ADDITION_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 add_vv_vari : public op_vv_vari {
14  public:
15  add_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_ += adj_;
25  bvi_->adj_ += adj_;
26  }
27  }
28  };
29 
30  class add_vd_vari : public op_vd_vari {
31  public:
32  add_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_;
41  }
42  };
43  }
44 
83  inline var operator+(const var& a, const var& b) {
84  return var(new add_vv_vari(a.vi_,b.vi_));
85  }
86 
98  inline var operator+(const var& a, const double b) {
99  if (b == 0.0)
100  return a;
101  return var(new add_vd_vari(a.vi_,b));
102  }
103 
115  inline var operator+(const double a, const var& b) {
116  if (a == 0.0)
117  return b;
118  return var(new add_vd_vari(b.vi_,a)); // by symmetry
119  }
120 
121  }
122 }
123 #endif
bool isnan(const stan::agrad::var &v)
Checks if the given number is NaN.
fvar< T > operator+(const fvar< T > &x1, const fvar< T > &x2)
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

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