Stan Math Library  2.20.0
reverse mode automatic differentiation
fmod.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_FWD_SCAL_FUN_FMOD_HPP
2 #define STAN_MATH_FWD_SCAL_FUN_FMOD_HPP
3 
4 #include <stan/math/fwd/meta.hpp>
5 #include <stan/math/fwd/core.hpp>
9 
10 namespace stan {
11 namespace math {
12 
13 template <typename T>
14 inline fvar<T> fmod(const fvar<T>& x1, const fvar<T>& x2) {
15  using std::floor;
16  using std::fmod;
17  return fvar<T>(fmod(x1.val_, x2.val_),
18  x1.d_ - x2.d_ * floor(x1.val_ / x2.val_));
19 }
20 
21 template <typename T>
22 inline fvar<T> fmod(const fvar<T>& x1, double x2) {
23  using std::fmod;
24  if (unlikely(is_any_nan(value_of(x1.val_), x2)))
25  return fvar<T>(fmod(x1.val_, x2), NOT_A_NUMBER);
26  else
27  return fvar<T>(fmod(x1.val_, x2), x1.d_ / x2);
28 }
29 
30 template <typename T>
31 inline fvar<T> fmod(double x1, const fvar<T>& x2) {
32  using std::floor;
33  using std::fmod;
34  return fvar<T>(fmod(x1, x2.val_), -x2.d_ * floor(x1 / x2.val_));
35 }
36 
37 } // namespace math
38 } // namespace stan
39 #endif
T d_
The tangent (derivative) of this variable.
Definition: fvar.hpp:50
const double NOT_A_NUMBER
(Quiet) not-a-number value.
Definition: constants.hpp:58
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition: value_of.hpp:17
var fmod(double a, const var &b)
Return the floating point remainder after dividing the first scalar by the second variable (cmath)...
Definition: fmod.hpp:133
bool is_any_nan(const T &x)
Returns true if the input is NaN and false otherwise.
Definition: is_any_nan.hpp:21
fvar< T > fmod(const fvar< T > &x1, const fvar< T > &x2)
Definition: fmod.hpp:14
#define unlikely(x)
Definition: likely.hpp:9
T val_
The value of this variable.
Definition: fvar.hpp:45
fvar< T > floor(const fvar< T > &x)
Definition: floor.hpp:12
This template class represents scalars used in forward-mode automatic differentiation, which consist of values and directional derivatives of the specified template type.
Definition: fvar.hpp:41

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