OGS
EigenMapTools.h
Go to the documentation of this file.
1 
11 #pragma once
12 
13 #include <Eigen/Core>
14 #include <cassert>
15 #include <vector>
16 
17 namespace MathLib
18 {
31 template <typename Matrix>
32 Eigen::Map<Matrix> createZeroedMatrix(std::vector<double>& data,
33  Eigen::MatrixXd::Index rows,
34  Eigen::MatrixXd::Index cols)
35 {
36  static_assert(Matrix::IsRowMajor || Matrix::IsVectorAtCompileTime,
37  "The default storage order in OGS is row major storage for "
38  "dense matrices.");
39  assert(Matrix::RowsAtCompileTime == Eigen::Dynamic ||
40  Matrix::RowsAtCompileTime == rows);
41  assert(Matrix::ColsAtCompileTime == Eigen::Dynamic ||
42  Matrix::ColsAtCompileTime == cols);
43  assert(data.empty()); // in order that resize fills the vector with zeros.
44 
45  data.resize(rows * cols);
46  return {data.data(), rows, cols};
47 }
48 
55 inline Eigen::Map<
56  Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>
57 createZeroedMatrix(std::vector<double>& data,
58  Eigen::MatrixXd::Index rows,
59  Eigen::MatrixXd::Index cols)
60 {
61  return createZeroedMatrix<
62  Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>(
63  data, rows, cols);
64 }
65 
71 template <typename Matrix>
72 Eigen::Map<const Matrix> toMatrix(std::vector<double> const& data,
73  Eigen::MatrixXd::Index rows,
74  Eigen::MatrixXd::Index cols)
75 {
76  static_assert(Matrix::IsRowMajor || Matrix::IsVectorAtCompileTime,
77  "The default storage order in OGS is row major storage for "
78  "dense matrices.");
79  assert(Matrix::RowsAtCompileTime == Eigen::Dynamic ||
80  Matrix::RowsAtCompileTime == rows);
81  assert(Matrix::ColsAtCompileTime == Eigen::Dynamic ||
82  Matrix::ColsAtCompileTime == cols);
83  assert(static_cast<Eigen::MatrixXd::Index>(data.size()) == rows * cols);
84 
85  return {data.data(), rows, cols};
86 }
87 
93 inline Eigen::Map<
94  const Eigen::
95  Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>
96 toMatrix(std::vector<double> const& data,
97  Eigen::MatrixXd::Index rows,
98  Eigen::MatrixXd::Index cols)
99 {
100  return toMatrix<
101  Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>(
102  data, rows, cols);
103 }
104 
110 template <typename Matrix>
111 Eigen::Map<Matrix> toMatrix(std::vector<double>& data,
112  Eigen::MatrixXd::Index rows,
113  Eigen::MatrixXd::Index cols)
114 {
115  static_assert(Matrix::IsRowMajor || Matrix::IsVectorAtCompileTime,
116  "The default storage order in OGS is row major storage for "
117  "dense matrices.");
118  assert(Matrix::RowsAtCompileTime == Eigen::Dynamic ||
119  Matrix::RowsAtCompileTime == rows);
120  assert(Matrix::ColsAtCompileTime == Eigen::Dynamic ||
121  Matrix::ColsAtCompileTime == cols);
122  assert(static_cast<Eigen::MatrixXd::Index>(data.size()) == rows * cols);
123 
124  return {data.data(), rows, cols};
125 }
126 
132 inline Eigen::Map<
133  Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>
134 toMatrix(std::vector<double>& data,
135  Eigen::MatrixXd::Index rows,
136  Eigen::MatrixXd::Index cols)
137 {
138  return toMatrix<
139  Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>(
140  data, rows, cols);
141 }
142 
152 template <typename Vector>
153 Eigen::Map<Vector> createZeroedVector(std::vector<double>& data,
154  Eigen::VectorXd::Index size)
155 {
156  static_assert(Vector::IsVectorAtCompileTime, "A vector type is required.");
157  assert(Vector::SizeAtCompileTime == Eigen::Dynamic ||
158  Vector::SizeAtCompileTime == size);
159  assert(data.empty()); // in order that resize fills the vector with zeros.
160 
161  data.resize(size);
162  return {data.data(), size};
163 }
164 
166 template <typename Vector>
167 Eigen::Map<const Vector> toVector(std::vector<double> const& data,
168  Eigen::VectorXd::Index size)
169 {
170  static_assert(Vector::IsVectorAtCompileTime, "A vector type is required.");
171  assert(Vector::SizeAtCompileTime == Eigen::Dynamic ||
172  Vector::SizeAtCompileTime == size);
173  assert(static_cast<Eigen::VectorXd::Index>(data.size()) == size);
174 
175  return {data.data(), size};
176 }
177 
179 template <typename Vector>
180 Eigen::Map<Vector> toVector(std::vector<double>& data,
181  Eigen::VectorXd::Index size)
182 {
183  static_assert(Vector::IsVectorAtCompileTime, "A vector type is required.");
184  assert(Vector::SizeAtCompileTime == Eigen::Dynamic ||
185  Vector::SizeAtCompileTime == size);
186  assert(static_cast<Eigen::VectorXd::Index>(data.size()) == size);
187 
188  return {data.data(), size};
189 }
190 
196 inline Eigen::Map<const Eigen::VectorXd> toVector(
197  std::vector<double> const& data)
198 {
199  return {data.data(), static_cast<Eigen::VectorXd::Index>(data.size())};
200 }
201 
207 inline Eigen::Map<Eigen::VectorXd> toVector(std::vector<double>& data)
208 {
209  return {data.data(), static_cast<Eigen::VectorXd::Index>(data.size())};
210 }
211 
212 } // namespace MathLib
Eigen::Map< const Vector > toVector(std::vector< double > const &data, Eigen::VectorXd::Index size)
Creates an Eigen mapped vector from the given data vector.
Eigen::Map< Matrix > createZeroedMatrix(std::vector< double > &data, Eigen::MatrixXd::Index rows, Eigen::MatrixXd::Index cols)
Definition: EigenMapTools.h:32
Eigen::Map< Vector > createZeroedVector(std::vector< double > &data, Eigen::VectorXd::Index size)
Eigen::Map< const Matrix > toMatrix(std::vector< double > const &data, Eigen::MatrixXd::Index rows, Eigen::MatrixXd::Index cols)
Definition: EigenMapTools.h:72