Stan Math Library  2.20.0
reverse mode automatic differentiation
operator_addition.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_CORE_OPERATOR_ADDITION_HPP
2 #define STAN_MATH_REV_CORE_OPERATOR_ADDITION_HPP
3 
8 #include <limits>
9 
10 namespace stan {
11 namespace math {
12 
13 namespace internal {
14 class add_vv_vari : public op_vv_vari {
15  public:
16  add_vv_vari(vari* avi, vari* bvi)
17  : op_vv_vari(avi->val_ + bvi->val_, avi, bvi) {}
18  void chain() {
19  if (unlikely(is_any_nan(avi_->val_, bvi_->val_))) {
20  avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
21  bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
22  } else {
23  avi_->adj_ += adj_;
24  bvi_->adj_ += adj_;
25  }
26  }
27 };
28 
29 class add_vd_vari : public op_vd_vari {
30  public:
31  add_vd_vari(vari* avi, double b) : op_vd_vari(avi->val_ + b, avi, b) {}
32  void chain() {
33  if (unlikely(is_any_nan(avi_->val_, bd_)))
34  avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
35  else
36  avi_->adj_ += adj_;
37  }
38 };
39 } // namespace internal
40 
79 inline var operator+(const var& a, const var& b) {
80  return var(new internal::add_vv_vari(a.vi_, b.vi_));
81 }
82 
94 inline var operator+(const var& a, double b) {
95  if (b == 0.0)
96  return a;
97  return var(new internal::add_vd_vari(a.vi_, b));
98 }
99 
111 inline var operator+(double a, const var& b) {
112  if (a == 0.0)
113  return b;
114  return var(new internal::add_vd_vari(b.vi_, a)); // by symmetry
115 }
116 
117 } // namespace math
118 } // namespace stan
119 #endif
The variable implementation base class.
Definition: vari.hpp:30
bool is_any_nan(const T &x)
Returns true if the input is NaN and false otherwise.
Definition: is_any_nan.hpp:21
void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:33
friend class var
Definition: vari.hpp:32
const double val_
The value of this variable.
Definition: vari.hpp:38
void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
#define unlikely(x)
Definition: likely.hpp:9
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:45
fvar< T > operator+(const fvar< T > &x1, const fvar< T > &x2)
Return the sum of the specified forward mode addends.
double adj_
The adjoint of this variable, which is the partial derivative of this variable with respect to the ro...
Definition: vari.hpp:44

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