OGS
EigenMapTools.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#pragma once
5
6#include <Eigen/Core>
7#include <cassert>
8#include <vector>
9
10namespace MathLib
11{
24template <typename Matrix>
25Eigen::Map<Matrix> createZeroedMatrix(std::vector<double>& data,
26 Eigen::MatrixXd::Index rows,
27 Eigen::MatrixXd::Index cols)
28{
29 static_assert(Matrix::IsRowMajor || Matrix::IsVectorAtCompileTime,
30 "The default storage order in OGS is row major storage for "
31 "dense matrices.");
32 assert(Matrix::RowsAtCompileTime == Eigen::Dynamic ||
33 Matrix::RowsAtCompileTime == rows);
34 assert(Matrix::ColsAtCompileTime == Eigen::Dynamic ||
35 Matrix::ColsAtCompileTime == cols);
36 assert(data.empty()); // in order that resize fills the vector with zeros.
37
38 data.resize(rows * cols);
39 return {data.data(), rows, cols};
40}
41
48inline Eigen::Map<
49 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>
50createZeroedMatrix(std::vector<double>& data,
51 Eigen::MatrixXd::Index rows,
52 Eigen::MatrixXd::Index cols)
53{
54 return createZeroedMatrix<
55 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>(
56 data, rows, cols);
57}
58
64template <typename Matrix>
65Eigen::Map<const Matrix> toMatrix(std::vector<double> const& data,
66 Eigen::MatrixXd::Index rows,
67 Eigen::MatrixXd::Index cols)
68{
69 static_assert(Matrix::IsRowMajor || Matrix::IsVectorAtCompileTime,
70 "The default storage order in OGS is row major storage for "
71 "dense matrices.");
72 assert(Matrix::RowsAtCompileTime == Eigen::Dynamic ||
73 Matrix::RowsAtCompileTime == rows);
74 assert(Matrix::ColsAtCompileTime == Eigen::Dynamic ||
75 Matrix::ColsAtCompileTime == cols);
76 assert(static_cast<Eigen::MatrixXd::Index>(data.size()) == rows * cols);
77
78 return {data.data(), rows, cols};
79}
80
86inline Eigen::Map<
87 const Eigen::
88 Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>
89toMatrix(std::vector<double> const& data,
90 Eigen::MatrixXd::Index rows,
91 Eigen::MatrixXd::Index cols)
92{
93 return toMatrix<
94 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>(
95 data, rows, cols);
96}
97
103template <typename Matrix>
104Eigen::Map<Matrix> toMatrix(std::vector<double>& data,
105 Eigen::MatrixXd::Index rows,
106 Eigen::MatrixXd::Index cols)
107{
108 static_assert(Matrix::IsRowMajor || Matrix::IsVectorAtCompileTime,
109 "The default storage order in OGS is row major storage for "
110 "dense matrices.");
111 assert(Matrix::RowsAtCompileTime == Eigen::Dynamic ||
112 Matrix::RowsAtCompileTime == rows);
113 assert(Matrix::ColsAtCompileTime == Eigen::Dynamic ||
114 Matrix::ColsAtCompileTime == cols);
115 assert(static_cast<Eigen::MatrixXd::Index>(data.size()) == rows * cols);
116
117 return {data.data(), rows, cols};
118}
119
125inline Eigen::Map<
126 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>
127toMatrix(std::vector<double>& data,
128 Eigen::MatrixXd::Index rows,
129 Eigen::MatrixXd::Index cols)
130{
131 return toMatrix<
132 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>(
133 data, rows, cols);
134}
135
145template <typename Vector>
146Eigen::Map<Vector> createZeroedVector(std::vector<double>& data,
147 Eigen::VectorXd::Index size)
148{
149 static_assert(Vector::IsVectorAtCompileTime, "A vector type is required.");
150 assert(Vector::SizeAtCompileTime == Eigen::Dynamic ||
151 Vector::SizeAtCompileTime == size);
152 assert(data.empty()); // in order that resize fills the vector with zeros.
153
154 data.resize(size);
155 return {data.data(), size};
156}
157
159template <typename Vector>
160Eigen::Map<const Vector> toVector(std::vector<double> const& data,
161 Eigen::VectorXd::Index size)
162{
163 static_assert(Vector::IsVectorAtCompileTime, "A vector type is required.");
164 assert(Vector::SizeAtCompileTime == Eigen::Dynamic ||
165 Vector::SizeAtCompileTime == size);
166 assert(static_cast<Eigen::VectorXd::Index>(data.size()) == size);
167
168 return {data.data(), size};
169}
170
172template <typename Vector>
173Eigen::Map<Vector> toVector(std::vector<double>& data,
174 Eigen::VectorXd::Index size)
175{
176 static_assert(Vector::IsVectorAtCompileTime, "A vector type is required.");
177 assert(Vector::SizeAtCompileTime == Eigen::Dynamic ||
178 Vector::SizeAtCompileTime == size);
179 assert(static_cast<Eigen::VectorXd::Index>(data.size()) == size);
180
181 return {data.data(), size};
182}
183
189inline Eigen::Map<const Eigen::VectorXd> toVector(
190 std::vector<double> const& data)
191{
192 return {data.data(), static_cast<Eigen::VectorXd::Index>(data.size())};
193}
194
200inline Eigen::Map<Eigen::VectorXd> toVector(std::vector<double>& data)
201{
202 return {data.data(), static_cast<Eigen::VectorXd::Index>(data.size())};
203}
204
205} // namespace MathLib
Eigen::Map< Vector > createZeroedVector(std::vector< double > &data, Eigen::VectorXd::Index size)
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)
Eigen::Map< const Matrix > toMatrix(std::vector< double > const &data, Eigen::MatrixXd::Index rows, Eigen::MatrixXd::Index cols)