Skip to content
Snippets Groups Projects
Verified Commit ef6e2391 authored by René Fritze's avatar René Fritze
Browse files

[random] provides a DefaultRNG for most vector types

parent 25939173
No related branches found
No related tags found
No related merge requests found
......@@ -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
{
......
......@@ -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
......
......@@ -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
{
......
// 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);
}
__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}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment