Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
eigen.hpp
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2//
3// Copyright (C) 2024 - 2026 by the Vulpes authors.
4// This file is part of Vulpes.
5//
6// Vulpes is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// Vulpes is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Vulpes. If not, see <http://www.gnu.org/licenses/>.
18//
19// ----------------------------------------------------------------------
20
21// Author: Francesco Daniele <francesco.daniele@polimi.it>
22// Author: Matteo Caldana <matteo.caldana@polimi.it>
23
24#ifndef VULPES_UTILS_EIGEN_HPP_
25#define VULPES_UTILS_EIGEN_HPP_
26
27#include <iostream>
28
29#include <Eigen/Dense>
30
32// SymMat
34
35// forward declaration
36namespace EigenVulpes {
37template <Eigen::Index CrossId, class ArgType>
38class SymMat;
39} // namespace EigenVulpes
40
41namespace Eigen {
42namespace internal {
43template <Eigen::Index CrossId, class ArgType>
44struct traits<EigenVulpes::SymMat<CrossId, ArgType>> {
45 typedef Eigen::Dense StorageKind;
46 typedef Eigen::MatrixXpr XprKind;
47 typedef typename ArgType::StorageIndex StorageIndex;
48 typedef typename ArgType::Scalar Scalar;
49 enum {
50 Flags = Eigen::ColMajor,
51 RowsAtCompileTime = ArgType::RowsAtCompileTime,
52 ColsAtCompileTime = ArgType::RowsAtCompileTime,
53 MaxRowsAtCompileTime = ArgType::MaxRowsAtCompileTime,
54 MaxColsAtCompileTime = ArgType::MaxRowsAtCompileTime
55 };
56};
57} // namespace internal
58} // namespace Eigen
59
60namespace Eigen {
61namespace internal {
62template <Eigen::Index CrossId, typename ArgType>
63struct evaluator<EigenVulpes::SymMat<CrossId, ArgType>>
64 : evaluator_base<EigenVulpes::SymMat<CrossId, ArgType>> {
66 typedef typename nested_eval<ArgType, XprType::ColsAtCompileTime>::type
68 typedef remove_all_t<ArgTypeNested> ArgTypeNestedCleaned;
69 typedef typename XprType::CoeffReturnType CoeffReturnType;
70
71 enum {
73 Flags = Eigen::ColMajor
74 };
75
76 explicit evaluator(const XprType& xpr)
77 : m_argImpl(xpr.m_arg), m_rows(xpr.rows()) {}
78
79 CoeffReturnType coeff(Index row, Index col) const {
80 if (row == CrossId) {
81 if (col == CrossId) {
82 return m_argImpl.coeff(CrossId);
83 }
84 return m_argImpl.coeff(col) / 2.0;
85 } else if (col == CrossId) {
86 return m_argImpl.coeff(row) / 2.0;
87 } else {
88 return 0.0;
89 }
90 }
91
93 const Index m_rows;
94};
95} // namespace internal
96} // namespace Eigen
97
98namespace EigenVulpes {
99
110template <Eigen::Index CrossId, class ArgType>
111class SymMat : public Eigen::MatrixBase<SymMat<CrossId, ArgType>> {
112 public:
113 explicit SymMat(const ArgType& arg) : m_arg(arg) {
114 EIGEN_STATIC_ASSERT(
115 ArgType::ColsAtCompileTime == 1,
116 YOU_TRIED_CALLING_A_VECTOR_METHOD_ON_A_MATRIX);
117 }
118
119 typedef typename Eigen::internal::ref_selector<SymMat>::type Nested;
120
121 typedef Eigen::Index Index;
122 Index rows() const { return m_arg.rows(); }
123 Index cols() const { return m_arg.rows(); }
124
125 typedef typename Eigen::internal::ref_selector<ArgType>::type ArgTypeNested;
127};
128
136template <Eigen::Index CrossId, class ArgType>
137SymMat<CrossId, ArgType> makeSymMat(const Eigen::MatrixBase<ArgType>& arg) {
138 return SymMat<CrossId, ArgType>(arg.derived());
139}
140} // namespace EigenVulpes
141
143// RowMat
145
146// forward declaration
147namespace EigenVulpes {
148template <Eigen::Index RowId, class ArgType>
149class RowMat;
150} // namespace EigenVulpes
151
152namespace Eigen {
153namespace internal {
154template <Eigen::Index RowId, class ArgType>
155struct traits<EigenVulpes::RowMat<RowId, ArgType>> {
156 typedef Eigen::Dense StorageKind;
157 typedef Eigen::MatrixXpr XprKind;
158 typedef typename ArgType::StorageIndex StorageIndex;
159 typedef typename ArgType::Scalar Scalar;
160 enum {
161 Flags = Eigen::ColMajor,
162 RowsAtCompileTime = ArgType::RowsAtCompileTime,
163 ColsAtCompileTime = ArgType::RowsAtCompileTime,
164 MaxRowsAtCompileTime = ArgType::MaxRowsAtCompileTime,
165 MaxColsAtCompileTime = ArgType::MaxRowsAtCompileTime
166 };
167};
168} // namespace internal
169} // namespace Eigen
170
171namespace Eigen {
172namespace internal {
173template <Eigen::Index RowId, typename ArgType>
174struct evaluator<EigenVulpes::RowMat<RowId, ArgType>>
175 : evaluator_base<EigenVulpes::RowMat<RowId, ArgType>> {
177 typedef typename nested_eval<ArgType, XprType::ColsAtCompileTime>::type
179 typedef remove_all_t<ArgTypeNested> ArgTypeNestedCleaned;
180 typedef typename XprType::CoeffReturnType CoeffReturnType;
181
182 enum {
184 Flags = Eigen::ColMajor
185 };
186
187 explicit evaluator(const XprType& xpr)
188 : m_argImpl(xpr.m_arg), m_rows(xpr.rows()) {}
189
190 CoeffReturnType coeff(Index row, Index col) const {
191 if (row == RowId) {
192 return m_argImpl.coeff(col);
193 } else {
194 return 0.0;
195 }
196 }
197
199 const Index m_rows;
200};
201} // namespace internal
202} // namespace Eigen
203
204namespace EigenVulpes {
205
216template <Eigen::Index RowId, class ArgType>
217class RowMat : public Eigen::MatrixBase<RowMat<RowId, ArgType>> {
218 public:
219 explicit RowMat(const ArgType& arg) : m_arg(arg) {
220 EIGEN_STATIC_ASSERT(
221 ArgType::ColsAtCompileTime == 1,
222 YOU_TRIED_CALLING_A_VECTOR_METHOD_ON_A_MATRIX);
223 }
224
225 typedef typename Eigen::internal::ref_selector<RowMat>::type Nested;
226
227 typedef Eigen::Index Index;
228 Index rows() const { return m_arg.rows(); }
229 Index cols() const { return m_arg.rows(); }
230
231 typedef typename Eigen::internal::ref_selector<ArgType>::type ArgTypeNested;
233};
234
242template <Eigen::Index RowId, class ArgType>
243RowMat<RowId, ArgType> makeRowMat(const Eigen::MatrixBase<ArgType>& arg) {
244 return RowMat<RowId, ArgType>(arg.derived());
245}
246} // namespace EigenVulpes
247
249// OneComponentVector
251
252namespace EigenVulpes {
253template <Eigen::Index Idx, typename Scalar_, Eigen::Index Size_>
255}
256
257namespace Eigen {
258namespace internal {
259template <Eigen::Index Idx, typename Scalar_, Eigen::Index Size_>
260struct traits<EigenVulpes::OneComponentVector<Idx, Scalar_, Size_>> {
261 using Scalar = Scalar_;
262 using StorageIndex = Eigen::Index;
263 using StorageKind = Eigen::Dense;
264 using XprKind = Eigen::MatrixXpr;
265
266 enum {
271 Flags = Eigen::ColMajor | Eigen::PacketAccessBit
272 };
273};
274
275} // namespace internal
276} // namespace Eigen
277
278namespace Eigen {
279namespace internal {
280template <Eigen::Index Idx, typename Scalar_, Eigen::Index Size_>
281struct evaluator<EigenVulpes::OneComponentVector<Idx, Scalar_, Size_>>
282 : evaluator_base<EigenVulpes::OneComponentVector<Idx, Scalar_, Size_>> {
284 using CoeffReturnType = Scalar_;
285
286 enum {
287 CoeffReadCost = NumTraits<Scalar_>::ReadCost,
288 Flags = Eigen::ColMajor
289 };
290
291 explicit evaluator(const XprType& xpr) : m_value(xpr.value()) {}
292
293 EIGEN_STRONG_INLINE Scalar_ coeff(Index row, Index col = 0) const {
294 EIGEN_UNUSED_VARIABLE(col);
295 return row == Idx ? m_value : Scalar_(0);
296 }
297
298 Scalar_ m_value;
299};
300} // namespace internal
301} // namespace Eigen
302
303namespace EigenVulpes {
316template <Eigen::Index Idx, typename Scalar_, Eigen::Index Size_>
318 : public Eigen::MatrixBase<OneComponentVector<Idx, Scalar_, Size_>> {
319 public:
320 EIGEN_STATIC_ASSERT(Idx >= 0 && Idx < Size_, INDEX_OUT_OF_BOUNDS);
321
322 using Scalar = Scalar_;
323 using Index = Eigen::Index;
324 using Base = Eigen::MatrixBase<OneComponentVector<Idx, Scalar_, Size_>>;
325 using Nested =
326 typename Eigen::internal::ref_selector<OneComponentVector>::type;
327
329
330 Index rows() const { return Size_; }
331 Index cols() const { return 1; }
332
333 Scalar coeff(Index row, Index col = 0) const {
334 EIGEN_UNUSED_VARIABLE(col);
335 return row == Idx ? m_value : Scalar(0);
336 }
337
338 Scalar value() const { return m_value; }
339
340 private:
342};
343} // namespace EigenVulpes
344
346
347namespace EigenVulpes {
354template <typename Derived>
355auto toMap(Eigen::MatrixBase<Derived>& mat) {
356 return Eigen::Map<Eigen::Matrix<
357 typename Derived::Scalar, Derived::RowsAtCompileTime,
358 Derived::ColsAtCompileTime>>(
359 mat.derived().data(), mat.rows(), mat.cols());
360}
361
368template <typename Derived>
369auto toConstMap(const Eigen::MatrixBase<Derived>& mat) {
370 return Eigen::Map<const Eigen::Matrix<
371 typename Derived::Scalar, Derived::RowsAtCompileTime,
372 Derived::ColsAtCompileTime>>(
373 mat.derived().data(), mat.rows(), mat.cols());
374}
375
389 const Eigen::MatrixXd& mat1,
390 const Eigen::MatrixXd& mat2,
391 const double tolerance = 1e-12) {
392 if (mat1.rows() != mat2.rows() || mat1.cols() != mat2.cols()) {
393 std::cout << "Matrix sizes do not match: (" << mat1.rows() << ","
394 << mat1.cols() << ") vs (" << mat2.rows() << "," << mat2.cols()
395 << ")" << std::endl;
396 return false;
397 }
398
399 double norm_diff = (mat1 - mat2).norm();
400 double norm_ref = mat2.norm();
401
402 bool result = norm_diff < tolerance * (norm_ref + 1.0);
403
404 if (!result) {
405 std::cout << "Matrix comparison failed: ||diff||=" << norm_diff
406 << ", ||ref||=" << norm_ref << ", tolerance=" << tolerance
407 << std::endl;
408 }
409
410 return result;
411}
412
426 const Eigen::VectorXd& vec1,
427 const Eigen::VectorXd& vec2,
428 const double tolerance = 1e-12) {
429 if (vec1.size() != vec2.size()) {
430 std::cout << "Vector sizes do not match: " << vec1.size() << " vs "
431 << vec2.size() << std::endl;
432 return false;
433 }
434
435 double norm_diff = (vec1 - vec2).norm();
436 double norm_ref = vec2.norm();
437
438 bool result = norm_diff < tolerance * (norm_ref + 1.0);
439
440 if (!result) {
441 std::cout << "Vector comparison failed: ||diff||=" << norm_diff
442 << ", ||ref||=" << norm_ref << ", tolerance=" << tolerance
443 << std::endl;
444 }
445
446 return result;
447}
448
449} // namespace EigenVulpes
450
451#endif // VULPES_UTILS_EIGEN_HPP_
Eigen expression for a vector with a single non-zero component.
Definition eigen.hpp:318
typename Eigen::internal::ref_selector< OneComponentVector >::type Nested
Definition eigen.hpp:325
Scalar value() const
Definition eigen.hpp:338
Scalar m_value
Definition eigen.hpp:341
Index rows() const
Definition eigen.hpp:330
Eigen::MatrixBase< OneComponentVector< Idx, Scalar_, Size_ > > Base
Definition eigen.hpp:324
Index cols() const
Definition eigen.hpp:331
Eigen::Index Index
Definition eigen.hpp:323
EIGEN_STATIC_ASSERT(Idx >=0 &&Idx< Size_, INDEX_OUT_OF_BOUNDS)
Scalar_ Scalar
Definition eigen.hpp:322
Scalar coeff(Index row, Index col=0) const
Definition eigen.hpp:333
OneComponentVector(Scalar value=Scalar(1))
Definition eigen.hpp:328
Eigen expression representing a vector inserted as one matrix row.
Definition eigen.hpp:217
Eigen::internal::ref_selector< RowMat >::type Nested
Definition eigen.hpp:225
ArgTypeNested m_arg
Definition eigen.hpp:232
Index rows() const
Definition eigen.hpp:228
Eigen::internal::ref_selector< ArgType >::type ArgTypeNested
Definition eigen.hpp:231
Index cols() const
Definition eigen.hpp:229
RowMat(const ArgType &arg)
Definition eigen.hpp:219
Eigen::Index Index
Definition eigen.hpp:227
Eigen expression representing a rank-1-derived symmetric matrix.
Definition eigen.hpp:111
ArgTypeNested m_arg
Definition eigen.hpp:126
Index rows() const
Definition eigen.hpp:122
Eigen::internal::ref_selector< SymMat >::type Nested
Definition eigen.hpp:119
Eigen::Index Index
Definition eigen.hpp:121
SymMat(const ArgType &arg)
Definition eigen.hpp:113
Index cols() const
Definition eigen.hpp:123
Eigen::internal::ref_selector< ArgType >::type ArgTypeNested
Definition eigen.hpp:125
Definition eigen.hpp:36
bool compareEigenVectors(const Eigen::VectorXd &vec1, const Eigen::VectorXd &vec2, const double tolerance=1e-12)
Compare two dense vectors for approximate equality.
Definition eigen.hpp:425
SymMat< CrossId, ArgType > makeSymMat(const Eigen::MatrixBase< ArgType > &arg)
Build a SymMat expression from a column vector.
Definition eigen.hpp:137
auto toConstMap(const Eigen::MatrixBase< Derived > &mat)
View a dense Eigen matrix/vector as a read-only Eigen::Map.
Definition eigen.hpp:369
bool compareEigenMatrices(const Eigen::MatrixXd &mat1, const Eigen::MatrixXd &mat2, const double tolerance=1e-12)
Compare two dense matrices for approximate equality.
Definition eigen.hpp:388
auto toMap(Eigen::MatrixBase< Derived > &mat)
View a dense Eigen matrix/vector as a mutable Eigen::Map.
Definition eigen.hpp:355
RowMat< RowId, ArgType > makeRowMat(const Eigen::MatrixBase< ArgType > &arg)
Build a RowMat expression from a column vector.
Definition eigen.hpp:243
Definition eigen.hpp:42
Definition eigen.hpp:41
EigenVulpes::OneComponentVector< Idx, Scalar_, Size_ > XprType
Definition eigen.hpp:283
EIGEN_STRONG_INLINE Scalar_ coeff(Index row, Index col=0) const
Definition eigen.hpp:293
XprType::CoeffReturnType CoeffReturnType
Definition eigen.hpp:180
remove_all_t< ArgTypeNested > ArgTypeNestedCleaned
Definition eigen.hpp:179
evaluator< ArgTypeNestedCleaned > m_argImpl
Definition eigen.hpp:198
evaluator(const XprType &xpr)
Definition eigen.hpp:187
CoeffReturnType coeff(Index row, Index col) const
Definition eigen.hpp:190
nested_eval< ArgType, XprType::ColsAtCompileTime >::type ArgTypeNested
Definition eigen.hpp:178
EigenVulpes::RowMat< RowId, ArgType > XprType
Definition eigen.hpp:176
CoeffReturnType coeff(Index row, Index col) const
Definition eigen.hpp:79
evaluator< ArgTypeNestedCleaned > m_argImpl
Definition eigen.hpp:92
EigenVulpes::SymMat< CrossId, ArgType > XprType
Definition eigen.hpp:65
XprType::CoeffReturnType CoeffReturnType
Definition eigen.hpp:69
remove_all_t< ArgTypeNested > ArgTypeNestedCleaned
Definition eigen.hpp:68
nested_eval< ArgType, XprType::ColsAtCompileTime >::type ArgTypeNested
Definition eigen.hpp:67
ArgType::StorageIndex StorageIndex
Definition eigen.hpp:158
ArgType::StorageIndex StorageIndex
Definition eigen.hpp:47