16# define M_PI 3.14159265358979323846264338327950288
49 template <
typename T,
typename Table>
50 constexpr auto vdc_digit_sum(
unsigned long n,
unsigned long base,
const Table& weights)
55 const auto remainder = n % base;
57 reslt +=
static_cast<T
>(remainder) * weights[idx];
91 : gen{g}, index{idx} {}
98 auto temp_idx = gen->get_index();
100 auto value = gen->pop();
101 gen->reseed(temp_idx);
128 return index == other.index;
135 return index != other.index;
141 [[nodiscard]]
auto get_index() const ->
unsigned long {
return index; }
154 template <
typename G,
typename V>
156 { g.pop() } -> std::convertible_to<V>;
157 { g.peek() } -> std::convertible_to<V>;
158 g.skip(
static_cast<unsigned int>(n));
160 { g.get_index() } -> std::convertible_to<unsigned long>;
193 = this->
count_.fetch_add(1, std::memory_order_relaxed) + 1;
194 return derived().value_at(count_value);
202 [[nodiscard]]
auto peek() -> Value {
203 auto count_value = this->
count_.load(std::memory_order_relaxed) + 1;
204 return derived().value_at(count_value);
212 auto skip(
unsigned int n) ->
void { this->
count_.fetch_add(n, std::memory_order_relaxed); }
219 auto reseed(
const unsigned long& seed) ->
void {
220 this->
count_.store(seed, std::memory_order_relaxed);
229 return this->
count_.load(std::memory_order_relaxed);
240 auto derived() -> Derived& {
return static_cast<Derived&
>(*this); }
279 std::numeric_limits<unsigned long>::max());
301 constexpr auto vdc(
unsigned long count,
unsigned long base) ->
double {
305 const auto remainder = count % base;
307 denom *=
static_cast<double>(base);
308 reslt +=
static_cast<double>(remainder) / denom;
348 std::array<double, MAX_REVERSE_BITS> rev_lst{};
350 "MAX_REVERSE_BITS must be at least the number of bits in unsigned long");
362 explicit VdCorput(
const unsigned long base) : base{base} {
363 double reverse = 1.0;
365 reverse /=
static_cast<double>(base);
366 this->rev_lst[i] = reverse;
381 return detail::vdc_digit_sum<double>(n, this->base, this->rev_lst);
431 Halton(
const unsigned long base0,
const unsigned long base1) : vdc0(base0), vdc1(base1) {}
441 auto value_at(
unsigned long n)
const -> std::array<double, 2> {
493 explicit Circle(
const unsigned long base) :
vdc(base) {}
503 auto value_at(
unsigned long n)
const -> std::array<double, 2> {
505 return {std::cos(theta), std::sin(theta)};
559 Disk(
const unsigned long base0,
const unsigned long base1) : vdc0(base0), vdc1(base1) {}
570 auto value_at(
unsigned long n)
const -> std::array<double, 2> {
572 auto radius = std::sqrt(this->vdc1.
value_at(n));
573 return {radius * std::cos(theta), radius * std::sin(theta)};
629 Sphere(
const unsigned long base0,
const unsigned long base1)
630 : vdcgen(base0), cirgen(base1) {}
641 auto value_at(
unsigned long n)
const -> std::array<double, 3> {
643 auto sinphi = std::sqrt(1.0 - (cosphi * cosphi));
644 auto arr = this->cirgen.
value_at(n);
645 return {sinphi * arr[0], sinphi * arr[1], cosphi};
693 Sphere3Hopf(
const unsigned long base0,
const unsigned long base1,
const unsigned long base2)
694 : vdc0(base0), vdc1(base1), vdc2(base2) {}
714 auto value_at(
unsigned long n)
const -> std::array<double, 4> {
718 auto cos_eta = std::sqrt(
vdc);
719 auto sin_eta = std::sqrt(1.0 -
vdc);
721 cos_eta * std::cos(psy),
722 cos_eta * std::sin(psy),
723 sin_eta * std::cos(phi + psy),
724 sin_eta * std::sin(phi + psy),
730 static_assert(SequenceGenerator<VdCorput, double>);
731 static_assert(SequenceGenerator<Halton, std::array<double, 2>>);
732 static_assert(SequenceGenerator<Circle, std::array<double, 2>>);
733 static_assert(SequenceGenerator<Disk, std::array<double, 2>>);
734 static_assert(SequenceGenerator<Sphere, std::array<double, 3>>);
735 static_assert(SequenceGenerator<Sphere3Hopf, std::array<double, 4>>);
742 extern unsigned long dummy(
unsigned int index);
Circle sequence generator.
Definition lds.hpp:481
Circle(const unsigned long base)
Construct a new Circle object.
Definition lds.hpp:493
auto value_at(unsigned long n) const -> std::array< double, 2 >
Evaluate the point on the unit circle at a given index (pure, no state change)
Definition lds.hpp:503
Disk sequence generator.
Definition lds.hpp:546
Disk(const unsigned long base0, const unsigned long base1)
Construct a new Disk object.
Definition lds.hpp:559
auto value_at(unsigned long n) const -> std::array< double, 2 >
Evaluate the point in the unit disk at a given index (pure, no state change)
Definition lds.hpp:570
CRTP base implementing the sequence generator protocol.
Definition lds.hpp:182
auto get_index() const -> unsigned long
Get current index in the sequence.
Definition lds.hpp:228
auto pop() -> Value
Generate the next value in the sequence (advances state).
Definition lds.hpp:191
auto skip(unsigned int n) -> void
Skip n values in the sequence.
Definition lds.hpp:212
auto reseed(const unsigned long &seed) -> void
Reset the generator to a specific seed value.
Definition lds.hpp:219
std::atomic< unsigned long > count_
Current sequence index (single source of state)
Definition lds.hpp:233
auto peek() -> Value
Peek at the next value without advancing state.
Definition lds.hpp:202
CRTP mixin adding STL iterator support to a GeneratorBase.
Definition lds.hpp:254
auto end() const -> GeneratorIterator< Derived, Value >
Get iterator to end (infinite sequence)
Definition lds.hpp:277
auto begin() -> GeneratorIterator< Derived, Value >
Get iterator to beginning.
Definition lds.hpp:266
Forward iterator for sequence generators.
Definition lds.hpp:79
auto get_index() const -> unsigned long
Get current index.
Definition lds.hpp:141
const value_type * pointer
Definition lds.hpp:87
value_type reference
Definition lds.hpp:88
auto operator!=(const GeneratorIterator &other) const -> bool
Inequality comparison.
Definition lds.hpp:134
std::input_iterator_tag iterator_category
Definition lds.hpp:84
auto operator==(const GeneratorIterator &other) const -> bool
Equality comparison.
Definition lds.hpp:127
auto operator++(int) -> GeneratorIterator
Post-increment operator.
Definition lds.hpp:118
Value value_type
Definition lds.hpp:85
auto operator++() -> GeneratorIterator &
Pre-increment operator.
Definition lds.hpp:110
GeneratorIterator(Generator *g=nullptr, unsigned long idx=0)
Definition lds.hpp:90
auto operator*() const -> Value
Dereference operator.
Definition lds.hpp:96
std::ptrdiff_t difference_type
Definition lds.hpp:86
Halton sequence generator.
Definition lds.hpp:417
Halton(const unsigned long base0, const unsigned long base1)
Construct a new Halton object.
Definition lds.hpp:431
auto value_at(unsigned long n) const -> std::array< double, 2 >
Evaluate the 2D Halton point at a given index (pure, no state change)
Definition lds.hpp:441
S(3) sequence generator by Hopf fibration.
Definition lds.hpp:677
auto value_at(unsigned long n) const -> std::array< double, 4 >
Evaluate the point on the 3-sphere at a given index (pure, no state change)
Definition lds.hpp:714
Sphere3Hopf(const unsigned long base0, const unsigned long base1, const unsigned long base2)
Construct a new Sphere 3 Hopf object.
Definition lds.hpp:693
Sphere sequence generator.
Definition lds.hpp:615
Sphere(const unsigned long base0, const unsigned long base1)
Construct a new Sphere object.
Definition lds.hpp:629
auto value_at(unsigned long n) const -> std::array< double, 3 >
Evaluate the point on the unit sphere at a given index (pure, no state change)
Definition lds.hpp:641
Van der Corput sequence generator.
Definition lds.hpp:346
auto value_at(unsigned long n) const -> double
Evaluate the sequence value at a given index (pure, no state change)
Definition lds.hpp:380
VdCorput(const unsigned long base)
Construct a new VdCorput object.
Definition lds.hpp:362
Concept for the sequence generator protocol.
Definition lds.hpp:155
#define M_PI
Definition lds.hpp:16
constexpr auto vdc_digit_sum(unsigned long n, unsigned long base, const Table &weights) -> T
Core base-b digit/weight summation shared by all van der Corput generators.
Definition lds.hpp:50
constexpr const auto TWO_PI
Definition lds.hpp:21
constexpr double MAPPING_FACTOR
Definition lds.hpp:25
unsigned long dummy(unsigned int index)
Dummy function (placeholder, not yet implemented).
constexpr unsigned int MAX_REVERSE_BITS
Definition lds.hpp:24
constexpr auto vdc(unsigned long count, unsigned long base) -> double
Van der Corput sequence.
Definition lds.hpp:301