1 #ifndef STAN_MATH_PRIM_FUN_ROUND_HPP 2 #define STAN_MATH_PRIM_FUN_ROUND_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 `round()` so it can be vectorized. 15 * 16 * @tparam T type of argument 17 * @param x argument variable 18 * @return Rounded value of x. 19 */ 20 struct round_fun { 21 template <typename T> funstan::math::round_fun22 static inline T fun(const T& x) { 23 using std::round; 24 return round(x); 25 } 26 }; 27 28 /** 29 * Vectorized version of `round()`. 30 * 31 * @tparam Container type of container 32 * @param x container 33 * @return Rounded value of each value in x. 34 */ 35 template <typename Container, 36 require_not_container_st<std::is_arithmetic, Container>* = nullptr, 37 require_all_not_nonscalar_prim_or_rev_kernel_expression_t< 38 Container>* = nullptr> round(const Container & x)39inline auto round(const Container& x) { 40 return apply_scalar_unary<round_fun, Container>::apply(x); 41 } 42 43 /** 44 * Version of `round()` that accepts std::vectors, Eigen Matrix/Array objects 45 * or expressions, and containers of these. 46 * 47 * @tparam Container Type of x 48 * @param x Container 49 * @return Rounded value of each value in x. 50 */ 51 template <typename Container, 52 require_container_st<std::is_arithmetic, Container>* = nullptr> round(const Container & x)53inline auto round(const Container& x) { 54 return apply_vector_unary<Container>::apply( 55 x, [](const auto& v) { return v.array().round(); }); 56 } 57 58 } // namespace math 59 } // namespace stan 60 61 #endif 62