PointSampler library (C++)
Loading...
Searching...
No Matches
latin_hypercube_sampling.hpp
Go to the documentation of this file.
1/* Copyright (c) 2025 Otto Link. Distributed under the terms of the GNU General
2 Public License. The full license is in the file LICENSE, distributed with
3 this software. */
4#pragma once
5#include <optional>
6
8
9namespace ps
10{
11
43template <typename T, std::size_t N>
44std::vector<Point<T, N>> latin_hypercube_sampling(
45 std::size_t sample_count,
46 const std::array<std::pair<T, T>, N> &axis_ranges,
47 std::optional<unsigned int> seed = std::nullopt)
48{
49 std::mt19937 rng(seed ? *seed : std::random_device{}());
50 std::uniform_real_distribution<T> jitter(0.0, 1.0);
51
52 std::vector<Point<T, N>> samples(sample_count);
53
54 for (std::size_t dim = 0; dim < N; ++dim)
55 {
56 std::vector<T> strata(sample_count);
57 T range_min = axis_ranges[dim].first;
58 T range_max = axis_ranges[dim].second;
60
61 // Generate stratified positions with jitter
62 for (std::size_t i = 0; i < sample_count; ++i)
63 strata[i] = range_min + (i + jitter(rng)) * stride;
64
65 std::shuffle(strata.begin(), strata.end(), rng);
66
67 // Assign values to the sample vector
68 for (std::size_t i = 0; i < sample_count; ++i)
69 samples[i][dim] = strata[i];
70 }
71
72 return samples;
73}
74
75} // namespace ps
Definition dbscan_clustering.hpp:11
std::vector< Point< T, N > > random(size_t count, const std::array< std::pair< T, T >, N > &axis_ranges, std::optional< unsigned int > seed=std::nullopt)
Generates a specified number of uniformly distributed random points in N-dimensional space.
Definition random.hpp:66
std::vector< Point< T, N > > latin_hypercube_sampling(std::size_t sample_count, const std::array< std::pair< T, T >, N > &axis_ranges, std::optional< unsigned int > seed=std::nullopt)
Generates samples using Latin Hypercube Sampling (LHS).
Definition latin_hypercube_sampling.hpp:44