Stan Math Library  2.11.0
reverse mode automatic differentiation
container_view.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_META_CONTAINER_VIEW_HPP
2 #define STAN_MATH_PRIM_MAT_META_CONTAINER_VIEW_HPP
3 
6 #include <vector>
7 
8 namespace stan {
9 
10  namespace math {
11 
22  template <typename T1, typename T2, int R, int C>
23  class container_view<Eigen::Matrix<T1, R, C>, Eigen::Matrix<T2, R, C> > {
24  public:
32  container_view(const Eigen::Matrix<T1, R, C>& x, T2* y)
33  : y_(y, x.rows(), x.cols()) { }
34 
40  Eigen::Map<Eigen::Matrix<T2, R, C> >& operator[](int i) {
41  return y_;
42  }
43  private:
44  Eigen::Map<Eigen::Matrix<T2, R, C> > y_;
45  };
46 
57  template <typename T1, typename T2, int R, int C>
58  class container_view<Eigen::Matrix<T1, R, C>, T2> {
59  public:
66  container_view(const Eigen::Matrix<T1, R, C>& x, T2* y)
67  : y_(y) { }
68 
73  T2& operator[](int i) {
74  return y_[i];
75  }
76  private:
77  T2* y_;
78  };
79 
90  template <typename T1, typename T2, int R, int C>
91  class container_view<std::vector<Eigen::Matrix<T1, R, C> >,
92  Eigen::Matrix<T2, R, C> > {
93  public:
104  container_view(const std::vector<Eigen::Matrix<T1, R, C> >& x, T2* y)
105  : y_view(y, 1, 1), y_(y) {
106  if (x.size() > 0) {
107  rows = x[0].rows();
108  cols = x[0].cols();
109  } else {
110  rows = 0;
111  cols = 0;
112  }
113  }
114 
119  Eigen::Map<Eigen::Matrix<T2, R, C> >& operator[](int i) {
120  int offset = i * rows * cols;
121  new (&y_view) Eigen::Map<Eigen::Matrix<T2, R, C> >
122  (y_ + offset, rows, cols);
123  return y_view;
124  }
125  private:
126  Eigen::Map<Eigen::Matrix<T2, R, C> > y_view;
127  T2* y_;
128  int rows;
129  int cols;
130  };
131  }
132 }
133 #endif
int rows(const Eigen::Matrix< T, R, C > &m)
Return the number of rows in the specified matrix, vector, or row vector.
Definition: rows.hpp:20
Eigen::Map< Eigen::Matrix< T2, R, C > > & operator[](int i)
operator[](int i) returns matrix view of scalartype T2 at appropriate index i in array y ...
(Expert) Numerical traits for algorithmic differentiation variables.
container_view(const Eigen::Matrix< T1, R, C > &x, T2 *y)
Constructor.
Eigen::Map< Eigen::Matrix< T2, R, C > > & operator[](int i)
operator[](int i) returns Eigen::Map y
int cols(const Eigen::Matrix< T, R, C > &m)
Return the number of columns in the specified matrix, vector, or row vector.
Definition: cols.hpp:20
T2 & operator[](int i)
operator[](int i) returns reference to scalar of type T2 at appropriate index i in array y ...
Primary template class for container view of array y with same structure as T1 and size as x...
container_view(const Eigen::Matrix< T1, R, C > &x, T2 *y)
Initialize Map dimensions with input matrix dimensions.
container_view(const std::vector< Eigen::Matrix< T1, R, C > > &x, T2 *y)
Constructor assumes all matrix elements in std::vector are of same dimension.

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