| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

operator_subtraction.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_CORE_OPERATOR_SUBTRACTION_HPP
2 #define STAN_MATH_REV_CORE_OPERATOR_SUBTRACTION_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/rev/core/dv_vari.hpp>
8 #include <stan/math/prim/scal/fun/is_nan.hpp>
9 #include <limits>
10
11 namespace stan {
12 namespace math {
13
14 namespace {
15 class subtract_vv_vari : public op_vv_vari {
16 public:
17 subtract_vv_vari(vari* avi, vari* bvi) :
18 op_vv_vari(avi->val_ - bvi->val_, avi, bvi) {
19 }
20 void chain() {
21 if (unlikely(is_nan(avi_->val_)
22 || is_nan(bvi_->val_))) {
23 avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
24 bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
25 } else {
26 avi_->adj_ += adj_;
27 bvi_->adj_ -= adj_;
28 }
29 }
30 };
31
32 class subtract_vd_vari : public op_vd_vari {
33 public:
34 subtract_vd_vari(vari* avi, double b) :
35 op_vd_vari(avi->val_ - b, avi, b) {
36 }
37 void chain() {
38 if (unlikely(is_nan(avi_->val_)
39 || is_nan(bd_)))
40 avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
41 else
42 avi_->adj_ += adj_;
43 }
44 };
45
46 class subtract_dv_vari : public op_dv_vari {
47 public:
48 subtract_dv_vari(double a, vari* bvi) :
49 op_dv_vari(a - bvi->val_, a, bvi) {
50 }
51 void chain() {
52 if (unlikely(is_nan(ad_)
53 || is_nan(bvi_->val_)))
54 bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
55 else
56 bvi_->adj_ -= adj_;
57 }
58 };
59 }
60
99 inline var operator-(const var& a, const var& b) {
100 return var(new subtract_vv_vari(a.vi_, b.vi_));
101 }
102
114 inline var operator-(const var& a, const double b) {
115 if (b == 0.0)
116 return a;
117 return var(new subtract_vd_vari(a.vi_, b));
118 }
119
131 inline var operator-(const double a, const var& b) {
132 return var(new subtract_dv_vari(a, b.vi_));
133 }
134
135 }
136 }
137 #endif
dv_vari.hpp
stan::math::operator-
fvar< T > operator-(const fvar< T > &x1, const fvar< T > &x2)
Definition: operator_subtraction.hpp:12
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
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.