diff --git a/dune/xt/common/random.hh b/dune/xt/common/random.hh index 9fa5ed875d3e98458ddd9ced0a18777f35c7ebfc..f6340c7707cb8dee9121a81a4ca6de141ee8b736 100644 --- a/dune/xt/common/random.hh +++ b/dune/xt/common/random.hh @@ -19,6 +19,8 @@ #include <boost/assign/list_of.hpp> #include <boost/numeric/conversion/cast.hpp> +#include <dune/xt/common/vector.hh> + namespace Dune { namespace XT { namespace Common { @@ -115,7 +117,7 @@ public: }; //! defaultrng with choice of uniform distribution and stl's default random engine based on T and its numeric_limits -template <class T> +template <class T, bool = is_vector<T>::value> class DefaultRNG : public RNG<T, typename UniformDistributionSelector<T>::type, std::default_random_engine> { typedef RNG<T, typename UniformDistributionSelector<T>::type, std::default_random_engine> BaseType; @@ -129,7 +131,7 @@ public: }; template <class T> -class DefaultRNG<std::complex<T>> +class DefaultRNG<std::complex<T>, false> : public RNG<std::complex<T>, typename UniformDistributionSelector<T>::type, std::default_random_engine> { typedef RNG<std::complex<T>, typename UniformDistributionSelector<T>::type, std::default_random_engine> BaseType; @@ -142,6 +144,38 @@ public: } }; +template <class VectorType> +class DefaultRNG<VectorType, true> +{ + typedef typename VectorAbstraction<VectorType>::S T; + typedef DefaultRNG<T> RngType; + +public: + DefaultRNG(VectorType min_vec = VectorType(std::numeric_limits<T>::min()), + VectorType max_vec = VectorType(std::numeric_limits<T>::max()), + VectorType seed_vec = VectorType(std::random_device()())) + { + std::size_t idx = 0; + for (auto&& min : min_vec) { + rngs_.emplace_back(min, max_vec[idx], seed_vec[idx]); + ++idx; + } + } + inline VectorType operator()() + { + VectorType result_vec = VectorAbstraction<VectorType>::create(rngs_.size()); + std::size_t idx = 0; + for (auto&& res : result_vec) { + res = rngs_.at(idx)(); + ++idx; + } + return result_vec; + } + +private: + std::vector<RngType> rngs_; +}; + template <> class DefaultRNG<std::string> : public RandomStrings { diff --git a/dune/xt/common/test/common.hh b/dune/xt/common/test/common.hh index e08330ffdb27350d7df28fedecf30e72d626013d..3dec3a871a648eb481787a896c34c8456509d160 100644 --- a/dune/xt/common/test/common.hh +++ b/dune/xt/common/test/common.hh @@ -21,6 +21,7 @@ #include <dune/xt/common/convergence-study.hh> #include <dune/xt/common/compiler.hh> +#include <dune/xt/common/vector.hh> template <template <class> class Test> struct TestRunner @@ -78,6 +79,17 @@ void print_collected_eoc_study_results(const std::map<std::string, std::vector<d // returns unsigned int on purpose, see GridProvider unsigned int grid_elements(); +template <typename T> +static typename std::enable_if<Common::is_vector<T>::value, T>::type init_bound(int val) +{ + const auto size = Common::VectorAbstraction<T>::has_static_size ? Common::VectorAbstraction<T>::static_size : 3u; + return Common::VectorAbstraction<T>::create(size, val); +} +template <typename T> +static typename std::enable_if<!Common::is_vector<T>::value, T>::type init_bound(int val) +{ + return T(val); +} } // namespace Test } // namespace XT } // namespace Dune diff --git a/dune/xt/common/test/math.cc b/dune/xt/common/test/math.cc index 38ea3532914bf61633350399513e112c33821dc8..68e072b57138662b3bc10874ffcc233221aa65bd 100644 --- a/dune/xt/common/test/math.cc +++ b/dune/xt/common/test/math.cc @@ -19,21 +19,12 @@ #include <dune/xt/common/ranges.hh> using namespace Dune::XT::Common; +using namespace Dune::XT::Test; typedef testing::Types<double, int, Dune::FieldVector<double, 3>, std::vector<double>> ClampTestTypes; typedef testing::Types<double, int> TestTypes; typedef testing::Types<std::complex<double>, double, int> ComplexTestTypes; -template <typename T> -static typename std::enable_if<is_vector<T>::value, T>::type init_bound(int val) -{ - const auto size = VectorAbstraction<T>::has_static_size ? VectorAbstraction<T>::static_size : 3u; - return VectorAbstraction<T>::create(size, val); -} -template <typename T> -static typename std::enable_if<!is_vector<T>::value, T>::type init_bound(int val) -{ - return T(val); -} + template <class T> struct ClampTest : public testing::Test { diff --git a/dune/xt/common/test/random.cc b/dune/xt/common/test/random.cc new file mode 100644 index 0000000000000000000000000000000000000000..0da9ff3b8a85b2d46c8fb82e83868b186250a60a --- /dev/null +++ b/dune/xt/common/test/random.cc @@ -0,0 +1,43 @@ +// This file is part of the dune-xt-common project: +// https://github.com/dune-community/dune-xt-common +// The copyright lies with the authors of this file (see below). +// License: Dual licensed as BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause) +// or GPL-2.0+ (http://opensource.org/licenses/gpl-license) +// with "runtime exception" (http://www.dune-project.org/license.html) +// Authors: +// Felix Schindler (2014 - 2016) +// Rene Milk (2012 - 2015) +// Tobias Leibner (2014) + +#include <dune/xt/common/test/main.hxx> + +#include <vector> + +#include <dune/common/dynvector.hh> +#include <dune/common/densevector.hh> + +#include <dune/xt/common/random.hh> +#include <dune/xt/common/float_cmp.hh> + +using namespace Dune::XT::Common; +using namespace Dune::XT::Test; +using namespace std; + +TEST(Init, Random) +{ + typedef RNG_FIELD_TYPE T; + typedef DefaultRNG<T> RNG; +// typedef typename VectorAbstraction<T>::S Scalar; + RNG rng1; + RNG rng2(init_bound<T>(1)); + RNG rng3(init_bound<T>(1),init_bound<T>(2)); + RNG rng4(init_bound<T>(1),init_bound<T>(2),init_bound<T>(0)); + for(auto& r : std::array<RNG, 4>{{rng1, rng2, rng3, rng4}}) { + auto DXTC_UNUSED(val) = r(); + //!TODO check val + } + DefaultRNG<std::string> str_rng(2); + std::string rstr = str_rng(); + EXPECT_EQ(rstr.size(), 2); +} + diff --git a/dune/xt/common/test/random.mini b/dune/xt/common/test/random.mini new file mode 100644 index 0000000000000000000000000000000000000000..dad1df530085888441b77edfa9fbe1e605e1020e --- /dev/null +++ b/dune/xt/common/test/random.mini @@ -0,0 +1,7 @@ + +__exec_suffix = {suf_testtype} +testtype = double, int, Dune::FieldVector<double\, 3> | expand field +suf_testtype = double, int, field_vector_double_3 | expand field + +[__static] +RNG_FIELD_TYPE = {testtype}