use alloc::vec::Vec;
#[must_use]
pub fn das_dennis(m: usize, p: usize) -> Vec<Vec<f64>> {
assert!(m > 0 && p > 0, "das_dennis needs m > 0 and p > 0");
let mut points = Vec::new();
let mut current = alloc::vec![0usize; m];
compositions(&mut current, 0, p, &mut |c| {
#[expect(clippy::cast_precision_loss, reason = "divisions are tiny integers")]
let point = c.iter().map(|&k| k as f64 / p as f64).collect();
points.push(point);
});
points
}
fn compositions(
current: &mut [usize],
index: usize,
remaining: usize,
emit: &mut impl FnMut(&[usize]),
) {
if index == current.len() - 1 {
current[index] = remaining;
emit(current);
return;
}
for k in 0..=remaining {
current[index] = k;
compositions(current, index + 1, remaining - k, emit);
}
}
#[must_use]
pub fn two_layer(m: usize, outer: usize, inner: usize) -> Vec<Vec<f64>> {
#[expect(clippy::cast_precision_loss, reason = "objective counts are tiny")]
let centre = 1.0 / m as f64;
let mut points = das_dennis(m, outer);
points.extend(
das_dennis(m, inner)
.into_iter()
.map(|p| p.into_iter().map(|w| 0.5 * w + 0.5 * centre).collect()),
);
points
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn simplex_points() {
for (m, p, count) in [(2, 5, 6), (3, 4, 15), (5, 3, 35)] {
let points = das_dennis(m, p);
assert_eq!(points.len(), count);
assert!(
points
.iter()
.all(|w| (w.iter().sum::<f64>() - 1.0).abs() < 1e-12)
);
}
let layered = two_layer(3, 3, 2);
assert_eq!(layered.len(), 10 + 6);
assert!(
layered
.iter()
.all(|w| (w.iter().sum::<f64>() - 1.0).abs() < 1e-12)
);
}
}