Stan Math Library  2.14.0
reverse mode automatic differentiation
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 
20  template <typename T>
21  inline int rank(const std::vector<T> & v, int s) {
22  int size = static_cast<int>(v.size());
23  check_range("rank", "v", size, s);
24  --s;
25  int count(0);
26  T compare(v[s]);
27  for (int i = 0; i < size; ++i)
28  if (v[i] < compare)
29  ++count;
30  return count;
31  }
32 
42  template <typename T, int R, int C>
43  inline int rank(const Eigen::Matrix<T, R, C> & v, int s) {
44  int size = v.size();
45  check_range("rank", "v", size, s);
46  --s;
47  const T * vv = v.data();
48  int count(0);
49  T compare(vv[s]);
50  for (int i = 0; i < size; ++i)
51  if (vv[i] < compare)
52  ++count;
53  return count;
54  }
55 
56  }
57 }
58 #endif
void check_range(const char *function, const char *name, int max, int index, int nested_level, const char *error_msg)
Check if specified index is within range.
Definition: check_range.hpp:28
int rank(const std::vector< T > &v, int s)
Return the number of components of v less than v[s].
Definition: rank.hpp:21
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17

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