diff --git a/bin/generate_compare_functions.py b/bin/generate_compare_functions.py new file mode 100755 index 0000000000000000000000000000000000000000..cfbb506b9cd3d01ac4e9169403f8494631c584e7 --- /dev/null +++ b/bin/generate_compare_functions.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python + +import sys +import os +from string import Template + +common_tpl = ''' +template <Style style, + class FirstType, + class SecondType, + class ToleranceType = typename MT<FirstType>::Eps> + typename std::enable_if<internal::cmp_type_check<FirstType, SecondType, ToleranceType>::value, bool>::type ${id}( + const FirstType& first, + const SecondType& second, + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& rtol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, style>::value(), + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& atol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, style>::value()) + { + return internal::Call<FirstType, SecondType, typename Dune::FloatCmp::EpsilonType<ToleranceType>::Type, style>:: + ${id}(first, second, rtol, atol); + } + + template <class FirstType, class SecondType, class ToleranceType = typename MT<FirstType>::Eps> + typename std::enable_if<internal::cmp_type_check<FirstType, SecondType, ToleranceType>::value, bool>::type ${id}( + const FirstType& first, + const SecondType& second, + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& rtol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType>::value(), + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& atol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType>::value()) + { + return ${id}<Style::defaultStyle>(first, second, rtol, atol); + } +''' + +test_tpl = ''' + template <Dune::XT::Common::FloatCmp::Style style, + class FirstType, + class SecondType, + class ToleranceType = typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps> + typename std:: + enable_if<Dune::XT::Common::FloatCmp::internal::cmp_type_check<FirstType, SecondType, ToleranceType>::value, + void>::type + DXTC_EXPECT_FLOAT_${ID}(const FirstType& expected, + const SecondType& actual, + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& rtol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, style>::value(), + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& atol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, style>::value()) + { + const auto ${id} = Dune::XT::Common::FloatCmp::internal:: + Call<FirstType, SecondType, typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps, style>::${id}; + EXPECT_PRED4(${id}, expected, actual, rtol, atol); + } + + template <class FirstType, class SecondType, class ToleranceType = typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps> + typename std::enable_if<Dune::XT::Common::FloatCmp::internal::cmp_type_check<FirstType, SecondType, ToleranceType>:: + value, + void>::type + DXTC_EXPECT_FLOAT_${ID}( + const FirstType& first, + const SecondType& second, + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& rtol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, + Dune::XT::Common::FloatCmp::Style::defaultStyle>::value(), + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& atol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, + Dune::XT::Common::FloatCmp::Style::defaultStyle>::value()) + { + DXTC_EXPECT_FLOAT_${ID}<Dune::XT::Common::FloatCmp::Style::defaultStyle>(first, second, rtol, atol); + } +''' + +bindir = os.path.dirname(os.path.abspath(__file__)) +fn_test = os.path.join(bindir, '..', 'dune', 'xt', 'common', 'test', 'float_cmp_generated.hh') +fn_common = os.path.join(bindir, '..', 'dune', 'xt', 'common', 'float_cmp_generated.hh') +cmps = ['eq', 'ne', 'gt', 'lt', 'ge', 'le'] +with open(fn_test, 'wt') as test_header, open(fn_common, 'wt') as common_header: + for name in cmps: + common_header.write(Template(common_tpl).substitute(id=name)) + test_header.write(Template(test_tpl).substitute(id=name, ID=name.upper())) diff --git a/dune/xt/common/float_cmp.hh b/dune/xt/common/float_cmp.hh index 6599dcfd94f74f4894b4d7f9bb9526aa07d08724..3d2408b17f69b9f5efcfdc20eb9796cd6c75c37e 100644 --- a/dune/xt/common/float_cmp.hh +++ b/dune/xt/common/float_cmp.hh @@ -95,42 +95,13 @@ struct DefaultEpsilon<T, Style::numpy, true> template <class V> struct MT { - typedef typename VectorAbstraction<V>::S T; + typedef typename std::conditional<Dune::XT::Common::is_matrix<V>::value, + typename Dune::XT::Common::MatrixAbstraction<V>::S, + typename Dune::XT::Common::VectorAbstraction<V>::S>::type T; typedef typename Dune::FloatCmp::EpsilonType<typename MT<V>::T>::Type Eps; }; -#define DUNE_XT_COMMON_FLOAT_CMP_GENERATOR(id) \ - template <Style style, class FirstType, class SecondType> \ - typename std::enable_if<internal::cmp_type_check<FirstType, SecondType, typename MT<FirstType>::T>::value, \ - bool>::type \ - id(const FirstType& first, \ - const SecondType& second, \ - const typename MT<FirstType>::Eps& rtol = DefaultEpsilon<typename MT<FirstType>::T, style>::value(), \ - const typename MT<FirstType>::Eps& atol = DefaultEpsilon<typename MT<FirstType>::T, style>::value()) \ - { \ - return internal::Call<FirstType, SecondType, typename MT<FirstType>::Eps, style>::id(first, second, rtol, atol); \ - } \ - \ - template <class FirstType, class SecondType> \ - typename std::enable_if<internal::cmp_type_check<FirstType, SecondType, typename MT<FirstType>::T>::value, \ - bool>::type \ - id(const FirstType& first, \ - const SecondType& second, \ - const typename MT<FirstType>::Eps& rtol = \ - DefaultEpsilon<typename MT<FirstType>::T, Style::defaultStyle>::value(), \ - const typename MT<FirstType>::Eps& atol = \ - DefaultEpsilon<typename MT<FirstType>::T, Style::defaultStyle>::value()) \ - { \ - return id<Style::defaultStyle>(first, second, rtol, atol); \ - } - -DUNE_XT_COMMON_FLOAT_CMP_GENERATOR(eq) -DUNE_XT_COMMON_FLOAT_CMP_GENERATOR(ne) -DUNE_XT_COMMON_FLOAT_CMP_GENERATOR(gt) -DUNE_XT_COMMON_FLOAT_CMP_GENERATOR(lt) -DUNE_XT_COMMON_FLOAT_CMP_GENERATOR(ge) -DUNE_XT_COMMON_FLOAT_CMP_GENERATOR(le) -#undef DUNE_XT_COMMON_FLOAT_CMP_GENERATOR +#include "float_cmp_generated.hh" } // namespace FloatCmp } // namespace Common diff --git a/dune/xt/common/float_cmp_generated.hh b/dune/xt/common/float_cmp_generated.hh new file mode 100644 index 0000000000000000000000000000000000000000..450ce76107e63be525836f111fc1b48adf92ed7d --- /dev/null +++ b/dune/xt/common/float_cmp_generated.hh @@ -0,0 +1,168 @@ + +template <Style style, + class FirstType, + class SecondType, + class ToleranceType = typename MT<FirstType>::Eps> + typename std::enable_if<internal::cmp_type_check<FirstType, SecondType, ToleranceType>::value, bool>::type eq( + const FirstType& first, + const SecondType& second, + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& rtol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, style>::value(), + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& atol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, style>::value()) + { + return internal::Call<FirstType, SecondType, typename Dune::FloatCmp::EpsilonType<ToleranceType>::Type, style>:: + eq(first, second, rtol, atol); + } + + template <class FirstType, class SecondType, class ToleranceType = typename MT<FirstType>::Eps> + typename std::enable_if<internal::cmp_type_check<FirstType, SecondType, ToleranceType>::value, bool>::type eq( + const FirstType& first, + const SecondType& second, + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& rtol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType>::value(), + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& atol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType>::value()) + { + return eq<Style::defaultStyle>(first, second, rtol, atol); + } + +template <Style style, + class FirstType, + class SecondType, + class ToleranceType = typename MT<FirstType>::Eps> + typename std::enable_if<internal::cmp_type_check<FirstType, SecondType, ToleranceType>::value, bool>::type ne( + const FirstType& first, + const SecondType& second, + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& rtol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, style>::value(), + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& atol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, style>::value()) + { + return internal::Call<FirstType, SecondType, typename Dune::FloatCmp::EpsilonType<ToleranceType>::Type, style>:: + ne(first, second, rtol, atol); + } + + template <class FirstType, class SecondType, class ToleranceType = typename MT<FirstType>::Eps> + typename std::enable_if<internal::cmp_type_check<FirstType, SecondType, ToleranceType>::value, bool>::type ne( + const FirstType& first, + const SecondType& second, + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& rtol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType>::value(), + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& atol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType>::value()) + { + return ne<Style::defaultStyle>(first, second, rtol, atol); + } + +template <Style style, + class FirstType, + class SecondType, + class ToleranceType = typename MT<FirstType>::Eps> + typename std::enable_if<internal::cmp_type_check<FirstType, SecondType, ToleranceType>::value, bool>::type gt( + const FirstType& first, + const SecondType& second, + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& rtol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, style>::value(), + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& atol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, style>::value()) + { + return internal::Call<FirstType, SecondType, typename Dune::FloatCmp::EpsilonType<ToleranceType>::Type, style>:: + gt(first, second, rtol, atol); + } + + template <class FirstType, class SecondType, class ToleranceType = typename MT<FirstType>::Eps> + typename std::enable_if<internal::cmp_type_check<FirstType, SecondType, ToleranceType>::value, bool>::type gt( + const FirstType& first, + const SecondType& second, + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& rtol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType>::value(), + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& atol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType>::value()) + { + return gt<Style::defaultStyle>(first, second, rtol, atol); + } + +template <Style style, + class FirstType, + class SecondType, + class ToleranceType = typename MT<FirstType>::Eps> + typename std::enable_if<internal::cmp_type_check<FirstType, SecondType, ToleranceType>::value, bool>::type lt( + const FirstType& first, + const SecondType& second, + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& rtol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, style>::value(), + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& atol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, style>::value()) + { + return internal::Call<FirstType, SecondType, typename Dune::FloatCmp::EpsilonType<ToleranceType>::Type, style>:: + lt(first, second, rtol, atol); + } + + template <class FirstType, class SecondType, class ToleranceType = typename MT<FirstType>::Eps> + typename std::enable_if<internal::cmp_type_check<FirstType, SecondType, ToleranceType>::value, bool>::type lt( + const FirstType& first, + const SecondType& second, + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& rtol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType>::value(), + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& atol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType>::value()) + { + return lt<Style::defaultStyle>(first, second, rtol, atol); + } + +template <Style style, + class FirstType, + class SecondType, + class ToleranceType = typename MT<FirstType>::Eps> + typename std::enable_if<internal::cmp_type_check<FirstType, SecondType, ToleranceType>::value, bool>::type ge( + const FirstType& first, + const SecondType& second, + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& rtol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, style>::value(), + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& atol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, style>::value()) + { + return internal::Call<FirstType, SecondType, typename Dune::FloatCmp::EpsilonType<ToleranceType>::Type, style>:: + ge(first, second, rtol, atol); + } + + template <class FirstType, class SecondType, class ToleranceType = typename MT<FirstType>::Eps> + typename std::enable_if<internal::cmp_type_check<FirstType, SecondType, ToleranceType>::value, bool>::type ge( + const FirstType& first, + const SecondType& second, + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& rtol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType>::value(), + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& atol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType>::value()) + { + return ge<Style::defaultStyle>(first, second, rtol, atol); + } + +template <Style style, + class FirstType, + class SecondType, + class ToleranceType = typename MT<FirstType>::Eps> + typename std::enable_if<internal::cmp_type_check<FirstType, SecondType, ToleranceType>::value, bool>::type le( + const FirstType& first, + const SecondType& second, + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& rtol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, style>::value(), + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& atol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, style>::value()) + { + return internal::Call<FirstType, SecondType, typename Dune::FloatCmp::EpsilonType<ToleranceType>::Type, style>:: + le(first, second, rtol, atol); + } + + template <class FirstType, class SecondType, class ToleranceType = typename MT<FirstType>::Eps> + typename std::enable_if<internal::cmp_type_check<FirstType, SecondType, ToleranceType>::value, bool>::type le( + const FirstType& first, + const SecondType& second, + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& rtol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType>::value(), + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& atol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType>::value()) + { + return le<Style::defaultStyle>(first, second, rtol, atol); + } diff --git a/dune/xt/common/float_cmp_internal.hh b/dune/xt/common/float_cmp_internal.hh index b53adba1d7503239946018170002e30115eee4c8..1f4c6d974cae9805c3b778e4669a7f6bc4deb510 100644 --- a/dune/xt/common/float_cmp_internal.hh +++ b/dune/xt/common/float_cmp_internal.hh @@ -39,6 +39,13 @@ struct VectorAbstraction; template <class VecType> struct is_vector; +template <class MatType> +struct MatrixAbstraction; + +template <class MatType> +struct is_matrix; + + namespace FloatCmp { namespace internal { @@ -78,6 +85,27 @@ float_cmp_eq(const XType& xx, const YType& yy, const TolType& rtol, const TolTyp return true; } // ... float_cmp(...) +template <class XType, class YType, class TolType> +typename std::enable_if<is_matrix<XType>::value && is_matrix<YType>::value && std::is_arithmetic<TolType>::value + && std::is_same<typename MatrixAbstraction<XType>::R, TolType>::value + && std::is_same<typename MatrixAbstraction<YType>::R, TolType>::value, + bool>::type +float_cmp_eq(const XType& xx, const YType& yy, const TolType& rtol, const TolType& atol) +{ + const auto rows = MatrixAbstraction<XType>::rows(xx); + const auto cols = MatrixAbstraction<XType>::cols(xx); + if (MatrixAbstraction<YType>::rows(yy) != rows || MatrixAbstraction<YType>::cols(yy) != cols) + return false; + for (size_t ii = 0; ii < rows; ++ii) + for (size_t jj = 0; jj < cols; ++jj) + if (!float_cmp_eq(MatrixAbstraction<XType>::get_entry(xx, ii, jj), + MatrixAbstraction<YType>::get_entry(yy, ii, jj), + rtol, + atol)) + return false; + return true; +} // ... float_cmp(...) + template <Dune::FloatCmp::CmpStyle style, class T> typename std::enable_if<std::is_arithmetic<T>::value, bool>::type dune_float_cmp_eq(const T& xx, const T& yy, const T& eps) @@ -109,6 +137,25 @@ dune_float_cmp_eq(const XType& xx, const YType& yy, const EpsType& eps) return true; } // ... dune_float_cmp(...) +template <Dune::FloatCmp::CmpStyle style, class XType, class YType, class EpsType> +typename std::enable_if<is_matrix<XType>::value && is_matrix<YType>::value && std::is_arithmetic<EpsType>::value + && std::is_same<typename MatrixAbstraction<XType>::R, EpsType>::value + && std::is_same<typename MatrixAbstraction<YType>::R, EpsType>::value, + bool>::type +dune_float_cmp_eq(const XType& xx, const YType& yy, const EpsType& eps) +{ + const auto rows = MatrixAbstraction<XType>::rows(xx); + const auto cols = MatrixAbstraction<XType>::cols(xx); + if (MatrixAbstraction<YType>::rows(yy) != rows || MatrixAbstraction<YType>::cols(yy) != cols) + return false; + for (size_t ii = 0; ii < rows; ++ii) + for (size_t jj = 0; jj < cols; ++jj) + if (!dune_float_cmp_eq<style>( + MatrixAbstraction<XType>::get_entry(xx, ii, jj), MatrixAbstraction<YType>::get_entry(yy, ii, jj), eps)) + return false; + return true; +} // ... float_cmp(...){ + template <class T> typename std::enable_if<std::is_arithmetic<T>::value, bool>::type cmp_gt(const T& xx, const T& yy) { @@ -138,6 +185,24 @@ cmp_gt(const XType& xx, const YType& yy) return true; } // ... cmp_gt(...) +template <class XType, class YType> +typename std::enable_if<is_matrix<XType>::value && is_matrix<YType>::value + && std::is_same<typename MatrixAbstraction<XType>::S, + typename MatrixAbstraction<YType>::S>::value, + bool>::type +cmp_gt(const XType& xx, const YType& yy) +{ + const auto rows = MatrixAbstraction<XType>::rows(xx); + const auto cols = MatrixAbstraction<XType>::cols(xx); + if (MatrixAbstraction<YType>::rows(yy) != rows || MatrixAbstraction<YType>::cols(yy) != cols) + return false; + for (size_t ii = 0; ii < rows; ++ii) + for (size_t jj = 0; jj < cols; ++jj) + if (!cmp_gt(MatrixAbstraction<XType>::get_entry(xx, ii, jj), MatrixAbstraction<YType>::get_entry(yy, ii, jj))) + return false; + return true; +} // ... cmp_gt(...) + template <class T> typename std::enable_if<std::is_arithmetic<T>::value, bool>::type cmp_lt(const T& xx, const T& yy) { @@ -167,6 +232,25 @@ cmp_lt(const XType& xx, const YType& yy) return true; } // ... cmp_lt(...) +template <class XType, class YType> +typename std::enable_if<is_matrix<XType>::value && is_matrix<YType>::value + && std::is_same<typename MatrixAbstraction<XType>::S, + typename MatrixAbstraction<YType>::S>::value, + bool>::type +cmp_lt(const XType& xx, const YType& yy) +{ + const auto rows = MatrixAbstraction<XType>::rows(xx); + const auto cols = MatrixAbstraction<XType>::cols(xx); + if (MatrixAbstraction<YType>::rows(yy) != rows || MatrixAbstraction<YType>::cols(yy) != cols) + return false; + for (size_t ii = 0; ii < rows; ++ii) + for (size_t jj = 0; jj < cols; ++jj) + if (!cmp_lt(MatrixAbstraction<XType>::get_entry(xx, ii, jj), MatrixAbstraction<YType>::get_entry(yy, ii, jj))) + return false; + return true; +} // ... cmp_gt(...) + + template <class FirstType, class SecondType, class ToleranceType, Style style> struct Call { @@ -240,10 +324,15 @@ struct cmp_type_check { static constexpr bool is_ok_scalar = (std::is_arithmetic<FirstType>::value || is_complex<FirstType>::value) && std::is_same<FirstType, SecondType>::value; - static constexpr bool is_ok_vector = is_vector<FirstType>::value && is_vector<SecondType>::value + static constexpr bool is_ok_vector = is_vector<FirstType>::value + && is_vector<SecondType>::value && std::is_same<ToleranceType, typename VectorAbstraction<FirstType>::S>::value && std::is_same<ToleranceType, typename VectorAbstraction<SecondType>::S>::value; - static constexpr bool value = is_ok_scalar || is_ok_vector; + static constexpr bool is_ok_matrix = is_matrix<FirstType>::value + && is_matrix<SecondType>::value + && std::is_same<ToleranceType, typename MatrixAbstraction<FirstType>::S>::value + && std::is_same<ToleranceType, typename MatrixAbstraction<SecondType>::S>::value; + static constexpr bool value = is_ok_scalar || is_ok_vector || is_ok_matrix; }; } // namespace internal @@ -253,5 +342,6 @@ struct cmp_type_check } // namespace Dune #include <dune/xt/common/vector.hh> +#include <dune/xt/common/matrix.hh> #endif // DUNE_XT_COMMON_FLOAT_CMP_INTERNAL_HH diff --git a/dune/xt/common/matrix.hh b/dune/xt/common/matrix.hh index 16f4640a78c71a94ee5e17a831da8ea4afa45cb0..cd2cb7e8ea23aab30ca7936b6084189127b1fe2d 100644 --- a/dune/xt/common/matrix.hh +++ b/dune/xt/common/matrix.hh @@ -39,7 +39,9 @@ struct MatrixAbstraction { typedef MatType MatrixType; typedef MatType ScalarType; + typedef MatType RealType; typedef MatType S; + typedef MatType R; static const bool is_matrix = false; @@ -85,8 +87,10 @@ template <class K> struct MatrixAbstraction<Dune::DynamicMatrix<K>> { typedef Dune::DynamicMatrix<K> MatrixType; - typedef K ScalarType; + typedef typename Dune::FieldTraits<K>::field_type ScalarType; + typedef typename Dune::FieldTraits<K>::real_type RealType; typedef ScalarType S; + typedef RealType R; static const bool is_matrix = true; @@ -131,8 +135,10 @@ template <class K, int N, int M> struct MatrixAbstraction<Dune::FieldMatrix<K, N, M>> { typedef Dune::FieldMatrix<K, N, M> MatrixType; - typedef K ScalarType; + typedef typename Dune::FieldTraits<K>::field_type ScalarType; + typedef typename Dune::FieldTraits<K>::real_type RealType; typedef ScalarType S; + typedef RealType R; static const bool is_matrix = true; @@ -185,8 +191,10 @@ template <class K, int N, int M> struct MatrixAbstraction<Dune::XT::Common::FieldMatrix<K, N, M>> { typedef Dune::XT::Common::FieldMatrix<K, N, M> MatrixType; - typedef K ScalarType; + typedef typename Dune::FieldTraits<K>::field_type ScalarType; + typedef typename Dune::FieldTraits<K>::real_type RealType; typedef ScalarType S; + typedef RealType R; static const bool is_matrix = true; @@ -235,9 +243,9 @@ struct is_matrix template <class MatrixType> typename std::enable_if<is_matrix<MatrixType>::value, MatrixType>::type -create(const size_t sz, const typename MatrixAbstraction<MatrixType>::S& val) +create(const size_t rows, const size_t cols, const typename MatrixAbstraction<MatrixType>::S& val) { - return MatrixAbstraction<MatrixType>::create(sz, val); + return MatrixAbstraction<MatrixType>::create(rows, cols, val); } } // namespace Common diff --git a/dune/xt/common/random.hh b/dune/xt/common/random.hh index c6f930fdb9c05b844f9fa13c9c9571672b587750..e786780563ad62b9d955fbfcc39f9ef6e283d0cf 100644 --- a/dune/xt/common/random.hh +++ b/dune/xt/common/random.hh @@ -124,8 +124,8 @@ class DefaultRNG : public RNG<T, typename UniformDistributionSelector<T>::type, public: explicit DefaultRNG(T min = std::numeric_limits<T>::min(), - T max = std::numeric_limits<T>::max(), - T seed = std::random_device()()) + T max = std::numeric_limits<T>::max(), + T seed = std::random_device()()) : BaseType(std::default_random_engine(seed), typename UniformDistributionSelector<T>::type(min, max)) { } diff --git a/dune/xt/common/test/float_cmp.cc b/dune/xt/common/test/float_cmp.cc index c9ff23d8a9389e5da749b8579722db0e156b3a2b..6837220db5683677491a61912dc5c68908717953 100644 --- a/dune/xt/common/test/float_cmp.cc +++ b/dune/xt/common/test/float_cmp.cc @@ -16,6 +16,7 @@ #include <dune/common/dynvector.hh> #include <dune/common/fvector.hh> +#include <dune/common/fmatrix.hh> #include <dune/xt/common/float_cmp.hh> #include <dune/xt/common/test/float_cmp.hh> @@ -23,8 +24,14 @@ using namespace Dune; using XT::Common::create; -using namespace XT::Common::FloatCmp; +using XT::Common::FloatCmp::Style; using XT::Common::VectorAbstraction; +using XT::Common::MatrixAbstraction; +static const Style numpy = Style::numpy; +static const Style relativeWeak = Style::relativeWeak; +static const Style relativeStrong = Style::relativeStrong; +static const Style absolute = Style::absolute; +static const Style defaultStyle = Style::defaultStyle; struct FloatCmpTest : public testing::Test { diff --git a/dune/xt/common/test/float_cmp.hh b/dune/xt/common/test/float_cmp.hh index a5f100db620ada07e6e6b330f423f4fee17a3ba8..ff03125f8aa298cb3b8f7bae0e4503d50a911da2 100644 --- a/dune/xt/common/test/float_cmp.hh +++ b/dune/xt/common/test/float_cmp.hh @@ -17,53 +17,6 @@ #include <dune/xt/common/test/gtest/gtest.h> #include <dune/xt/common/float_cmp.hh> -#define DUNE_XT_COMMON_FLOAT_TEST_CMP_GENERATOR(ID, id) \ - template <Dune::XT::Common::FloatCmp::Style style, class FirstType, class SecondType> \ - typename std::enable_if<Dune::XT::Common::FloatCmp::internal:: \ - cmp_type_check<FirstType, \ - SecondType, \ - typename Dune::XT::Common::FloatCmp::MT<FirstType>::T>::value, \ - void>::type \ - DXTC_EXPECT_FLOAT_##ID( \ - const FirstType& expected, \ - const SecondType& actual, \ - const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& rtol = \ - Dune::XT::Common::FloatCmp::DefaultEpsilon<typename Dune::XT::Common::FloatCmp::MT<FirstType>::T, \ - style>::value(), \ - const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& atol = \ - Dune::XT::Common::FloatCmp::DefaultEpsilon<typename Dune::XT::Common::FloatCmp::MT<FirstType>::T, \ - style>::value()) \ - { \ - const auto id = Dune::XT::Common::FloatCmp::internal:: \ - Call<FirstType, SecondType, typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps, style>::id; \ - EXPECT_PRED4(id, expected, actual, rtol, atol); \ - } \ - \ - template <class FirstType, class SecondType> \ - typename std::enable_if<Dune::XT::Common::FloatCmp::internal:: \ - cmp_type_check<FirstType, \ - SecondType, \ - typename Dune::XT::Common::FloatCmp::MT<FirstType>::T>::value, \ - void>::type \ - DXTC_EXPECT_FLOAT_##ID( \ - const FirstType& first, \ - const SecondType& second, \ - const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& rtol = \ - Dune::XT::Common::FloatCmp::DefaultEpsilon<typename Dune::XT::Common::FloatCmp::MT<FirstType>::T, \ - Dune::XT::Common::FloatCmp::Style::defaultStyle>::value(), \ - const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& atol = \ - Dune::XT::Common::FloatCmp::DefaultEpsilon<typename Dune::XT::Common::FloatCmp::MT<FirstType>::T, \ - Dune::XT::Common::FloatCmp::Style::defaultStyle>::value()) \ - { \ - DXTC_EXPECT_FLOAT_##ID<Dune::XT::Common::FloatCmp::Style::defaultStyle>(first, second, rtol, atol); \ - } - -DUNE_XT_COMMON_FLOAT_TEST_CMP_GENERATOR(EQ, eq) -DUNE_XT_COMMON_FLOAT_TEST_CMP_GENERATOR(NE, ne) -DUNE_XT_COMMON_FLOAT_TEST_CMP_GENERATOR(GT, gt) -DUNE_XT_COMMON_FLOAT_TEST_CMP_GENERATOR(LT, lt) -DUNE_XT_COMMON_FLOAT_TEST_CMP_GENERATOR(GE, ge) -DUNE_XT_COMMON_FLOAT_TEST_CMP_GENERATOR(LE, le) -#undef DUNE_XT_COMMON_FLOAT_TEST_CMP_GENERATOR +#include "float_cmp_generated.hh" #endif // DUNE_XT_COMMON_TEST_FLOAT_COMPARE_HH diff --git a/dune/xt/common/test/float_cmp.mini b/dune/xt/common/test/float_cmp.mini index 0c454a334fe5dfda1e20c335d92e8456e3e93131..fe0cbedaa4913d9303ad8e2a61fbb89032de4dd2 100644 --- a/dune/xt/common/test/float_cmp.mini +++ b/dune/xt/common/test/float_cmp.mini @@ -11,7 +11,7 @@ __local.default_eps = DefaultEpsilon<S>, DefaultEpsilon<S\,Style::{__local.cmpst __local.fieldtype = std::size_t, long, double, std::complex<double> | expand field __local.fieldtype_short = std_size_t, long, double, complex | expand field -__local.vectortype = std::vector<{__local.fieldtype}>, Dune::FieldVector<{__local.fieldtype}\,{__local.vec_size}>, FieldVector<{__local.fieldtype}\,{__local.vec_size}>, Dune::DynamicVector<{__local.fieldtype}> | expand vector +__local.vectortype = std::vector<{__local.fieldtype}>, Dune::FieldVector<{__local.fieldtype}\,{__local.vec_size}>, Dune::XT::Common::FieldVector<{__local.fieldtype}\,{__local.vec_size}>, Dune::DynamicVector<{__local.fieldtype}> | expand vector __local.vectortype_short = std_vector, dune_fieldvector, xt_fieldvector, dynamic_vector | expand vector __local.testtype = {__local.fieldtype}, {__local.vectortype} | expand test __local.testtype_short = {__local.fieldtype_short}, {__local.vectortype_short}_{__local.fieldtype_short} | expand test @@ -26,12 +26,12 @@ cmpstyle_is_numpy = true, {__local.cmpstyle_is_numpy} | expand cmpstyle_templ TESTTYPE = {__local.testtype} VECSIZE = {__local.vec_size} DEFAULT_EPSILON = {__local.default_eps} -FLOATCMP_EQ = eq{__local.cmpstyle_template} -FLOATCMP_NE = ne{__local.cmpstyle_template} -FLOATCMP_GT = gt{__local.cmpstyle_template} -FLOATCMP_LT = lt{__local.cmpstyle_template} -FLOATCMP_GE = ge{__local.cmpstyle_template} -FLOATCMP_LE = le{__local.cmpstyle_template} +FLOATCMP_EQ = Dune::XT::Common::FloatCmp::eq{__local.cmpstyle_template} +FLOATCMP_NE = Dune::XT::Common::FloatCmp::ne{__local.cmpstyle_template} +FLOATCMP_GT = Dune::XT::Common::FloatCmp::gt{__local.cmpstyle_template} +FLOATCMP_LT = Dune::XT::Common::FloatCmp::lt{__local.cmpstyle_template} +FLOATCMP_GE = Dune::XT::Common::FloatCmp::ge{__local.cmpstyle_template} +FLOATCMP_LE = Dune::XT::Common::FloatCmp::le{__local.cmpstyle_template} TEST_DXTC_EXPECT_FLOAT_EQ = DXTC_EXPECT_FLOAT_EQ{__local.cmpstyle_template} TEST_DXTC_EXPECT_FLOAT_NE = DXTC_EXPECT_FLOAT_NE{__local.cmpstyle_template} TEST_DXTC_EXPECT_FLOAT_GT = DXTC_EXPECT_FLOAT_GT{__local.cmpstyle_template} diff --git a/dune/xt/common/test/float_cmp_generated.hh b/dune/xt/common/test/float_cmp_generated.hh new file mode 100644 index 0000000000000000000000000000000000000000..acca8cddf0c37a983c78e08172dfcd53618abd60 --- /dev/null +++ b/dune/xt/common/test/float_cmp_generated.hh @@ -0,0 +1,216 @@ + + template <Dune::XT::Common::FloatCmp::Style style, + class FirstType, + class SecondType, + class ToleranceType = typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps> + typename std:: + enable_if<Dune::XT::Common::FloatCmp::internal::cmp_type_check<FirstType, SecondType, ToleranceType>::value, + void>::type + DXTC_EXPECT_FLOAT_EQ(const FirstType& expected, + const SecondType& actual, + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& rtol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, style>::value(), + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& atol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, style>::value()) + { + const auto eq = Dune::XT::Common::FloatCmp::internal:: + Call<FirstType, SecondType, typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps, style>::eq; + EXPECT_PRED4(eq, expected, actual, rtol, atol); + } + + template <class FirstType, class SecondType, class ToleranceType = typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps> + typename std::enable_if<Dune::XT::Common::FloatCmp::internal::cmp_type_check<FirstType, SecondType, ToleranceType>:: + value, + void>::type + DXTC_EXPECT_FLOAT_EQ( + const FirstType& first, + const SecondType& second, + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& rtol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, + Dune::XT::Common::FloatCmp::Style::defaultStyle>::value(), + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& atol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, + Dune::XT::Common::FloatCmp::Style::defaultStyle>::value()) + { + DXTC_EXPECT_FLOAT_EQ<Dune::XT::Common::FloatCmp::Style::defaultStyle>(first, second, rtol, atol); + } + + template <Dune::XT::Common::FloatCmp::Style style, + class FirstType, + class SecondType, + class ToleranceType = typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps> + typename std:: + enable_if<Dune::XT::Common::FloatCmp::internal::cmp_type_check<FirstType, SecondType, ToleranceType>::value, + void>::type + DXTC_EXPECT_FLOAT_NE(const FirstType& expected, + const SecondType& actual, + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& rtol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, style>::value(), + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& atol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, style>::value()) + { + const auto ne = Dune::XT::Common::FloatCmp::internal:: + Call<FirstType, SecondType, typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps, style>::ne; + EXPECT_PRED4(ne, expected, actual, rtol, atol); + } + + template <class FirstType, class SecondType, class ToleranceType = typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps> + typename std::enable_if<Dune::XT::Common::FloatCmp::internal::cmp_type_check<FirstType, SecondType, ToleranceType>:: + value, + void>::type + DXTC_EXPECT_FLOAT_NE( + const FirstType& first, + const SecondType& second, + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& rtol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, + Dune::XT::Common::FloatCmp::Style::defaultStyle>::value(), + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& atol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, + Dune::XT::Common::FloatCmp::Style::defaultStyle>::value()) + { + DXTC_EXPECT_FLOAT_NE<Dune::XT::Common::FloatCmp::Style::defaultStyle>(first, second, rtol, atol); + } + + template <Dune::XT::Common::FloatCmp::Style style, + class FirstType, + class SecondType, + class ToleranceType = typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps> + typename std:: + enable_if<Dune::XT::Common::FloatCmp::internal::cmp_type_check<FirstType, SecondType, ToleranceType>::value, + void>::type + DXTC_EXPECT_FLOAT_GT(const FirstType& expected, + const SecondType& actual, + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& rtol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, style>::value(), + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& atol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, style>::value()) + { + const auto gt = Dune::XT::Common::FloatCmp::internal:: + Call<FirstType, SecondType, typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps, style>::gt; + EXPECT_PRED4(gt, expected, actual, rtol, atol); + } + + template <class FirstType, class SecondType, class ToleranceType = typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps> + typename std::enable_if<Dune::XT::Common::FloatCmp::internal::cmp_type_check<FirstType, SecondType, ToleranceType>:: + value, + void>::type + DXTC_EXPECT_FLOAT_GT( + const FirstType& first, + const SecondType& second, + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& rtol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, + Dune::XT::Common::FloatCmp::Style::defaultStyle>::value(), + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& atol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, + Dune::XT::Common::FloatCmp::Style::defaultStyle>::value()) + { + DXTC_EXPECT_FLOAT_GT<Dune::XT::Common::FloatCmp::Style::defaultStyle>(first, second, rtol, atol); + } + + template <Dune::XT::Common::FloatCmp::Style style, + class FirstType, + class SecondType, + class ToleranceType = typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps> + typename std:: + enable_if<Dune::XT::Common::FloatCmp::internal::cmp_type_check<FirstType, SecondType, ToleranceType>::value, + void>::type + DXTC_EXPECT_FLOAT_LT(const FirstType& expected, + const SecondType& actual, + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& rtol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, style>::value(), + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& atol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, style>::value()) + { + const auto lt = Dune::XT::Common::FloatCmp::internal:: + Call<FirstType, SecondType, typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps, style>::lt; + EXPECT_PRED4(lt, expected, actual, rtol, atol); + } + + template <class FirstType, class SecondType, class ToleranceType = typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps> + typename std::enable_if<Dune::XT::Common::FloatCmp::internal::cmp_type_check<FirstType, SecondType, ToleranceType>:: + value, + void>::type + DXTC_EXPECT_FLOAT_LT( + const FirstType& first, + const SecondType& second, + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& rtol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, + Dune::XT::Common::FloatCmp::Style::defaultStyle>::value(), + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& atol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, + Dune::XT::Common::FloatCmp::Style::defaultStyle>::value()) + { + DXTC_EXPECT_FLOAT_LT<Dune::XT::Common::FloatCmp::Style::defaultStyle>(first, second, rtol, atol); + } + + template <Dune::XT::Common::FloatCmp::Style style, + class FirstType, + class SecondType, + class ToleranceType = typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps> + typename std:: + enable_if<Dune::XT::Common::FloatCmp::internal::cmp_type_check<FirstType, SecondType, ToleranceType>::value, + void>::type + DXTC_EXPECT_FLOAT_GE(const FirstType& expected, + const SecondType& actual, + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& rtol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, style>::value(), + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& atol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, style>::value()) + { + const auto ge = Dune::XT::Common::FloatCmp::internal:: + Call<FirstType, SecondType, typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps, style>::ge; + EXPECT_PRED4(ge, expected, actual, rtol, atol); + } + + template <class FirstType, class SecondType, class ToleranceType = typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps> + typename std::enable_if<Dune::XT::Common::FloatCmp::internal::cmp_type_check<FirstType, SecondType, ToleranceType>:: + value, + void>::type + DXTC_EXPECT_FLOAT_GE( + const FirstType& first, + const SecondType& second, + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& rtol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, + Dune::XT::Common::FloatCmp::Style::defaultStyle>::value(), + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& atol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, + Dune::XT::Common::FloatCmp::Style::defaultStyle>::value()) + { + DXTC_EXPECT_FLOAT_GE<Dune::XT::Common::FloatCmp::Style::defaultStyle>(first, second, rtol, atol); + } + + template <Dune::XT::Common::FloatCmp::Style style, + class FirstType, + class SecondType, + class ToleranceType = typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps> + typename std:: + enable_if<Dune::XT::Common::FloatCmp::internal::cmp_type_check<FirstType, SecondType, ToleranceType>::value, + void>::type + DXTC_EXPECT_FLOAT_LE(const FirstType& expected, + const SecondType& actual, + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& rtol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, style>::value(), + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& atol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, style>::value()) + { + const auto le = Dune::XT::Common::FloatCmp::internal:: + Call<FirstType, SecondType, typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps, style>::le; + EXPECT_PRED4(le, expected, actual, rtol, atol); + } + + template <class FirstType, class SecondType, class ToleranceType = typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps> + typename std::enable_if<Dune::XT::Common::FloatCmp::internal::cmp_type_check<FirstType, SecondType, ToleranceType>:: + value, + void>::type + DXTC_EXPECT_FLOAT_LE( + const FirstType& first, + const SecondType& second, + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& rtol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, + Dune::XT::Common::FloatCmp::Style::defaultStyle>::value(), + const typename Dune::XT::Common::FloatCmp::MT<FirstType>::Eps& atol = + Dune::XT::Common::FloatCmp::DefaultEpsilon<ToleranceType, + Dune::XT::Common::FloatCmp::Style::defaultStyle>::value()) + { + DXTC_EXPECT_FLOAT_LE<Dune::XT::Common::FloatCmp::Style::defaultStyle>(first, second, rtol, atol); + }