polyzonotope_reach/
linear.rs1use alloc::vec::Vec;
5
6use nalgebra::{DMatrix, DVector};
7use polyzonotope::{Interval, IntervalMatrix, SparsePolyZonotope, Zonotope};
8
9use crate::ReachError;
10use crate::taylor::Taylor;
11
12#[derive(Clone, Debug, PartialEq)]
14pub struct LinearSystem {
15 pub a: DMatrix<f64>,
17 pub b: DMatrix<f64>,
19}
20
21#[derive(Clone, Debug, PartialEq)]
23pub struct Step {
24 pub time_point: SparsePolyZonotope,
26 pub time_interval: SparsePolyZonotope,
28 pub duration: f64,
31}
32
33pub struct LinearReach {
35 system: LinearSystem,
37 taylor: Taylor,
39 exponential: IntervalMatrix,
41 input_solution: IntervalMatrix,
43 state_correction: IntervalMatrix,
45 input_correction: IntervalMatrix,
47}
48
49impl LinearReach {
50 pub fn new(system: LinearSystem, step: f64, taylor_terms: usize) -> Result<Self, ReachError> {
63 let n = system.a.nrows();
64 assert_eq!(system.a.ncols(), n, "A must be square");
65 assert_eq!(system.b.nrows(), n, "B needs one row per state");
66 let taylor = Taylor::new(&system.a, step, taylor_terms)?;
67
68 Ok(Self {
69 exponential: taylor.exponential(),
70 input_solution: taylor.input_solution(),
71 state_correction: taylor.state_correction(),
72 input_correction: taylor.input_correction(),
73 taylor,
74 system,
75 })
76 }
77
78 #[must_use]
91 pub fn step(&self, x: &SparsePolyZonotope, u: &Zonotope) -> Step {
92 let input = u.linear_map(&self.system.b);
95 let constant = input.center().clone();
96 let varying = Zonotope::new(DVector::zeros(input.dim()), input.generators().clone());
97 let particular = self.varying_input_response(&varying);
98
99 let input_effect = point_response(&self.input_solution, &constant);
100 let time_point = x
101 .interval_map(&self.exponential)
102 .minkowski_sum(&input_effect)
103 .minkowski_sum(&particular);
104 let time_interval = hull_with_successor(x, &self.exponential, &input_effect)
105 .minkowski_sum(&x.interval_map(&self.state_correction))
106 .minkowski_sum(&point_response(&self.input_correction, &constant))
107 .minkowski_sum(&particular);
108
109 Step {
110 time_point,
111 time_interval,
112 duration: self.taylor.step,
113 }
114 }
115
116 fn varying_input_response(&self, v: &Zonotope) -> SparsePolyZonotope {
118 let pz = without_factors(v);
119 (0..=self.taylor.terms())
120 .map(|i| pz.interval_map(&self.taylor.input_term(i)))
121 .fold(
122 pz.interval_map(
123 &self
124 .taylor
125 .remainder
126 .scale(Interval::point(self.taylor.step)),
127 ),
128 |acc, term| acc.minkowski_sum(&term),
129 )
130 }
131}
132
133fn point_response(m: &IntervalMatrix, v: &DVector<f64>) -> SparsePolyZonotope {
135 let point = Zonotope::new(v.clone(), DMatrix::zeros(v.len(), 0));
136 without_factors(&point.interval_map(m))
137}
138
139pub(crate) fn without_factors(z: &Zonotope) -> SparsePolyZonotope {
142 SparsePolyZonotope::new(
143 z.center().clone(),
144 DMatrix::zeros(z.dim(), 0),
145 z.generators().clone(),
146 DMatrix::zeros(0, 0),
147 Vec::new(),
148 )
149 .expect("consistent shapes")
150}
151
152fn hull_with_successor(
165 x: &SparsePolyZonotope,
166 phi: &IntervalMatrix,
167 offset: &SparsePolyZonotope,
168) -> SparsePolyZonotope {
169 let n = x.dim();
170 let (center, radius) = phi.center_radius();
171 let identity = DMatrix::identity(n, n);
172 let halved = |sign: f64| {
175 IntervalMatrix::point(&identity)
176 .add(&IntervalMatrix::point(&(¢er * sign)))
177 .scale(Interval::point(0.5))
178 };
179 let offset_point = Zonotope::new(offset.center().clone(), DMatrix::zeros(n, 0));
180 let half_offset = without_factors(&offset_point).linear_map(&(&identity * 0.5));
181
182 let mean = x.interval_map(&halved(1.0)).exact_plus(&half_offset);
183 let difference = x
184 .interval_map(&halved(-1.0))
185 .exact_plus(&half_offset.linear_map(&-&identity));
186 let swing = SparsePolyZonotope::new(
187 DVector::zeros(n),
188 DMatrix::zeros(n, 0),
189 columns(&[
190 difference.dependent_generators(),
191 difference.independent_generators(),
192 &DMatrix::from_column_slice(n, 1, difference.center().as_slice()),
193 ]),
194 DMatrix::zeros(0, 0),
195 Vec::new(),
196 )
197 .expect("consistent shapes");
198 let spread = IntervalMatrix(radius.map(|r| Interval::new(-r, r)));
199 let offset_spread = offset.translate(&-offset.center());
200
201 mean.minkowski_sum(&swing)
202 .minkowski_sum(&x.interval_map(&spread))
203 .minkowski_sum(&offset_spread)
204}
205
206fn columns(parts: &[&DMatrix<f64>]) -> DMatrix<f64> {
208 let rows = parts[0].nrows();
209 let all: Vec<f64> = parts.iter().flat_map(|m| m.iter().copied()).collect();
210 DMatrix::from_vec(rows, all.len() / rows.max(1), all)
211}