Automatic Differentiation
 
Loading...
Searching...
No Matches
fmod.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_FMOD_HPP
2#define STAN_MATH_REV_FUN_FMOD_HPP
3
8#include <cmath>
9
10namespace stan {
11namespace math {
12
13namespace internal {
14class fmod_vv_vari : public op_vv_vari {
15 public:
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_ = NOT_A_NUMBER;
21 bvi_->adj_ = NOT_A_NUMBER;
22 } else {
23 avi_->adj_ += adj_;
24 bvi_->adj_ -= adj_ * std::trunc(avi_->val_ / bvi_->val_);
25 }
26 }
27};
28
29class 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_ = NOT_A_NUMBER;
36 } else {
37 avi_->adj_ += adj_;
38 }
39 }
40};
41
42class fmod_dv_vari : public op_dv_vari {
43 public:
44 fmod_dv_vari(double a, vari* bvi)
45 : op_dv_vari(std::fmod(a, bvi->val_), a, bvi) {}
46 void chain() {
47 if (unlikely(is_any_nan(bvi_->val_, ad_))) {
48 bvi_->adj_ = NOT_A_NUMBER;
49 } else {
50 int d = std::trunc(ad_ / bvi_->val_);
51 bvi_->adj_ -= adj_ * d;
52 }
53 }
54};
55} // namespace internal
56
100inline var fmod(const var& a, const var& b) {
101 return var(new internal::fmod_vv_vari(a.vi_, b.vi_));
102}
103
117inline var fmod(const var& a, double b) {
118 return var(new internal::fmod_vd_vari(a.vi_, b));
119}
120
134inline var fmod(double a, const var& b) {
135 return var(new internal::fmod_dv_vari(a, b.vi_));
136}
137
138} // namespace math
139} // namespace stan
140#endif
fmod_dv_vari(double a, vari *bvi)
Definition fmod.hpp:44
fmod_vd_vari(vari *avi, double b)
Definition fmod.hpp:31
fmod_vv_vari(vari *avi, vari *bvi)
Definition fmod.hpp:16
#define unlikely(x)
static constexpr double NOT_A_NUMBER
(Quiet) not-a-number value.
Definition constants.hpp:56
var_value< double > var
Definition var.hpp:1187
fvar< T > fmod(const fvar< T > &x1, const fvar< T > &x2)
Definition fmod.hpp:15
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 ...
STL namespace.