Automatic Differentiation
 
Loading...
Searching...
No Matches
fdim.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_FDIM_HPP
2#define STAN_MATH_REV_FUN_FDIM_HPP
3
9
10namespace stan {
11namespace math {
12
13namespace internal {
14class fdim_vv_vari : public op_vv_vari {
15 public:
17 : op_vv_vari(avi->val_ - bvi->val_, avi, bvi) {}
18 void chain() {
19 if (unlikely(is_any_nan(avi_->val_, bvi_->val_))) {
20 avi_->adj_ = NOT_A_NUMBER;
21 bvi_->adj_ = NOT_A_NUMBER;
22 } else {
23 avi_->adj_ += adj_;
24 bvi_->adj_ -= adj_;
25 }
26 }
27};
28
29class fdim_vd_vari : public op_vd_vari {
30 public:
31 fdim_vd_vari(vari* avi, double b) : op_vd_vari(avi->val_ - b, avi, b) {}
32 void chain() {
33 if (unlikely(is_any_nan(avi_->val_, bd_))) {
34 avi_->adj_ = NOT_A_NUMBER;
35 } else {
36 avi_->adj_ += adj_;
37 }
38 }
39};
40
41class fdim_dv_vari : public op_dv_vari {
42 public:
43 fdim_dv_vari(double a, vari* bvi) : op_dv_vari(a - bvi->val_, a, bvi) {}
44 void chain() {
45 if (unlikely(is_any_nan(bvi_->val_, ad_))) {
46 bvi_->adj_ = NOT_A_NUMBER;
47 } else {
48 bvi_->adj_ -= adj_;
49 }
50 }
51};
52} // namespace internal
53
92inline var fdim(const var& a, const var& b) {
93 // reversed test to get NaN vals automatically in second case
94 return (a.vi_->val_ <= b.vi_->val_)
95 ? var(new vari(0.0))
96 : var(new internal::fdim_vv_vari(a.vi_, b.vi_));
97}
98
111inline var fdim(double a, const var& b) {
112 // reversed test to get NaN vals automatically in second case
113 return a <= b.vi_->val_ ? var(new vari(0.0))
114 : var(new internal::fdim_dv_vari(a, b.vi_));
115}
116
128inline var fdim(const var& a, double b) {
129 // reversed test to get NaN vals automatically in second case
130 return a.vi_->val_ <= b ? var(new vari(0.0))
131 : var(new internal::fdim_vd_vari(a.vi_, b));
132}
133
134} // namespace math
135} // namespace stan
136#endif
fdim_dv_vari(double a, vari *bvi)
Definition fdim.hpp:43
fdim_vd_vari(vari *avi, double b)
Definition fdim.hpp:31
fdim_vv_vari(vari *avi, vari *bvi)
Definition fdim.hpp:16
#define unlikely(x)
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
static constexpr double NOT_A_NUMBER
(Quiet) not-a-number value.
Definition constants.hpp:56
vari_value< double > vari
Definition vari.hpp:197
var_value< double > var
Definition var.hpp:1187
bool is_any_nan(const T &x)
Returns true if the input is NaN and false otherwise.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...