| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

fdim.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_SCAL_FUN_FDIM_HPP
2 #define STAN_MATH_REV_SCAL_FUN_FDIM_HPP
3
4 #include <stan/math/rev/core.hpp>
5 #include <stan/math/prim/scal/fun/is_nan.hpp>
6 #include <stan/math/prim/scal/meta/likely.hpp>
7 #include <limits>
8
9 namespace stan {
10 namespace math {
11
12 namespace {
13 class fdim_vv_vari : public op_vv_vari {
14 public:
15 fdim_vv_vari(vari* avi, vari* bvi) :
16 op_vv_vari(avi->val_ - bvi->val_, avi, bvi) {
17 }
18 void chain() {
19 if (unlikely(is_nan(avi_->val_)
20 || is_nan(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 fdim_vd_vari : public op_vd_vari {
31 public:
32 fdim_vd_vari(vari* avi, double b) :
33 op_vd_vari(avi->val_ - b, avi, b) {
34 }
35 void chain() {
36 if (unlikely(is_nan(avi_->val_)
37 || is_nan(bd_)))
38 avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
39 else
40 avi_->adj_ += adj_;
41 }
42 };
43
44 class fdim_dv_vari : public op_dv_vari {
45 public:
46 fdim_dv_vari(double a, vari* bvi) :
47 op_dv_vari(a - bvi->val_, a, bvi) {
48 }
49 void chain() {
50 if (unlikely(is_nan(bvi_->val_)
51 || is_nan(ad_)))
52 bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
53 else
54 bvi_->adj_ -= adj_;
55 }
56 };
57 }
58
110 inline var fdim(const var& a,
111 const var& b) {
112 if (!(a.vi_->val_ <= b.vi_->val_))
113 return var(new fdim_vv_vari(a.vi_, b.vi_));
114 else
115 return var(new vari(0.0));
116 }
117
135 inline var fdim(double a,
136 const var& b) {
137 return a <= b.vi_->val_
138 ? var(new vari(0.0))
139 : var(new fdim_dv_vari(a, b.vi_));
140 }
141
158 inline var fdim(const var& a,
159 double b) {
160 return a.vi_->val_ <= b
161 ? var(new vari(0.0))
162 : var(new fdim_vd_vari(a.vi_, b));
163 }
164
165 }
166 }
167 #endif
core.hpp
is_nan.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::vari
The variable implementation base class.
Definition: vari.hpp:30
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:30
stan::math::vari::val_
const double val_
The value of this variable.
Definition: vari.hpp:38
unlikely
#define unlikely(x)
Definition: likely.hpp:9
stan::math::fdim
fvar< T > fdim(const fvar< T > &x1, const fvar< T > &x2)
Definition: fdim.hpp:11
stan::math::var::vi_
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:42
likely.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.