Automatic Differentiation
 
Loading...
Searching...
No Matches
rank.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_RANK_HPP
2#define STAN_MATH_PRIM_FUN_RANK_HPP
3
6
7namespace stan {
8namespace math {
9
19template <typename C, require_container_t<C>* = nullptr>
20inline int rank(C&& v, int s) {
21 check_range("rank", "v", v.size(), s);
22 --s; // adjust for indexing by one
23 return apply_vector_unary<C>::reduce(std::forward<C>(v), [s](auto&& vec) {
24 auto&& vec_ref = to_ref(std::forward<decltype(vec)>(vec));
25 return (vec_ref.array() < vec_ref.coeff(s)).template cast<int>().sum();
26 });
27}
28
29} // namespace math
30} // namespace stan
31
32#endif
int rank(const T &v, int s)
Return the number of components of v less than v[s].
Definition rank.hpp:24
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:18
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.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...