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

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