polyzonotope-reach 0.1.3

Validated reachable-set over-approximation of linear and nonlinear dynamics with sparse polynomial zonotopes
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
//! Reachable sets of nonlinear systems `x' = f(x, u)` by conservative
//! linearization (Althoff, Stursberg & Buss 2008) or conservative
//! polynomialization (Althoff 2013), with polynomial zonotopes (Kochdumper &
//! Althoff 2021).
//!
//! Each step linearizes `f` around a point `z* = (x*, u*)` and treats the
//! rest, the abstraction error, as an extra uncertain input of the linear
//! system. The error depends on the time-interval set it is meant to
//! produce, so it is guessed, checked and enlarged until the check passes.
//! With [`Method::Polynomialization`], the second-order error at the start
//! set is computed once, as a polynomial zonotope over the set's factors,
//! and fed in exactly; only the rest is guessed:
//!
//! ```mermaid
//! flowchart LR
//!     L[linearize at z*] --> P[static error Ψ_s over X, polynomialization only]
//!     P --> R[linear step with error box Ψ̄]
//!     R --> E[error Ψ over the time-interval set]
//!     E -->|Ψ ⊆ Ψ̄| A[accept, reduce]
//!     E -->|Ψ ⊄ Ψ̄| W[Ψ̄ := widen Ψ̄ ∪ Ψ]
//!     W --> R
//!     W -->|too many tries| S[split the set or refuse]
//! ```

use alloc::vec;
use alloc::vec::Vec;

use nalgebra::{DMatrix, DVector};
use polyzonotope::{Interval, IntervalBox, Reduction, SparsePolyZonotope, Zonotope};

use crate::{Dynamics, LinearReach, LinearSystem, ReachError, Step};

mod driver;
mod error;
mod polynomialization;
mod split;

pub use driver::Adaptive;

#[cfg(test)]
mod ablation;

/// How [`NonlinearReach`] abstracts the dynamics in each step.
///
/// Non-exhaustive: more abstractions may be added.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum Method {
    /// Conservative linearization (Althoff, Stursberg & Buss 2008): the
    /// whole abstraction error `½ Δzᵀ H(ξ) Δz` over the time-interval set is
    /// one box, guessed and checked. Needs the Hessians only. The default,
    /// because every [`Dynamics`] supports it.
    #[default]
    Linearization,
    /// Conservative polynomialization (Althoff 2013): the second- and
    /// third-order error at the start set stays a polynomial zonotope over
    /// the set's factors and moves with the set; only what changes during
    /// the step and the Lagrange remainder around the third derivatives at
    /// `z*` go into the box. Needs [`Dynamics::third_derivatives`]. Tighter
    /// for large sets, since the box shrinks from `O(w²)` to `O(w r + w² r)`
    /// plus the spread of `∂³f` times `w³` for a set of width `w`; that spread
    /// comes from rounding only for fields of degree 3 or less.
    ///
    /// # The error decomposition
    ///
    /// Let `f` be three times continuously differentiable, `z* = (x*, u*)` the
    /// linearization point and `[A B]` the center of the enclosure of `J(z*)`,
    /// with `w ∈ f(z*) − A x* − B u*` the model offset. For a trajectory with
    /// `x(t_k) = x_k` and any input signal `u(t) ∈ U`, write
    ///
    /// - `Δz(t) = z(t) − z*` with `z(t) = (x(t), u(t))`,
    /// - `s = (x_k − x_m, 0)`, the start of the trajectory relative to the
    ///   middle `x_m` of the start set's hull, which is constant along the
    ///   trajectory, and
    /// - `δ(t) = Δz(t) − s = (x(t) − x_k − c, u(t) − u*)`, what changes, with
    ///   `c = x* − x_m` the half step from `x_m` to the linearization point.
    ///
    /// Any constant split of `Δz` would do; this one centers both parts. With
    /// `s = (x_k − x*, 0)` instead, `s` would be off-center by the half step and
    /// `δ` would range over the whole step's drift `[0, r] f`, which made the
    /// sets of small starts larger than those of linearization in a trial on
    /// Van der Pol. Centered, `δ` spans about `±½ r f`.
    ///
    /// Taylor's theorem with the Lagrange remainder gives, row by row and for
    /// some `ξ(t)` on the segment from `z*` to `z(t)`,
    ///
    /// ```text
    /// f(z) = f(z*) + J(z*) Δz + ½ Δzᵀ H(z*) Δz + ⅙ Σ_{abc} ∂³f(ξ)_{abc} Δz_a Δz_b Δz_c.
    /// ```
    ///
    /// With `½ H(z*) ∈ Q ± R_Q`, `J(z*) ∈ [A B] ± R_J` and a point tensor
    /// `P ≈ ⅙ ∂³f(z*)` (the midpoints, divided by 6 and rounded), write
    /// `T_c` for an enclosure of `6 P` and
    ///
    /// ```text
    /// ⅙ T(ξ) Δz³ = P s³ + ⅙ T_c (Δz³ − s³) + ⅙ (T(ξ) − T_c) Δz³,
    /// ```
    ///
    /// exact for the real `6 P ∈ T_c`. The error of the linear model,
    /// `e = f(z) − (A x + B u + w)`, then splits into
    ///
    /// ```text
    /// e = ψ_s(x_k) + ψ_d(t),
    /// ψ_s(x_k) = sᵀ Q s + P s³                                   (static)
    /// ψ_d(t)  ∈ sᵀ [±R_Q] s + sᵀ (½H) δ + δᵀ (½H) s + δᵀ (½H) δ  (dynamic)
    ///           + ⅙ T_c (Δz³ − s³) + ⅙ (T(ξ) − T_c) Δz³ + [±R_J] |Δz|.
    /// ```
    ///
    /// The change of the cubic term is bounded through the telescoping
    /// identity `Δz_a Δz_b Δz_c − s_a s_b s_c = δ_a Δz_b Δz_c + s_a δ_b Δz_c +
    /// s_a s_b δ_c`, so it is `O(w² r)` instead of the `O(w³)` of the whole
    /// cube over `□Δz`.
    ///
    /// The inputs are never part of `s`: `u(t)` may jump anywhere in `U` during
    /// the step, so only the state at `t_k` is constant along a trajectory.
    /// Every term with `u − u*` is dynamic.
    ///
    /// # Why the sets enclose every trajectory
    ///
    /// The static part is a polynomial of `x_k` alone. The step computes
    /// `Ψ_s = { sᵀ Q s + P s³ | x_k ∈ X }` with the quadratic and the cubic
    /// map of the polynomial zonotope `X − x_m`, joined by `exact_plus`, which
    /// keeps the factors of `X`: the point of `Ψ_s` that belongs to
    /// `x_k = X(α, β)` uses the same `α`. Degrees above the cap are
    /// enclosed as independent generators, which only adds points.
    ///
    /// The static error acts on the trajectory as a constant input `v = ψ_s(x_k)`.
    /// The step therefore runs the linear reach on the augmented system
    /// `x' = A x + v + B u + w + ψ_d`, `v' = 0`, `x₀' = 0`, from
    /// `{ (x_k, ψ_s(x_k), x_k) | x_k ∈ X }`, built by one linear map of `X`
    /// and [`SparsePolyZonotope::exact_plus`](polyzonotope::SparsePolyZonotope::exact_plus),
    /// which keep the shared factors and pair the independent generators of
    /// both copies of `x_k`. The linear
    /// reach is sound for any input signal in its input set, so with
    /// `ψ_d(t) ∈ Ψ̄` for all `t` of the step, the projection of its sets onto
    /// `x` holds the trajectory at `t_{k+1}` and during the step. The static
    /// error never needs a guess: it is exact up to the degree cap, and it moves
    /// with the set rather than being smeared over the whole step as a box.
    ///
    /// The dynamic part needs boxes of `s`, `δ` and `Δz`. The step
    /// takes `s` from the interval hull of `X − x_m`, `Δz` from the hull of the
    /// time-interval set `R(τ)` and `U`, the input part of `δ` from `U − u*`, and
    /// the state part, minus `c`, from two enclosures of `x(t) − x_k`,
    /// intersected: the flow,
    /// `x(t) − x_k = ∫_{t_k}^t f(z(σ)) dσ ∈ [0, r] · f(□R(τ) × □U)`
    /// (enclosed in interval arithmetic), and the hull of `x − x₀` over the
    /// augmented time-interval set, where the factors of `x_k` cancel. Both
    /// hold as long as the trajectory stays in `R(τ)` up to `t` with its
    /// error in `Ψ̄`. That is the same hypothesis the
    /// guess-and-check loop proves for every other term, so the usual argument
    /// closes: if the dynamic error computed over `R(τ)` lies in the guess `Ψ̄`,
    /// no trajectory can leave `R(τ)` during the step, because up to the first
    /// exit its error lies in `Ψ̄` and the linear model with an input in `Ψ̄`
    /// keeps it in `R(τ)`. The remainder evaluates `∂³f` over a box that holds
    /// every segment from `z*` into `z* + □Δz`, which holds every `ξ(t)`.
    ///
    /// The decomposition is exact algebra, every box encloses its quantity, and
    /// every product and sum of boxes is interval arithmetic, outward rounded
    /// with the `sound` feature. Removing any one term makes the claim false;
    /// the unit tests show that for each term that floating-point rounding does
    /// not dominate.
    Polynomialization,
}

/// How [`NonlinearReach::reach`] splits a piece whose step diverged.
///
/// Non-exhaustive: more strategies may be added.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum Splitting {
    /// Factor splits and box splits compete, and the one whose larger half
    /// has the smaller second-order error bound wins, split by split. The
    /// default: on the cubic drift `x₀' = x₀³` from radius 0.5, linearization
    /// with factor splits alone needs 4 pieces where this needs 2.
    #[default]
    Cheapest,
    /// Factor splits only: every piece keeps all dependencies of the start
    /// set, at the price of more pieces.
    Factors,
}

/// What becomes of the generators that order reduction and the linear step
/// make independent.
///
/// Non-exhaustive: more restructurings may be added.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum Restructure {
    /// They stay independent generators. Their products in the next step's
    /// quadratic error are enclosed one by one, so a wide set loses some of
    /// its shape step by step. The default: the time-interval hull pairs
    /// each independent generator with its image, which keeps most of that
    /// shape, and [`Lift`](Self::Lift) costs more for little gain.
    #[default]
    Off,
    /// After each step's Girard reduction, every independent generator of
    /// the time-point set becomes a dependent factor of its own. That is
    /// exact (`g β` with `β ∈ [-1, 1]` is the monomial `g α` of a factor
    /// nowhere else), and from then on the quadratic maps of later steps
    /// treat it as a polynomial: `α²` stays non-negative and products
    /// cancel. Where nothing merges, it costs 1.5 to 1.7 times as much as
    /// [`Off`](Self::Off) (40 steps of Van der Pol from radius 0.5). The number
    /// of factors stays at most the number of generators, since
    /// [`compact`](polyzonotope::SparsePolyZonotope::compact) drops the
    /// unused ones.
    ///
    /// The gain is small: on Van der Pol from radius 0.5, polynomialization
    /// ends 40 steps 3% narrower than with [`Off`](Self::Off), and from
    /// radius 1 it proves 26 steps instead of 25. On the damped cubic
    /// `x₀' = −x₀ − x₀³` from radius 1, both need 6 pieces, and the lifted
    /// sets end 31% wider; but lifted pieces merge back to 3, where
    /// unlifted ones stay apart, so that run takes 4.6 s instead of 6.8 s.
    Lift,
}

/// How [`NonlinearReach`] steps through time.
#[derive(Clone, Debug, PartialEq)]
pub struct NonlinearSettings {
    /// Time step `r`, positive.
    pub step: f64,
    /// Terms of the matrix exponential after the first, see [`LinearReach::new`].
    pub taylor_terms: usize,
    /// Order (generators per dimension, at least 1) the sets are reduced to
    /// after every step and before the error is computed.
    pub order: f64,
    /// Most guesses of the error per step, at least 1: the first guess plus
    /// up to `max_iterations − 1` enlargements before the step is given up.
    pub max_iterations: usize,
    /// Relative widening of the error guess on each retry: every bound moves
    /// outward by this fraction of the interval's magnitude.
    pub enlarge: f64,
    /// Most pieces a set may be split into per time step before a failing
    /// step is refused; 1 never splits.
    pub max_pieces: usize,
    /// Which splits [`reach`](NonlinearReach::reach) may use.
    pub splitting: Splitting,
    /// The abstraction of each step.
    pub method: Method,
    /// How much larger than the two pieces' interval hulls together the
    /// hull of their merged enclosure may be, by volume, as a fraction (0.1
    /// allows 10%), for [`reach`](NonlinearReach::reach) to merge them into
    /// one piece for the next step. Non-negative. A merge is also never
    /// wider than half the narrowest piece that diverged so far, so splits
    /// are not undone while the pieces are still too large to step whole.
    pub merge_tolerance: f64,
    /// Highest total degree the static error of
    /// [`Method::Polynomialization`] keeps exact, at least 1. It is cubic
    /// in the set, so without a cap the degree of the sets would triple
    /// with every step; higher monomials are enclosed in independent
    /// generators.
    pub max_degree: u32,
    /// Whether the time-point set's independent generators become
    /// dependent factors before the next step.
    pub restructure: Restructure,
    /// Adaptive step lengths for [`reach`](NonlinearReach::reach) and
    /// [`reach_for`](NonlinearReach::reach_for), starting from `step`; `None`
    /// keeps every step at `step`.
    pub adaptive: Option<Adaptive>,
}

impl NonlinearSettings {
    /// Defaults for time step `step`: 4 Taylor terms, order 20, 10
    /// iterations, 10% widening, up to 8 pieces from the cheapest splits,
    /// merged within 10%, independent generators kept, fixed steps,
    /// linearization, and degree 4 for polynomialization.
    #[must_use]
    pub fn new(step: f64) -> Self {
        Self {
            step,
            taylor_terms: 4,
            order: 20.0,
            max_iterations: 10,
            enlarge: 0.1,
            max_pieces: 8,
            splitting: Splitting::Cheapest,
            method: Method::Linearization,
            merge_tolerance: 0.1,
            max_degree: 4,
            restructure: Restructure::Off,
            adaptive: None,
        }
    }
}

/// One-step and multi-step reachability of [`Dynamics`].
///
/// # Examples
///
/// ```
/// use nalgebra::{DMatrix, DVector};
/// use polyzonotope_reach::polyzonotope::{SparsePolyZonotope, Zonotope};
/// use polyzonotope_reach::{NonlinearReach, NonlinearSettings, Polynomial};
///
/// // Van der Pol with μ = 1, no inputs.
/// let vdp = Polynomial::autonomous(2)
///     .term(0, 1.0, &[0, 1])
///     .term(1, 1.0, &[0, 1])
///     .term(1, -1.0, &[2, 1])
///     .term(1, -1.0, &[1, 0]);
/// let reach = NonlinearReach::new(vdp, NonlinearSettings::new(0.02));
/// let start = SparsePolyZonotope::from_zonotope(&Zonotope::new(
///     DVector::from_vec(vec![1.4, 2.4]),
///     DMatrix::from_diagonal_element(2, 2, 0.05),
/// ));
///
/// let flowpipe = reach.reach_autonomous(&start, 10)?;
/// let hull = flowpipe[9][0].time_point.interval_hull();
/// assert!(hull.0[0].lo > 1.4 && hull.0[1].hi < 2.4);
/// # Ok::<(), polyzonotope_reach::ReachFailure>(())
/// ```
pub struct NonlinearReach<D> {
    /// The vector field.
    dynamics: D,
    /// Step size and limits, checked by [`new`](Self::new).
    settings: NonlinearSettings,
}

/// The linear model `x' = A x + B u + w` of one step, with
/// `w ∈ f(z*) − A x* − B u*`.
pub(crate) struct Linearization {
    /// The point `z* = (x*, u*)`.
    point: Vec<f64>,
    /// `x_m`, the middle of the interval hull of the start set, from which
    /// `x*` is half a step along `f`; `n` entries.
    middle: Vec<f64>,
    /// `A`, the center of `∂f/∂x` at `z*`.
    a: DMatrix<f64>,
    /// `B`, the center of `∂f/∂u` at `z*`.
    b: DMatrix<f64>,
    /// An enclosure of `f(z*) − A x* − B u*`.
    offset: Vec<Interval>,
    /// The radius of `∂f/∂z` at `z*` around `[A B]`: zero unless rounding
    /// made the point evaluation an interval.
    jacobian_radius: DMatrix<f64>,
}

impl<D: Dynamics> NonlinearReach<D> {
    /// Reachability of `dynamics` with `settings`.
    ///
    /// # Panics
    ///
    /// Panics if the step is not positive and finite, the order is below 1,
    /// `enlarge` or `merge_tolerance` is negative, `max_iterations`,
    /// `max_pieces` or `max_degree` is zero, or, with [`Adaptive`] steps, `min_step` is not
    /// positive, `max_step` not finite, `step` not in
    /// `[min_step, max_step]`, `shrink` not in `(0, 1)` or `grow` below 1.
    #[must_use]
    pub fn new(dynamics: D, settings: NonlinearSettings) -> Self {
        assert!(
            settings.step > 0.0 && settings.step.is_finite(),
            "step must be positive"
        );
        assert!(settings.order >= 1.0, "order must be at least 1");
        assert!(settings.enlarge >= 0.0, "enlarge must not be negative");
        assert!(
            settings.merge_tolerance >= 0.0,
            "merge_tolerance must not be negative"
        );
        assert!(
            settings.max_iterations >= 1,
            "max_iterations must be at least 1"
        );
        assert!(settings.max_pieces >= 1, "max_pieces must be at least 1");
        assert!(settings.max_degree >= 1, "max_degree must be at least 1");
        if let Some(adaptive) = &settings.adaptive {
            adaptive.check(settings.step);
        }

        Self { dynamics, settings }
    }

    /// The reachable sets one step after `x`, for any integrable input
    /// signal with values in `u`; for an autonomous system, `u` is the
    /// zero-dimensional zonotope (or use
    /// [`step_autonomous`](Self::step_autonomous)).
    ///
    /// Both sets are reduced to the configured order, so the time-point set
    /// can go straight into the next step.
    ///
    /// # Errors
    ///
    /// - [`ReachError::StepTooLarge`] if the matrix exponential's remainder
    ///   bound fails for the linearized `A`.
    /// - [`ReachError::AbstractionDiverged`] if no error guess was confirmed
    ///   within `max_iterations`, or the model or the error stopped being
    ///   finite: the set or the step is too large for the abstraction. No
    ///   set is returned then, since none is proven.
    /// - [`ReachError::MissingDerivatives`] with
    ///   [`Method::Polynomialization`] if the dynamics supply no third
    ///   derivatives.
    ///
    /// # Panics
    ///
    /// Panics if `x` does not have one row per state or `u` one per input.
    pub fn step(&self, x: &SparsePolyZonotope, u: &Zonotope) -> Result<Step, ReachError> {
        let attempt = Attempt {
            step: self.settings.step,
            terms: Terms::ALL,
        };
        self.step_with(x, u, attempt).map(|(step, _)| step)
    }

    /// [`step`](Self::step) with the step length and error terms of
    /// `attempt`, and how many error guesses it took; the tests drop one term
    /// at a time to show that each is needed.
    fn step_with(
        &self,
        x: &SparsePolyZonotope,
        u: &Zonotope,
        attempt: Attempt,
    ) -> Result<(Step, usize), ReachError> {
        assert_eq!(x.dim(), self.dynamics.states(), "x needs one row per state");
        assert_eq!(u.dim(), self.dynamics.inputs(), "u needs one row per input");

        let model = self
            .linearize(x, u, attempt.step)
            .ok_or(ReachError::AbstractionDiverged)?;
        match self.settings.method {
            Method::Linearization => self.step_linearized(x, u, &model, attempt),
            Method::Polynomialization => self.step_polynomialized(x, u, &model, attempt),
        }
    }

    /// One step of [`Method::Linearization`]: the linear model with the
    /// whole error as a guessed box.
    fn step_linearized(
        &self,
        x: &SparsePolyZonotope,
        u: &Zonotope,
        model: &Linearization,
        attempt: Attempt,
    ) -> Result<(Step, usize), ReachError> {
        let n = self.dynamics.states();
        let system = LinearSystem {
            a: model.a.clone(),
            b: DMatrix::identity(n, n),
        };
        let linear = LinearReach::new(system, attempt.step, self.settings.taylor_terms)?;
        let known = u.linear_map(&model.b);

        self.confirm(n, attempt.step, |assumed| {
            let step = linear.step(x, &with_error(&known, &model.offset, assumed));
            let states = step
                .time_interval
                .reduce(self.settings.order, Reduction::Girard);
            let actual =
                error::abstraction_error(&self.dynamics, model, (&states, u), attempt.terms)
                    .ok_or(ReachError::AbstractionDiverged)?;

            Ok((step.time_point, states, actual))
        })
    }

    /// One step of [`Method::Polynomialization`]: the static error `Ψ_s`
    /// rides along as the constant input of an augmented linear system, and
    /// only the dynamic error is a guessed box. The soundness argument is on
    /// [`Method::Polynomialization`].
    fn step_polynomialized(
        &self,
        x: &SparsePolyZonotope,
        u: &Zonotope,
        model: &Linearization,
        attempt: Attempt,
    ) -> Result<(Step, usize), ReachError> {
        let n = self.dynamics.states();
        let limits = (self.settings.max_degree, self.settings.order);
        let expansion =
            polynomialization::static_error(&self.dynamics, model, x, limits, attempt.terms)?;
        let start = if attempt.terms.has(Term::StaticSet) {
            polynomialization::augmented_start(x, &expansion.static_set)
        } else {
            polynomialization::augmented_start(
                x,
                &SparsePolyZonotope::from_zonotope(&Zonotope::new(
                    DVector::zeros(n),
                    DMatrix::zeros(n, 0),
                )),
            )
        };
        let system = polynomialization::augmented_system(&model.a);
        let linear = LinearReach::new(system, attempt.step, self.settings.taylor_terms)?;
        let known = u.linear_map(&model.b);

        self.confirm(n, attempt.step, |assumed| {
            let step = linear.step(&start, &with_error(&known, &model.offset, assumed));
            let states = polynomialization::project(&step.time_interval, n)
                .reduce(self.settings.order, Reduction::Girard);
            let drift = polynomialization::drift(&step.time_interval, n);
            let actual = polynomialization::dynamic_error(
                &self.dynamics,
                (model, &expansion),
                (&states, &drift, u),
                attempt.step,
                attempt.terms,
            )?;

            Ok((
                polynomialization::project(&step.time_point, n),
                states,
                actual,
            ))
        })
    }

    /// `set` reduced to the configured order, as the start of the next step,
    /// with its independent generators lifted to factors under
    /// [`Restructure::Lift`].
    fn restructure(&self, set: &SparsePolyZonotope) -> SparsePolyZonotope {
        let reduced = set.reduce(self.settings.order, Reduction::Girard);
        match self.settings.restructure {
            Restructure::Off => reduced,
            Restructure::Lift => split::lift_independent(&reduced),
        }
    }

    /// The guess-and-check loop: `attempt` runs the step with the error
    /// guess `Ψ̄` and returns the time-point set, the reduced time-interval
    /// set and the error over it. The first attempt whose error lies in its
    /// guess is accepted, with the number of guesses it took; otherwise the
    /// guess widens, up to `max_iterations` times. `step` is the length of
    /// the step, recorded in the result.
    ///
    /// # Errors
    ///
    /// The first error of `attempt`, or [`ReachError::AbstractionDiverged`]
    /// if no guess was confirmed or the guess stopped being finite.
    fn confirm(
        &self,
        n: usize,
        step: f64,
        mut attempt: impl FnMut(
            &[Interval],
        ) -> Result<
            (SparsePolyZonotope, SparsePolyZonotope, Vec<Interval>),
            ReachError,
        >,
    ) -> Result<(Step, usize), ReachError> {
        let mut assumed = vec![Interval::point(0.0); n];
        for guesses in 1..=self.settings.max_iterations {
            let (time_point, time_interval, actual) = attempt(&assumed)?;
            if encloses(&assumed, &actual) {
                let step = Step {
                    time_point: self.restructure(&time_point),
                    time_interval,
                    duration: step,
                };
                return Ok((step, guesses));
            }

            assumed = widen(&assumed, &actual, self.settings.enlarge);
            if !all_finite(assumed.iter()) {
                break;
            }
        }

        Err(ReachError::AbstractionDiverged)
    }

    /// The linear model around `z* = (x* , c_u)`, where `x*` is the middle
    /// of `x`'s interval hull moved half a step along `f` (as CORA does);
    /// `None` if a value along the way is not finite.
    fn linearize(&self, x: &SparsePolyZonotope, u: &Zonotope, step: f64) -> Option<Linearization> {
        let n = self.dynamics.states();
        let middle: Vec<f64> = x
            .interval_hull()
            .0
            .iter()
            .map(|iv| f64::midpoint(iv.lo, iv.hi))
            .collect();
        let inputs = u.center().iter().copied();
        let drift = self
            .dynamics
            .flow(&points(middle.iter().copied().chain(inputs.clone())));
        let point: Vec<f64> = middle
            .iter()
            .zip(&drift)
            .map(|(&m, d)| m + 0.5 * step * f64::midpoint(d.lo, d.hi))
            .chain(inputs)
            .collect();
        if !point.iter().all(|v| v.is_finite()) {
            return None;
        }

        let at = points(point.iter().copied());
        let jacobian = self.dynamics.jacobian(&at);
        let flow = self.dynamics.flow(&at);
        if !all_finite(jacobian.0.iter()) || !all_finite(flow.iter()) {
            return None;
        }

        let (jacobian, jacobian_radius) = jacobian.center_radius();
        let offset = model_offset(&flow, &jacobian, &point)?;
        if !all_finite(offset.iter()) {
            return None;
        }

        Some(Linearization {
            a: jacobian.columns(0, n).into_owned(),
            b: jacobian.columns(n, point.len() - n).into_owned(),
            point,
            middle,
            offset,
            jacobian_radius,
        })
    }
}

/// The step length and error terms of one step attempt.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct Attempt {
    /// The step length `r`, positive and finite.
    pub(crate) step: f64,
    /// The error terms to include: all of them outside the tests.
    pub(crate) terms: Terms,
}

/// One term of the abstraction error, so that the tests can drop it.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Term {
    /// Linearization: `Δzᵀ Q Δz` over the time-interval set.
    Quadratic,
    /// Linearization: the spread of `½ H` over the segment box.
    HessianSpread,
    /// Both: the spread of the Jacobian at `z*`, from rounding only.
    JacobianSpread,
    /// Polynomialization: the static polynomial `Ψ_s = sᵀ Q s + P s³`.
    StaticSet,
    /// Polynomialization: the spread of `½ H(z*)` around `Q` over `s`, from
    /// rounding only.
    StaticRadius,
    /// Polynomialization: `sᵀ (½H) δ + δᵀ (½H) s`.
    Cross,
    /// Polynomialization: `δᵀ (½H) δ`.
    DynamicQuadratic,
    /// Polynomialization: the cubic form `P s³` in the static set.
    CubicStatic,
    /// Polynomialization: `⅙ T_c (Δz³ − s³)`, the change of the cubic term
    /// at `z*` during the step.
    CubicCross,
    /// Polynomialization: the third-order Lagrange remainder around `T_c`,
    /// `⅙ (T(ξ) − T_c) Δz³`.
    Remainder,
    /// Polynomialization: the input part `u − u*` of `δ`.
    InputDeviation,
}

/// A set of [`Term`]s, as a bit mask.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct Terms(u16);

impl Terms {
    /// Every term: what the public API always uses.
    pub(crate) const ALL: Self = Self(u16::MAX);

    /// Whether `term` is in the set.
    pub(crate) fn has(self, term: Term) -> bool {
        self.0 & Self::bit(term) != 0
    }

    /// The set without `term`.
    #[cfg(test)]
    pub(crate) fn without(self, term: Term) -> Self {
        Self(self.0 & !Self::bit(term))
    }

    /// The bit of `term`.
    fn bit(term: Term) -> u16 {
        1 << term as u16
    }
}

/// Whether every bound in `intervals` is finite (and so not NaN either).
fn all_finite<'a>(mut intervals: impl Iterator<Item = &'a Interval>) -> bool {
    intervals.all(|iv| iv.lo.is_finite() && iv.hi.is_finite())
}

/// Point intervals for `values`.
fn points(values: impl Iterator<Item = f64>) -> Vec<Interval> {
    values.map(Interval::point).collect()
}

/// `f(z*) − J z*` row by row in interval arithmetic, `J = [A B]`, or `None`
/// once a product or partial sum overflows: under `sound`, subtracting an
/// infinite bound panics instead of giving an infinite result.
fn model_offset(
    flow: &[Interval],
    jacobian: &DMatrix<f64>,
    point: &[f64],
) -> Option<Vec<Interval>> {
    flow.iter()
        .enumerate()
        .map(|(i, &f)| {
            point.iter().enumerate().try_fold(f, |acc, (j, &z)| {
                let term = Interval::point(jacobian[(i, j)]) * Interval::point(z);
                all_finite([acc, term].iter()).then(|| acc - term)
            })
        })
        .collect()
}

/// The input of the linear model: `B U ⊕ (w + Ψ̄)` with the box around its
/// midpoint, so its center is the constant part.
fn with_error(known: &Zonotope, offset: &[Interval], assumed: &[Interval]) -> Zonotope {
    let total = offset.iter().zip(assumed).map(|(&w, &e)| w + e).collect();
    known.minkowski_sum(&Zonotope::from_box(&IntervalBox(total)))
}

/// Whether every interval of `actual` lies in the matching one of `assumed`.
fn encloses(assumed: &[Interval], actual: &[Interval]) -> bool {
    assumed
        .iter()
        .zip(actual)
        .all(|(a, e)| a.lo <= e.lo && e.hi <= a.hi)
}

/// The hull of `assumed` and `actual`, every bound moved outward by
/// `enlarge` times the interval's magnitude.
fn widen(assumed: &[Interval], actual: &[Interval], enlarge: f64) -> Vec<Interval> {
    assumed
        .iter()
        .zip(actual)
        .map(|(a, e)| {
            let hull = Interval::new(a.lo.min(e.lo), a.hi.max(e.hi));
            let margin = enlarge * hull.mag();
            Interval::new(hull.lo - margin, hull.hi + margin)
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    /// An overflowing `J z*` gives no offset instead of panicking under `sound`.
    #[test]
    fn overflowing_offset_is_refused() {
        let jacobian = DMatrix::from_row_slice(1, 2, &[1e300, 1.0]);
        let flow = [Interval::point(1.0)];
        assert_eq!(model_offset(&flow, &jacobian, &[1e10, 1.0]), None);
        assert_eq!(
            model_offset(&flow, &jacobian, &[1.0, 1.0]),
            Some(vec![
                Interval::point(1.0) - Interval::point(1e300) - Interval::point(1.0)
            ])
        );
    }

    /// Widening covers both boxes and moves out by the relative margin.
    #[test]
    fn widening_covers_both() {
        let w = widen(&[Interval::new(-1.0, 1.0)], &[Interval::new(0.0, 2.0)], 0.5);
        assert_eq!(w, vec![Interval::new(-2.0, 3.0)]);
        assert!(encloses(&w, &[Interval::new(-1.0, 2.0)]));
        assert!(!encloses(&w, &[Interval::new(-1.0, 3.5)]));
    }
}