8#include<dune/common/hybridutilities.hh>
9#include<dune/common/indices.hh>
23 using IsScalar = std::negation<Dune::Std::is_detected<StaticIndexAccessConcept, std::remove_reference_t<C>>>;
29 struct IsRowMajorSparse : std::false_type {};
32 template <
class B,
class A>
33 struct IsRowMajorSparse<BCRSMatrix<B,A>> : std::true_type {};
35 template <
class K,
int n>
36 struct IsRowMajorSparse<DiagonalMatrix<K,n>> : std::true_type {};
38 template <
class K,
int n>
39 struct IsRowMajorSparse<ScaledIdentityMatrix<K,n>> : std::true_type {};
42 template <
class Matrix>
43 auto rows(Matrix
const& , PriorityTag<2>) -> std::integral_constant<std::size_t,
Matrix::N()> {
return {}; }
45 template <
class Matrix>
46 auto cols(Matrix
const& , PriorityTag<2>) -> std::integral_constant<std::size_t,
Matrix::M()> {
return {}; }
48 template <
class Matrix>
49 auto rows(Matrix
const& matrix, PriorityTag<1>) ->
decltype(matrix.N()) {
return matrix.N(); }
51 template <
class Matrix>
52 auto cols(Matrix
const& matrix, PriorityTag<1>) ->
decltype(matrix.M()) {
return matrix.M(); }
54 template <
class Vector>
55 auto size(Vector
const& , PriorityTag<2>) -> std::integral_constant<std::size_t, Vector::size()> {
return {}; }
57 template <
class Vector>
58 auto size(Vector
const& vector, PriorityTag<1>) ->
decltype(vector.size()) {
return vector.size(); }
65 template <
class Matrix>
66 auto rows(Matrix
const& matrix) {
return Impl::rows(matrix, PriorityTag<5>{}); }
68 template <
class Matrix>
69 auto cols(Matrix
const& matrix) {
return Impl::cols(matrix, PriorityTag<5>{}); }
71 template <
class Vector>
72 auto size(Vector
const& vector) {
return Impl::size(vector, PriorityTag<5>{}); }
91template <
class Vector,
class F>
94 using V = std::decay_t<Vector>;
95 if constexpr( Impl::IsScalar<V>::value )
128template <
class Matrix,
class F>
129std::pair<std::size_t,std::size_t>
flatMatrixForEach(
Matrix&& matrix, F&& f, std::size_t rowOffset = 0, std::size_t colOffset = 0)
131 using M = std::decay_t<Matrix>;
132 if constexpr ( Impl::IsScalar<M>::value )
134 f(matrix,rowOffset,colOffset);
141 if constexpr ( Impl::IsRowMajorSparse<M>::value )
143 using Block = std::decay_t<
decltype(matrix[0][0])>;
147 for (
auto const& row : matrix)
148 for (
auto const& entry : row)
157 assert( ( blockRows!=0 or blockCols!=0 ) and
"the block size can't be zero");
159 for (
auto rowIt = matrix.begin(); rowIt != matrix.end(); rowIt++ )
162 auto rowIdx = rowIt.index();
163 for (
auto colIt = row.begin(); colIt != row.end(); colIt++ )
165 auto&& entry = *colIt;
166 auto colIdx = colIt.index();
167 auto [ dummyRows, dummyCols ] =
flatMatrixForEach(entry, f, rowOffset + rowIdx*blockRows, colOffset + colIdx*blockCols);
168 assert( dummyRows == blockRows and dummyCols == blockCols and
"we need the same size of each block in this matrix type");
172 return { matrix.N()*blockRows, matrix.M()*blockCols };
177 std::size_t r = 0, c = 0;
178 std::size_t nRows, nCols;
183 std::tie(nRows,nCols) =
flatMatrixForEach(matrix[i][j], f, rowOffset + r, colOffset + c);
A generic dynamic dense matrix.
Definition: matrix.hh:559
size_type M() const
Return the number of columns.
Definition: matrix.hh:698
size_type N() const
Return the number of rows.
Definition: matrix.hh:693
This file implements a quadratic diagonal matrix of fixed size.
Implementation of the BCRSMatrix class.
constexpr index_constant< 0 > _0
Compile time index with value 0.
Definition: indices.hh:51
constexpr void forEach(Range &&range, F &&f)
Range based for loop.
Definition: hybridutilities.hh:266
Dune namespace.
Definition: alignedallocator.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:129
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:92
This file implements a quadratic matrix of fixed size which is a multiple of the identity.