5#ifndef DUNE_COMMON_TRANSPOSE_HH
6#define DUNE_COMMON_TRANSPOSE_HH
11#include <dune/common/std/type_traits.hh>
14#include <dune/common/referencehelper.hh>
16#include <dune/common/matrixconcepts.hh>
24 template<class M, bool = IsStaticSizeMatrix<M>::value>
25 struct TransposedDenseMatrixTraits
31 struct TransposedDenseMatrixTraits<M, false>
37 using TransposedDenseMatrixTraits_t =
typename TransposedDenseMatrixTraits<M>::type;
43 template<class WM, bool staticSize = IsStaticSizeMatrix<WM>::value>
44 class TransposedMatrixWrapperMixin {};
47 class TransposedMatrixWrapperMixin<WM, true>
52 constexpr static int rows = WM::cols;
54 constexpr static int cols = WM::rows;
60 class TransposedMatrixWrapper;
66struct FieldTraits< Impl::TransposedMatrixWrapper<M> >
68 using field_type =
typename FieldTraits<ResolveRef_t<M>>::field_type;
69 using real_type =
typename FieldTraits<ResolveRef_t<M>>::real_type;
81 class TransposedMatrixWrapper :
82 public TransposedMatrixWrapperMixin<ResolveRef_t<M>>
84 constexpr static bool hasStaticSize = IsStaticSizeMatrix<ResolveRef_t<M>>::value;
86 using WrappedMatrix = ResolveRef_t<M>;
88 const WrappedMatrix& wrappedMatrix()
const {
93 using value_type =
typename WrappedMatrix::value_type;
94 using field_type =
typename FieldTraits<WrappedMatrix>::field_type;
96 TransposedMatrixWrapper(M&& matrix) : matrix_(
std::move(matrix)) {}
97 TransposedMatrixWrapper(
const M& matrix) : matrix_(matrix) {}
99 template<
class MatrixA,
101 ((not Impl::IsFieldMatrix<MatrixA>::value) or (not hasStaticSize))
102 and Impl::IsDenseMatrix_v<MatrixA>,
int> = 0>
103 friend auto operator* (
const MatrixA& matrixA,
const TransposedMatrixWrapper& matrixB)
105 using FieldA =
typename FieldTraits<MatrixA>::field_type;
106 using FieldB =
typename FieldTraits<TransposedMatrixWrapper>::field_type;
107 using Field =
typename PromotionTraits<FieldA, FieldB>::PromotedType;
112 if constexpr(IsStaticSizeMatrix_v<MatrixA> and IsStaticSizeMatrix_v<WrappedMatrix>)
114 FieldMatrix<Field, MatrixA::rows, WrappedMatrix::rows> result;
115 for (std::size_t j=0; j<MatrixA::rows; ++j)
116 matrixB.wrappedMatrix().mv(matrixA[j], result[j]);
121 DynamicMatrix<Field> result(matrixA.N(), matrixB.wrappedMatrix().N());
122 for (std::size_t j=0; j<matrixA.N(); ++j)
123 matrixB.wrappedMatrix().mv(matrixA[j], result[j]);
128 template<
class X,
class Y>
129 void mv (
const X& x, Y& y)
const
131 wrappedMatrix().mtv(x,y);
134 template<
class X,
class Y>
135 void mtv (
const X& x, Y& y)
const
137 wrappedMatrix().mv(x,y);
145 TransposedDenseMatrixTraits_t<WrappedMatrix> asDense()
const
147 TransposedDenseMatrixTraits_t<WrappedMatrix> MT;
148 if constexpr(not IsStaticSizeMatrix<WrappedMatrix>::value)
150 MT.resize(wrappedMatrix().M(), wrappedMatrix().N(), 0);
164 using MemberFunctionTransposedConcept = std::void_t<decltype(std::declval<M>().transposed())>;
181template<
class Matrix,
182 std::enable_if_t<Impl::HasMemberFunctionTransposed<Matrix>::value,
int> = 0>
184 return matrix.transposed();
210template<
class Matrix,
211 std::enable_if_t<not Impl::HasMemberFunctionTransposed<std::decay_t<Matrix>>::value,
int> = 0>
213 return Impl::TransposedMatrixWrapper(std::forward<Matrix>(matrix));
244template<
class Matrix>
245auto transpose(
const std::reference_wrapper<Matrix>& matrix) {
246 return Impl::TransposedMatrixWrapper(matrix);
260template<
class Matrix>
Construct a matrix with a dynamic size.
Definition: dynmatrix.hh:61
A dense n x m matrix.
Definition: fmatrix.hh:117
A generic dynamic dense matrix.
Definition: matrix.hh:561
This file implements a dense matrix with dynamic numbers of rows and columns.
Implements a matrix constructed from a given type representing a field and compile-time given number ...
constexpr T & resolveRef(T &gf) noexcept
Helper function to resolve std::reference_wrapper.
Definition: referencehelper.hh:47
typename detected_or< nonesuch, Op, Args... >::value_t is_detected
Detects whether Op<Args...> is valid.
Definition: type_traits.hh:141
auto sparseRange(Range &&range)
Allow structured-binding for-loops for sparse iterators.
Definition: rangeutilities.hh:821
Dune namespace.
Definition: alignedallocator.hh:13
auto transpose(const Matrix &matrix)
Return the transposed of the given matrix.
Definition: transpose.hh:183
auto transposedView(const Matrix &matrix)
Create a view modelling the transposed matrix.
Definition: transpose.hh:261