Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
segment.hpp
Go to the documentation of this file.
1 #ifndef STAN__MATH__MATRIX__SEGMENT_HPP
2 #define STAN__MATH__MATRIX__SEGMENT_HPP
3 
5 #include <vector>
8 
9 
10 namespace stan {
11  namespace math {
12 
17  template <typename T>
18  inline
19  Eigen::Matrix<T,Eigen::Dynamic,1>
20  segment(const Eigen::Matrix<T,Eigen::Dynamic,1>& v,
21  size_t i, size_t n) {
22  stan::math::check_greater("segment(%1%)",i,0.0,"n",(double*)0);
23  stan::math::check_less_or_equal("segment(%1%)",i,static_cast<size_t>(v.rows()),"n",(double*)0);
24  if (n != 0) {
25  stan::math::check_greater("segment(%1%)",i+n-1,0.0,"n",(double*)0);
26  stan::math::check_less_or_equal("segment(%1%)",i+n-1,static_cast<size_t>(v.rows()),"n",(double*)0);
27  }
28  return v.segment(i-1,n);
29  }
30 
31  template <typename T>
32  inline
33  Eigen::Matrix<T,1,Eigen::Dynamic>
34  segment(const Eigen::Matrix<T,1,Eigen::Dynamic>& v,
35  size_t i, size_t n) {
36  stan::math::check_greater("segment(%1%)",i,0.0,"n",(double*)0);
37  stan::math::check_less_or_equal("segment(%1%)",i,static_cast<size_t>(v.cols()),"n",(double*)0);
38  if (n != 0) {
39  stan::math::check_greater("segment(%1%)",i+n-1,0.0,"n",(double*)0);
40  stan::math::check_less_or_equal("segment(%1%)",i+n-1,static_cast<size_t>(v.cols()),"n",(double*)0);
41  }
42 
43  return v.segment(i-1,n);
44  }
45 
46 
47  template <typename T>
48  std::vector<T>
49  segment(const std::vector<T>& sv,
50  size_t i, size_t n) {
51  stan::math::check_greater("segment(%1%)",i,0.0,"i",(double*)0);
52  stan::math::check_less_or_equal("segment(%1%)",i,sv.size(),"i",(double*)0);
53  if (n != 0) {
54  stan::math::check_greater("segment(%1%)",i+n-1,0.0,"i+n-1",(double*)0);
55  stan::math::check_less_or_equal("segment(%1%)",i+n-1,static_cast<size_t>(sv.size()),"i+n-1",
56  (double*)0);
57  }
58  std::vector<T> s;
59  for (size_t j = 0; j < n; ++j)
60  s.push_back(sv[i + j - 1]);
61  return s;
62  }
63 
64  }
65 }
66 #endif
bool check_greater(const char *function, const T_y &y, const T_low &low, const char *name, T_result *result)
bool check_less_or_equal(const char *function, const T_y &y, const T_high &high, const char *name, T_result *result)
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:20

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