Stan Math Library  2.20.0
reverse mode automatic differentiation
fmod.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_SCAL_FUN_FMOD_HPP
2 #define STAN_MATH_REV_SCAL_FUN_FMOD_HPP
3 
4 #include <stan/math/rev/meta.hpp>
5 #include <stan/math/rev/core.hpp>
7 #include <cmath>
8 #include <limits>
9 
10 namespace stan {
11 namespace math {
12 
13 namespace internal {
14 class fmod_vv_vari : public op_vv_vari {
15  public:
16  fmod_vv_vari(vari* avi, vari* bvi)
17  : op_vv_vari(std::fmod(avi->val_, bvi->val_), avi, bvi) {}
18  void chain() {
19  if (unlikely(is_any_nan(avi_->val_, bvi_->val_))) {
20  avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
21  bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
22  } else {
23  avi_->adj_ += adj_;
24  bvi_->adj_ -= adj_ * static_cast<int>(avi_->val_ / bvi_->val_);
25  }
26  }
27 };
28 
29 class fmod_vd_vari : public op_vd_vari {
30  public:
31  fmod_vd_vari(vari* avi, double b)
32  : op_vd_vari(std::fmod(avi->val_, b), avi, b) {}
33  void chain() {
34  if (unlikely(is_any_nan(avi_->val_, bd_)))
35  avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
36  else
37  avi_->adj_ += adj_;
38  }
39 };
40 
41 class fmod_dv_vari : public op_dv_vari {
42  public:
43  fmod_dv_vari(double a, vari* bvi)
44  : op_dv_vari(std::fmod(a, bvi->val_), a, bvi) {}
45  void chain() {
46  if (unlikely(is_any_nan(bvi_->val_, ad_))) {
47  bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
48  } else {
49  int d = static_cast<int>(ad_ / bvi_->val_);
50  bvi_->adj_ -= adj_ * d;
51  }
52  }
53 };
54 } // namespace internal
55 
99 inline var fmod(const var& a, const var& b) {
100  return var(new internal::fmod_vv_vari(a.vi_, b.vi_));
101 }
102 
116 inline var fmod(const var& a, double b) {
117  return var(new internal::fmod_vd_vari(a.vi_, b));
118 }
119 
133 inline var fmod(double a, const var& b) {
134  return var(new internal::fmod_dv_vari(a, b.vi_));
135 }
136 
137 } // namespace math
138 } // namespace stan
139 #endif
fmod_vd_vari(vari *avi, double b)
Definition: fmod.hpp:31
void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
Definition: fmod.hpp:45
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
fmod_vv_vari(vari *avi, vari *bvi)
Definition: fmod.hpp:16
fvar< T > fmod(const fvar< T > &x1, const fvar< T > &x2)
Definition: fmod.hpp:14
friend class var
Definition: vari.hpp:32
const double val_
The value of this variable.
Definition: vari.hpp:38
#define unlikely(x)
Definition: likely.hpp:9
fmod_dv_vari(double a, vari *bvi)
Definition: fmod.hpp:43
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:45
void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
Definition: fmod.hpp:33
void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
Definition: fmod.hpp:18
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.