floor.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_SCAL_FUN_FLOOR_HPP
2 #define STAN_MATH_REV_SCAL_FUN_FLOOR_HPP
3 
4 #include <stan/math/rev/core.hpp>
7 #include <cmath>
8 #include <limits>
9 
10 namespace stan {
11 namespace math {
12 
13 namespace {
14 class floor_vari : public op_v_vari {
15  public:
16  explicit floor_vari(vari* avi) : op_v_vari(std::floor(avi->val_), avi) {}
17  void chain() {
18  if (unlikely(is_nan(avi_->val_)))
19  avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
20  }
21 };
22 } // namespace
23 
24 /**
25  * Return the floor of the specified variable (cmath).
26  *
27  * The derivative of the floor function is defined and
28  * zero everywhere but at integers, so we set these derivatives
29  * to zero for convenience,
30  *
31  * \f$\frac{d}{dx} {\lfloor x \rfloor} = 0\f$.
32  *
33  * The floor function rounds down. For double values, this is the largest
34  * integral value that is not greater than the specified value.
35  * Although this function is not differentiable because it is
36  * discontinuous at integral values, its gradient is returned as
37  * zero everywhere.
38  *
39  \f[
40  \mbox{floor}(x) =
41  \begin{cases}
42  \lfloor x \rfloor & \mbox{if } -\infty\leq x \leq \infty \\[6pt]
43  \textrm{NaN} & \mbox{if } x = \textrm{NaN}
44  \end{cases}
45  \f]
46 
47  \f[
48  \frac{\partial\, \mbox{floor}(x)}{\partial x} =
49  \begin{cases}
50  0 & \mbox{if } -\infty\leq x\leq \infty \\[6pt]
51  \textrm{NaN} & \mbox{if } x = \textrm{NaN}
52  \end{cases}
53  \f]
54  *
55  * @param a Input variable.
56  * @return Floor of the variable.
57  */
58 inline var floor(const var& a) { return var(new floor_vari(a.vi_)); }
59 
60 } // namespace math
61 } // namespace stan
62 #endif
const double a
chain
Check that an output directory exists.
vari * vi_
Definition: var.hpp:45
#define unlikely(expr)
Definition: lz4.cxx:121
fvar< T > floor(const fvar< T > &x)
Definition: floor.hpp:11
int is_nan(const fvar< T > &x)
Definition: is_nan.hpp:19