Stan Math Library: stan/math/prim/mat/fun/segment.hpp Source File

| Stan Math Library 2.10.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
4 #include <stan/math/prim/mat/fun/Eigen.hpp>
5 #include <stan/math/prim/scal/err/check_greater.hpp>
6 #include <stan/math/prim/scal/err/check_less_or_equal.hpp>
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 stan::math::check_greater("segment", "n", i, 0.0);
22 stan::math::check_less_or_equal("segment", "n", i,
23 static_cast<size_t>(v.rows()));
24 if (n != 0) {
25 stan::math::check_greater("segment", "n", i+n-1, 0.0);
26 stan::math::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 stan::math::check_greater("segment", "n", i, 0.0);
38 stan::math::check_less_or_equal("segment", "n", i,
39 static_cast<size_t>(v.cols()));
40 if (n != 0) {
41 stan::math::check_greater("segment", "n", i+n-1, 0.0);
42 stan::math::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
50 template <typename T>
51 std::vector<T>
52 segment(const std::vector<T>& sv,
53 size_t i, size_t n) {
54 stan::math::check_greater("segment", "i", i, 0.0);
55 stan::math::check_less_or_equal("segment", "i", i, sv.size());
56 if (n != 0) {
57 stan::math::check_greater("segment", "i+n-1", i + n - 1, 0.0);
58 stan::math::check_less_or_equal("segment", "i+n-1", i + n - 1,
59 static_cast<size_t>(sv.size()));
60 }
61 std::vector<T> s;
62 for (size_t j = 0; j < n; ++j)
63 s.push_back(sv[i + j - 1]);
64 return s;
65 }
66
67 }
68 }
69 #endif
stan
Definition: log_sum_exp.hpp:8
check_greater.hpp
check_less_or_equal.hpp
Eigen.hpp
stan::math::check_less_or_equal
bool check_less_or_equal(const char *function, const char *name, const T_y &y, const T_high &high)
Return true if y is less or equal to high.
Definition: check_less_or_equal.hpp:81
stan::math::segment
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::math::check_greater
bool check_greater(const char *function, const char *name, const T_y &y, const T_low &low)
Return true if y is strictly greater than low.
Definition: check_greater.hpp:84

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