Stan Math Library  2.8.0
reverse mode automatic differentiation
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
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/core.hpp>
5 #include <boost/math/special_functions/fpclassify.hpp>
6 #include <cmath>
7 #include <limits>
8 
9 namespace stan {
10  namespace math {
11 
12  namespace {
13  class fmod_vv_vari : public op_vv_vari {
14  public:
15  fmod_vv_vari(vari* avi, vari* bvi) :
16  op_vv_vari(std::fmod(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_ * static_cast<int>(avi_->val_ / bvi_->val_);
26  }
27  }
28  };
29 
30  class fmod_vd_vari : public op_vd_vari {
31  public:
32  fmod_vd_vari(vari* avi, double b) :
33  op_vd_vari(std::fmod(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 fmod_dv_vari : public op_dv_vari {
45  public:
46  fmod_dv_vari(double a, vari* bvi) :
47  op_dv_vari(std::fmod(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  int d = static_cast<int>(ad_ / bvi_->val_);
55  bvi_->adj_ -= adj_ * d;
56  }
57  }
58  };
59  }
60 
103  inline var fmod(const var& a, const var& b) {
104  return var(new fmod_vv_vari(a.vi_, b.vi_));
105  }
106 
120  inline var fmod(const var& a, const double b) {
121  return var(new fmod_vd_vari(a.vi_, b));
122  }
123 
137  inline var fmod(const double a, const var& b) {
138  return var(new fmod_dv_vari(a, b.vi_));
139  }
140 
141  }
142 }
143 #endif
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:32
fvar< T > fmod(const fvar< T > &x1, const fvar< T > &x2)
Definition: fmod.hpp:16
bool isnan(const stan::math::var &v)
Checks if the given number is NaN.
Definition: boost_isnan.hpp:22
#define unlikely(x)
Definition: likely.hpp:9
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:44

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