VCTR
Loading...
Searching...
No Matches
compile_time_random_array.h
1/*
2 ==============================================================================
3 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
5 Copyright 2022- by sonible GmbH.
6
7 This file is part of VCTR - Versatile Container Templates Reconceptualized.
8
9 VCTR is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License version 3
11 only, as published by the Free Software Foundation.
12
13 VCTR is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License version 3 for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 version 3 along with VCTR. If not, see <https://www.gnu.org/licenses/>.
20 ==============================================================================
21*/
22
23#pragma once
24
25#include <array>
26#include <cstdint>
27#include <numbers>
28
29#ifdef _MSC_VER
30__pragma(warning(push))
31__pragma(warning(disable:4244)) // conversion from 'long double' to 'T', possible loss of data
32#endif
33#include <gcem.hpp>
34#ifdef _MSC_VER
35__pragma(warning(pop))
36#endif
37
56#ifndef VCTR_RANDOM_SEED
57#error "VCTR_RANDOM_SEED must be defined to a positive integer to use compile_time_random_array.h, e.g. -DVCTR_RANDOM_SEED=1234"
58#endif
59
60namespace vctr::random::detail
61{
67consteval uint32_t uniformDistribution (uint32_t& previous)
68{
69 constexpr uint32_t lce_a = 48271;
70 constexpr uint32_t lce_c = 0;
71 constexpr uint32_t lce_m = std::numeric_limits<int32_t>::max();
72 previous = ((lce_a * previous + lce_c) % lce_m);
73 return previous;
74}
75
77template <typename T>
78consteval T uniformDistribution (T min, T max, uint32_t& seed)
79{
80 auto span = double (max - min);
81 constexpr auto scale = double (std::numeric_limits<int32_t>::max());
82 return T (double (uniformDistribution (seed)) / scale * (span)) + min;
83}
84
89template <size_t size, typename T>
90consteval auto makeArrayWithUniformDistribution (T min, T max, uint32_t seed)
91{
92 std::array<T, size> dst {};
93 for (auto& el : dst)
94 el = uniformDistribution (min, max, seed);
95
96 return dst;
97}
98
105template <size_t size, std::floating_point T>
106consteval auto makeArrayWithNormalDistribution (T mean, T sigma, uint32_t seed)
107{
108 std::array<T, size> dst {};
109
110 for (size_t i = 0; i < size - 1; i += 2)
111 {
112 auto u1 = T (0);
113 auto u2 = T (0);
114
115 // first two uniformly distributed values between [0.0, 1.0] are calculated
116 // u1 must not be zero
117 do
118 {
119 u1 = uniformDistribution (T (0), T (1), seed);
120 u2 = uniformDistribution (T (0), T (1), seed);
121 } while (u1 <= std::numeric_limits<T>::epsilon());
122
123 // from these values two normally distributed values are calculated
124 // these values are then transformed to a general gaussian value to achieve the desired mean and sigma
125 // z1 = sqrt(-2 * ln(u1)) * cos (2 * PI * u2)
126 // z1 = sigma * z1 + mean
127 // z2 = sqrt(-2 * ln(u1)) * sin (2 * PI * u2)
128 // z2 = sigma * z2 + mean
129
130 auto mag = sigma * gcem::sqrt (T (-2) * gcem::log (u1));
131 auto twoPiU2 = std::numbers::pi_v<T> * 2 * u2;
132 dst[i] = mag * gcem::cos (twoPiU2) + mean;
133
134 // in case of odd array sizes, the last value is simply discarded
135 if (i + 1 < size)
136 dst[i + 1] = mag * gcem::sin (twoPiU2) + mean;
137 }
138
139 return dst;
140}
141} // namespace vctr::random::detail
142
143namespace vctr::random
144{
145
147consteval uint32_t getSeed()
148{
149 return static_cast<uint32_t> (VCTR_RANDOM_SEED);
150}
151
157template <size_t size, typename T>
158consteval auto makeArrayWithUniformDistribution (T min, T max, uint32_t seedOffset = 0)
159{
160 return detail::makeArrayWithUniformDistribution<size, T> (min, max, getSeed() + seedOffset);
161}
162
166template <size_t size, std::floating_point T>
167consteval auto makeArrayWithNormalDistribution (T mean, T sigma)
168{
169 return detail::makeArrayWithNormalDistribution<size, T> (mean, sigma, getSeed());
170}
171
172} // namespace vctr::random
constexpr ExpressionChainBuilder< expressions::Mean > mean
Computes the mean value of the source values.
Definition: Mean.h:252
constexpr ExpressionChainBuilder< expressions::Max > max
Computes the maximum value of the source values.
Definition: Max.h:198
constexpr ExpressionChainBuilder< expressions::Min > min
Computes the minimum value of the source values.
Definition: Min.h:198