1 #ifndef STAN_MATH_REV_FUN_CEIL_HPP
2 #define STAN_MATH_REV_FUN_CEIL_HPP
3 
4 #include <stan/math/rev/meta.hpp>
5 #include <stan/math/rev/core.hpp>
6 #include <stan/math/prim/fun/constants.hpp>
7 #include <stan/math/prim/fun/is_nan.hpp>
8 #include <cmath>
9 
10 namespace stan {
11 namespace math {
12 
13 /**
14  * Return the ceiling of the specified variable (cmath).
15  *
16  * The derivative of the ceiling function is defined and
17  * zero everywhere but at integers, and we set them to zero for
18  * convenience,
19  *
20  * \f$\frac{d}{dx} {\lceil x \rceil} = 0\f$.
21  *
22  * The ceiling function rounds up.  For double values, this is the
23  * smallest integral value that is not less than the specified
24  * value.  Although this function is not differentiable because it
25  * is discontinuous at integral values, its gradient is returned
26  * as zero everywhere.
27  *
28    \f[
29    \mbox{ceil}(x) =
30    \begin{cases}
31      \lceil x\rceil & \mbox{if } -\infty\leq x \leq \infty \\[6pt]
32      \textrm{NaN} & \mbox{if } x = \textrm{NaN}
33    \end{cases}
34    \f]
35 
36    \f[
37    \frac{\partial\, \mbox{ceil}(x)}{\partial x} =
38    \begin{cases}
39      0 & \mbox{if } -\infty\leq x\leq \infty \\[6pt]
40      \textrm{NaN} & \mbox{if } x = \textrm{NaN}
41    \end{cases}
42    \f]
43  *
44  * @param a Input variable.
45  * @return Ceiling of the variable.
46  */
ceil(const var & a)47 inline var ceil(const var& a) { return var(std::ceil(a.val())); }
48 
49 template <typename T, require_matrix_t<T>* = nullptr>
ceil(const var_value<T> & a)50 inline auto ceil(const var_value<T>& a) {
51   return var_value<T>(a.val().array().ceil());
52 }
53 
54 }  // namespace math
55 }  // namespace stan
56 #endif
57