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
8
9namespace stan {
10namespace math {
11
12namespace internal {
13class fdim_vv_vari : public op_vv_vari {
14 public:
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_ = NOT_A_NUMBER;
20 bvi_->adj_ = NOT_A_NUMBER;
21 } else {
22 avi_->adj_ += adj_;
23 bvi_->adj_ -= adj_;
24 }
25 }
26};
27
28class 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_ = NOT_A_NUMBER;
34 } else {
35 avi_->adj_ += adj_;
36 }
37 }
38};
39
40class fdim_dv_vari : public op_dv_vari {
41 public:
42 fdim_dv_vari(double a, vari* bvi) : op_dv_vari(a - bvi->val_, a, bvi) {}
43 void chain() {
44 if (unlikely(is_any_nan(bvi_->val_, ad_))) {
45 bvi_->adj_ = NOT_A_NUMBER;
46 } else {
47 bvi_->adj_ -= adj_;
48 }
49 }
50};
51} // namespace internal
52
91inline var fdim(const var& a, const var& b) {
92 // reversed test to get NaN vals automatically in second case
93 return (a.vi_->val_ <= b.vi_->val_)
94 ? var(new vari(0.0))
95 : var(new internal::fdim_vv_vari(a.vi_, b.vi_));
96}
97
110inline var fdim(double a, const var& b) {
111 // reversed test to get NaN vals automatically in second case
112 return a <= b.vi_->val_ ? var(new vari(0.0))
113 : var(new internal::fdim_dv_vari(a, b.vi_));
114}
115
127inline var fdim(const var& a, double b) {
128 // reversed test to get NaN vals automatically in second case
129 return a.vi_->val_ <= b ? var(new vari(0.0))
130 : var(new internal::fdim_vd_vari(a.vi_, b));
131}
132
133} // namespace math
134} // namespace stan
135#endif
fdim_dv_vari(double a, vari *bvi)
Definition fdim.hpp:42
fdim_vd_vari(vari *avi, double b)
Definition fdim.hpp:30
fdim_vv_vari(vari *avi, vari *bvi)
Definition fdim.hpp:15
#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 ...