OGS
EigenBlockMatrixView.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
8namespace MathLib
9{
10template <int D, typename M>
12{
13public:
14 static constexpr int rows = M::RowsAtCompileTime == Eigen::Dynamic
15 ? Eigen::Dynamic
16 : D * M::RowsAtCompileTime;
17 static constexpr int cols = M::ColsAtCompileTime == Eigen::Dynamic
18 ? Eigen::Dynamic
19 : D * M::ColsAtCompileTime;
20 using Scalar = typename M::Scalar;
21 using Matrix = Eigen::Matrix<Scalar, rows, cols, Eigen::ColMajor>;
22
23 constexpr explicit EigenBlockMatrixViewFunctor(const M& matrix)
24 : matrix_(matrix){};
25
26 constexpr const Scalar operator()(Eigen::Index row, Eigen::Index col) const
27 {
28 if (row / matrix_.rows() != col / matrix_.cols())
29 {
30 return 0;
31 }
32 return matrix_(row % matrix_.rows(), col % matrix_.cols());
33 }
34
35private:
36 const typename M::Nested& matrix_;
37};
38
39template <int D, typename M>
40constexpr Eigen::CwiseNullaryOp<
43eigenBlockMatrixView(const Eigen::MatrixBase<M>& matrix)
44{
45 using Matrix = typename EigenBlockMatrixViewFunctor<D, M>::Matrix;
46 return Matrix::NullaryExpr(
47 D * matrix.rows(), D * matrix.cols(),
48 EigenBlockMatrixViewFunctor<D, M>(matrix.derived()));
49}
50} // namespace MathLib
constexpr const Scalar operator()(Eigen::Index row, Eigen::Index col) const
constexpr EigenBlockMatrixViewFunctor(const M &matrix)
Eigen::Matrix< Scalar, rows, cols, Eigen::ColMajor > Matrix
constexpr Eigen::CwiseNullaryOp< EigenBlockMatrixViewFunctor< D, M >, typename EigenBlockMatrixViewFunctor< D, M >::Matrix > eigenBlockMatrixView(const Eigen::MatrixBase< M > &matrix)