WaveBlocksND
adaptors.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "../types.hpp"
4 #include "../wavepackets/shapes/tiny_multi_index.hpp"
5 
6 
7 namespace waveblocks {
8  namespace utilities {
9 
10  template <class Matrix>
11  using grid_element_type =
13 
14  template <class Matrix>
15  class MatrixToGrid;
16 
21  template <class Matrix>
23  {
24  private:
26 
27  static const int N = Matrix::RowsAtCompileTime;
28  static const int M = Matrix::ColsAtCompileTime;
30  int i;
31 
32  public:
34  : g( adaptor ), i( i ) {}
36  return other.i != i;
37  }
38 
39  void operator++() {
40  ++i;
41  }
43  return g[i];
44  }
45  };
46 
55  template <class Matrix>
56  class MatrixToGrid
57  {
58  private:
59  static const int N = Matrix::RowsAtCompileTime;
60  static const int M = Matrix::ColsAtCompileTime;
62 
63  const Matrix &matrix;
64 
65  public:
66  size_t size() const {
67  return M;
68  }
69 
70  MatrixToGrid( const Matrix &matrix ) : matrix( matrix ) {}
71 
73  return matrix.template block<N, 1>( 0, i );
74  }
75 
77  return MatrixToGridIterator<Matrix>( *this, 0 );
78  }
80  return MatrixToGridIterator<Matrix>( *this, M );
81  }
82 
83  // "grid class template"
84  template <class I>
86  };
87 
88 
102  template <class Matrix, template <typename...> class Grid = std::vector>
103  Grid<grid_element_type<Matrix> > matrix_to_grid( const Matrix &m )
104  {
105  const int N = m.rows();
106  const int M = m.cols();
107 
108  Grid<grid_element_type<Matrix> > result( M );
109  auto it = result.begin();
110 
111  for ( int i = 0; i < M; ++i ) {
112  *it = m.template block<N, 1>( 0, i );
113  ++it;
114  }
115 
116  return result;
117  }
118  template <class Matrix, template <typename...> class Grid = std::vector>
132  {
133  const int N = Matrix::RowsAtCompileTime;
134 
135  Matrix m;
136  auto it = g.begin();
137 
138  for ( int i = 0; it != g.end(); ++i, ++it ) {
139  m.template block<N, 1>( 0, i ) = *it;
140  }
141 
142  return m;
143  }
144 
145  template<class Packet>
147  static CVector<Eigen::Dynamic> to(const Packet& packet) {
148 
149  // Compute size
150  int size = 0;
151  for (const auto& component: packet.components()) {
152  size += component.coefficients().size();
153  }
154  // Allocate memory
155  CVector<Eigen::Dynamic> coefficients;
156  coefficients.resize(size);
157 
158  // Copy
159  int j_offset = 0;
160  for(auto& component : packet.components()) {
161  int j_size = component.coefficients().size();
162  for (int j = 0; j < j_size; ++j) {
163  coefficients[j+j_offset] = component.coefficients()[j];
164  }
165  j_offset += j_size;
166  }
167 
168  return coefficients;
169  }
170 
171  static void from(const CVector<Eigen::Dynamic>& coefficients, Packet& packet) {
172 
173  // Compute size
174  int size = 0;
175  for (const auto& component: packet.components()) {
176  size += component.coefficients().size();
177  }
178 
179  // Copy
180  int j_offset = 0;
181  for(auto& component : packet.components()) {
182  int j_size = component.coefficients().size();
183  for (int j = 0; j < j_size; ++j) {
184  component.coefficients()[j] = coefficients[j+j_offset];
185  }
186  j_offset += j_size;
187  }
188  }
189  };
190 
191  template<int D, class MultiIndex>
192  struct PacketToCoefficients<wavepackets::ScalarHaWp<D,MultiIndex>> {
194  return packet.coefficients();
195  }
196 
197  static void from(const CVector<Eigen::Dynamic>& coefficients, wavepackets::ScalarHaWp<D,MultiIndex>& packet) {
198  packet.coefficients() = coefficients;
199  }
200 
201  };
202  }
203 }
Grid< grid_element_type< Matrix > > matrix_to_grid(const Matrix &m)
Copies a Eigen::Matrix into a grid.
Definition: adaptors.hpp:103
GVector< complex_t, R > CVector
Definition: types.hpp:50
Forward iterator for the MatrixToGrid class.
Definition: adaptors.hpp:22
Definition: coefficients_file_parser.cpp:10
Coefficients & coefficients()
Grants writeable access to the coefficients of the wavepacket.
Definition: hawp_commons.hpp:262
GMatrix< I, R, 1 > GVector
Definition: types.hpp:47
static CVector< Eigen::Dynamic > to(const Packet &packet)
Definition: adaptors.hpp:147
static void from(const CVector< Eigen::Dynamic > &coefficients, Packet &packet)
Definition: adaptors.hpp:171
GVector< typename Matrix::Scalar, N > grid_element_type
Definition: adaptors.hpp:29
static const int M
Definition: adaptors.hpp:28
MatrixToGridIterator(MatrixToGrid< Matrix > &adaptor, int i)
Definition: adaptors.hpp:33
int i
Definition: adaptors.hpp:30
size_t size() const
Definition: adaptors.hpp:66
MatrixToGrid< Matrix > & g
Definition: adaptors.hpp:25
grid_element_type operator[](int i) const
Definition: adaptors.hpp:72
static const CVector< Eigen::Dynamic > & to(const wavepackets::ScalarHaWp< D, MultiIndex > &packet)
Definition: adaptors.hpp:193
MatrixToGrid(const Matrix &matrix)
Definition: adaptors.hpp:70
void operator++()
Definition: adaptors.hpp:39
Adaptor which accepts an Eigen::Matrix and emulates some behavior of a std::vector.
Definition: adaptors.hpp:15
static void from(const CVector< Eigen::Dynamic > &coefficients, wavepackets::ScalarHaWp< D, MultiIndex > &packet)
Definition: adaptors.hpp:197
MatrixToGridIterator< Matrix > end() const
Definition: adaptors.hpp:79
Matrix grid_to_matrix(Grid< grid_element_type< Matrix > > g)
Copies a grid into a Eigen::Matrix.
Definition: adaptors.hpp:131
Concrete implementation of a scalar Hagedorn wavepacket.
Definition: hawp_commons.hpp:209
const Matrix & matrix
Definition: adaptors.hpp:63
static const int N
Definition: adaptors.hpp:27
GVector< typename Matrix::Scalar, N > grid_element_type
Definition: adaptors.hpp:61
MatrixToGridIterator< Matrix > begin() const
Definition: adaptors.hpp:76
GVector< typename Matrix::Scalar, Matrix::RowsAtCompileTime > grid_element_type
Definition: adaptors.hpp:12
bool operator!=(MatrixToGridIterator< Matrix > other) const
Definition: adaptors.hpp:35
grid_element_type operator*() const
Definition: adaptors.hpp:42