cMHN 1.1
C++ library for learning MHNs with pRC
Loading...
Searching...
No Matches
uniform.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef pRC_CORE_RANDOM_UNIFORM_H
4#define pRC_CORE_RANDOM_UNIFORM_H
5
12
13namespace pRC
14{
15 template<class T>
17 {
18 public:
19 constexpr explicit UniformDistribution(
20 T const a = zero(),
21 T const b =
22 []()
23 {
24 if constexpr(IsFloat<T>())
25 {
26 return identity<T>();
27 }
28 else
29 {
30 return NumericLimits<T>::max();
31 }
32 }())
33 : mA(a)
34 , mB(b)
35 {
36 if constexpr(IsFloat<T>())
37 {
38 if(mA >= mB)
39 {
41 "Parameter a of uniform real distribution must be less "
42 "than parameter b. Range of values is [a,b).");
43 }
44 }
45 else
46 {
47 if(mA > mB)
48 {
50 "Parameter a of uniform integer distribution must be "
51 "less than or equal to parameter b. Range of values is "
52 "[a,b].");
53 }
54 }
55 }
56
57 constexpr auto reset() {}
58
59 constexpr auto &a() const
60 {
61 return mA;
62 }
63
64 constexpr auto &b() const
65 {
66 return mB;
67 }
68
69 constexpr auto min() const
70 {
71 return a();
72 }
73
74 constexpr auto max() const
75 {
76 return b();
77 }
78
79 template<class URNG>
80 constexpr auto operator()(URNG &rng)
81 {
82 if constexpr(IsFloat<T>())
83 {
84 return (b() - a()) * generateCanonical<T>(rng) + a();
85 }
86 else
87 {
88 using UInt = Common<typename URNG::result_type,
89 typename T::template ChangeSigned<false>>;
90
91 constexpr auto range = [](auto const a, auto const b)
92 {
93 if constexpr(typename T::Signed())
94 {
95 if(a < UInt(0))
96 {
97 return UInt(b) + UInt(-a);
98 }
99 }
100 return UInt(b) - UInt(a);
101 };
102
103 auto const r = range(a(), b());
104 if(r == UInt(0))
105 {
106 return a();
107 }
108 constexpr auto R = range(URNG::min(), URNG::max());
109
110 UInt basic;
111 if(R == r)
112 {
113 basic = UInt(rng()) - URNG::min();
114 }
115 else if(R > r)
116 {
117 auto const scaling = R / (r + UInt(1));
118 auto const past = (r + UInt(1)) * scaling;
119 do
120 {
121 basic = UInt(rng()) - URNG::min();
122 }
123 while(basic >= past);
124 basic /= scaling;
125 }
126 else
127 {
128 if constexpr(R != NumericLimits<UInt>::max())
129 {
130 UInt tmp;
131 do
132 {
133 tmp = ((R + UInt(1)) *
134 operator()(rng,
135 param_type(0, r / (R + UInt(1)))));
136 basic = tmp + (UInt(rng()) - URNG::min());
137 }
138 while(basic > r || basic < tmp);
139 }
140 else
141 {
143 "Found the impossible basic R < r of same type "
144 "with R being the maximum limit.");
145 }
146 }
147
148 return basic + a();
149 }
150 }
151
152 private:
153 T mA;
154 T mB;
155 };
156
157 template<class T>
158 constexpr auto operator==(UniformDistribution<T> const &lhs,
160 {
161 return lhs.a() == rhs.a() && lhs.b() == rhs.b();
162 }
163
164 template<class T>
165 constexpr auto operator!=(UniformDistribution<T> const &lhs,
167 {
168 return !(lhs == rhs);
169 }
170}
171#endif // pRC_CORE_RANDOM_UNIFORM_H
True<> Signed
Definition float.hpp:35
constexpr UniformDistribution(T const a=zero(), T const b=[]() { if constexpr(IsFloat< T >()) { return identity< T >();} else { return NumericLimits< T >::max();} }())
Definition uniform.hpp:19
constexpr auto & a() const
Definition uniform.hpp:59
constexpr auto & b() const
Definition uniform.hpp:64
constexpr auto operator()(URNG &rng)
Definition uniform.hpp:80
Definition type_traits.hpp:49
static void error(Xs &&...args)
Definition log.hpp:14
Definition cholesky.hpp:18
Any< IsSignedInteger< T >, IsUnsignedInteger< T > > IsInteger
Definition type_traits.hpp:69
static constexpr auto makeConstantSequence()
Definition sequence.hpp:402
typename CommonTypes< Ts... >::Type Common
Definition common.hpp:55
static constexpr auto zero()
Definition zero.hpp:12
std::disjunction< Bs... > Any
Definition type_traits.hpp:80
std::enable_if_t< B{}, int > If
Definition type_traits.hpp:68
static constexpr auto range(F &&f, Xs &&...args)
Definition range.hpp:16
static constexpr auto operator!=(JacobiRotation< TA > const &a, JacobiRotation< TB > const &b)
Definition jacobi_rotation.hpp:304
static constexpr auto operator==(JacobiRotation< TA > const &a, JacobiRotation< TB > const &b)
Definition jacobi_rotation.hpp:297
Definition type_traits.hpp:16
Definition limits.hpp:13