1 #ifndef STAN_MATH_PRIM_FUN_CEIL_HPP
2 #define STAN_MATH_PRIM_FUN_CEIL_HPP
3
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/fun/Eigen.hpp>
6 #include <stan/math/prim/functor/apply_scalar_unary.hpp>
7 #include <stan/math/prim/functor/apply_vector_unary.hpp>
8 #include <cmath>
9
10 namespace stan {
11 namespace math {
12
13 /**
14 * Structure to wrap `ceil()` so it can be vectorized.
15 *
16 * @tparam T type of variable
17 * @param x variable
18 * @return Least integer >= x.
19 */
20 struct ceil_fun {
21 template <typename T>
funstan::math::ceil_fun22 static inline T fun(const T& x) {
23 using std::ceil;
24 return ceil(x);
25 }
26 };
27
28 /**
29 * Returns the elementwise `ceil()` of the input,
30 * which may be a scalar or any Stan container of numeric scalars.
31 *
32 * @tparam Container type of container
33 * @param x container
34 * @return Least integer >= each value in x.
35 */
36 template <typename Container,
37 require_not_container_st<std::is_arithmetic, Container>* = nullptr,
38 require_all_not_nonscalar_prim_or_rev_kernel_expression_t<
39 Container>* = nullptr>
ceil(const Container & x)40 inline auto ceil(const Container& x) {
41 return apply_scalar_unary<ceil_fun, Container>::apply(x);
42 }
43
44 /**
45 * Version of `ceil()` that accepts std::vectors, Eigen Matrix/Array objects
46 * or expressions, and containers of these.
47 *
48 * @tparam Container Type of x
49 * @param x Container
50 * @return Least integer >= each value in x.
51 */
52 template <typename Container,
53 require_container_st<std::is_arithmetic, Container>* = nullptr,
54 require_not_var_matrix_t<Container>* = nullptr>
ceil(const Container & x)55 inline auto ceil(const Container& x) {
56 return apply_vector_unary<Container>::apply(
57 x, [](const auto& v) { return v.array().ceil(); });
58 }
59
60 } // namespace math
61 } // namespace stan
62
63 #endif
64