@@ -9,41 +9,21 @@ namespace stan {
9
9
namespace math {
10
10
11
11
/* *
12
- * Return the specified number of elements as a vector
13
- * from the front of the specified vector.
12
+ * Return the specified number of elements as a vector or row vector (same as
13
+ * input) from the front of the specified vector or row vector.
14
14
*
15
- * @tparam T type of elements in the vector
15
+ * @tparam T type of the vector
16
16
* @param v Vector input.
17
17
* @param n Size of return.
18
18
* @return The first n elements of v.
19
19
* @throw std::out_of_range if n is out of range.
20
20
*/
21
- template <typename T>
22
- inline Eigen::Matrix<T, Eigen::Dynamic, 1 > head (
23
- const Eigen::Matrix<T, Eigen::Dynamic, 1 >& v, size_t n) {
21
+ template <typename T, typename = require_eigen_vector_t <T>>
22
+ inline auto head (const T& v, size_t n) {
24
23
if (n != 0 ) {
25
- check_row_index (" head" , " n" , v, n);
24
+ check_vector_index (" head" , " n" , v, n);
26
25
}
27
- return v.head (n);
28
- }
29
-
30
- /* *
31
- * Return the specified number of elements as a row vector
32
- * from the front of the specified row vector.
33
- *
34
- * @tparam T type of elements in the vector
35
- * @param rv Row vector.
36
- * @param n Size of return row vector.
37
- * @return The first n elements of rv.
38
- * @throw std::out_of_range if n is out of range.
39
- */
40
- template <typename T>
41
- inline Eigen::Matrix<T, 1 , Eigen::Dynamic> head (
42
- const Eigen::Matrix<T, 1 , Eigen::Dynamic>& rv, size_t n) {
43
- if (n != 0 ) {
44
- check_column_index (" head" , " n" , rv, n);
45
- }
46
- return rv.head (n);
26
+ return v.head (n).eval ();
47
27
}
48
28
49
29
/* *
@@ -62,10 +42,7 @@ std::vector<T> head(const std::vector<T>& sv, size_t n) {
62
42
check_std_vector_index (" head" , " n" , sv, n);
63
43
}
64
44
65
- std::vector<T> s;
66
- for (size_t i = 0 ; i < n; ++i) {
67
- s.push_back (sv[i]);
68
- }
45
+ std::vector<T> s (sv.begin (), sv.begin () + n);
69
46
return s;
70
47
}
71
48
0 commit comments