-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsampling.h
125 lines (105 loc) · 3.23 KB
/
sampling.h
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#ifndef SAMPLING_H
#define SAMPLING_H
#include <cstddef>
#include <vector>
#include <cassert>
#include "types.h"
namespace SAMP
{
// Global initialization function for the sampling module
void Initialize();
// Prime numbers
const size_t PRIME_TBL_SIZE = 1000;
extern const uint g_prime_table[PRIME_TBL_SIZE];
uint PrimeToIndex(uint prime);
// Radical inverse functions
double RadicalInverse(uint n, uint base, const uint *perm = nullptr);
double FoldedRadicalInverse(uint n, uint base);
double RadicalInverseBase2(uint32 n, uint32 scramble = 0);
double SobolRadicalInverseBase2(uint32 n, uint32 scramble = 0);
double LarcherPillichshammerRadicalInverseBase2(uint32 n, uint32 scramble = 0);
// Halton & Hammersley sequences
template <class S> double HaltonSequence(uint n, uint dim);
template <class S> double HammersleySequence(uint n, uint dim, uint num_smp);
double HaltonZarembaSequence(uint n, uint dim);
double HammersleyZarembaSequence(uint n, uint dim, uint num_smp);
double CranleyPattersonRotation(double x, double e);
// Braaten-Weller permutations
const size_t BW_TBL_SIZE = 16;
extern std::vector<uint> g_braaten_weller_table[BW_TBL_SIZE];
struct ScrambleBraatenWeller
{
const uint * operator () (uint prime_base_idx) const
{
assert(g_braaten_weller_table[0].empty() == false);
if (prime_base_idx < BW_TBL_SIZE)
return &g_braaten_weller_table[prime_base_idx][0];
else
return nullptr;
}
};
// Faure permutations
const size_t FAURE_TBL_SIZE = 128;
extern std::vector<uint> g_faure_table[FAURE_TBL_SIZE];
struct ScrambleFaure
{
const uint * operator () (uint prime_base_idx) const
{
assert(g_faure_table[0].empty() == false);
if (prime_base_idx < FAURE_TBL_SIZE)
return &g_faure_table[prime_base_idx][0];
else
return nullptr;
}
};
// Reverse permutations
const size_t REVERSE_TBL_SIZE = 128;
extern std::vector<uint> g_reverse_table[REVERSE_TBL_SIZE];
struct ScrambleReverse
{
const uint * operator () (uint prime_base_idx) const
{
assert(g_reverse_table[0].empty() == false);
if (prime_base_idx < REVERSE_TBL_SIZE)
return &g_reverse_table[prime_base_idx][0];
else
return nullptr;
}
};
// Randomized permutations
const size_t RANDOMIZED_TBL_SIZE = 128;
extern std::vector<uint> g_randomized_table[RANDOMIZED_TBL_SIZE];
struct ScrambleRandomized
{
const uint * operator () (uint prime_base_idx) const
{
assert(g_randomized_table[0].empty() == false);
if (prime_base_idx < RANDOMIZED_TBL_SIZE)
return &g_randomized_table[prime_base_idx][0];
else
return nullptr;
}
};
// No scrambling
struct ScrambleNone
{
const uint * operator () (uint) const { return nullptr; }
};
//
// Template inline implementations
//
template <class S> double HaltonSequence(uint n, uint dim)
{
S scramble;
return RadicalInverse(n, g_prime_table[dim], scramble(dim));
}
template <class S> double HammersleySequence(uint n, uint dim, uint num_smp)
{
S scramble;
if (dim == 0)
return double(n) / double(num_smp);
else
return RadicalInverse(n, g_prime_table[dim - 1], scramble(dim));
}
} // namespace SAMP
#endif // SAMPLING_H