| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
4 #include <stan/math/rev/core/var.hpp>
5 #include <stan/math/rev/core/vv_vari.hpp>
6 #include <stan/math/rev/core/vd_vari.hpp>
7 #include <stan/math/prim/scal/fun/is_nan.hpp>
8 #include <limits>
9
10 namespace stan {
11 namespace math {
12
13 namespace {
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 }
19 void chain() {
20 if (unlikely(is_nan(avi_->val_)
21 || is_nan(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_ += adj_;
26 bvi_->adj_ += adj_;
27 }
28 }
29 };
30
31 class add_vd_vari : public op_vd_vari {
32 public:
33 add_vd_vari(vari* avi, double b) :
34 op_vd_vari(avi->val_ + b, avi, b) {
35 }
36 void chain() {
37 if (unlikely(is_nan(avi_->val_)
38 || is_nan(bd_)))
39 avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
40 else
41 avi_->adj_ += adj_;
42 }
43 };
44 }
45
84 inline var operator+(const var& a, const var& b) {
85 return var(new add_vv_vari(a.vi_, b.vi_));
86 }
87
99 inline var operator+(const var& a, const double b) {
100 if (b == 0.0)
101 return a;
102 return var(new add_vd_vari(a.vi_, b));
103 }
104
116 inline var operator+(const double a, const var& b) {
117 if (a == 0.0)
118 return b;
119 return var(new add_vd_vari(b.vi_, a)); // by symmetry
120 }
121
122 }
123 }
124 #endif
is_nan.hpp
stan
Definition: log_sum_exp.hpp:8
vd_vari.hpp
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:30
unlikely
#define unlikely(x)
Definition: likely.hpp:9
stan::math::var::vi_
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:42
var.hpp
stan::math::operator+
fvar< T > operator+(const fvar< T > &x1, const fvar< T > &x2)
Definition: operator_addition.hpp:12
vv_vari.hpp
stan::math::is_nan
int is_nan(const fvar< T > &x)
Returns 1 if the input's value is NaN and 0 otherwise.
Definition: is_nan.hpp:21

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