Stan Math Library  2.12.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  namespace math {
10 
21  template <typename T1, typename T2, int R, int C>
22  class container_view<Eigen::Matrix<T1, R, C>, Eigen::Matrix<T2, R, C> > {
23  public:
31  container_view(const Eigen::Matrix<T1, R, C>& x, T2* y)
32  : y_(y, x.rows(), x.cols()) { }
33 
39  Eigen::Map<Eigen::Matrix<T2, R, C> >& operator[](int i) {
40  return y_;
41  }
42  private:
43  Eigen::Map<Eigen::Matrix<T2, R, C> > y_;
44  };
45 
56  template <typename T1, typename T2, int R, int C>
57  class container_view<Eigen::Matrix<T1, R, C>, T2> {
58  public:
65  container_view(const Eigen::Matrix<T1, R, C>& x, T2* y)
66  : y_(y) { }
67 
72  T2& operator[](int i) {
73  return y_[i];
74  }
75  private:
76  T2* y_;
77  };
78 
89  template <typename T1, typename T2, int R, int C>
90  class container_view<std::vector<Eigen::Matrix<T1, R, C> >,
91  Eigen::Matrix<T2, R, C> > {
92  public:
103  container_view(const std::vector<Eigen::Matrix<T1, R, C> >& x, T2* y)
104  : y_view(y, 1, 1), y_(y) {
105  if (x.size() > 0) {
106  rows = x[0].rows();
107  cols = x[0].cols();
108  } else {
109  rows = 0;
110  cols = 0;
111  }
112  }
113 
118  Eigen::Map<Eigen::Matrix<T2, R, C> >& operator[](int i) {
119  int offset = i * rows * cols;
120  new (&y_view) Eigen::Map<Eigen::Matrix<T2, R, C> >
121  (y_ + offset, rows, cols);
122  return y_view;
123  }
124  private:
125  Eigen::Map<Eigen::Matrix<T2, R, C> > y_view;
126  T2* y_;
127  int rows;
128  int cols;
129  };
130  }
131 }
132 #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.