1 #ifndef STAN_MATH_PRIM_FUN_MAX_HPP 2 #define STAN_MATH_PRIM_FUN_MAX_HPP 3 4 #include <stan/math/prim/meta.hpp> 5 #include <stan/math/prim/err.hpp> 6 #include <stan/math/prim/fun/Eigen.hpp> 7 #include <stan/math/prim/fun/constants.hpp> 8 #include <algorithm> 9 #include <vector> 10 11 namespace stan { 12 namespace math { 13 14 /** 15 * Returns the maximum coefficient in the specified 16 * matrix, vector, row vector or std vector. 17 * 18 * @tparam T type of the container 19 * @param m specified matrix, vector, row vector or std vector 20 * @return maximum coefficient value in the container, or -infinity if the 21 * container is size zero and the scalar type in container is floating point 22 * number 23 * @throws <code>std::invalid_argument</code> if the vector is size zero and the 24 * scalar type in the container is integer 25 */ 26 template <typename T, require_container_t<T>* = nullptr> max(const T & m)27inline value_type_t<T> max(const T& m) { 28 if (std::is_integral<value_type_t<T>>::value) { 29 check_nonzero_size("max", "int vector", m); 30 } else if (m.size() == 0) { 31 return NEGATIVE_INFTY; 32 } 33 return apply_vector_unary<T>::reduce( 34 m, [](const auto& x) { return x.maxCoeff(); }); 35 } 36 37 } // namespace math 38 } // namespace stan 39 40 #endif 41