Stan Math Library: stan/math/rev/core/operator_addition.hpp Source File

| Stan Math Library 2.10.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 <boost/math/special_functions/fpclassify.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(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_ += 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(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_;
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
stan
Definition: log_sum_exp.hpp:8
vd_vari.hpp
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:31
boost::math::isnan
bool isnan(const stan::math::var &v)
Checks if the given number is NaN.
Definition: boost_isnan.hpp:22
unlikely
#define unlikely(x)
Definition: likely.hpp:9
stan::math::var::vi_
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:43
var.hpp
stan::math::operator+
fvar< T > operator+(const fvar< T > &x1, const fvar< T > &x2)
Definition: operator_addition.hpp:13
vv_vari.hpp

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