-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathowen.cpp
More file actions
145 lines (111 loc) · 2.88 KB
/
Copy pathowen.cpp
File metadata and controls
145 lines (111 loc) · 2.88 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// SPDX-License-Identifier: Apache-2.0
// Copyright Contributors to the OpenQMC Project.
#include "hypothesis.h"
#include <oqmc/owen.h>
#include <oqmc/pcg.h>
#include <gtest/gtest.h>
#include <array>
#include <cstdint>
namespace
{
struct SamplerV1
{
void initialise(int seed)
{
hash0 = oqmc::pcg::hash(seed * 2 + 0);
hash1 = oqmc::pcg::hash(seed * 2 + 1);
}
void sample(int index, std::uint32_t out[2]) const
{
std::uint32_t sample[2];
oqmc::shuffledScrambledSobol<1>(index, hash0, &sample[0]);
oqmc::shuffledScrambledSobol<1>(index, hash1, &sample[1]);
out[0] = sample[0];
out[1] = sample[1];
}
std::uint32_t hash0;
std::uint32_t hash1;
};
template <int X, int Y>
struct SamplerV2
{
void initialise(int seed)
{
hash = oqmc::pcg::hash(seed);
}
void sample(int index, std::uint32_t out[2]) const
{
std::uint32_t rnd[4];
oqmc::shuffledScrambledSobol<4>(index, hash, rnd);
out[0] = rnd[X];
out[1] = rnd[Y];
}
std::uint32_t hash;
};
ALL_HYPOTHESIS_TESTS(OwenTest, SampleIndpendent, (SamplerV1()))
ALL_HYPOTHESIS_TESTS(OwenTest, SampleDims01, (SamplerV2<0, 1>()))
ALL_HYPOTHESIS_TESTS(OwenTest, SampleDims02, (SamplerV2<0, 2>()))
ALL_HYPOTHESIS_TESTS(OwenTest, SampleDims03, (SamplerV2<0, 3>()))
ALL_HYPOTHESIS_TESTS(OwenTest, SampleDims12, (SamplerV2<1, 2>()))
ALL_HYPOTHESIS_TESTS(OwenTest, SampleDims13, (SamplerV2<1, 3>()))
ALL_HYPOTHESIS_TESTS(OwenTest, SampleDims23, (SamplerV2<2, 3>()))
TEST(OwenTest, 02Sequence)
{
constexpr auto m = 8;
constexpr auto n = 1 << m;
std::array<bool, n> strata;
for(int i = 0; i < m + 1; ++i)
{
const int xResolution = 1 << i;
const int yResolution = 1 << (m - i);
ASSERT_EQ(xResolution * yResolution, n);
const std::uint32_t xWidth = UINT32_MAX / xResolution;
const std::uint32_t yWidth = UINT32_MAX / yResolution;
strata.fill(false);
for(int index = 0; index < n; ++index)
{
std::uint32_t out[2];
oqmc::shuffledScrambledSobol<2>(index, oqmc::pcg::hash(0), out);
const int x = out[0] / xWidth;
const int y = out[1] / yWidth;
const int coordinate = x + y * xResolution;
auto& stratum = strata[coordinate];
ASSERT_FALSE(stratum);
stratum = true;
}
for(auto stratum : strata)
{
EXPECT_TRUE(stratum);
}
}
}
TEST(OwenTest, ShirleyRemapping)
{
constexpr auto numStratum = 8;
constexpr auto numSamples = numStratum * numStratum;
std::array<bool, numStratum> strata;
for(int i = 0; i < numStratum; ++i)
{
constexpr auto width = UINT32_MAX / numStratum;
strata.fill(false);
for(int index = 0; index < numSamples; ++index)
{
std::uint32_t out[2];
oqmc::shuffledScrambledSobol<2>(index, oqmc::pcg::hash(0), out);
const int x = out[0] / width;
const int y = out[1] / width;
if(x != i)
{
continue;
}
auto& stratum = strata[y];
ASSERT_FALSE(stratum);
stratum = true;
}
for(auto stratum : strata)
{
EXPECT_TRUE(stratum);
}
}
}
} // namespace