| Stan Math Library 2.11.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 <boost/math/special_functions/fpclassify.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(boost::math::isnan(avi_->val_)
20 || boost::math::isnan(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(boost::math::isnan(avi_->val_)
37 || boost::math::isnan(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(boost::math::isnan(bvi_->val_)
51 || boost::math::isnan(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 stan::math::var& a,
111 const stan::math::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(const double& a,
136 const stan::math::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 stan::math::var& a,
159 const 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
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:31
stan::math::vari::val_
const double val_
The value of this variable.
Definition: vari.hpp:38
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::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:43
likely.hpp

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