diff --git a/dune/matrix-vector/algorithm.hh b/dune/matrix-vector/algorithm.hh index 3fb24530a73a2d4de4947d003c180ae1eccc6ca1..81d55f7ee8724d1051b80fd9826c91337a9042a7 100644 --- a/dune/matrix-vector/algorithm.hh +++ b/dune/matrix-vector/algorithm.hh @@ -4,13 +4,13 @@ #define DUNE_MATRIX_VECTOR_ALGORITHM_HH #include <dune/common/hybridutilities.hh> -#include <dune/matrix-vector/traitutilities.hh> +#include <dune/matrix-vector/traits/utilities.hh> namespace Dune { namespace MatrixVector { //! \brief Hybrid for loop over sparse range (static/tuple-like candidate) -template <class Range, class F, std::enable_if_t<isTupleOrDerived<Range>(), int> = 0> +template <class Range, class F, EnableTupleOrDerived<Range, int> = 0> void sparseRangeFor(Range&& range, F&& f) { using namespace Dune::Hybrid; forEach(integralRange(size(range)), [&](auto&& i) { @@ -19,7 +19,7 @@ void sparseRangeFor(Range&& range, F&& f) { } //! \brief Hybrid for loop over sparse range (dynamic/sparse candidate) -template<class Range, class F, std::enable_if_t<not isTupleOrDerived<Range>(), int> = 0> +template<class Range, class F, DisableTupleOrDerived<Range, int> = 0> void sparseRangeFor(Range&& range, F&& f) { auto it = range.begin(); @@ -29,12 +29,12 @@ void sparseRangeFor(Range&& range, F&& f) } //! \brief Hybrid access to first sparse range element (static/tuple-like candiate) -template <class Range, class F, std::enable_if_t<isTupleOrDerived<Range>(), int> = 0> +template <class Range, class F, EnableTupleOrDerived<Range, int> = 0> void sparseRangeFirst(Range&& range, F&& f) { f(range[Indices::_0]); } //! \brief Hybrid access to first sparse range element (dynamic/sparse candiate) -template<class Range, class F, std::enable_if_t<not isTupleOrDerived<Range>(), int> = 0> +template <class Range, class F, DisableTupleOrDerived<Range, int> = 0> void sparseRangeFirst(Range&& range, F&& f) { f(*range.begin()); diff --git a/dune/matrix-vector/axpy.hh b/dune/matrix-vector/axpy.hh index fe6740fa48755669a4eea8be4026b29a12784a52..4672c202c1c00da05c6c00acf4c170065b58d3ab 100644 --- a/dune/matrix-vector/axpy.hh +++ b/dune/matrix-vector/axpy.hh @@ -8,9 +8,8 @@ #include <dune/istl/bcrsmatrix.hh> #include <dune/istl/scaledidmatrix.hh> -#include "algorithm.hh" -#include "matrixtraits.hh" -#include "scalartraits.hh" +#include <dune/matrix-vector/algorithm.hh> +#include <dune/matrix-vector/traits/utilities.hh> namespace Dune { namespace MatrixVector { @@ -38,8 +37,8 @@ namespace MatrixVector { */ template <class A, class B, class C> void addProduct(A& a, const B& b, const C& c) { - ProductHelper<A, B, C, ScalarTraits<A>::isScalar, ScalarTraits<B>::isScalar, - ScalarTraits<C>::isScalar>::addProduct(a, b, c); + ProductHelper<A, B, C, isScalar<A>(), isScalar<B>(), + isScalar<C>()>::addProduct(a, b, c); } /** \brief Subtract a product from some matrix or vector @@ -55,9 +54,8 @@ namespace MatrixVector { */ template <class A, class B, class C> void subtractProduct(A& a, const B& b, const C& c) { - ScaledProductHelper<A, int, B, C, ScalarTraits<A>::isScalar, - ScalarTraits<B>::isScalar, - ScalarTraits<C>::isScalar>::addProduct(a, -1, b, c); + ScaledProductHelper<A, int, B, C, isScalar<A>(), isScalar<B>(), + isScalar<C>()>::addProduct(a, -1, b, c); } /** \brief Add a scaled product to some matrix or vector @@ -72,11 +70,10 @@ namespace MatrixVector { * 1-dim vector or a 1 by 1 matrix. */ template <class A, class B, class C, class D> - typename std::enable_if_t<ScalarTraits<B>::isScalar, void> addProduct( + EnableScalar<B> addProduct( A& a, const B& b, const C& c, const D& d) { - ScaledProductHelper<A, B, C, D, ScalarTraits<A>::isScalar, - ScalarTraits<C>::isScalar, - ScalarTraits<D>::isScalar>::addProduct(a, b, c, d); + ScaledProductHelper<A, B, C, D, isScalar<A>(), isScalar<C>(), + isScalar<D>()>::addProduct(a, b, c, d); } /** \brief Subtract a scaled product from some matrix or vector @@ -91,11 +88,10 @@ namespace MatrixVector { * 1-dim vector or a 1 by 1 matrix. */ template <class A, class B, class C, class D> - typename std::enable_if_t<ScalarTraits<B>::isScalar, void> + EnableScalar<B> subtractProduct(A& a, const B& b, const C& c, const D& d) { - ScaledProductHelper<A, B, C, D, ScalarTraits<A>::isScalar, - ScalarTraits<C>::isScalar, - ScalarTraits<D>::isScalar>::addProduct(a, -b, c, d); + ScaledProductHelper<A, B, C, D, isScalar<A>(), isScalar<C>(), + isScalar<D>()>::addProduct(a, -b, c, d); } /** \brief Internal helper class for product operations @@ -104,16 +100,12 @@ namespace MatrixVector { template <class A, class B, class C, bool AisScalar, bool BisScalar, bool CisScalar> struct ProductHelper { - template < - class ADummy = A, - std::enable_if_t<!MatrixTraits<ADummy>::isMatrix, int> SFINAE_Dummy = 0> + template <class ADummy = A, DisableMatrix<ADummy, int> = 0> static void addProduct(A& a, const B& b, const C& c) { b.umv(c, a); } - template < - class ADummy = A, - std::enable_if_t<MatrixTraits<ADummy>::isMatrix, int> SFINAE_Dummy = 0> + template <class ADummy = A, EnableMatrix<ADummy, int> = 0> static void addProduct(A& a, const B& b, const C& c) { sparseRangeFor(b, [&](auto&& bi, auto&& i) { sparseRangeFor(bi, [&](auto&& bik, auto&& k) { @@ -130,16 +122,12 @@ namespace MatrixVector { template <class A, class Scalar, class B, class C, bool AisScalar, bool BisScalar, bool CisScalar> struct ScaledProductHelper { - template < - class ADummy = A, - std::enable_if_t<!MatrixTraits<ADummy>::isMatrix, int> SFINAE_Dummy = 0> + template <class ADummy = A, DisableMatrix<ADummy, int> = 0> static void addProduct(A& a, const Scalar& scalar, const B& b, const C& c) { b.usmv(scalar, c, a); } - template < - class ADummy = A, - std::enable_if_t<MatrixTraits<ADummy>::isMatrix, int> SFINAE_Dummy = 0> + template <class ADummy = A, EnableMatrix<ADummy, int> = 0> static void addProduct(A& a, const Scalar& scalar, const B& b, const C& c) { sparseRangeFor(b, [&](auto&& bi, auto&& i) { sparseRangeFor(bi, [&](auto&& bik, auto&& k) { @@ -317,16 +305,12 @@ namespace MatrixVector { struct ProductHelper<A, ScalarB, C, AisScalar, true, CisScalar> { typedef ScalarB B; - template < - class ADummy = A, - std::enable_if_t<!MatrixTraits<ADummy>::isMatrix, int> SFINAE_Dummy = 0> + template <class ADummy = A, DisableMatrix<ADummy, int> = 0> static void addProduct(A& a, const B& b, const C& c) { a.axpy(b, c); } - template < - class ADummy = A, - std::enable_if_t<MatrixTraits<ADummy>::isMatrix, int> SFINAE_Dummy = 0> + template <class ADummy = A, EnableMatrix<ADummy, int> = 0> static void addProduct(A& a, const B& b, const C& c) { sparseRangeFor(c, [&](auto&& ci, auto && i) { sparseRangeFor(ci, [&](auto&& cij, auto && j) { @@ -342,16 +326,12 @@ namespace MatrixVector { CisScalar> { typedef ScalarB B; - template < - class ADummy = A, - std::enable_if_t<!MatrixTraits<ADummy>::isMatrix, int> SFINAE_Dummy = 0> + template <class ADummy = A, DisableMatrix<ADummy, int> = 0> static void addProduct(A& a, const Scalar& scalar, const B& b, const C& c) { a.axpy(scalar * b, c); } - template < - class ADummy = A, - std::enable_if_t<MatrixTraits<ADummy>::isMatrix, int> SFINAE_Dummy = 0> + template <class ADummy = A, EnableMatrix<ADummy, int> = 0> static void addProduct(A& a, const Scalar& scalar, const B& b, const C& c) { sparseRangeFor(c, [&](auto&& ci, auto&& i) { sparseRangeFor(ci, [&](auto&& cij, auto&& j) { @@ -398,6 +378,7 @@ namespace MatrixVector { } }; -} -} -#endif +} // end namespace MatrixVector +} // end namespace Dune + +#endif // DUNE_MATRIX_VECTOR_AXPY_HH diff --git a/dune/matrix-vector/axy.hh b/dune/matrix-vector/axy.hh index c626b128dd023d6a2cdfc0f06a98dd8f038cce87..016ae8a94b42d0c8a922a0e3a707c15201983a6d 100644 --- a/dune/matrix-vector/axy.hh +++ b/dune/matrix-vector/axy.hh @@ -3,17 +3,21 @@ #include <cassert> -#include "axpy.hh" -#include "matrixtraits.hh" -#include "algorithm.hh" +#include <dune/matrix-vector/algorithm.hh> +#include <dune/matrix-vector/axpy.hh> +#include <dune/matrix-vector/traits/utilities.hh> namespace Dune { namespace MatrixVector { + /** \brief Internal helper class for Matrix operations * */ - template <class OperatorType, bool isMatrix> - struct OperatorHelper { + template <class T, typename Enable = void> + struct OperatorHelper; + + template <class OperatorType> + struct OperatorHelper<OperatorType, DisableMatrix<OperatorType>> { template <class VectorType, class VectorType2> static typename VectorType::field_type Axy(const OperatorType &A, const VectorType &x, @@ -36,7 +40,7 @@ namespace MatrixVector { }; template <class MatrixType> - struct OperatorHelper<MatrixType, true> { + struct OperatorHelper<MatrixType, EnableMatrix<MatrixType>> { template <class VectorType, class VectorType2> static typename VectorType::field_type Axy(const MatrixType &A, const VectorType &x, @@ -90,8 +94,7 @@ namespace MatrixVector { typename VectorType::field_type Axy(const OperatorType &A, const VectorType &x, const VectorType2 &y) { - return OperatorHelper<OperatorType, - MatrixTraits<OperatorType>::isMatrix>::Axy(A, x, y); + return OperatorHelper<OperatorType>::Axy(A, x, y); } //! Compute \f$(b-Ax,y)\f$ @@ -100,11 +103,10 @@ namespace MatrixVector { const VectorType2 &b, const VectorType &x, const VectorType2 &y) { - return OperatorHelper<OperatorType, - MatrixTraits<OperatorType>::isMatrix>::bmAxy(A, b, x, - y); + return OperatorHelper<OperatorType>::bmAxy(A, b, x, y); } -} -} -#endif +} // end namespace MatrixVector +} // end namespace Dune + +#endif // DUNE_MATRIX_VECTOR_AXY_HH diff --git a/dune/matrix-vector/genericvectortools.hh b/dune/matrix-vector/genericvectortools.hh index 9b29545930305792d4b9c9bb242e284662c8d5fb..62bf76d337cf49e5610df237eff21e8997211b30 100644 --- a/dune/matrix-vector/genericvectortools.hh +++ b/dune/matrix-vector/genericvectortools.hh @@ -9,14 +9,11 @@ #include <iostream> #include <vector> -#include <dune/common/classname.hh> -#include <dune/common/concept.hh> #include <dune/common/fvector.hh> #include <dune/common/hybridutilities.hh> -#include <dune/common/typetraits.hh> #include <dune/matrix-vector/algorithm.hh> -#include <dune/matrix-vector/matrixtraits.hh> +#include <dune/matrix-vector/traits/utilities.hh> //! \brief Various tools for working with istl vectors of arbitrary nesting depth namespace Dune { @@ -51,8 +48,7 @@ namespace Impl { //! recursion helper for scalars nested in iterables (vectors) template <class Vector> -struct ScalarSwitch<Vector, - typename std::enable_if_t<not IsNumber<Vector>::value>> { +struct ScalarSwitch<Vector, DisableNumber<Vector>> { static void writeBinary(std::ostream& s, const Vector& v) { Hybrid::forEach(v, [&s](auto&& vi) { Generic::writeBinary(s, vi); }); @@ -72,8 +68,7 @@ struct ScalarSwitch<Vector, //! recursion anchors for scalars template <class Scalar> -struct ScalarSwitch<Scalar, - typename std::enable_if_t<IsNumber<Scalar>::value>> { +struct ScalarSwitch<Scalar, EnableNumber<Scalar>> { static void writeBinary(std::ostream& s, const Scalar& v) { s.write(reinterpret_cast<const char*>(&v), sizeof(Scalar)); diff --git a/dune/matrix-vector/resize.hh b/dune/matrix-vector/resize.hh index 41ec988465709636321835f112575b7333cf9e94..95c77e6d74a0c3ac5c699cb69c4aee41d808d20e 100644 --- a/dune/matrix-vector/resize.hh +++ b/dune/matrix-vector/resize.hh @@ -12,8 +12,7 @@ #include <dune/matrix-vector/algorithm.hh> #include <dune/matrix-vector/concepts.hh> -#include <dune/matrix-vector/matrixtraits.hh> -#include <dune/matrix-vector/traitutilities.hh> +#include <dune/matrix-vector/traits/utilities.hh> namespace Dune { namespace MatrixVector { diff --git a/dune/matrix-vector/test/resizetest.cc b/dune/matrix-vector/test/resizetest.cc index 59bfc291a7f499a7ae4153660abb95db85204490..bdb6923b1d7a5a7bf455c74cfe611894867a914d 100644 --- a/dune/matrix-vector/test/resizetest.cc +++ b/dune/matrix-vector/test/resizetest.cc @@ -17,7 +17,6 @@ #include <dune/matrix-vector/algorithm.hh> #include <dune/matrix-vector/resize.hh> - /// custom multitype types to allow for BlockVector nesting template <class... Args> struct CustomMultiTypeBlockVector : public Dune::MultiTypeBlockVector<Args...> { @@ -28,19 +27,19 @@ struct CustomMultiTypeBlockMatrix : public Dune::MultiTypeBlockMatrix<Args...> { constexpr static int blocklevel = 1; // fake value }; // inject matrix traits for CustomMultiTypeBlockMatrix -namespace Dune { namespace MatrixVector { +namespace Dune { namespace MatrixVector { namespace Traits { template <class... Args> struct MatrixTraits<CustomMultiTypeBlockMatrix<Args...>> { constexpr static bool isMatrix = true; }; -}} +}}} // inject vector identification trait for CustomMultiTypeBlockVector -namespace Dune { namespace MatrixVector { +namespace Dune { namespace MatrixVector { namespace Traits { template <class... Args> struct VectorTraits<CustomMultiTypeBlockVector<Args...>> { constexpr static bool isVector = true; }; -}} +}}} class ResizeTestSuite { diff --git a/dune/matrix-vector/transformmatrix.hh b/dune/matrix-vector/transformmatrix.hh index 3a81411771a92e59069b6aaaac38ebadf4476539..35df78280e82f98eec1b277dfeb97f54f19e9ab9 100644 --- a/dune/matrix-vector/transformmatrix.hh +++ b/dune/matrix-vector/transformmatrix.hh @@ -3,11 +3,12 @@ #include <dune/common/diagonalmatrix.hh> #include <dune/common/fmatrix.hh> + #include <dune/istl/matrixindexset.hh> #include <dune/istl/scaledidmatrix.hh> -#include "axpy.hh" -#include "scalartraits.hh" +#include <dune/matrix-vector/axpy.hh> +#include <dune/matrix-vector/traits/utilities.hh> namespace Dune { namespace MatrixVector { @@ -255,12 +256,8 @@ namespace MatrixVector { const MatrixB& B, const TransformationMatrix2& T2) { TransformMatrixHelper< MatrixA, TransformationMatrix1, MatrixB, TransformationMatrix2, - ScalarTraits<MatrixA>::isScalar, - ScalarTraits<TransformationMatrix1>::isScalar, - ScalarTraits<MatrixB>::isScalar, - ScalarTraits< - TransformationMatrix2>::isScalar>::addTransformedMatrix(A, T1, B, - T2); + isScalar<MatrixA>(), isScalar<TransformationMatrix1>(), isScalar<MatrixB>(), + isScalar<TransformationMatrix2>()>::addTransformedMatrix(A, T1, B, T2); } template <class MatrixA, class TransformationMatrix1, class MatrixB, @@ -270,12 +267,8 @@ namespace MatrixVector { A = 0; TransformMatrixHelper< MatrixA, TransformationMatrix1, MatrixB, TransformationMatrix2, - ScalarTraits<MatrixA>::isScalar, - ScalarTraits<TransformationMatrix1>::isScalar, - ScalarTraits<MatrixB>::isScalar, - ScalarTraits< - TransformationMatrix2>::isScalar>::addTransformedMatrix(A, T1, B, - T2); + isScalar<MatrixA>(), isScalar<TransformationMatrix1>(), isScalar<MatrixB>(), + isScalar<TransformationMatrix2>()>::addTransformedMatrix(A, T1, B, T2); } template <class MatrixBlockA, class TransformationMatrix1, class MatrixB,