| Stan Math Library 2.11.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
core.hpp
stan
Definition: log_sum_exp.hpp:8
std
Definition: std_numeric_limits.hpp:7
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:31
stan::math::fmod
fvar< T > fmod(const fvar< T > &x1, const fvar< T > &x2)
Definition: fmod.hpp:16
boost::math::isnan
bool isnan(const stan::math::var &v)
Checks if the given number is NaN.
Definition: boost_isnan.hpp:22
unlikely
#define unlikely(x)
Definition: likely.hpp:9
stan::math::var::vi_
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:43

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