All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
fmod.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_REV_SCAL_FUN_FMOD_HPP
#define STAN_MATH_REV_SCAL_FUN_FMOD_HPP
#include <stan/math/rev/core.hpp>
#include <boost/math/special_functions/fpclassify.hpp>
#include <cmath>
#include <limits>
namespace stan {
namespace math {
namespace {
class fmod_vv_vari : public op_vv_vari {
public:
fmod_vv_vari(vari* avi, vari* bvi) :
op_vv_vari(std::fmod(avi->val_, bvi->val_), avi, bvi) {
}
void chain() {
if (unlikely(boost::math::isnan(avi_->val_)
|| boost::math::isnan(bvi_->val_))) {
avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
} else {
avi_->adj_ += adj_;
bvi_->adj_ -= adj_ * static_cast<int>(avi_->val_ / bvi_->val_);
}
}
};
class fmod_vd_vari : public op_vd_vari {
public:
fmod_vd_vari(vari* avi, double b) :
op_vd_vari(std::fmod(avi->val_, b), avi, b) {
}
void chain() {
if (unlikely(boost::math::isnan(avi_->val_)
|| boost::math::isnan(bd_)))
avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
else
avi_->adj_ += adj_;
}
};
class fmod_dv_vari : public op_dv_vari {
public:
fmod_dv_vari(double a, vari* bvi) :
op_dv_vari(std::fmod(a, bvi->val_), a, bvi) {
}
void chain() {
if (unlikely(boost::math::isnan(bvi_->val_)
|| boost::math::isnan(ad_))) {
bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
} else {
int d = static_cast<int>(ad_ / bvi_->val_);
bvi_->adj_ -= adj_ * d;
}
}
};
}
inline var fmod(const var& a, const var& b) {
return var(new fmod_vv_vari(a.vi_, b.vi_));
}
inline var fmod(const var& a, const double b) {
return var(new fmod_vd_vari(a.vi_, b));
}
inline var fmod(const double a, const var& b) {
return var(new fmod_dv_vari(a, b.vi_));
}
}
}
#endif
core.hpp
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:32
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:44

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