use alloc::vec::Vec;
use nalgebra::{DMatrix, DVector};
use polyzonotope::{
Interval, IntervalBox, IntervalMatrix, Reduction, SparsePolyZonotope, Zonotope,
};
use super::error::{deviation, segment_box};
use super::{Linearization, Term, Terms, points};
use crate::linear::without_factors;
use crate::{Dynamics, IntervalTensor, LinearSystem, ReachError};
const TAME: f64 = 1e30;
const CUBIC_GENERATORS: u32 = 40;
#[expect(
clippy::cast_precision_loss,
reason = "state counts stay far below 2^52"
)]
fn cubic_order(order: f64, n: usize) -> f64 {
(f64::from(CUBIC_GENERATORS) / n as f64).clamp(1.0, order.max(1.0))
}
pub(super) struct Expansion {
half_hessians: Vec<IntervalMatrix>,
cubic: Vec<IntervalTensor>,
pub(super) static_set: SparsePolyZonotope,
static_radius: Vec<f64>,
start: Vec<Interval>,
}
pub(super) fn static_error<D: Dynamics>(
dynamics: &D,
model: &Linearization,
x: &SparsePolyZonotope,
(max_degree, order): (u32, f64),
terms: Terms,
) -> Result<Expansion, ReachError> {
let at = points(model.point.iter().copied());
let third = dynamics
.third_derivatives(&at)
.ok_or(ReachError::MissingDerivatives)?;
if !third.iter().all(|t| tame(tensor_entries(t))) {
return Err(ReachError::AbstractionDiverged);
}
let sixth: Vec<IntervalTensor> = third.iter().map(sixth_of_center).collect();
let cubic: Vec<IntervalTensor> = sixth.iter().map(times_six).collect();
let n = x.dim();
let shifted = x.translate(&-DVector::from_column_slice(&model.middle));
let hull = shifted.interval_hull();
let half_hessians: Vec<IntervalMatrix> = dynamics
.hessians(&at)
.iter()
.map(|h| h.scale(Interval::point(0.5)))
.collect();
let tame = tame(hull.0.iter()) && half_hessians.iter().all(|h| tame(h.0.iter()));
if !tame {
return Err(ReachError::AbstractionDiverged);
}
let (quadratic, radii): (Vec<_>, Vec<_>) = half_hessians
.iter()
.map(|h| {
let (c, r) = h.center_radius();
(c.view((0, 0), (n, n)).into_owned(), r)
})
.unzip();
let mut polynomial = shifted.quadratic_map(&quadratic);
if terms.has(Term::CubicStatic) {
let coarse = shifted.reduce(cubic_order(order, n), Reduction::Girard);
polynomial = polynomial.exact_plus(&coarse.cubic_map(&state_slices(&sixth, n)));
}
let static_set = cap_degree(&polynomial, max_degree).reduce(order, Reduction::Girard);
let mut start = hull.0;
start.resize(model.point.len(), Interval::point(0.0));
Ok(Expansion {
static_radius: radii.iter().map(|r| spread(r, &start)).collect(),
half_hessians,
cubic,
static_set,
start,
})
}
pub(super) fn augmented_system(a: &DMatrix<f64>) -> LinearSystem {
let n = a.nrows();
let mut big = DMatrix::zeros(3 * n, 3 * n);
big.view_mut((0, 0), (n, n)).copy_from(a);
big.view_mut((0, n), (n, n))
.copy_from(&DMatrix::identity(n, n));
LinearSystem {
a: big,
b: DMatrix::identity(3 * n, n),
}
}
pub(super) fn augmented_start(
x: &SparsePolyZonotope,
static_set: &SparsePolyZonotope,
) -> SparsePolyZonotope {
let n = x.dim();
let mut twice = DMatrix::zeros(3 * n, n);
twice
.view_mut((0, 0), (n, n))
.copy_from(&DMatrix::identity(n, n));
twice
.view_mut((2 * n, 0), (n, n))
.copy_from(&DMatrix::identity(n, n));
let mut middle = DMatrix::zeros(3 * n, n);
middle
.view_mut((n, 0), (n, n))
.copy_from(&DMatrix::identity(n, n));
x.linear_map(&twice)
.exact_plus(&static_set.linear_map(&middle))
}
pub(super) fn drift(set: &SparsePolyZonotope, n: usize) -> Vec<Interval> {
let mut difference = DMatrix::zeros(n, set.dim());
difference
.view_mut((0, 0), (n, n))
.copy_from(&DMatrix::identity(n, n));
difference
.view_mut((0, 2 * n), (n, n))
.copy_from(&-DMatrix::identity(n, n));
set.linear_map(&difference).interval_hull().0
}
pub(super) fn project(set: &SparsePolyZonotope, n: usize) -> SparsePolyZonotope {
set.linear_map(&DMatrix::identity(n, set.dim()))
}
pub(super) fn dynamic_error<D: Dynamics>(
dynamics: &D,
(model, expansion): (&Linearization, &Expansion),
(states, drift, u): (&SparsePolyZonotope, &[Interval], &Zonotope),
step: f64,
terms: Terms,
) -> Result<Vec<Interval>, ReachError> {
let boxes = Boxes::new(dynamics, model, expansion, (states, drift, u), step)?;
let mut deviation = boxes.deviation.clone();
if !terms.has(Term::InputDeviation) {
deviation[states.dim()..].fill(Interval::point(0.0));
}
let third = dynamics
.third_derivatives(&segment_box(&IntervalBox(boxes.full.clone()), &model.point))
.ok_or(ReachError::MissingDerivatives)?;
if !third.iter().all(|t| tame(tensor_entries(t))) {
return Err(ReachError::AbstractionDiverged);
}
let error = (0..states.dim())
.map(|i| {
let h = &expansion.half_hessians[i];
let parts = [
(Term::StaticRadius, symmetric(expansion.static_radius[i])),
(Term::Cross, cross(h, &expansion.start, &deviation)),
(Term::DynamicQuadratic, quadratic_form(h, &deviation)),
(
Term::CubicCross,
cubic_change(
&expansion.cubic[i],
&expansion.start,
&deviation,
&boxes.full,
),
),
(
Term::Remainder,
remainder(&difference(&third[i], &expansion.cubic[i]), &boxes.full),
),
(Term::JacobianSpread, jacobian_spread(model, i, &boxes.full)),
];
parts
.iter()
.filter(|(term, _)| terms.has(*term))
.fold(Interval::point(0.0), |acc, &(_, v)| acc + v)
})
.collect();
Ok(error)
}
struct Boxes {
full: Vec<Interval>,
deviation: Vec<Interval>,
}
impl Boxes {
fn new<D: Dynamics>(
dynamics: &D,
model: &Linearization,
expansion: &Expansion,
(states, drift, u): (&SparsePolyZonotope, &[Interval], &Zonotope),
step: f64,
) -> Result<Self, ReachError> {
let n = states.dim();
let full = deviation(states, u, &model.point).interval_hull().0;
if !tame(full.iter()) {
return Err(ReachError::AbstractionDiverged);
}
let region: Vec<Interval> = full
.iter()
.zip(&model.point)
.map(|(&d, &z)| d + Interval::point(z))
.collect();
let flow = dynamics.flow(®ion);
if !tame(flow.iter()) || !tame(expansion.start.iter()) {
return Err(ReachError::AbstractionDiverged);
}
let duration = Interval::new(0.0, step);
if !tame(drift.iter()) {
return Err(ReachError::AbstractionDiverged);
}
let deviation = flow
.iter()
.zip(drift)
.zip(model.point.iter().zip(&model.middle))
.map(|((&f, &d), (&star, &middle))| {
intersect(duration * f, d) - (Interval::point(star) - Interval::point(middle))
})
.chain(full[n..].iter().copied())
.collect();
Ok(Self { full, deviation })
}
}
fn cross(half_hessian: &IntervalMatrix, start: &[Interval], deviation: &[Interval]) -> Interval {
pairs(start.len()).fold(Interval::point(0.0), |acc, (a, b)| {
let m = half_hessian.0[(a, b)];
acc + m * start[a] * deviation[b] + m * deviation[a] * start[b]
})
}
fn quadratic_form(half_hessian: &IntervalMatrix, deviation: &[Interval]) -> Interval {
pairs(deviation.len()).fold(Interval::point(0.0), |acc, (a, b)| {
let product = if a == b {
square(deviation[a])
} else {
deviation[a] * deviation[b]
};
acc + half_hessian.0[(a, b)] * product
})
}
fn remainder(tensor: &IntervalTensor, full: &[Interval]) -> Interval {
let w = full.len();
let mut sum = Interval::point(0.0);
for a in 0..w {
for b in a..w {
for c in b..w {
sum = sum + coefficient(tensor, (a, b, c)) * monomial(full, (a, b, c));
}
}
}
sum / Interval::point(6.0)
}
fn cubic_change(
tensor: &IntervalTensor,
start: &[Interval],
deviation: &[Interval],
full: &[Interval],
) -> Interval {
let w = full.len();
let mut sum = Interval::point(0.0);
for a in 0..w {
for b in 0..w {
for c in 0..w {
let change = deviation[a] * full[b] * full[c]
+ start[a] * deviation[b] * full[c]
+ start[a] * start[b] * deviation[c];
sum = sum + tensor[(a, b, c)] * change;
}
}
}
sum / Interval::point(6.0)
}
fn difference(tensor: &IntervalTensor, cubic: &IntervalTensor) -> IntervalTensor {
let d = tensor.dim();
let mut out = IntervalTensor::zeros(d);
for k in 0..d * d * d {
let at = (k / (d * d), (k / d) % d, k % d);
out[at] = tensor[at] - cubic[at];
}
out
}
fn sixth_of_center(tensor: &IntervalTensor) -> IntervalTensor {
map_entries(tensor, |iv| {
Interval::point(f64::midpoint(iv.lo, iv.hi) / 6.0)
})
}
fn times_six(sixth: &IntervalTensor) -> IntervalTensor {
map_entries(sixth, |iv| iv * Interval::point(6.0))
}
fn map_entries(tensor: &IntervalTensor, f: impl Fn(Interval) -> Interval) -> IntervalTensor {
let d = tensor.dim();
let mut out = IntervalTensor::zeros(d);
for k in 0..d * d * d {
let at = (k / (d * d), (k / d) % d, k % d);
out[at] = f(tensor[at]);
}
out
}
fn state_slices(sixth: &[IntervalTensor], n: usize) -> Vec<Vec<DMatrix<f64>>> {
sixth
.iter()
.map(|t| {
(0..n)
.map(|a| DMatrix::from_fn(n, n, |b, c| t[(a, b, c)].lo))
.collect()
})
.collect()
}
fn coefficient(tensor: &IntervalTensor, (a, b, c): (usize, usize, usize)) -> Interval {
let mut orderings = [
(a, b, c),
(a, c, b),
(b, a, c),
(b, c, a),
(c, a, b),
(c, b, a),
];
orderings.sort_unstable();
let mut distinct = orderings.to_vec();
distinct.dedup();
distinct
.iter()
.fold(Interval::point(0.0), |acc, &at| acc + tensor[at])
}
fn monomial(full: &[Interval], (a, b, c): (usize, usize, usize)) -> Interval {
match (a == b, b == c) {
(true, true) => square(full[a]) * full[a],
(true, false) => square(full[a]) * full[c],
(false, true) => full[a] * square(full[b]),
(false, false) => full[a] * full[b] * full[c],
}
}
fn jacobian_spread(model: &Linearization, row: usize, full: &[Interval]) -> Interval {
let rho = full
.iter()
.enumerate()
.fold(Interval::point(0.0), |acc, (j, d)| {
acc + Interval::point(model.jacobian_radius[(row, j)]) * Interval::point(d.mag())
});
symmetric(rho.hi)
}
fn spread(radius: &DMatrix<f64>, start: &[Interval]) -> f64 {
pairs(start.len())
.fold(Interval::point(0.0), |acc, (a, b)| {
acc + Interval::point(radius[(a, b)])
* Interval::point(start[a].mag())
* Interval::point(start[b].mag())
})
.hi
}
fn pairs(w: usize) -> impl Iterator<Item = (usize, usize)> {
(0..w).flat_map(move |a| (0..w).map(move |b| (a, b)))
}
fn square(iv: Interval) -> Interval {
let p = iv * iv;
Interval::new(p.lo.max(0.0), p.hi)
}
fn intersect(a: Interval, b: Interval) -> Interval {
let (lo, hi) = (a.lo.max(b.lo), a.hi.min(b.hi));
if lo <= hi { Interval::new(lo, hi) } else { a }
}
fn symmetric(r: f64) -> Interval {
Interval::new(-r, r)
}
fn tame<'a>(mut intervals: impl Iterator<Item = &'a Interval>) -> bool {
intervals.all(|iv| iv.lo.is_finite() && iv.hi.is_finite() && iv.mag() <= TAME)
}
fn tensor_entries(tensor: &IntervalTensor) -> impl Iterator<Item = &Interval> {
let d = tensor.dim();
(0..d * d * d).map(move |k| &tensor[(k / (d * d), (k / d) % d, k % d)])
}
pub(super) fn cap_degree(pz: &SparsePolyZonotope, max_degree: u32) -> SparsePolyZonotope {
let e = pz.exponents();
let (low, high): (Vec<usize>, Vec<usize>) =
(0..e.ncols()).partition(|&j| e.column(j).iter().sum::<u32>() <= max_degree);
if high.is_empty() {
return pz.clone();
}
let n = pz.dim();
let part = |columns: &[usize], c: DVector<f64>, gi: DMatrix<f64>| {
SparsePolyZonotope::new(
c,
pz.dependent_generators().select_columns(columns),
gi,
e.select_columns(columns),
pz.ids().to_vec(),
)
.expect("columns of a valid set")
};
let kept = part(
&low,
pz.center().clone(),
pz.independent_generators().clone(),
);
let enclosed = part(&high, DVector::zeros(n), DMatrix::zeros(n, 0)).to_zonotope();
kept.exact_plus(&without_factors(&enclosed))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn coefficients_count_orderings_once() {
let mut t = IntervalTensor::zeros(2);
for (k, at) in [(0, 0, 0), (0, 0, 1), (0, 1, 0), (1, 0, 0), (1, 1, 1)]
.into_iter()
.enumerate()
{
t[at] = Interval::point(f64::from(u8::try_from(k + 1).unwrap()));
}
assert_eq!(coefficient(&t, (0, 0, 0)), Interval::point(1.0));
assert_eq!(coefficient(&t, (0, 0, 1)), Interval::point(9.0));
assert_eq!(coefficient(&t, (1, 1, 1)), Interval::point(5.0));
}
#[test]
fn remainder_keeps_squares_non_negative() {
let mut t = IntervalTensor::zeros(2);
for at in [(0, 0, 1), (0, 1, 0), (1, 0, 0)] {
t[at] = Interval::point(2.0);
}
let r = remainder(&t, &[Interval::new(-1.0, 1.0), Interval::new(1.0, 2.0)]);
assert!(r.lo >= 0.0 && r.contains(2.0, 1e-12) && r.hi <= 2.0 + 1e-12);
}
#[test]
fn capped_set_contains_the_original() {
let pz = SparsePolyZonotope::new(
DVector::from_element(1, 0.5),
DMatrix::from_row_slice(1, 2, &[1.0, 1.0]),
DMatrix::zeros(1, 0),
DMatrix::from_row_slice(1, 2, &[1, 3]),
polyzonotope::FactorId::fresh_n(1),
)
.unwrap();
let capped = cap_degree(&pz, 1);
assert!(capped.exponents().iter().all(|&e| e <= 1));
assert_eq!(capped.ids(), pz.ids());
for k in 0..=20 {
let alpha = -1.0 + 0.1 * f64::from(k);
let value = 0.5 + alpha + alpha * alpha * alpha;
let beta: Vec<f64> = (0..capped.independent_generators().ncols())
.map(|k| if k == 0 { alpha * alpha * alpha } else { 0.0 })
.collect();
let at = capped.evaluate(&[alpha], &beta);
assert!((at[0] - value).abs() < 1e-12, "α = {alpha}");
}
}
#[test]
fn augmentation_layout() {
let a = DMatrix::from_row_slice(2, 2, &[1.0, 2.0, 3.0, 4.0]);
let system = augmented_system(&a);
let mut expected = DMatrix::zeros(6, 6);
expected
.view_mut((0, 0), (2, 4))
.copy_from(&DMatrix::from_row_slice(
2,
4,
&[1.0, 2.0, 1.0, 0.0, 3.0, 4.0, 0.0, 1.0],
));
assert_eq!(system.a, expected);
assert_eq!(system.b, DMatrix::identity(6, 2));
}
#[test]
fn start_copies_cancel() {
let x = SparsePolyZonotope::new(
DVector::from_column_slice(&[1.0, 2.0]),
DMatrix::from_row_slice(2, 1, &[0.5, 0.25]),
DMatrix::from_row_slice(2, 1, &[0.1, -0.2]),
DMatrix::from_row_slice(1, 1, &[2]),
polyzonotope::FactorId::fresh_n(1),
)
.unwrap();
let static_set = SparsePolyZonotope::from_zonotope(&Zonotope::new(
DVector::from_column_slice(&[3.0, 4.0]),
DMatrix::identity(2, 2),
));
let start = augmented_start(&x, &static_set);
assert_eq!(project(&start, 2).interval_hull(), x.interval_hull());
for d in drift(&start, 2) {
assert!(d.lo.abs() < 1e-15 && d.hi.abs() < 1e-15, "{d:?}");
}
}
}