Stan Math Library  2.20.0
reverse mode automatic differentiation
segment.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_SEGMENT_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_SEGMENT_HPP
3 
7 #include <vector>
8 
9 namespace stan {
10 namespace math {
11 
16 template <typename T>
17 inline Eigen::Matrix<T, Eigen::Dynamic, 1> segment(
18  const Eigen::Matrix<T, Eigen::Dynamic, 1>& 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.rows()));
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.rows()));
25  }
26  return v.segment(i - 1, n);
27 }
28 
29 template <typename T>
30 inline Eigen::Matrix<T, 1, Eigen::Dynamic> segment(
31  const Eigen::Matrix<T, 1, Eigen::Dynamic>& v, size_t i, size_t n) {
32  check_greater("segment", "n", i, 0.0);
33  check_less_or_equal("segment", "n", i, static_cast<size_t>(v.cols()));
34  if (n != 0) {
35  check_greater("segment", "n", i + n - 1, 0.0);
36  check_less_or_equal("segment", "n", i + n - 1,
37  static_cast<size_t>(v.cols()));
38  }
39 
40  return v.segment(i - 1, n);
41 }
42 
43 template <typename T>
44 std::vector<T> segment(const std::vector<T>& sv, size_t i, size_t n) {
45  check_greater("segment", "i", i, 0.0);
46  check_less_or_equal("segment", "i", i, sv.size());
47  if (n != 0) {
48  check_greater("segment", "i+n-1", i + n - 1, 0.0);
49  check_less_or_equal("segment", "i+n-1", i + n - 1,
50  static_cast<size_t>(sv.size()));
51  }
52  std::vector<T> s;
53  for (size_t j = 0; j < n; ++j)
54  s.push_back(sv[i + j - 1]);
55  return s;
56 }
57 
58 } // namespace math
59 } // namespace stan
60 #endif
void check_less_or_equal(const char *function, const char *name, const T_y &y, const T_high &high)
Check if y is less or equal to high.
void check_greater(const char *function, const char *name, const T_y &y, const T_low &low)
Check if y is strictly greater than low.
Eigen::Matrix< T, Eigen::Dynamic, 1 > segment(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &v, size_t i, size_t n)
Return the specified number of elements as a vector starting from the specified element - 1 of the sp...
Definition: segment.hpp:17

     [ Stan Home Page ] © 2011–2018, Stan Development Team.