1 #ifndef STAN_MATH_PRIM_FUN_TAIL_HPP
2 #define STAN_MATH_PRIM_FUN_TAIL_HPP
3 
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/err.hpp>
6 #include <stan/math/prim/fun/Eigen.hpp>
7 #include <vector>
8 
9 namespace stan {
10 namespace math {
11 
12 /**
13  * Return the specified number of elements as a vector or row vector (same as
14  * input) from the back of the specified vector or row vector.
15  *
16  * @tparam T type of the vector
17  * @param v Vector input.
18  * @param n Size of return.
19  * @return The last n elements of v.
20  * @throw std::out_of_range if n is out of range.
21  */
22 template <typename T, require_vector_t<T>* = nullptr>
tail(const T & v,size_t n)23 inline auto tail(const T& v, size_t n) {
24   if (n != 0) {
25     check_vector_index("tail", "n", v, n);
26   }
27   return v.tail(n);
28 }
29 
30 /**
31  * Return the specified number of elements as a standard vector
32  * from the back of the specified standard vector.
33  *
34  * @tparam T type of elements in the vector
35  * @param sv Standard vector.
36  * @param n Size of return.
37  * @return The last n elements of sv.
38  * @throw std::out_of_range if n is out of range.
39  */
40 template <typename T>
tail(const std::vector<T> & sv,size_t n)41 std::vector<T> tail(const std::vector<T>& sv, size_t n) {
42   if (n != 0) {
43     check_std_vector_index("tail", "n", sv, n);
44   }
45   std::vector<T> s(sv.end() - n, sv.end());
46   return s;
47 }
48 
49 }  // namespace math
50 }  // namespace stan
51 
52 #endif
53