9#include<dune/common/std/type_traits.hh>
10#include<dune/common/diagonalmatrix.hh>
11#include<dune/common/hybridutilities.hh>
12#include<dune/common/indices.hh>
23 using StaticIndexAccessConcept =
decltype(std::declval<C>()[Dune::Indices::_0]);
26 using IsScalar = std::negation<Dune::Std::is_detected<StaticIndexAccessConcept, std::remove_reference_t<C>>>;
32 struct IsRowMajorSparse : std::false_type {};
35 template <
class B,
class A>
36 struct IsRowMajorSparse<BCRSMatrix<B,A>> : std::true_type {};
38 template <
class K,
int n>
39 struct IsRowMajorSparse<DiagonalMatrix<K,n>> : std::true_type {};
41 template <
class K,
int n>
42 struct IsRowMajorSparse<ScaledIdentityMatrix<K,n>> : std::true_type {};
45 template <
class Matrix>
46 auto rows(Matrix
const& , PriorityTag<2>) -> std::integral_constant<std::size_t,
Matrix::N()> {
return {}; }
48 template <
class Matrix>
49 auto cols(Matrix
const& , PriorityTag<2>) -> std::integral_constant<std::size_t,
Matrix::M()> {
return {}; }
51 template <
class Matrix>
52 auto rows(Matrix
const& matrix, PriorityTag<1>) ->
decltype(matrix.N()) {
return matrix.N(); }
54 template <
class Matrix>
55 auto cols(Matrix
const& matrix, PriorityTag<1>) ->
decltype(matrix.M()) {
return matrix.M(); }
57 template <
class Vector>
58 auto size(Vector
const& , PriorityTag<2>) -> std::integral_constant<std::size_t, Vector::size()> {
return {}; }
60 template <
class Vector>
61 auto size(Vector
const& vector, PriorityTag<1>) ->
decltype(vector.size()) {
return vector.size(); }
68 template <
class Matrix>
69 auto rows(
Matrix const& matrix) {
return Impl::rows(matrix, PriorityTag<5>{}); }
71 template <
class Matrix>
72 auto cols(
Matrix const& matrix) {
return Impl::cols(matrix, PriorityTag<5>{}); }
74 template <
class Vector>
75 auto size(Vector
const& vector) {
return Impl::size(vector, PriorityTag<5>{}); }
94template <
class Vector,
class F>
97 using V = std::decay_t<Vector>;
98 if constexpr( Impl::IsScalar<V>::value )
106 Hybrid::forEach(Dune::range(
ForEach::size(vector)), [&](
auto i) {
131template <
class Matrix,
class F>
132std::pair<std::size_t,std::size_t>
flatMatrixForEach(
Matrix&& matrix, F&& f, std::size_t rowOffset = 0, std::size_t colOffset = 0)
134 using M = std::decay_t<Matrix>;
135 if constexpr ( Impl::IsScalar<M>::value )
137 f(matrix,rowOffset,colOffset);
144 if constexpr ( Impl::IsRowMajorSparse<M>::value )
146 using Block = std::decay_t<
decltype(matrix[0][0])>;
150 for (
auto const& row : matrix)
151 for (
auto const& entry : row)
160 assert( ( blockRows!=0 or blockCols!=0 ) and
"the block size can't be zero");
162 for (
auto rowIt = matrix.begin(); rowIt != matrix.end(); ++rowIt)
165 auto rowIdx = rowIt.index();
166 for (
auto colIt = row.begin(); colIt != row.end(); colIt++)
168 auto&& entry = *colIt;
169 auto colIdx = colIt.index();
172 auto [ numRows, numCols ] =
174 flatMatrixForEach(entry, f, rowOffset + rowIdx*blockRows, colOffset + colIdx*blockCols);
175 assert( numRows == blockRows and numCols == blockCols and
"we need the same size of each block in this matrix type");
179 return { matrix.N()*blockRows, matrix.M()*blockCols };
184 std::size_t r = 0, c = 0;
185 std::size_t nRows, nCols;
187 Hybrid::forEach(Dune::range(
ForEach::rows(matrix)), [&](
auto i) {
189 Hybrid::forEach(Dune::range(
ForEach::cols(matrix)), [&](
auto j) {
190 std::tie(nRows,nCols) =
flatMatrixForEach(matrix[i][j], f, rowOffset + r, colOffset + c);
Implementation of the BCRSMatrix class.
This file implements a quadratic matrix of fixed size which is a multiple of the identity.
Definition allocator.hh:11
std::pair< std::size_t, std::size_t > flatMatrixForEach(Matrix &&matrix, F &&f, std::size_t rowOffset=0, std::size_t colOffset=0)
Traverse a blocked matrix and call a functor at each scalar entry.
Definition foreach.hh:132
std::size_t flatVectorForEach(Vector &&vector, F &&f, std::size_t offset=0)
Traverse a blocked vector and call a functor at each scalar entry.
Definition foreach.hh:95
auto rows(Matrix const &matrix)
Definition foreach.hh:69
auto cols(Matrix const &matrix)
Definition foreach.hh:72
auto size(Vector const &vector)
Definition foreach.hh:75
A generic dynamic dense matrix.
Definition matrix.hh:561
size_type M() const
Return the number of columns.
Definition matrix.hh:696
size_type N() const
Return the number of rows.
Definition matrix.hh:691