bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
uniform_distribution.h
Go to the documentation of this file.
1#ifndef BITSIM_MATHS_STATISTICS_DISTRIBUTIONS_UNIFORM_DISTRIBUTION_H
2#define BITSIM_MATHS_STATISTICS_DISTRIBUTIONS_UNIFORM_DISTRIBUTION_H
3
5
6#include <initializer_list>
7#include <random>
8#include <utility>
9#include <vector>
10
11namespace bitsim
12{
13namespace maths
14{
15namespace stats
16{
17
18namespace
19{
20template <typename t> struct uniform_distribution_selector;
21
22template <> struct uniform_distribution_selector<uint_t>
23{
24 using distribution_type = std::uniform_int_distribution<uint_t>;
25};
26
27template <> struct uniform_distribution_selector<int_t>
28{
29 using distribution_type = std::uniform_int_distribution<int_t>;
30};
31
32template <> struct uniform_distribution_selector<lint_t>
33{
34 using distribution_type = std::uniform_int_distribution<lint_t>;
35};
36
37template <> struct uniform_distribution_selector<real_t>
38{
39 using distribution_type = std::uniform_real_distribution<real_t>;
40};
41
42template <> struct uniform_distribution_selector<float_t>
43{
44 using distribution_type = std::uniform_real_distribution<float_t>;
45};
46
47} // namespace
48
53template <typename OutType> class UniformDist
54{
55 public:
56 using out_type = OutType;
57 using range_type = std::pair<out_type, out_type>;
58
63
68
73
78
82 range_type range() const { return range_; }
83
84 private:
85 range_type range_;
86 uniform_distribution_selector<out_type>::distribution_type distribution_;
87};
88
89template <typename OutType>
92 : range_(a, b), distribution_(a, b)
93{
94}
95
96template <typename OutType>
98 : UniformDist<OutType>(range.first, range.second)
99{
100}
101
103{
104
105 // a seed source for the random number engine
106 std::random_device rd;
107
108 // mersenne_twister_engine seeded with rd()
109 std::mt19937 gen(rd());
110
111 return distribution_(gen);
112}
113
114template <typename OutType>
116{
117 // mersenne_twister_engine seeded with seed
118 std::mt19937 gen(seed);
119 return distribution_(gen);
120}
121
122template <typename OutType, typename WeightType = real_t> class UniformWeightedDist
123{
124 public:
125 using out_type = OutType;
126 using weights_type = std::vector<WeightType>;
127
132 template <typename VectorType> UniformWeightedDist(const VectorType &weights);
133
138 UniformWeightedDist(std::initializer_list<WeightType> weights);
139
144
148 out_type sample(uint_t seed);
149
153 std::vector<real_t> probabilities() const { return distribution_.probabilities(); }
154
155 private:
156 weights_type weights_;
157 std::discrete_distribution<out_type> distribution_;
158};
159
160template <typename OutType, typename WeightType>
161template <typename VectorType>
163 : weights_(weights), distribution_(weights.begin(), weights.end())
164{
165}
166
167template <typename OutType, typename WeightType>
169 std::initializer_list<WeightType> weights)
170 : weights_(weights), distribution_(weights)
171{
172}
173
174template <typename OutType, typename WeightType>
177{
178
179 // a seed source for the random number engine
180 std::random_device rd;
181
182 // mersenne_twister_engine seeded with rd()
183 std::mt19937 gen(rd());
184 return distribution_(gen);
185}
186
187template <typename OutType, typename WeightType>
190{
191 // mersenne_twister_engine seeded with seed
192 std::mt19937 gen(seed);
193 return distribution_(gen);
194}
195
196} // namespace stats
197} // namespace maths
198} // namespace bitsim
199
200#endif // BITSIM_MATHS_STATISTICS_DISTRIBUTIONS_UNIFORM_DISTRIBUTION_H
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
range_type range() const
Definition uniform_distribution.h:82
UniformDist(range_type range)
Construct with [a, b].
out_type sample()
Get a sample.
Definition uniform_distribution.h:102
UniformDist(out_type a, out_type b)
Construct with [a, b].
OutType out_type
Definition uniform_distribution.h:56
std::pair< out_type, out_type > range_type
Definition uniform_distribution.h:57
out_type sample()
Get a sample.
Definition uniform_distribution.h:176
OutType out_type
Definition uniform_distribution.h:125
std::vector< real_t > probabilities() const
Definition uniform_distribution.h:153
UniformWeightedDist(const VectorType &weights)
Initialize with a list of weights see also: https://www.cplusplus.com/reference/random/discrete_distr...
Definition uniform_distribution.h:162
std::vector< WeightType > weights_type
Definition uniform_distribution.h:126
Definition bernoulli_dist.cpp:6
Definition math_utils.h:14
Definition dc_motor_backend_base.h:9
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
float float_t
float
Definition bitsim_types.h:25
double real_t
real_t
Definition bitsim_types.h:20
int int_t
integer type
Definition bitsim_types.h:30
long int lint_t
long int type
Definition bitsim_types.h:35