Recti 1.2.4
Loading...
Searching...
No Matches
ilds.hpp
Go to the documentation of this file.
1
5#pragma once
6
7#include <array>
8#include <cmath>
9
10namespace ilds {
11
12 using std::array;
13
14 // Constants for magic numbers
21 constexpr unsigned int DEFAULT_SCALE = 10;
22 constexpr unsigned int MAX_REVERSE_BITS = 64;
23
31 template <unsigned long Base = 2> class VdCorput {
32 unsigned long _count;
33 std::array<unsigned long, MAX_REVERSE_BITS>
34 factor_lst;
35 static_assert(MAX_REVERSE_BITS >= sizeof(unsigned long) * 8,
36 "MAX_REVERSE_BITS must be at least the number of bits in unsigned long");
37
38 public:
44 constexpr explicit VdCorput(unsigned int scale = DEFAULT_SCALE) : _count{0}, factor_lst{} {
45 unsigned long factor = static_cast<unsigned long>(std::pow(Base, scale));
46 for (unsigned int i = 0; i < MAX_REVERSE_BITS; ++i) {
47 factor /= Base;
48 this->factor_lst[i] = factor;
49 }
50 }
51
57 [[nodiscard]] constexpr auto pop() -> unsigned long {
58 unsigned long count = ++this->_count;
59 unsigned long reslt = 0;
60 unsigned int idx = 0;
61 while (count != 0) {
62 const unsigned long remainder = count % Base;
63 count /= Base;
64 reslt += remainder * this->factor_lst[idx];
65 ++idx;
66 }
67 return reslt;
68 }
69
75 constexpr auto reseed(const unsigned long& seed) -> void { this->_count = seed; }
76
77 VdCorput(VdCorput&&) noexcept = delete;
78 VdCorput& operator=(VdCorput&&) noexcept = delete;
79 };
80
94 template <unsigned long Base1, unsigned long Base2> class Halton {
95 VdCorput<Base1> vdc0;
96 VdCorput<Base2> vdc1;
97
98 public:
107 constexpr explicit Halton(const std::array<unsigned int, 2>& scale)
108 : vdc0(scale[0]), vdc1(scale[1]) {}
109
117 constexpr auto pop() -> array<unsigned long, 2> { //
118 return {this->vdc0.pop(), this->vdc1.pop()};
119 }
120
128 constexpr auto reseed(const unsigned long& seed) -> void {
129 this->vdc0.reseed(seed);
130 this->vdc1.reseed(seed);
131 }
132 };
133
134} // namespace ilds
Halton sequence generator.
Definition ilds.hpp:94
constexpr Halton(const std::array< unsigned int, 2 > &scale)
Construct a new Halton object.
Definition ilds.hpp:107
constexpr auto reseed(const unsigned long &seed) -> void
Reset the state of the Halton sequence generator.
Definition ilds.hpp:128
constexpr auto pop() -> array< unsigned long, 2 >
Generate the next point in the Halton sequence.
Definition ilds.hpp:117
van der Corput sequence generator
Definition ilds.hpp:31
constexpr VdCorput(unsigned int scale=DEFAULT_SCALE)
Construct a new VdCorput object.
Definition ilds.hpp:44
constexpr auto pop() -> unsigned long
Increments count and calculates the next value in the sequence.
Definition ilds.hpp:57
constexpr auto reseed(const unsigned long &seed) -> void
Resets the state of the sequence generator.
Definition ilds.hpp:75
VdCorput(VdCorput &&) noexcept=delete
Definition ilds.hpp:10
constexpr unsigned int MAX_REVERSE_BITS
Definition ilds.hpp:22
constexpr unsigned int DEFAULT_SCALE
Default number of digits for the van der Corput sequence.
Definition ilds.hpp:21