Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
check_ordered.hpp
Go to the documentation of this file.
1 #ifndef STAN__MATH__ERROR_HANDLING__MATRIX__CHECK_ORDERED_HPP
2 #define STAN__MATH__ERROR_HANDLING__MATRIX__CHECK_ORDERED_HPP
3 
4 #include <sstream>
5 
9 #include <stan/meta/traits.hpp>
10 
11 namespace stan {
12  namespace math {
13 
28  template <typename T_y, typename T_result>
29  bool check_ordered(const char* function,
30  const Eigen::Matrix<T_y,Eigen::Dynamic,1>& y,
31  const char* name,
32  T_result* result) {
33  using Eigen::Dynamic;
34  using Eigen::Matrix;
35  typedef typename index_type<Matrix<T_y,Dynamic,1> >::type size_t;
36 
37  if (y.size() == 0) {
38  return true;
39  }
40  for (size_t n = 1; n < y.size(); n++) {
41  if (!(y[n] > y[n-1])) {
42  std::ostringstream stream;
43  stream << " is not a valid ordered vector."
44  << " The element at " << stan::error_index::value + n
45  << " is %1%, but should be greater than the previous element, "
46  << y[n-1];
47  std::string msg(stream.str());
48  return dom_err(function,y[n],name,
49  msg.c_str(),"",
50  result);
51  return false;
52  }
53  }
54  return true;
55  }
56  template <typename T_y, typename T_result>
57  bool check_ordered(const char* function,
58  const std::vector<T_y>& y,
59  const char* name,
60  T_result* result) {
61  if (y.size() == 0) {
62  return true;
63  }
64  for (int n = 1; n < y.size(); n++) {
65  if (!(y[n] > y[n-1])) {
66  std::ostringstream stream;
67  stream << " is not a valid ordered vector."
68  << " The element at " << stan::error_index::value + n
69  << " is %1%, but should be greater than the previous element, "
70  << y[n-1];
71  std::string msg(stream.str());
72  return dom_err(function,y[n],name,
73  msg.c_str(),"",
74  result);
75  return false;
76  }
77  }
78  return true;
79  }
80  template <typename T>
81  bool check_ordered(const char* function,
82  const Eigen::Matrix<T,Eigen::Dynamic,1>& y,
83  const char* name,
84  T* result = 0) {
85  return check_ordered<T,T>(function,y,name,result);
86  }
87  template <typename T>
88  bool check_ordered(const char* function,
89  const std::vector<T>& y,
90  const char* name,
91  T* result = 0) {
92  return check_ordered<T,T>(function,y,name,result);
93  }
94 
95  }
96 }
97 #endif
bool check_ordered(const char *function, const Eigen::Matrix< T_y, Eigen::Dynamic, 1 > &y, const char *name, T_result *result)
Return true if the specified vector is sorted into increasing order.
bool dom_err(const char *function, const T &y, const char *name, const char *error_msg, const T_msg error_msg2, T_result *result)
Definition: dom_err.hpp:36
Primary template class for the metaprogram to compute the index type of a container.
Definition: index_type.hpp:21

     [ Stan Home Page ] © 2011–2014, Stan Development Team.