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
9#include <cmath>
10
11namespace stan {
12namespace math {
13
14namespace internal {
15class fmod_vv_vari : public op_vv_vari {
16 public:
18 : op_vv_vari(std::fmod(avi->val_, bvi->val_), avi, bvi) {}
19 void chain() {
20 if (unlikely(is_any_nan(avi_->val_, bvi_->val_))) {
21 avi_->adj_ = NOT_A_NUMBER;
22 bvi_->adj_ = NOT_A_NUMBER;
23 } else {
24 avi_->adj_ += adj_;
25 bvi_->adj_ -= adj_ * std::trunc(avi_->val_ / bvi_->val_);
26 }
27 }
28};
29
30class 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 void chain() {
35 if (unlikely(is_any_nan(avi_->val_, bd_))) {
36 avi_->adj_ = NOT_A_NUMBER;
37 } else {
38 avi_->adj_ += adj_;
39 }
40 }
41};
42
43class fmod_dv_vari : public op_dv_vari {
44 public:
45 fmod_dv_vari(double a, vari* bvi)
46 : op_dv_vari(std::fmod(a, bvi->val_), a, bvi) {}
47 void chain() {
48 if (unlikely(is_any_nan(bvi_->val_, ad_))) {
49 bvi_->adj_ = NOT_A_NUMBER;
50 } else {
51 int d = std::trunc(ad_ / bvi_->val_);
52 bvi_->adj_ -= adj_ * d;
53 }
54 }
55};
56} // namespace internal
57
101inline var fmod(const var& a, const var& b) {
102 return var(new internal::fmod_vv_vari(a.vi_, b.vi_));
103}
104
118inline var fmod(const var& a, double b) {
119 return var(new internal::fmod_vd_vari(a.vi_, b));
120}
121
135inline var fmod(double a, const var& b) {
136 return var(new internal::fmod_dv_vari(a, b.vi_));
137}
138
139} // namespace math
140} // namespace stan
141#endif
fmod_dv_vari(double a, vari *bvi)
Definition fmod.hpp:45
fmod_vd_vari(vari *avi, double b)
Definition fmod.hpp:32
fmod_vv_vari(vari *avi, vari *bvi)
Definition fmod.hpp:17
#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:17
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.