1 #ifndef STAN_MATH_PRIM_MAT_ERR_CHECK_ORDERED_HPP
2 #define STAN_MATH_PRIM_MAT_ERR_CHECK_ORDERED_HPP
3 
4 #include <stan/math/prim/mat/fun/Eigen.hpp>
5 #include <stan/math/prim/mat/meta/index_type.hpp>
6 #include <stan/math/prim/scal/err/domain_error.hpp>
7 #include <stan/math/prim/scal/meta/error_index.hpp>
8 #include <sstream>
9 #include <vector>
10 #include <string>
11 
12 namespace stan {
13 namespace math {
14 
15 /**
16  * Check if the specified vector is sorted into
17  * strictly increasing order.
18  *
19  * @tparam T_y Type of scalar
20  *
21  * @param function Function name (for error messages)
22  * @param name Variable name (for error messages)
23  * @param y Vector to test
24  *
25  * @throw <code>std::domain_error</code> if the vector elements are
26  *   not ordered, if there are duplicated
27  *   values, or if any element is <code>NaN</code>.
28  */
29 template <typename T_y>
check_ordered(const char * function,const char * name,const Eigen::Matrix<T_y,Eigen::Dynamic,1> & y)30 void check_ordered(const char* function, const char* name,
31                    const Eigen::Matrix<T_y, Eigen::Dynamic, 1>& y) {
32   using Eigen::Dynamic;
33   using Eigen::Matrix;
34 
35   typedef typename index_type<Matrix<T_y, Dynamic, 1> >::type size_t;
36 
37   for (size_t n = 1; n < y.size(); n++) {
38     if (!(y[n] > y[n - 1])) {
39       std::ostringstream msg1;
40       msg1 << "is not a valid ordered vector."
41            << " The element at " << stan::error_index::value + n << " is ";
42       std::string msg1_str(msg1.str());
43       std::ostringstream msg2;
44       msg2 << ", but should be greater than the previous element, " << y[n - 1];
45       std::string msg2_str(msg2.str());
46       domain_error(function, name, y[n], msg1_str.c_str(), msg2_str.c_str());
47     }
48   }
49 }
50 
51 }  // namespace math
52 }  // namespace stan
53 #endif
54