diff --git a/dune/matrix-vector/CMakeLists.txt b/dune/matrix-vector/CMakeLists.txt index ce19f4ce370a5654b850f495fa96e2604feb7de0..fb8e1171a8b2f60152290a54546234f796b7f36b 100644 --- a/dune/matrix-vector/CMakeLists.txt +++ b/dune/matrix-vector/CMakeLists.txt @@ -1,4 +1,5 @@ add_subdirectory(test) +add_subdirectory(traits) #install headers install(FILES @@ -12,9 +13,7 @@ install(FILES crossproduct.hh genericvectortools.hh ldlt.hh - matrixtraits.hh promote.hh - scalartraits.hh singlenonzerocolumnmatrix.hh singlenonzerorowmatrix.hh tranpose.hh 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/matrixtraits.hh b/dune/matrix-vector/matrixtraits.hh index 73c3c0d9860dae48eadb7e72be4dd752756cc0bc..bd2b4b13d27262c9df3f280838c5432fae1d5664 100644 --- a/dune/matrix-vector/matrixtraits.hh +++ b/dune/matrix-vector/matrixtraits.hh @@ -1,62 +1,20 @@ #ifndef DUNE_MATRIX_VECTOR_MATRIXTRAITS_HH #define DUNE_MATRIX_VECTOR_MATRIXTRAITS_HH -#include <dune/common/diagonalmatrix.hh> -#include <dune/common/fmatrix.hh> -#include <dune/istl/bcrsmatrix.hh> -#include <dune/istl/multitypeblockmatrix.hh> -#include <dune/istl/scaledidmatrix.hh> +#include <dune/matrix-vector/traits/matrixtraits.hh> -namespace Dune { - namespace MatrixVector { +#warning \ + This file is deprecated and might vanish soon. \ + Please use Dune::MatrixVector::Traits::MatrixTraits \ + from <dune/matrix-vector/traits/matrixtraits.hh> instead \ + or the trait utilities in namespace Dune::MatrixVector \ + to be included via <dune/matrix-vector/traits/utilities.hh>. - /** \brief Class to identify matrix types and extract information - * - * Specialize this class for all types that can be used like a matrix. - */ - template<class T> - struct MatrixTraits - { - constexpr static bool isMatrix = false; - constexpr static int rows = -1; - constexpr static int cols = -1; - }; +namespace Dune { namespace MatrixVector { - template<class T, int n, int m> - struct MatrixTraits<Dune::FieldMatrix<T,n,m> > - { - constexpr static bool isMatrix = true; - constexpr static int rows = n; - constexpr static int cols = m; - }; +template <class T> +using MatrixTraits = Traits::MatrixTraits<T>; - template<class T, int n> - struct MatrixTraits<Dune::DiagonalMatrix<T,n> > - { - constexpr static bool isMatrix = true; - constexpr static int rows = n; - constexpr static int cols = n; - }; +}} - template<class T, int n> - struct MatrixTraits<Dune::ScaledIdentityMatrix<T,n> > - { - constexpr static bool isMatrix = true; - constexpr static int rows = n; - constexpr static int cols = n; - }; - - template<class T> - struct MatrixTraits<Dune::BCRSMatrix<T> > - { - constexpr static bool isMatrix = true; - }; - - template<class... T> - struct MatrixTraits<MultiTypeBlockMatrix<T...> > - { - constexpr static bool isMatrix = true; - }; - } -} -#endif +#endif // DUNE_MATRIX_VECTOR_MATRIXTRAITS_HH 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/scalartraits.hh b/dune/matrix-vector/scalartraits.hh index 66b47c3bf0f964893f9d7fd99f35fba7ede9eb9f..2c4285aeed5c28219fb53fdebdf93c622c18a110 100644 --- a/dune/matrix-vector/scalartraits.hh +++ b/dune/matrix-vector/scalartraits.hh @@ -1,43 +1,20 @@ #ifndef DUNE_MATRIX_VECTOR_SCALARTRAITS_HH #define DUNE_MATRIX_VECTOR_SCALARTRAITS_HH -#include <dune/common/diagonalmatrix.hh> -#include <dune/common/fmatrix.hh> -#include <dune/common/typetraits.hh> -#include <dune/istl/bcrsmatrix.hh> -#include <dune/istl/scaledidmatrix.hh> +#include <dune/matrix-vector/traits/scalartraits.hh> -namespace Dune { -namespace MatrixVector { - /** \brief Class to identify scalar types - * - * Specialize this class for all types that can be used - * like scalar quantities. - */ - template <class T> - struct ScalarTraits { - constexpr static bool isScalar = Dune::IsNumber<T>::value; - }; +#warning \ + This file is deprecated and might vanish soon. \ + Please use Dune::MatrixVector::Traits::ScalarTraits \ + from <dune/matrix-vector/traits/scalartraits.hh> instead \ + or the trait utilities in namespace Dune::MatrixVector \ + to be included via <dune/matrix-vector/traits/utilities.hh>. - template <class T> - struct ScalarTraits<Dune::FieldVector<T, 1>> { - constexpr static bool isScalar = true; - }; +namespace Dune { namespace MatrixVector { - template <class T> - struct ScalarTraits<Dune::FieldMatrix<T, 1, 1>> { - constexpr static bool isScalar = true; - }; +template <class T> +using ScalarTraits = Traits::ScalarTraits<T>; - template <class T> - struct ScalarTraits<Dune::DiagonalMatrix<T, 1>> { - constexpr static bool isScalar = true; - }; +}} - template <class T> - struct ScalarTraits<Dune::ScaledIdentityMatrix<T, 1>> { - constexpr static bool isScalar = true; - }; -} -} -#endif +#endif // DUNE_MATRIX_VECTOR_SCALARTRAITS_HH 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/traits/CMakeLists.txt b/dune/matrix-vector/traits/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..fceabef21c8f600ea8b1cfcfa254d4fc2aa599d3 --- /dev/null +++ b/dune/matrix-vector/traits/CMakeLists.txt @@ -0,0 +1,8 @@ +#install headers +install(FILES + fieldtraits.hh + matrixtraits.hh + scalartraits.hh + utilities.hh + vectortraits.hh + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/dune/matrix-vector/traits) diff --git a/dune/matrix-vector/traits/fieldtraits.hh b/dune/matrix-vector/traits/fieldtraits.hh new file mode 100644 index 0000000000000000000000000000000000000000..b3bd743dfb18002ff02d2034f3c2730002688011 --- /dev/null +++ b/dune/matrix-vector/traits/fieldtraits.hh @@ -0,0 +1,22 @@ +#ifndef DUNE_MATRIX_VECTOR_TRAITS_FIELDTRAITS_HH +#define DUNE_MATRIX_VECTOR_TRAITS_FIELDTRAITS_HH + +#include <dune/common/typetraits.hh> +#include <dune/istl/scaledidmatrix.hh> + +/** + * \file + * Add FieldTraits that are not in the core modules. + */ + +namespace Dune { + +template <class K, int n> +struct FieldTraits<ScaledIdentityMatrix<K, n>> { + using field_type = field_t<K>; + using real_type = real_t<K>; +}; + +} // end namespace Dune + +#endif // DUNE_MATRIX_VECTOR_TRAITS_FIELDTRAITS_HH diff --git a/dune/matrix-vector/traits/matrixtraits.hh b/dune/matrix-vector/traits/matrixtraits.hh new file mode 100644 index 0000000000000000000000000000000000000000..ead971ed7948de79f066d285f6f37362c362d63c --- /dev/null +++ b/dune/matrix-vector/traits/matrixtraits.hh @@ -0,0 +1,59 @@ +#ifndef DUNE_MATRIX_VECTOR_TRAITS_MATRIXTRAITS_HH +#define DUNE_MATRIX_VECTOR_TRAITS_MATRIXTRAITS_HH + +#include <dune/common/diagonalmatrix.hh> +#include <dune/common/fmatrix.hh> + +#include <dune/istl/bcrsmatrix.hh> +#include <dune/istl/multitypeblockmatrix.hh> +#include <dune/istl/scaledidmatrix.hh> + +namespace Dune { +namespace MatrixVector { +namespace Traits { + +/** \brief Class to identify matrix types and extract information + * + * Specialize this class for all types that can be used like a matrix. + */ +template<class T> +struct MatrixTraits { + constexpr static bool isMatrix = false; +}; + +template<class T, int n, int m> +struct MatrixTraits<FieldMatrix<T, n, m>> { + constexpr static bool isMatrix = true; + constexpr static int rows = n; + constexpr static int cols = m; +}; + +template<class T, int n> +struct MatrixTraits<DiagonalMatrix<T, n>> { + constexpr static bool isMatrix = true; + constexpr static int rows = n; + constexpr static int cols = n; +}; + +template<class T, int n> +struct MatrixTraits<ScaledIdentityMatrix<T, n>> { + constexpr static bool isMatrix = true; + constexpr static int rows = n; + constexpr static int cols = n; +}; + +template<class T> +struct MatrixTraits<BCRSMatrix<T>> { + constexpr static bool isMatrix = true; +}; + +template<class... T> +struct MatrixTraits<MultiTypeBlockMatrix<T...> > { + constexpr static bool isMatrix = true; +}; + +} // end namespace Traits +} // end namespace MatrixVector +} // end namespace Dune + +#endif // DUNE_MATRIX_VECTOR_TRAITS_MATRIXTRAITS_HH diff --git a/dune/matrix-vector/traits/scalartraits.hh b/dune/matrix-vector/traits/scalartraits.hh new file mode 100644 index 0000000000000000000000000000000000000000..2c284b4256addf2cf6b609e6258581840bf80042 --- /dev/null +++ b/dune/matrix-vector/traits/scalartraits.hh @@ -0,0 +1,48 @@ +#ifndef DUNE_MATRIX_VECTOR_TRAITS_SCALARTRAITS_HH +#define DUNE_MATRIX_VECTOR_TRAITS_SCALARTRAITS_HH + +#include <dune/common/diagonalmatrix.hh> +#include <dune/common/fmatrix.hh> +#include <dune/common/typetraits.hh> +#include <dune/istl/bcrsmatrix.hh> +#include <dune/istl/scaledidmatrix.hh> + +namespace Dune { +namespace MatrixVector { +namespace Traits { + +/** \brief Class to identify scalar types + * + * Specialize this class for all types that can be used + * like scalar quantities. + */ +template <class T> +struct ScalarTraits { + constexpr static bool isScalar = Dune::IsNumber<T>::value; +}; + +template <class T> +struct ScalarTraits<Dune::FieldVector<T, 1>> { + constexpr static bool isScalar = true; +}; + +template <class T> +struct ScalarTraits<Dune::FieldMatrix<T, 1, 1>> { + constexpr static bool isScalar = true; +}; + +template <class T> +struct ScalarTraits<Dune::DiagonalMatrix<T, 1>> { + constexpr static bool isScalar = true; +}; + +template <class T> +struct ScalarTraits<Dune::ScaledIdentityMatrix<T, 1>> { + constexpr static bool isScalar = true; +}; + +} // end namespace Traits +} // end namespace MatrixVector +} // end namespace Dune + +#endif // DUNE_MATRIX_VECTOR_TRAITS_SCALARTRAITS_HH diff --git a/dune/matrix-vector/traits/utilities.hh b/dune/matrix-vector/traits/utilities.hh new file mode 100644 index 0000000000000000000000000000000000000000..ecc17b580b11f45ae2cf34562a092288fa0a44ef --- /dev/null +++ b/dune/matrix-vector/traits/utilities.hh @@ -0,0 +1,85 @@ +#ifndef DUNE_MATRIX_VECTOR_UTILITIES_HH +#define DUNE_MATRIX_VECTOR_UTILITIES_HH + +#include <type_traits> + +#include <dune/common/std/type_traits.hh> +#include <dune/common/typetraits.hh> + +#include <dune/matrix-vector/concepts.hh> +#include <dune/matrix-vector/traits/scalartraits.hh> +#include <dune/matrix-vector/traits/matrixtraits.hh> +#include <dune/matrix-vector/traits/vectortraits.hh> + +namespace Dune { +namespace MatrixVector { + +// convenience compile-time functions to classify types +template <class T> +constexpr auto isNumber() { + return Std::bool_constant<IsNumber<std::decay_t<T>>::value>(); +} + +template <class T> +constexpr auto isScalar() { + return Std::bool_constant<Traits::ScalarTraits<std::decay_t<T>>::isScalar>(); +} + +template <class T> +constexpr auto isVector() { + return Std::bool_constant<Traits::VectorTraits<std::decay_t<T>>::isVector>(); +} + +template <class T> +constexpr auto isMatrix() { + return Std::bool_constant<Traits::MatrixTraits<std::decay_t<T>>::isMatrix>(); +} + +template <class T> +constexpr auto isTupleOrDerived() { + return Std::bool_constant<IsTupleOrDerived<std::decay_t<T>>::value>(); +} + +// enable_if typedefs for traits ... +template <class T, class R = void> +using EnableNumber = std::enable_if_t<isNumber<T>(), R>; + +template <class T, class R = void> +using EnableScalar = std::enable_if_t<isScalar<T>(), R>; + +template <class T, class R = void> +using EnableVector = std::enable_if_t<isVector<T>(), R>; + +template <class T, class R = void> +using EnableMatrix = std::enable_if_t<isMatrix<T>(), R>; + +template <class T, class R = void> +using EnableTupleOrDerived = std::enable_if_t<isTupleOrDerived<T>(), R>; + +// ... and their negations +template <class T, class R = void> +using DisableNumber = std::enable_if_t<not isNumber<T>(), R>; + +template <class T, class R = void> +using DisableScalar = std::enable_if_t<not isScalar<T>(), R>; + +template <class T, class R = void> +using DisableVector = std::enable_if_t<not isVector<T>(), R>; + +template <class T, class R = void> +using DisableMatrix = std::enable_if_t<not isMatrix<T>(), R>; + +template <class T, class R = void> +using DisableTupleOrDerived = std::enable_if_t<not isTupleOrDerived<T>(), R>; + +// compile-time property checks +template <class T> +constexpr auto isSparseRangeIterable() { + return Std::bool_constant<isTupleOrDerived<T>() or + models<Concept::HasBegin, T>()>(); +} + +} // end namespace MatrixVector +} // end namespace Dune + +#endif // DUNE_MATRIX_VECTOR_TRAITS_UTILITIES_HH diff --git a/dune/matrix-vector/vectortraits.hh b/dune/matrix-vector/traits/vectortraits.hh similarity index 77% rename from dune/matrix-vector/vectortraits.hh rename to dune/matrix-vector/traits/vectortraits.hh index e505a7e59ea5228ada12e72625b2ce8018c15293..a58b7c19eb48846ddfef893c5f546f3f868a50bd 100644 --- a/dune/matrix-vector/vectortraits.hh +++ b/dune/matrix-vector/traits/vectortraits.hh @@ -1,5 +1,5 @@ -#ifndef DUNE_MATRIX_VECTOR_VECTORTRAITS_HH -#define DUNE_MATRIX_VECTOR_VECTORTRAITS_HH +#ifndef DUNE_MATRIX_VECTOR_TRAITS_VECTORTRAITS_HH +#define DUNE_MATRIX_VECTOR_TRAITS_VECTORTRAITS_HH #include <dune/common/fvector.hh> #include <dune/istl/bvector.hh> @@ -7,6 +7,7 @@ namespace Dune { namespace MatrixVector { +namespace Traits { /** \brief Class to identify vector types and extract information * @@ -27,7 +28,8 @@ struct VectorTraits<BlockVector<Block>> { constexpr static bool isVector = true; }; +} // end namespace Traits } // end namespace MatrixVector } // end namespace Dune -#endif // DUNE_MATRIX_VECTOR_VECTORTRAITS_HH +#endif // DUNE_MATRIX_VECTOR_TRAITS_VECTORTRAITS_HH diff --git a/dune/matrix-vector/traitutilities.hh b/dune/matrix-vector/traitutilities.hh deleted file mode 100644 index 7be19506cc9669e04bcc31ceccdf9c05459b3fc9..0000000000000000000000000000000000000000 --- a/dune/matrix-vector/traitutilities.hh +++ /dev/null @@ -1,79 +0,0 @@ -#ifndef DUNE_MATRIX_VECTOR_TRAITUTILITIES_HH -#define DUNE_MATRIX_VECTOR_TRAITUTILITIES_HH - -#include <type_traits> - -#include <dune/common/std/type_traits.hh> -#include <dune/common/typetraits.hh> - -#include <dune/matrix-vector/concepts.hh> -#include <dune/matrix-vector/scalartraits.hh> -#include <dune/matrix-vector/matrixtraits.hh> -#include <dune/matrix-vector/vectortraits.hh> - -namespace Dune { -namespace MatrixVector { - -// convenience compile-time functions to classify types -template <class T> -constexpr auto isNumber() { - return Std::bool_constant<IsNumber<T>::value>(); -} - -template <class T> -constexpr auto isScalar() { - return Std::bool_constant<ScalarTraits<T>::isScalar>(); -} - -template <class T> -constexpr auto isVector() { - return Std::bool_constant<VectorTraits<T>::isVector>(); -} - -template <class T> -constexpr auto isMatrix() { - return Std::bool_constant<MatrixTraits<T>::isMatrix>(); -} - -template <class T> -constexpr auto isTupleOrDerived() { - return Std::bool_constant<IsTupleOrDerived<std::decay_t<T>>::value>(); -} - -// enable_if typedefs for traits ... -template <class T> -using EnableNumber = std::enable_if_t<isNumber<T>()>; - -template <class T> -using EnableScalar = std::enable_if_t<isScalar<T>()>; - -template <class T> -using EnableVector = std::enable_if_t<isVector<T>()>; - -template <class T> -using EnableMatrix = std::enable_if_t<isMatrix<T>()>; - -// ... and their negations -template <class T> -using DisableNumber = std::enable_if_t<not isNumber<T>()>; - -template <class T> -using DisableScalar = std::enable_if_t<not isScalar<T>()>; - -template <class T> -using DisableVector = std::enable_if_t<not isVector<T>()>; - -template <class T> -using DisableMatrix = std::enable_if_t<not isMatrix<T>()>; - -// compile-time property checks -template <class T> -constexpr auto isSparseRangeIterable() { - return Std::bool_constant<isTupleOrDerived<T>() or - models<Concept::HasBegin, T>()>(); -} - -} // end namespace MatrixVector -} // end namespace Dune - -#endif // DUNE_MATRIX_VECTOR_TRAITUTILITIES_HH 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,