use alloc::vec::Vec;
use core::cmp::Ordering;
use nalgebra::{DMatrix, DVector};
use crate::round::{self, RowErrors};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Reduction {
Girard,
Pca,
}
pub(crate) fn select(
count: usize,
dim: usize,
order: f64,
metric: impl Fn(usize) -> f64,
) -> Option<(Vec<usize>, Vec<usize>)> {
assert!(
order >= 1.0,
"the reduced order must be at least 1, got {order}"
);
let budget = order * as_f64(dim);
if as_f64(count) <= budget {
return None;
}
let keep = floor_count(budget) - dim;
let mut by_metric: Vec<(usize, f64)> = (0..count).map(|j| (j, metric(j))).collect();
by_metric.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(Ordering::Equal));
let (kept, dropped) = by_metric.split_at(keep);
let indices = |part: &[(usize, f64)]| {
let mut v: Vec<usize> = part.iter().map(|&(j, _)| j).collect();
v.sort_unstable();
v
};
Some((indices(kept), indices(dropped)))
}
pub(crate) fn girard_metric<'a>(g: impl Iterator<Item = &'a f64>) -> f64 {
let (sum, max) = g.fold((0.0, 0.0f64), |(s, m), x| (s + x.abs(), m.max(x.abs())));
sum - max
}
pub(crate) fn enclose(g: &DMatrix<f64>, method: Reduction, errs: &mut RowErrors) -> DMatrix<f64> {
let n = g.nrows();
match method {
Reduction::Girard => {
for i in 0..n {
errs.record(i, round::abs_sum_up(g.row(i).iter()));
}
DMatrix::zeros(n, 0)
}
Reduction::Pca => pca_box(g, errs),
}
}
fn pca_box(g: &DMatrix<f64>, errs: &mut RowErrors) -> DMatrix<f64> {
let n = g.nrows();
let u = (g * g.transpose()).symmetric_eigen().eigenvectors;
let mut rotated_errs = RowErrors::new(n);
let rotated = round::mat_mul(&u.transpose(), g, &mut rotated_errs);
let reach = DVector::from_fn(n, |k, _| round::abs_sum_up(g.row(k).iter()));
for (i, bound) in round::residual_bound(&u, &reach).iter().enumerate() {
errs.record(i, *bound);
}
let columns: Vec<DVector<f64>> = (0..n)
.map(|i| {
let r = round::add_up(
round::abs_sum_up(rotated.row(i).iter()),
rotated_errs.bound(i),
);
round::scaled(&u.column(i).into_owned(), r, errs)
})
.filter(|c| c.iter().any(|&x| x != 0.0))
.collect();
crate::from_column_list(n, &columns)
}
#[expect(clippy::cast_precision_loss, reason = "counts stay far below 2^52")]
fn as_f64(n: usize) -> f64 {
n as f64
}
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "x is a non-negative generator budget far below usize::MAX"
)]
fn floor_count(x: f64) -> usize {
x as usize
}