Stan Math Library  2.14.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  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:
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:
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:
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:
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
int size() const
Definition: seq_view.hpp:49
pass_type< T >::type operator[](int n) const
Definition: seq_view.hpp:46
pass_type< double >::type operator[](int n) const
Definition: seq_view.hpp:179
seq_view(typename pass_type< Eigen::Matrix< S, 1, Eigen::Dynamic > >::type x)
Definition: seq_view.hpp:77
seq_view(typename pass_type< S >::type x)
Definition: seq_view.hpp:42
(Expert) Numerical traits for algorithmic differentiation variables.
seq_view(typename pass_type< Eigen::Matrix< S, Eigen::Dynamic, 1 > >::type x)
Definition: seq_view.hpp:59
seq_view(typename pass_type< std::vector< S > >::type x)
Definition: seq_view.hpp:118
seq_view(pass_type< std::vector< int > >::type x)
Definition: seq_view.hpp:176
seq_view(typename pass_type< std::vector< std::vector< T > > >::type x)
Definition: seq_view.hpp:158
pass_type< T >::type operator[](int n) const
Definition: seq_view.hpp:142
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17
seq_view(typename pass_type< Eigen::Matrix< S, Eigen::Dynamic, Eigen::Dynamic > >::type x)
Definition: seq_view.hpp:98
pass_type< T >::type operator[](int n) const
Definition: seq_view.hpp:123
seq_view(typename pass_type< std::vector< T > >::type x)
Definition: seq_view.hpp:138

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