Stan Math Library  2.20.0
reverse mode automatic differentiation
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/meta.hpp>
6 #include <stan/math/rev/core.hpp>
7 #include <limits>
8 
9 namespace stan {
10 namespace math {
11 
12 namespace internal {
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  void chain() {
18  if (unlikely(is_any_nan(avi_->val_, bvi_->val_))) {
19  avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
20  bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
21  } else {
22  avi_->adj_ += adj_;
23  bvi_->adj_ -= adj_;
24  }
25  }
26 };
27 
28 class fdim_vd_vari : public op_vd_vari {
29  public:
30  fdim_vd_vari(vari* avi, double b) : op_vd_vari(avi->val_ - b, avi, b) {}
31  void chain() {
32  if (unlikely(is_any_nan(avi_->val_, bd_)))
33  avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
34  else
35  avi_->adj_ += adj_;
36  }
37 };
38 
39 class fdim_dv_vari : public op_dv_vari {
40  public:
41  fdim_dv_vari(double a, vari* bvi) : op_dv_vari(a - bvi->val_, a, bvi) {}
42  void chain() {
43  if (unlikely(is_any_nan(bvi_->val_, ad_)))
44  bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
45  else
46  bvi_->adj_ -= adj_;
47  }
48 };
49 } // namespace internal
50 
89 inline var fdim(const var& a, const var& b) {
90  // reversed test to get NaN vals automatically in second case
91  return (a.vi_->val_ <= b.vi_->val_)
92  ? var(new vari(0.0))
93  : var(new internal::fdim_vv_vari(a.vi_, b.vi_));
94 }
95 
108 inline var fdim(double a, const var& b) {
109  // reversed test to get NaN vals automatically in second case
110  return a <= b.vi_->val_ ? var(new vari(0.0))
111  : var(new internal::fdim_dv_vari(a, b.vi_));
112 }
113 
125 inline var fdim(const var& a, double b) {
126  // reversed test to get NaN vals automatically in second case
127  return a.vi_->val_ <= b ? var(new vari(0.0))
128  : var(new internal::fdim_vd_vari(a.vi_, b));
129 }
130 
131 } // namespace math
132 } // namespace stan
133 #endif
void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
Definition: fdim.hpp:42
fdim_dv_vari(double a, vari *bvi)
Definition: fdim.hpp:41
The variable implementation base class.
Definition: vari.hpp:30
bool is_any_nan(const T &x)
Returns true if the input is NaN and false otherwise.
Definition: is_any_nan.hpp:21
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:33
friend class var
Definition: vari.hpp:32
const double val_
The value of this variable.
Definition: vari.hpp:38
void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
Definition: fdim.hpp:31
fvar< T > fdim(const fvar< T > &x, const fvar< T > &y)
Return the positive difference of the specified values (C++11).
Definition: fdim.hpp:21
fdim_vv_vari(vari *avi, vari *bvi)
Definition: fdim.hpp:15
#define unlikely(x)
Definition: likely.hpp:9
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:45
fdim_vd_vari(vari *avi, double b)
Definition: fdim.hpp:30
vari(double x)
Construct a variable implementation from a value.
Definition: vari.hpp:58
void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
Definition: fdim.hpp:17
double adj_
The adjoint of this variable, which is the partial derivative of this variable with respect to the ro...
Definition: vari.hpp:44

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