cMHN 1.2
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_DISTRIBUTIONS_UNIFORM_H
4#define pRC_CORE_RANDOM_DISTRIBUTIONS_UNIFORM_H
5
12
13namespace pRC
14{
15 template<IsValue T>
17 : public RandomDistribution<UniformDistribution<T>>
18 {
19 public:
20 constexpr explicit UniformDistribution(
21 T const a = zero(),
22 T const b =
23 []()
24 {
25 if constexpr(IsFloat<T>)
26 {
27 return identity<T>();
28 }
29 else
30 {
31 return NumericLimits<T>::max();
32 }
33 }())
34 : mA(a)
35 , mB(b)
36 {
37 if constexpr(IsFloat<T>)
38 {
39 if(mA >= mB)
40 {
42 "Parameter a of uniform real distribution must be less "
43 "than parameter b. Range of values is [a,b).");
44 }
45 }
46 else
47 {
48 if(mA > mB)
49 {
51 "Parameter a of uniform integer distribution must be "
52 "less than or equal to parameter b. Range of values is "
53 "[a,b].");
54 }
55 }
56 }
57
58 constexpr auto reset() {}
59
60 constexpr auto &a() const
61 {
62 return mA;
63 }
64
65 constexpr auto &b() const
66 {
67 return mB;
68 }
69
70 constexpr auto min() const
71 {
72 return a();
73 }
74
75 constexpr auto max() const
76 {
77 return b();
78 }
79
80 template<class URNG>
81 constexpr auto operator()(URNG &rng)
82 {
83 if constexpr(IsFloat<T>)
84 {
85 return (b() - a()) * generateCanonical<T>(rng) + a();
86 }
87 else
88 {
89 using UInt =
91
92 constexpr auto range = [](auto const a, auto const b)
93 {
94 if constexpr(IsSignedInteger<T>)
95 {
96 if(a < zero())
97 {
98 return UInt(b) + UInt(-a);
99 }
100 }
101 return UInt(b) - UInt(a);
102 };
103
104 auto const r = range(a(), b());
105 if(r == zero())
106 {
107 return a();
108 }
109 constexpr auto R = range(URNG::min(), URNG::max());
110
111 UInt basic;
112 if(R == r)
113 {
114 basic = UInt(rng()) - URNG::min();
115 }
116 else if(R > r)
117 {
118 auto const scaling = R / (r + UInt(1));
119 auto const past = (r + UInt(1)) * scaling;
120 do
121 {
122 basic = UInt(rng()) - URNG::min();
123 }
124 while(basic >= past);
125 basic /= scaling;
126 }
127 else
128 {
129 if constexpr(R != NumericLimits<UInt>::max())
130 {
131 UInt tmp;
132 do
133 {
134 tmp = ((R + UInt(1)) *
135 operator()(rng,
136 param_type(0, r / (R + UInt(1)))));
137 basic = tmp + (UInt(rng()) - URNG::min());
138 }
139 while(basic > r || basic < tmp);
140 }
141 else
142 {
144 "Found the impossible basic R < r of same type "
145 "with R being the maximum limit.");
146 }
147 }
148
149 return basic + a();
150 }
151 }
152
153 private:
154 T mA;
155 T mB;
156 };
157
158 template<class T>
159 constexpr auto operator==(UniformDistribution<T> const &lhs,
160 UniformDistribution<T> const &rhs)
161 {
162 return lhs.a() == rhs.a() && lhs.b() == rhs.b();
163 }
164
165 template<class T>
166 constexpr auto operator!=(UniformDistribution<T> const &lhs,
167 UniformDistribution<T> const &rhs)
168 {
169 return !(lhs == rhs);
170 }
171}
172#endif // pRC_CORE_RANDOM_DISTRIBUTIONS_UNIFORM_H
Definition value.hpp:12
Definition distribution.hpp:12
Definition uniform.hpp:18
constexpr auto min() const
Definition uniform.hpp:70
constexpr auto reset()
Definition uniform.hpp:58
constexpr UniformDistribution(T const a=zero(), T const b=[]() { if constexpr(IsFloat< T >) { return identity< T >();} else { return NumericLimits< T >::max();} }())
Definition uniform.hpp:20
constexpr auto operator()(URNG &rng)
Definition uniform.hpp:81
constexpr auto & b() const
Definition uniform.hpp:65
constexpr auto & a() const
Definition uniform.hpp:60
constexpr auto max() const
Definition uniform.hpp:75
Definition value.hpp:24
Definition value.hpp:42
static void error(Xs &&...args)
Definition log.hpp:14
Definition cholesky.hpp:10
std::common_type_t< Ts... > Common
Definition basics.hpp:53
static constexpr auto generateCanonical(RNG &rng)
Definition canonical.hpp:16
static constexpr auto operator!=(JacobiRotation< TA > const &a, JacobiRotation< TB > const &b)
Definition jacobi_rotation.hpp:291
static constexpr auto range(F &&f, Xs &&...args)
Definition range.hpp:18
static constexpr auto operator==(JacobiRotation< TA > const &a, JacobiRotation< TB > const &b)
Definition jacobi_rotation.hpp:284
static constexpr auto identity()
Definition identity.hpp:13
static constexpr auto zero()
Definition zero.hpp:12
Definition limits.hpp:13