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(const 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(v, [s](const auto& vec) {
24 const auto& vec_ref = to_ref(vec);
25
26 return (vec_ref.array() < vec_ref.coeff(s)).template cast<int>().sum();
27 });
28}
29
30} // namespace math
31} // namespace stan
32
33#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:17
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 ...
Definition fvar.hpp:9