Stan Math Library  2.14.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
18  Eigen::Matrix<T, Eigen::Dynamic, 1>
19  segment(const Eigen::Matrix<T, Eigen::Dynamic, 1>& v,
20  size_t i, size_t n) {
21  check_greater("segment", "n", i, 0.0);
22  check_less_or_equal("segment", "n", i,
23  static_cast<size_t>(v.rows()));
24  if (n != 0) {
25  check_greater("segment", "n", i+n-1, 0.0);
26  check_less_or_equal("segment", "n", i+n-1,
27  static_cast<size_t>(v.rows()));
28  }
29  return v.segment(i-1, n);
30  }
31 
32  template <typename T>
33  inline
34  Eigen::Matrix<T, 1, Eigen::Dynamic>
35  segment(const Eigen::Matrix<T, 1, Eigen::Dynamic>& v,
36  size_t i, size_t n) {
37  check_greater("segment", "n", i, 0.0);
38  check_less_or_equal("segment", "n", i,
39  static_cast<size_t>(v.cols()));
40  if (n != 0) {
41  check_greater("segment", "n", i+n-1, 0.0);
42  check_less_or_equal("segment", "n", i + n - 1,
43  static_cast<size_t>(v.cols()));
44  }
45 
46  return v.segment(i-1, n);
47  }
48 
49  template <typename T>
50  std::vector<T>
51  segment(const std::vector<T>& sv,
52  size_t i, size_t n) {
53  check_greater("segment", "i", i, 0.0);
54  check_less_or_equal("segment", "i", i, sv.size());
55  if (n != 0) {
56  check_greater("segment", "i+n-1", i + n - 1, 0.0);
57  check_less_or_equal("segment", "i+n-1", i + n - 1,
58  static_cast<size_t>(sv.size()));
59  }
60  std::vector<T> s;
61  for (size_t j = 0; j < n; ++j)
62  s.push_back(sv[i + j - 1]);
63  return s;
64  }
65 
66  }
67 }
68 #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:19

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