Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:40:41

0001 //  (C) Copyright Nick Thompson 2018.
0002 //  (C) Copyright Matt Borland 2021.
0003 //  Use, modification and distribution are subject to the
0004 //  Boost Software License, Version 1.0. (See accompanying file
0005 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 
0007 #include <cstddef>
0008 #include <random>
0009 #include <type_traits>
0010 #include <vector>
0011 
0012 namespace boost { namespace math {
0013 
0014 // To stress test, set global_seed = 0, global_size = huge.
0015 static constexpr std::size_t global_seed = 0;
0016 static constexpr std::size_t global_size = 128;
0017 
0018 template<typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true>
0019 std::vector<T> generate_random_vector(std::size_t size, std::size_t seed)
0020 {
0021     if (seed == 0)
0022     {
0023         std::random_device rd;
0024         seed = rd();
0025     }
0026     std::vector<T> v(size);
0027 
0028     std::mt19937_64 gen(seed);
0029 
0030     std::normal_distribution<T> dis(0, 1);
0031     for (auto& x : v)
0032     {
0033         x = dis(gen);
0034     }
0035     return v;
0036 }
0037 
0038 template<typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true>
0039 std::vector<T> generate_random_uniform_vector(std::size_t size, std::size_t seed, T lower_bound = T(0), T upper_bound = T(1))
0040 {
0041     if (seed == 0)
0042     {
0043         std::random_device rd;
0044         seed = rd();
0045     }
0046     std::vector<T> v(size);
0047 
0048     std::mt19937 gen(seed);
0049 
0050     std::uniform_real_distribution<T> dis(lower_bound, upper_bound);
0051 
0052     for (auto& x : v)
0053     {
0054         x = dis(gen);
0055     }
0056     
0057     return v;
0058 }
0059 
0060 template<typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true>
0061 std::vector<T> generate_random_vector(std::size_t size, std::size_t seed, T mean, T stddev)
0062 {
0063     if (seed == 0)
0064     {
0065         std::random_device rd;
0066         seed = rd();
0067     }
0068     std::vector<T> v(size);
0069 
0070     std::mt19937 gen(seed);
0071 
0072     std::normal_distribution<T> dis(mean, stddev);
0073     for (auto& x : v)
0074     {
0075         x = dis(gen);
0076     }
0077     return v;
0078 }
0079 
0080 template<typename T, typename std::enable_if<std::is_integral<T>::value, bool>::type = true>
0081 std::vector<T> generate_random_vector(std::size_t size, std::size_t seed)
0082 {
0083     if (seed == 0)
0084     {
0085         std::random_device rd;
0086         seed = rd();
0087     }
0088     std::vector<T> v(size);
0089 
0090     std::mt19937_64 gen(seed);
0091 
0092     // Rescaling by larger than 2 is UB!
0093     std::uniform_int_distribution<T> dis(std::numeric_limits<T>::lowest()/2, (std::numeric_limits<T>::max)()/2);
0094     for (auto& x : v)
0095     {
0096         x = dis(gen);
0097     }
0098     return v;
0099 }
0100 
0101 }} // Namespaces