Automatic Differentiation
 
Loading...
Searching...
No Matches
seq_view.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_META_SEQ_VIEW_HPP
2#define STAN_MATH_PRIM_META_SEQ_VIEW_HPP
3
5#include <vector>
6
7namespace stan {
8namespace math {
9
10template <typename T>
11struct store_type {
12 using type = const T &;
13};
14template <>
15struct store_type<double> {
16 using type = const double;
17};
18template <>
19struct store_type<int> {
20 using type = const int;
21};
22
23template <typename T>
24struct pass_type {
25 using type = const T &;
26};
27template <>
28struct pass_type<double> {
29 using type = double;
30};
31template <>
32struct pass_type<int> {
33 using type = int;
34};
35
36// S assignable to T
37template <typename T, typename S>
38class seq_view {
39 private:
41
42 public:
43 explicit seq_view(typename pass_type<S>::type x) : x_(x) {}
44 inline typename pass_type<T>::type operator[](int n) const { return x_; }
45 int size() const { return 1; }
46};
47
48template <typename T, typename S>
49class seq_view<T, Eigen::Matrix<S, Eigen::Dynamic, 1> > {
50 private:
52
53 public:
54 explicit seq_view(
55 typename pass_type<Eigen::Matrix<S, Eigen::Dynamic, 1> >::type x)
56 : x_(x) {}
57 inline typename pass_type<T>::type operator[](int n) const { return x_(n); }
58 int size() const { return x_.size(); }
59};
60
61template <typename T, typename S>
62class seq_view<T, Eigen::Matrix<S, 1, Eigen::Dynamic> > {
63 private:
65
66 public:
67 explicit seq_view(
68 typename pass_type<Eigen::Matrix<S, 1, Eigen::Dynamic> >::type x)
69 : x_(x) {}
70 inline typename pass_type<T>::type operator[](int n) const { return x_(n); }
71 int size() const { return x_.size(); }
72};
73
74// row-major order of returns to match std::vector
75template <typename T, typename S>
76class seq_view<T, Eigen::Matrix<S, Eigen::Dynamic, Eigen::Dynamic> > {
77 private:
80
81 public:
82 explicit seq_view(typename pass_type<
83 Eigen::Matrix<S, Eigen::Dynamic, Eigen::Dynamic> >::type x)
84 : x_(x) {}
85 inline typename pass_type<T>::type operator[](int n) const {
86 return x_(n / x_.cols(), n % x_.cols());
87 }
88 int size() const { return x_.size(); }
89};
90
91// question is how expensive the ctor is
92template <typename T, typename S>
93class seq_view<T, std::vector<S> > {
94 private:
95 typename store_type<std::vector<S> >::type x_;
96 const size_t elt_size_;
97
98 public:
99 explicit seq_view(typename pass_type<std::vector<S> >::type x)
100 : x_(x), elt_size_(x_.size() == 0 ? 0 : seq_view<T, S>(x_[0]).size()) {}
101 inline typename pass_type<T>::type operator[](int n) const {
102 return seq_view<T, S>(x_[n / elt_size_])[n % elt_size_];
103 }
104 int size() const { return x_.size() * elt_size_; }
105};
106
107// BELOW HERE JUST FOR EFFICIENCY
108
109template <typename T>
110class seq_view<T, std::vector<T> > {
111 private:
113
114 public:
115 explicit seq_view(typename pass_type<std::vector<T> >::type x) : x_(x) {}
116 inline typename pass_type<T>::type operator[](int n) const { return x_[n]; }
117 int size() const { return x_.size(); }
118};
119
120// if vector of S with S assignable to T, also works
121// use enable_if? (and disable_if for the general case)
122template <typename T>
123class seq_view<T, std::vector<std::vector<T> > > {
124 private:
126 const size_t cols_;
127
128 public:
129 explicit seq_view(typename pass_type<std::vector<std::vector<T> > >::type x)
130 : x_(x), cols_(x_.size() == 0 ? 0 : x_[0].size()) {}
131 inline typename pass_type<T>::type operator[](int n) const {
132 return x_[n / cols_][n % cols_];
133 }
134 int size() const { return x_.size() * cols_; }
135};
136
137template <>
138class seq_view<double, std::vector<int> > {
139 private:
141
142 public:
143 explicit seq_view(pass_type<std::vector<int> >::type x) : x_(x) {}
144 inline pass_type<double>::type operator[](int n) const { return x_[n]; }
145 int size() const { return x_.size(); }
146};
147
148} // namespace math
149} // namespace stan
150#endif
seq_view(typename pass_type< Eigen::Matrix< S, 1, Eigen::Dynamic > >::type x)
Definition seq_view.hpp:67
store_type< Eigen::Matrix< S, 1, Eigen::Dynamic > >::type x_
Definition seq_view.hpp:64
store_type< Eigen::Matrix< S, Eigen::Dynamic, 1 > >::type x_
Definition seq_view.hpp:51
seq_view(typename pass_type< Eigen::Matrix< S, Eigen::Dynamic, 1 > >::type x)
Definition seq_view.hpp:54
seq_view(typename pass_type< Eigen::Matrix< S, Eigen::Dynamic, Eigen::Dynamic > >::type x)
Definition seq_view.hpp:82
store_type< Eigen::Matrix< S, Eigen::Dynamic, Eigen::Dynamic > >::type x_
Definition seq_view.hpp:79
seq_view(typename pass_type< std::vector< S > >::type x)
Definition seq_view.hpp:99
store_type< std::vector< S > >::type x_
Definition seq_view.hpp:95
pass_type< T >::type operator[](int n) const
Definition seq_view.hpp:101
seq_view(typename pass_type< std::vector< T > >::type x)
Definition seq_view.hpp:115
store_type< std::vector< T > >::type x_
Definition seq_view.hpp:112
pass_type< T >::type operator[](int n) const
Definition seq_view.hpp:116
store_type< std::vector< std::vector< T > > >::type x_
Definition seq_view.hpp:125
seq_view(typename pass_type< std::vector< std::vector< T > > >::type x)
Definition seq_view.hpp:129
pass_type< double >::type operator[](int n) const
Definition seq_view.hpp:144
store_type< std::vector< int > >::type x_
Definition seq_view.hpp:140
seq_view(pass_type< std::vector< int > >::type x)
Definition seq_view.hpp:143
seq_view(typename pass_type< S >::type x)
Definition seq_view.hpp:43
pass_type< T >::type operator[](int n) const
Definition seq_view.hpp:44
store_type< S >::type x_
Definition seq_view.hpp:40
(Expert) Numerical traits for algorithmic differentiation variables.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
STL namespace.