Automatic Differentiation
 
Loading...
Searching...
No Matches
segment.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_SEGMENT_HPP
2#define STAN_MATH_PRIM_FUN_SEGMENT_HPP
3
6#include <vector>
7
8namespace stan {
9namespace math {
10
17template <typename Vec, require_vector_t<Vec>* = nullptr>
18inline auto segment(const Vec& v, size_t i, size_t n) {
19 check_greater("segment", "n", i, 0.0);
20 check_less_or_equal("segment", "n", i, static_cast<size_t>(v.size()));
21 if (n != 0) {
22 check_greater("segment", "n", i + n - 1, 0.0);
23 check_less_or_equal("segment", "n", i + n - 1,
24 static_cast<size_t>(v.size()));
25 }
26 return v.segment(i - 1, n);
27}
28
29template <typename T>
30std::vector<T> segment(const std::vector<T>& sv, size_t i, size_t n) {
31 check_greater("segment", "i", i, 0.0);
32 check_less_or_equal("segment", "i", i, sv.size());
33 if (n != 0) {
34 check_greater("segment", "i+n-1", i + n - 1, 0.0);
35 check_less_or_equal("segment", "i+n-1", i + n - 1,
36 static_cast<size_t>(sv.size()));
37 }
38 std::vector<T> s;
39 for (size_t j = 0; j < n; ++j) {
40 s.push_back(sv[i + j - 1]);
41 }
42 return s;
43}
44
45} // namespace math
46} // namespace stan
47
48#endif
void check_less_or_equal(const char *function, const char *name, const T_y &y, const T_high &high, Idxs... idxs)
Throw an exception if y is not less than high.
auto segment(T_x &&x, size_t i, size_t n)
Return the specified number of elements as a row/column vector starting from the specified element - ...
Definition segment.hpp:24
void check_greater(const char *function, const char *name, const T_y &y, const T_low &low, Idxs... idxs)
Throw an exception if y is not strictly greater than low.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9