use nalgebra::{DMatrix, DVector};
use crate::{Interval, IntervalBox, hcat};
#[derive(Clone, Debug, PartialEq)]
pub struct Zonotope {
pub center: DVector<f64>,
pub generators: DMatrix<f64>,
}
impl Zonotope {
#[must_use]
pub fn new(center: DVector<f64>, generators: DMatrix<f64>) -> Self {
assert_eq!(
center.len(),
generators.nrows(),
"generator rows must match dimension"
);
Self { center, generators }
}
#[must_use]
pub fn from_box(b: &IntervalBox) -> Self {
let c = DVector::from_iterator(b.dim(), b.0.iter().map(|i| f64::midpoint(i.lo, i.hi)));
let g = DMatrix::from_diagonal(&DVector::from_iterator(
b.dim(),
b.0.iter().map(|i| i.width() / 2.0),
));
Self::new(c, g)
}
#[must_use]
pub fn dim(&self) -> usize {
self.center.len()
}
#[must_use]
pub fn num_generators(&self) -> usize {
self.generators.ncols()
}
#[must_use]
#[expect(
clippy::cast_precision_loss,
reason = "generator counts and dimensions stay far below 2^52"
)]
pub fn order(&self) -> f64 {
self.num_generators() as f64 / self.dim() as f64
}
#[must_use]
pub fn evaluate(&self, beta: &[f64]) -> DVector<f64> {
&self.center + &self.generators * DVector::from_column_slice(beta)
}
#[must_use]
pub fn linear_map(&self, m: &DMatrix<f64>) -> Self {
Self::new(m * &self.center, m * &self.generators)
}
#[must_use]
pub fn translate(&self, v: &DVector<f64>) -> Self {
Self::new(&self.center + v, self.generators.clone())
}
#[must_use]
pub fn minkowski_sum(&self, o: &Self) -> Self {
Self::new(
&self.center + &o.center,
hcat(&self.generators, &o.generators),
)
}
#[must_use]
pub fn interval_hull(&self) -> IntervalBox {
IntervalBox(
(0..self.dim())
.map(|i| {
let r: f64 = self.generators.row(i).iter().map(|g| g.abs()).sum();
Interval::new(self.center[i] - r, self.center[i] + r)
})
.collect(),
)
}
}