Stan Math Library  2.11.0
reverse mode automatic differentiation
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 
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:
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:
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
pass_type< T >::type operator[](int n) const
Definition: seq_view.hpp:130
int size() const
Definition: seq_view.hpp:53
seq_view(typename pass_type< Eigen::Matrix< S, 1, Eigen::Dynamic > >::type x)
Definition: seq_view.hpp:82
seq_view(typename pass_type< S >::type x)
Definition: seq_view.hpp:46
(Expert) Numerical traits for algorithmic differentiation variables.
seq_view(typename pass_type< Eigen::Matrix< S, Eigen::Dynamic, 1 > >::type x)
Definition: seq_view.hpp:63
pass_type< double >::type operator[](int n) const
Definition: seq_view.hpp:186
seq_view(typename pass_type< std::vector< S > >::type x)
Definition: seq_view.hpp:125
seq_view(pass_type< std::vector< int > >::type x)
Definition: seq_view.hpp:183
seq_view(typename pass_type< std::vector< std::vector< T > > >::type x)
Definition: seq_view.hpp:165
pass_type< T >::type operator[](int n) const
Definition: seq_view.hpp:149
seq_view(typename pass_type< Eigen::Matrix< S, Eigen::Dynamic, Eigen::Dynamic > >::type x)
Definition: seq_view.hpp:105
seq_view(typename pass_type< std::vector< T > >::type x)
Definition: seq_view.hpp:145
pass_type< T >::type operator[](int n) const
Definition: seq_view.hpp:50

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