use std::sync::{Mutex, OnceLock};
use ndarray::{Array1, Array2, ArrayView1};
use rand::rngs::StdRng;
use rand::{Rng, SeedableRng};
use crate::Bounds;
const SMALL_PRIMES: [u64; 32] = [
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,
101, 103, 107, 109, 113, 127, 131,
];
fn prime_cache() -> &'static Mutex<Vec<u64>> {
static CACHE: OnceLock<Mutex<Vec<u64>>> = OnceLock::new();
CACHE.get_or_init(|| Mutex::new(SMALL_PRIMES.to_vec()))
}
fn nth_prime(index: usize) -> u64 {
if index < SMALL_PRIMES.len() {
return SMALL_PRIMES[index];
}
let mut cache = prime_cache().lock().expect("prime cache poisoned");
if index >= cache.len() {
let mut candidate = *cache.last().expect("non-empty prime table") + 2;
while cache.len() <= index {
let mut is_prime = true;
for &p in cache.iter() {
if p > candidate / p {
break;
}
if candidate % p == 0 {
is_prime = false;
break;
}
}
if is_prime {
cache.push(candidate);
}
candidate += 2;
}
}
cache[index]
}
pub fn radical_inverse(mut index: u64, base: u64) -> f64 {
assert!(base >= 2, "radical inverse base must be at least 2");
let inv_base = 1.0 / base as f64;
let mut fraction = inv_base;
let mut value = 0.0;
while index > 0 {
value += (index % base) as f64 * fraction;
index /= base;
fraction *= inv_base;
}
value
}
pub fn halton_unit(index: u64, dim: usize) -> Array1<f64> {
Array1::from_iter((0..dim).map(|axis| radical_inverse(index, nth_prime(axis))))
}
pub fn halton_points(bounds: &Bounds<f64>, n: usize, skip: u64) -> Array2<f64> {
let mut out = Array2::<f64>::zeros((n, bounds.dims));
for row in 0..n {
let unit = halton_unit(skip + row as u64, bounds.dims);
for axis in 0..bounds.dims {
let width = bounds.high[axis] - bounds.low[axis];
assert!(width >= 0.0, "bounds require high >= low on every axis");
out[[row, axis]] = bounds.low[axis] + width * unit[axis];
}
}
out
}
pub fn low_discrepancy_points(bounds: &Bounds<f64>, n: usize, skip: u64) -> Array2<f64> {
halton_points(bounds, n, skip)
}
pub fn shifted_halton_points(
bounds: &Bounds<f64>,
n: usize,
skip: u64,
shift: ArrayView1<'_, f64>,
) -> Array2<f64> {
assert_eq!(
shift.len(),
bounds.dims,
"shift dimension must match bounds dimension"
);
let mut out = Array2::<f64>::zeros((n, bounds.dims));
for row in 0..n {
let unit = halton_unit(skip + row as u64, bounds.dims);
for axis in 0..bounds.dims {
let width = bounds.high[axis] - bounds.low[axis];
assert!(width >= 0.0, "bounds require high >= low on every axis");
let shifted = (unit[axis] + shift[axis]).fract();
out[[row, axis]] = bounds.low[axis] + width * shifted;
}
}
out
}
pub fn shifted_low_discrepancy_points(
bounds: &Bounds<f64>,
n: usize,
skip: u64,
seed: u64,
) -> Array2<f64> {
let mut rng = StdRng::seed_from_u64(seed);
let shift = Array1::from_iter((0..bounds.dims).map(|_| rng.random::<f64>()));
shifted_halton_points(bounds, n, skip, shift.view())
}
fn vertex_count_for_design(dim: usize, n: usize) -> Option<usize> {
if dim >= usize::BITS as usize {
return None;
}
let count = 1usize << dim;
(count <= n).then_some(count)
}
pub fn boundary_anchored_low_discrepancy_points(
bounds: &Bounds<f64>,
n: usize,
skip: u64,
) -> Array2<f64> {
let mut out = halton_points(bounds, n, skip);
if n == 0 {
return out;
}
if let Some(vertex_count) = vertex_count_for_design(bounds.dims, n) {
for row in 0..vertex_count {
for axis in 0..bounds.dims {
out[[row, axis]] = if ((row >> axis) & 1) == 0 {
bounds.low[axis]
} else {
bounds.high[axis]
};
}
}
if vertex_count < n {
for axis in 0..bounds.dims {
out[[vertex_count, axis]] = 0.5 * (bounds.low[axis] + bounds.high[axis]);
}
}
return out;
}
for axis in 0..bounds.dims {
out[[0, axis]] = bounds.low[axis];
}
if n > 1 {
for axis in 0..bounds.dims {
out[[1, axis]] = bounds.high[axis];
}
}
if n > 2 {
for axis in 0..bounds.dims {
out[[2, axis]] = 0.5 * (bounds.low[axis] + bounds.high[axis]);
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn nth_prime_matches_known_values() {
assert_eq!(nth_prime(0), 2);
assert_eq!(nth_prime(31), 131);
assert_eq!(nth_prime(32), 137);
assert_eq!(nth_prime(999), 7919);
assert_eq!(nth_prime(9999), 104729);
}
#[test]
fn high_dimensional_halton_is_fast_and_bounded() {
let unit = halton_unit(7, 34_134);
assert_eq!(unit.len(), 34_134);
assert!(unit.iter().all(|&u| (0.0..1.0).contains(&u)));
}
}