-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimplex.hpp
More file actions
91 lines (69 loc) · 1.78 KB
/
Copy pathSimplex.hpp
File metadata and controls
91 lines (69 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// -*- C++ -*-
#pragma once
#include <cassert>
#include <cmath>
#include <cstddef>
#include <vector>
#include "uniform_distribution.hpp"
class Simplex
{
#pragma region typedefs
public: using container_type = std::vector<float>;
#pragma endregion
#pragma region Data
private: float _a;
private: float _b;
private: float _d; // delta
#pragma endregion
#pragma region Ctor/Dtor/ops
public: Simplex(float a = 0.0f, float b = 1.0f):
_a{a},
_b{b},
_d{_b - _a}
{
assert(a < b);
}
public: Simplex(const Simplex& s) = default;
public: Simplex(Simplex&& s) = default;
public: Simplex& operator=(const Simplex& s) = default;
public: Simplex& operator=(Simplex&& s) = default;
public: ~Simplex()
{
}
#pragma endregion
#pragma region Observers
public: float a() const
{
return _a;
}
public: float b() const
{
return _b;
}
#pragma endregion
#pragma region Sampling
private: static void make_degenerate_sample(container_type& con, int n);
private: void scale_and_shift(container_type& con, float sum) const;
public: template <typename RGEN> void sample(container_type& con, RGEN& rgen) const;
#pragma endregion
};
template <typename RGEN> void Simplex::sample(container_type& con, RGEN& rgen) const
{
assert(con.size() > 1);
std::uniform_distribution<float> dist;
float sum = 0.0f;
for(size_t k = 0; k != con.size(); ++k)
{
float t = dist(rgen); // uniform float in [0...1) range
if (t == 0.0f)
{
make_degenerate_sample(con, k);
sum = 1.0f;
break;
}
t = -log(t);
con[k] = t;
sum += t;
}
scale_and_shift(con, sum);
}