| Stan Math Library 2.11.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

seq_view.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_META_SEQ_VIEW_HPP
2 #define STAN_MATH_PRIM_MAT_META_SEQ_VIEW_HPP
3
4 #include <stan/math/prim/mat/fun/Eigen.hpp>
5 #include <vector>
6
7 namespace stan {
8
9 namespace math {
10
11
12 template <typename T>
13 struct store_type {
14 typedef const T& type;
15 };
16 template <>
17 struct store_type<double> {
18 typedef const double type;
19 };
20 template <>
21 struct store_type<int> {
22 typedef const int type;
23 };
24
25
26 template <typename T>
27 struct pass_type {
28 typedef const T& type;
29 };
30 template <>
31 struct pass_type<double> {
32 typedef double type;
33 };
34 template <>
35 struct pass_type<int> {
36 typedef int type;
37 };
38
39
40 // S assignable to T
41 template <typename T, typename S>
42 class seq_view {
43 private:
44 typename store_type<S>::type x_;
45 public:
46 explicit seq_view(typename pass_type<S>::type x)
47 : x_(x) {
48 }
49 inline typename pass_type<T>::type
50 operator[](int n) const {
51 return x_;
52 }
53 int size() const {
54 return 1;
55 }
56 };
57
58 template <typename T, typename S>
59 class seq_view<T, Eigen::Matrix<S, Eigen::Dynamic, 1> > {
60 private:
61 typename store_type<Eigen::Matrix<S, Eigen::Dynamic, 1> >::type x_;
62 public:
63 explicit seq_view(typename
64 pass_type<Eigen::Matrix<S, Eigen::Dynamic, 1> >::type x)
65 : x_(x) {
66 }
67 inline typename pass_type<T>::type
68 operator[](int n) const {
69 return x_(n);
70 }
71 int size() const {
72 return x_.size();
73 }
74 };
75
76
77 template <typename T, typename S>
78 class seq_view<T, Eigen::Matrix<S, 1, Eigen::Dynamic> > {
79 private:
80 typename store_type<Eigen::Matrix<S, 1, Eigen::Dynamic> >::type x_;
81 public:
82 explicit seq_view(typename pass_type
83 <Eigen::Matrix<S, 1, Eigen::Dynamic> >::type x)
84 : x_(x) {
85 }
86 inline typename pass_type<T>::type
87 operator[](int n) const {
88 return x_(n);
89 }
90 int size() const {
91 return x_.size();
92 }
93 };
94
95
96
97 // row-major order of returns to match std::vector
98 template <typename T, typename S>
99 class seq_view<T, Eigen::Matrix<S, Eigen::Dynamic, Eigen::Dynamic> > {
100 private:
101 typename store_type<Eigen::Matrix
102 <S, Eigen::Dynamic, Eigen::Dynamic> >::type x_;
103 public:
104 explicit
105 seq_view(typename pass_type<Eigen::Matrix
106 <S, Eigen::Dynamic, Eigen::Dynamic> >::type x)
107 : x_(x) {
108 }
109 inline typename pass_type<T>::type
110 operator[](int n) const {
111 return x_(n / x_.cols(), n % x_.cols());
112 }
113 int size() const {
114 return x_.size();
115 }
116 };
117
118 // question is how expensive the ctor is
119 template <typename T, typename S>
120 class seq_view<T, std::vector<S> > {
121 private:
122 typename store_type<std::vector<S> >::type x_;
123 const size_t elt_size_;
124 public:
125 explicit seq_view(typename pass_type<std::vector<S> >::type x)
126 : x_(x),
127 elt_size_(x_.size() == 0 ? 0 : seq_view<T, S>(x_[0]).size()) {
128 }
129 inline typename pass_type<T>::type
130 operator[](int n) const {
131 return seq_view<T, S>(x_[n / elt_size_])[n % elt_size_];
132 }
133 int size() const {
134 return x_.size() * elt_size_;
135 }
136 };
137
138 // BELOW HERE JUST FOR EFFICIENCY
139
140 template <typename T>
141 class seq_view<T, std::vector<T> > {
142 private:
143 typename store_type<std::vector<T> >::type x_;
144 public:
145 explicit seq_view(typename pass_type<std::vector<T> >::type x)
146 : x_(x) {
147 }
148 inline typename pass_type<T>::type
149 operator[](int n) const {
150 return x_[n];
151 }
152 int size() const {
153 return x_.size();
154 }
155 };
156
157 // if vector of S with S assignable to T, also works
158 // use enable_if? (and disable_if for the general case)
159 template <typename T>
160 class seq_view<T, std::vector<std::vector<T> > > {
161 private:
162 typename store_type<std::vector<std::vector<T> > >::type x_;
163 const size_t cols_;
164 public:
165 explicit seq_view(typename pass_type
166 <std::vector<std::vector<T> > >::type x)
167 : x_(x),
168 cols_(x_.size() == 0 ? 0 : x_[0].size()) { }
169 inline typename pass_type<T>::type
170 operator[](int n) const {
171 return x_[n / cols_][n % cols_];
172 }
173 int size() const {
174 return x_.size() * cols_;
175 }
176 };
177
178 template <>
179 class seq_view<double, std::vector<int> > {
180 private:
181 store_type<std::vector<int> >::type x_;
182 public:
183 explicit seq_view(pass_type<std::vector<int> >::type x)
184 : x_(x) {
185 }
186 inline pass_type<double>::type operator[](int n) const {
187 return x_[n];
188 }
189 int size() const {
190 return x_.size();
191 }
192 };
193
194
195
196
197 }
198 }
199
200 #endif
stan::math::seq_view< T, Eigen::Matrix< S, Eigen::Dynamic, Eigen::Dynamic > >::size
int size() const
Definition: seq_view.hpp:113
stan::math::seq_view< T, std::vector< S > >::operator[]
pass_type< T >::type operator[](int n) const
Definition: seq_view.hpp:130
stan::math::seq_view::size
int size() const
Definition: seq_view.hpp:53
stan
Definition: log_sum_exp.hpp:8
stan::math::seq_view< T, Eigen::Matrix< S, 1, Eigen::Dynamic > >::size
int size() const
Definition: seq_view.hpp:90
std
Definition: std_numeric_limits.hpp:7
stan::math::seq_view< T, std::vector< T > >::size
int size() const
Definition: seq_view.hpp:152
stan::math::seq_view< T, Eigen::Matrix< S, 1, Eigen::Dynamic > >::seq_view
seq_view(typename pass_type< Eigen::Matrix< S, 1, Eigen::Dynamic > >::type x)
Definition: seq_view.hpp:82
stan::math::seq_view::seq_view
seq_view(typename pass_type< S >::type x)
Definition: seq_view.hpp:46
Eigen
(Expert) Numerical traits for algorithmic differentiation variables.
Definition: Eigen_NumTraits.hpp:8
stan::math::seq_view< T, std::vector< S > >::size
int size() const
Definition: seq_view.hpp:133
stan::math::pass_type< double >::type
double type
Definition: seq_view.hpp:32
stan::math::seq_view< T, Eigen::Matrix< S, 1, Eigen::Dynamic > >::operator[]
pass_type< T >::type operator[](int n) const
Definition: seq_view.hpp:87
stan::math::pass_type< int >::type
int type
Definition: seq_view.hpp:36
stan::math::seq_view< T, Eigen::Matrix< S, Eigen::Dynamic, 1 > >::seq_view
seq_view(typename pass_type< Eigen::Matrix< S, Eigen::Dynamic, 1 > >::type x)
Definition: seq_view.hpp:63
stan::math::seq_view< double, std::vector< int > >::size
int size() const
Definition: seq_view.hpp:189
stan::math::pass_type
Definition: seq_view.hpp:27
stan::math::seq_view< double, std::vector< int > >::operator[]
pass_type< double >::type operator[](int n) const
Definition: seq_view.hpp:186
stan::math::seq_view< T, std::vector< S > >::seq_view
seq_view(typename pass_type< std::vector< S > >::type x)
Definition: seq_view.hpp:125
stan::math::seq_view< T, Eigen::Matrix< S, Eigen::Dynamic, 1 > >::size
int size() const
Definition: seq_view.hpp:71
stan::math::seq_view< double, std::vector< int > >::seq_view
seq_view(pass_type< std::vector< int > >::type x)
Definition: seq_view.hpp:183
Eigen.hpp
stan::math::seq_view< T, std::vector< std::vector< T > > >::size
int size() const
Definition: seq_view.hpp:173
stan::math::seq_view< T, std::vector< std::vector< T > > >::seq_view
seq_view(typename pass_type< std::vector< std::vector< T > > >::type x)
Definition: seq_view.hpp:165
stan::math::seq_view< T, Eigen::Matrix< S, Eigen::Dynamic, Eigen::Dynamic > >::operator[]
pass_type< T >::type operator[](int n) const
Definition: seq_view.hpp:110
stan::math::seq_view< T, std::vector< T > >::operator[]
pass_type< T >::type operator[](int n) const
Definition: seq_view.hpp:149
stan::math::seq_view
Definition: seq_view.hpp:42
stan::math::store_type< int >::type
const int type
Definition: seq_view.hpp:22
stan::math::pass_type::type
const T & type
Definition: seq_view.hpp:28
stan::math::seq_view< T, Eigen::Matrix< S, Eigen::Dynamic, 1 > >::operator[]
pass_type< T >::type operator[](int n) const
Definition: seq_view.hpp:68
stan::math::seq_view< T, std::vector< std::vector< T > > >::operator[]
pass_type< T >::type operator[](int n) const
Definition: seq_view.hpp:170
stan::math::seq_view< T, Eigen::Matrix< S, Eigen::Dynamic, Eigen::Dynamic > >::seq_view
seq_view(typename pass_type< Eigen::Matrix< S, Eigen::Dynamic, Eigen::Dynamic > >::type x)
Definition: seq_view.hpp:105
stan::math::store_type
Definition: seq_view.hpp:13
stan::math::store_type::type
const T & type
Definition: seq_view.hpp:14
stan::math::store_type< double >::type
const double type
Definition: seq_view.hpp:18
stan::math::seq_view< T, std::vector< T > >::seq_view
seq_view(typename pass_type< std::vector< T > >::type x)
Definition: seq_view.hpp:145
stan::math::seq_view::operator[]
pass_type< T >::type operator[](int n) const
Definition: seq_view.hpp:50

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