Stan Math Library  2.6.3
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
rank.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_RANK_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_RANK_HPP
3 
6 #include <vector>
7 
8 namespace stan {
9  namespace math {
10 
17  template <typename T>
18  inline size_t rank(const std::vector<T> & v, int s) {
20  size_t size = v.size();
21  check_range("rank", "v", size, s);
22  s--;
23  size_t count(0U);
24  T compare(v[s]);
25  for (size_t i = 0U; i < size; ++i)
26  if (v[i] < compare)
27  count++;
28  return count;
29  }
30 
37  template <typename T, int R, int C>
38  inline size_t rank(const Eigen::Matrix<T, R, C> & v, int s) {
40  size_t size = v.size();
41 
42  check_range("rank", "v", size, s);
43  s--;
44  const T * vv = v.data();
45  size_t count(0U);
46  T compare(vv[s]);
47  for (size_t i = 0U; i < size; ++i)
48  if (vv[i] < compare)
49  count++;
50  return count;
51  }
52 
53  }
54 }
55 #endif
bool check_range(const char *function, const char *name, const int max, const int index, const int nested_level, const char *error_msg)
Return true if specified index is within range.
Definition: check_range.hpp:29
int size(const std::vector< T > &x)
Definition: size.hpp:11
size_t rank(const std::vector< T > &v, int s)
Return the number of components of v less than v[s].
Definition: rank.hpp:18

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