principia 0.2.0

Typed Newtonian numerical dynamics: state propagation, acceleration models, RK4/DOPRI5/DOP853 integrators, variational equations, STM, covariance, and gravity-field kernels.
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
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (C) 2026 Vallés Puig, Ramon

//! Cartesian state covariance and process noise.
//!
//! ## Scientific scope
//!
//! [`StateCovariance<F>`] stores a `6 × 6` Cartesian covariance in `[r, v]`
//! block form. [`ProcessNoise<F>`] stores an additive process-noise matrix in
//! the same layout. Covariances can be transported through a state-transition
//! matrix `Φ` and rotated between inertial and local trajectory frames.
//!
//! ## Technical scope
//!
//! The covariance uses the canonical block decomposition
//! `[[Σ_rr, Σ_rv], [Σ_vr, Σ_vv]]` with `Σ_vr = Σ_rvᵀ` derived on demand.
//!
//! ## References
//!
//! * Tapley, Schutz, Born, *Statistical Orbit Determination*, §4.
//! * Montenbruck & Gill, *Satellite Orbits*, §7.

use affn::frames::ReferenceFrame;
use affn::matrix3::{FrameMatrix3, SymmetricFrameMatrix3};
use affn::matrix6::FrameMatrix6;
use affn::ops::Rotation3;
use qtty::length::Kilometers;
use qtty::{KmPerSecond, KmPerSecondSquared, Quantity, RelativeTolerance, Second};

use crate::error::PrincipiaError;
use crate::frames::LocalTrajectoryFrame;
use crate::variational::StateTransitionMatrix;

/// Frame-tagged Cartesian state covariance stored as three `3 × 3` blocks.
#[derive(Debug, Clone, Copy)]
pub struct StateCovariance<F: ReferenceFrame> {
    rr: SymmetricFrameMatrix3<F>,
    rv: FrameMatrix3<F>,
    vv: SymmetricFrameMatrix3<F>,
}

#[cfg(feature = "serde")]
impl<F: ReferenceFrame> serde::Serialize for StateCovariance<F> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        self.to_row_major().serialize(serializer)
    }
}

#[cfg(feature = "serde")]
impl<'de, F: ReferenceFrame> serde::Deserialize<'de> for StateCovariance<F> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let matrix = <[[f64; 6]; 6]>::deserialize(deserializer)?;
        let mut flat = [0.0_f64; 36];
        for i in 0..6 {
            for j in 0..6 {
                flat[i * 6 + j] = matrix[i][j];
            }
        }
        Self::try_from_row_major(flat).map_err(serde::de::Error::custom)
    }
}

#[cfg(feature = "serde")]
impl<F: ReferenceFrame> serde::Serialize for ProcessNoise<F> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        self.data.serialize(serializer)
    }
}

#[cfg(feature = "serde")]
impl<'de, F: ReferenceFrame> serde::Deserialize<'de> for ProcessNoise<F> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let data = <[[f64; 6]; 6]>::deserialize(deserializer)?;
        for row in &data {
            for value in row {
                if !value.is_finite() {
                    return Err(serde::de::Error::custom(
                        "ProcessNoise: matrix entries must be finite",
                    ));
                }
            }
        }
        Ok(Self {
            data,
            _frame: core::marker::PhantomData,
        })
    }
}

impl<F: ReferenceFrame> StateCovariance<F> {
    /// Construct from explicit `rr`, `rv`, and `vv` blocks.
    #[inline]
    pub fn from_blocks(
        rr: SymmetricFrameMatrix3<F>,
        rv: FrameMatrix3<F>,
        vv: SymmetricFrameMatrix3<F>,
    ) -> Self {
        Self { rr, rv, vv }
    }

    /// Construct a diagonal covariance from typed one-sigma values.
    pub fn diagonal_from_sigmas(
        sigma_pos: [Kilometers; 3],
        sigma_vel: [Quantity<KmPerSecond>; 3],
    ) -> Self {
        Self {
            rr: SymmetricFrameMatrix3::from_diagonal([
                sigma_pos[0].value().powi(2),
                sigma_pos[1].value().powi(2),
                sigma_pos[2].value().powi(2),
            ]),
            rv: FrameMatrix3::zero(),
            vv: SymmetricFrameMatrix3::from_diagonal([
                sigma_vel[0].value().powi(2),
                sigma_vel[1].value().powi(2),
                sigma_vel[2].value().powi(2),
            ]),
        }
    }

    /// Construct a diagonal covariance from raw one-sigma values `[σx, σy, σz, σvx, σvy, σvz]`.
    pub fn try_diagonal_from_sigmas(sigmas: [f64; 6]) -> Result<Self, PrincipiaError> {
        for sigma in sigmas {
            if !sigma.is_finite() || sigma <= 0.0 {
                return Err(PrincipiaError::NonPositiveValue {
                    context: "StateCovariance: sigmas must be finite and positive",
                });
            }
        }
        Ok(Self::diagonal_from_sigmas(
            [
                Kilometers::new(sigmas[0]),
                Kilometers::new(sigmas[1]),
                Kilometers::new(sigmas[2]),
            ],
            [
                Quantity::<KmPerSecond>::new(sigmas[3]),
                Quantity::<KmPerSecond>::new(sigmas[4]),
                Quantity::<KmPerSecond>::new(sigmas[5]),
            ],
        ))
    }

    /// Construct from a raw row-major `6 × 6` matrix.
    pub fn from_row_major(m: [[f64; 6]; 6]) -> Self {
        Self {
            rr: SymmetricFrameMatrix3::from_upper([
                [m[0][0], m[0][1], m[0][2]],
                [m[1][0], m[1][1], m[1][2]],
                [m[2][0], m[2][1], m[2][2]],
            ]),
            rv: FrameMatrix3::from_array([
                [m[0][3], m[0][4], m[0][5]],
                [m[1][3], m[1][4], m[1][5]],
                [m[2][3], m[2][4], m[2][5]],
            ]),
            vv: SymmetricFrameMatrix3::from_upper([
                [m[3][3], m[3][4], m[3][5]],
                [m[4][3], m[4][4], m[4][5]],
                [m[5][3], m[5][4], m[5][5]],
            ]),
        }
    }

    /// Construct from a raw row-major `6 × 6` matrix flattened row-major.
    pub fn try_from_row_major(m: [f64; 36]) -> Result<Self, PrincipiaError> {
        let mut matrix = [[0.0; 6]; 6];
        for i in 0..6 {
            for j in 0..6 {
                matrix[i][j] = m[i * 6 + j];
            }
        }
        validate_covariance_matrix(&matrix)?;
        let covariance = Self::from_row_major(matrix);
        if !covariance.is_positive_semidefinite(RelativeTolerance::new(1e-12)) {
            return Err(PrincipiaError::InvalidParameter {
                reason:
                    "StateCovariance: covariance matrix must be symmetric positive semidefinite",
            });
        }
        Ok(covariance)
    }

    /// Construct from already-prepared block components.
    #[inline]
    pub fn from_block_components(
        rr: SymmetricFrameMatrix3<F>,
        rv: FrameMatrix3<F>,
        vv: SymmetricFrameMatrix3<F>,
    ) -> Self {
        Self::from_blocks(rr, rv, vv)
    }

    /// Return the `Σ_rr` block.
    #[inline]
    pub fn rr(&self) -> &SymmetricFrameMatrix3<F> {
        &self.rr
    }

    /// Return the `Σ_rv` block.
    #[inline]
    pub fn rv(&self) -> &FrameMatrix3<F> {
        &self.rv
    }

    /// Return the derived `Σ_vr = Σ_rvᵀ` block.
    #[inline]
    pub fn vr(&self) -> FrameMatrix3<F> {
        self.rv.transpose()
    }

    /// Return the `Σ_vv` block.
    #[inline]
    pub fn vv(&self) -> &SymmetricFrameMatrix3<F> {
        &self.vv
    }

    /// Export the covariance as a full row-major `6 × 6` matrix.
    pub fn to_row_major(&self) -> [[f64; 6]; 6] {
        let rr = self.rr.as_array();
        let rv = self.rv.as_array();
        let vv = self.vv.as_array();
        let mut out = [[0.0; 6]; 6];
        for i in 0..3 {
            for j in 0..3 {
                out[i][j] = rr[i][j];
                out[i][j + 3] = rv[i][j];
                out[i + 3][j] = rv[j][i];
                out[i + 3][j + 3] = vv[i][j];
            }
        }
        out
    }

    /// Rotate the covariance by an instantaneous `3 × 3` rotation.
    pub fn rotate_by<G: ReferenceFrame>(self, r: &Rotation3) -> StateCovariance<G> {
        StateCovariance {
            rr: self.rr.rotated_by::<G>(r),
            rv: self.rv.rotated_by::<G>(r),
            vv: self.vv.rotated_by::<G>(r),
        }
    }

    /// Relabel the covariance with a different frame tag without changing data.
    pub fn relabel<G: ReferenceFrame>(self) -> StateCovariance<G> {
        StateCovariance {
            rr: self.rr.relabel::<G>(),
            rv: self.rv.relabel::<G>(),
            vv: self.vv.relabel::<G>(),
        }
    }

    /// Return `true` if the full `6 × 6` matrix is symmetric within `tol`.
    #[allow(clippy::needless_range_loop)]
    pub fn is_symmetric(&self, tol: RelativeTolerance) -> bool {
        let m = self.to_row_major();
        let t = tol.value();
        for i in 0..6 {
            for j in (i + 1)..6 {
                let a = m[i][j];
                let b = m[j][i];
                let scale = a.abs().max(b.abs());
                let diff = (a - b).abs();
                if scale == 0.0 {
                    if diff != 0.0 {
                        return false;
                    }
                } else if diff > t * scale {
                    return false;
                }
            }
        }
        true
    }

    /// Return `true` if the covariance is positive semidefinite within `tol`.
    #[allow(clippy::needless_range_loop)]
    pub fn is_positive_semidefinite(&self, tol: RelativeTolerance) -> bool {
        let m = self.to_row_major();
        let mut a = [[0.0; 6]; 6];
        for i in 0..6 {
            for j in 0..6 {
                a[i][j] = 0.5 * (m[i][j] + m[j][i]);
            }
        }
        let trace: f64 = (0..6).map(|i| a[i][i]).sum();
        let eps = tol.value() * trace / 6.0;
        cholesky_in_place(&mut a, eps)
    }

    /// Symmetrize the stored blocks in place.
    pub fn symmetrise_in_place(&mut self) {
        let rr_arr = *self.rr.as_array();
        let vv_arr = *self.vv.as_array();
        let rv_arr = *self.rv.as_array();
        self.rr = SymmetricFrameMatrix3::from_upper([
            [
                rr_arr[0][0],
                0.5 * (rr_arr[0][1] + rr_arr[1][0]),
                0.5 * (rr_arr[0][2] + rr_arr[2][0]),
            ],
            [
                rr_arr[1][0],
                rr_arr[1][1],
                0.5 * (rr_arr[1][2] + rr_arr[2][1]),
            ],
            [rr_arr[2][0], rr_arr[2][1], rr_arr[2][2]],
        ]);
        self.vv = SymmetricFrameMatrix3::from_upper([
            [
                vv_arr[0][0],
                0.5 * (vv_arr[0][1] + vv_arr[1][0]),
                0.5 * (vv_arr[0][2] + vv_arr[2][0]),
            ],
            [
                vv_arr[1][0],
                vv_arr[1][1],
                0.5 * (vv_arr[1][2] + vv_arr[2][1]),
            ],
            [vv_arr[2][0], vv_arr[2][1], vv_arr[2][2]],
        ]);
        self.rv = FrameMatrix3::from_array([
            [
                rv_arr[0][0],
                0.5 * (rv_arr[0][1] + rv_arr[1][0]),
                0.5 * (rv_arr[0][2] + rv_arr[2][0]),
            ],
            [
                0.5 * (rv_arr[1][0] + rv_arr[0][1]),
                rv_arr[1][1],
                0.5 * (rv_arr[1][2] + rv_arr[2][1]),
            ],
            [
                0.5 * (rv_arr[2][0] + rv_arr[0][2]),
                0.5 * (rv_arr[2][1] + rv_arr[1][2]),
                rv_arr[2][2],
            ],
        ]);
    }

    /// Propagate the covariance through a state-transition matrix: `P ← Φ P Φᵀ`.
    pub fn transported_by(&self, phi: &StateTransitionMatrix<F>) -> Self {
        let p = FrameMatrix6::<F>::from_array(self.to_row_major());
        let propagated = phi.mat_mul(&p).mat_mul(&phi.transpose());
        Self::from_row_major(*propagated.as_array())
    }

    /// Rotate this covariance into a local trajectory frame.
    pub fn transform_into<Local: ReferenceFrame>(
        &self,
        frame: &LocalTrajectoryFrame<F, Local>,
    ) -> StateCovariance<Local> {
        StateCovariance {
            rr: frame.dcm.similarity::<Local>(&self.rr),
            rv: frame.dcm.similarity_general::<Local>(&self.rv),
            vv: frame.dcm.similarity::<Local>(&self.vv),
        }
    }
}

impl<Local: ReferenceFrame> StateCovariance<Local> {
    /// Rotate a local covariance back into its inertial parent frame.
    pub fn transform_into_inertial<Inertial: ReferenceFrame>(
        &self,
        frame: &LocalTrajectoryFrame<Inertial, Local>,
    ) -> StateCovariance<Inertial> {
        let r = frame.rotation_inverse();
        StateCovariance {
            rr: self.rr.rotated_by::<Inertial>(&r),
            rv: self.rv.rotated_by::<Inertial>(&r),
            vv: self.vv.rotated_by::<Inertial>(&r),
        }
    }
}

/// Frame-tagged additive process-noise matrix in `[r, v]` ordering.
#[derive(Debug, Clone, Copy)]
pub struct ProcessNoise<F: ReferenceFrame> {
    data: [[f64; 6]; 6],
    _frame: core::marker::PhantomData<F>,
}

impl<F: ReferenceFrame> ProcessNoise<F> {
    /// Return the zero process-noise matrix.
    pub fn zero() -> Self {
        Self {
            data: [[0.0; 6]; 6],
            _frame: core::marker::PhantomData,
        }
    }

    /// Build a diagonal process-noise model from position-rate and
    /// acceleration-rate sigmas over step `dt`.
    pub fn diagonal_from_sigmas(
        pos_rate: [Quantity<KmPerSecond>; 3],
        vel_rate: [Quantity<KmPerSecondSquared>; 3],
        dt: Second,
    ) -> Self {
        let dt_val = dt.value();
        let mut data = [[0.0; 6]; 6];
        for i in 0..3 {
            data[i][i] = pos_rate[i].value().powi(2) * dt_val;
            data[i + 3][i + 3] = vel_rate[i].value().powi(2) * dt_val;
        }
        Self {
            data,
            _frame: core::marker::PhantomData,
        }
    }

    /// Build a validated diagonal process-noise model from raw sigma rates.
    pub fn try_diagonal_from_sigmas(sigmas: [f64; 6], dt: Second) -> Result<Self, PrincipiaError> {
        for sigma in sigmas {
            if !sigma.is_finite() || sigma <= 0.0 {
                return Err(PrincipiaError::NonPositiveValue {
                    context: "ProcessNoise: sigmas must be finite and positive",
                });
            }
        }
        if !dt.value().is_finite() || dt.value() <= 0.0 {
            return Err(PrincipiaError::NonPositiveValue {
                context: "ProcessNoise: dt must be finite and positive",
            });
        }
        Ok(Self::diagonal_from_sigmas(
            [
                Quantity::<KmPerSecond>::new(sigmas[0]),
                Quantity::<KmPerSecond>::new(sigmas[1]),
                Quantity::<KmPerSecond>::new(sigmas[2]),
            ],
            [
                Quantity::<KmPerSecondSquared>::new(sigmas[3]),
                Quantity::<KmPerSecondSquared>::new(sigmas[4]),
                Quantity::<KmPerSecondSquared>::new(sigmas[5]),
            ],
            dt,
        ))
    }

    /// Add this process noise into a covariance in place.
    #[allow(clippy::needless_range_loop)]
    pub fn add_to(&self, cov: &mut StateCovariance<F>) {
        *cov = StateCovariance::from_row_major({
            let mut m = cov.to_row_major();
            for i in 0..6 {
                for j in 0..6 {
                    m[i][j] += self.data[i][j];
                }
            }
            m
        });
    }

    /// Export the raw row-major `6 × 6` matrix.
    #[inline]
    pub fn to_row_major(&self) -> [[f64; 6]; 6] {
        self.data
    }
}

#[allow(clippy::needless_range_loop)]
fn validate_covariance_matrix(m: &[[f64; 6]; 6]) -> Result<(), PrincipiaError> {
    for i in 0..6 {
        for j in 0..6 {
            if !m[i][j].is_finite() {
                return Err(PrincipiaError::InvalidParameter {
                    reason: "StateCovariance: matrix entries must be finite",
                });
            }
        }
        if m[i][i] <= 0.0 {
            return Err(PrincipiaError::InvalidParameter {
                reason: "StateCovariance: diagonal entries must be strictly positive",
            });
        }
    }
    for i in 0..6 {
        for j in (i + 1)..6 {
            let scale = m[i][j].abs().max(m[j][i].abs()).max(1.0);
            if (m[i][j] - m[j][i]).abs() > 1e-12 * scale {
                return Err(PrincipiaError::InvalidParameter {
                    reason: "StateCovariance: matrix must be symmetric",
                });
            }
        }
    }
    Ok(())
}

#[allow(clippy::needless_range_loop)]
fn cholesky_in_place(a: &mut [[f64; 6]; 6], eps: f64) -> bool {
    for i in 0..6 {
        let pivot = a[i][i] + eps;
        if pivot < 0.0 {
            return false;
        }
        let sqrt_pivot = pivot.sqrt();
        a[i][i] = sqrt_pivot;
        if sqrt_pivot == 0.0 {
            for j in (i + 1)..6 {
                a[j][i] = 0.0;
            }
            continue;
        }
        let inv = 1.0 / sqrt_pivot;
        for j in (i + 1)..6 {
            a[j][i] *= inv;
        }
        for j in (i + 1)..6 {
            let factor = a[j][i];
            for k in j..6 {
                a[k][j] -= factor * a[k][i];
            }
        }
    }
    true
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::frames::{LocalTrajectoryFrame, RTN};
    use affn::centers::ReferenceCenter;
    use affn::frames::ReferenceFrame;
    use affn::matrix6::FrameMatrix6;
    use qtty::{KmPerSecond, Second};
    use tempoch::{Time, TT};

    #[derive(Debug, Clone, Copy)]
    struct Inertial;
    impl ReferenceFrame for Inertial {
        fn frame_name() -> &'static str {
            "Inertial"
        }
    }

    #[derive(Debug, Clone, Copy)]
    struct Center;
    impl ReferenceCenter for Center {
        type Params = ();
        fn center_name() -> &'static str {
            "Center"
        }
    }

    fn state() -> crate::state::DynamicsState<TT, Center, Inertial> {
        crate::state::DynamicsState::new(
            Time::<TT>::from_raw_j2000_seconds(Second::new(0.0)).unwrap(),
            affn::cartesian::Position::<Center, Inertial, qtty::unit::Kilometer>::new(
                7000.0, 0.0, 0.0,
            ),
            affn::cartesian::Velocity::<Inertial, KmPerSecond>::new(0.0, 7.5, 0.0),
        )
    }

    #[test]
    fn diagonal_covariance_is_psd() {
        let p = StateCovariance::<Inertial>::diagonal_from_sigmas(
            [Kilometers::new(1.0); 3],
            [Quantity::<KmPerSecond>::new(1e-3); 3],
        );
        assert!(p.is_positive_semidefinite(RelativeTolerance::new(1e-10)));
    }

    #[test]
    fn try_diagonal_from_sigmas_rejects_non_positive_sigma() {
        let result = StateCovariance::<Inertial>::try_diagonal_from_sigmas([
            1.0, 1.0, 0.0, 1e-3, 1e-3, 1e-3,
        ]);
        assert!(matches!(
            result,
            Err(PrincipiaError::NonPositiveValue { .. })
        ));
    }

    #[test]
    fn try_from_row_major_rejects_non_finite_entry() {
        let mut matrix = [0.0_f64; 36];
        for i in 0..6 {
            matrix[i * 6 + i] = 1.0;
        }
        matrix[1] = f64::NAN;
        let result = StateCovariance::<Inertial>::try_from_row_major(matrix);
        assert!(matches!(
            result,
            Err(PrincipiaError::InvalidParameter { .. })
        ));
    }

    #[test]
    fn transport_with_identity_is_noop() {
        let p = StateCovariance::<Inertial>::diagonal_from_sigmas(
            [Kilometers::new(1.0); 3],
            [Quantity::<KmPerSecond>::new(1e-3); 3],
        );
        let phi = FrameMatrix6::<Inertial>::identity();
        assert_eq!(p.to_row_major(), p.transported_by(&phi).to_row_major());
    }

    #[test]
    fn inertial_local_round_trip() {
        let p = StateCovariance::<Inertial>::diagonal_from_sigmas(
            [Kilometers::new(1.0); 3],
            [Quantity::<KmPerSecond>::new(1e-3); 3],
        );
        let frame = LocalTrajectoryFrame::<Inertial, RTN>::try_from_state(&state()).unwrap();
        let back = p.transform_into(&frame).transform_into_inertial(&frame);
        let a = p.to_row_major();
        let b = back.to_row_major();
        for i in 0..6 {
            for j in 0..6 {
                assert!((a[i][j] - b[i][j]).abs() < 1e-12);
            }
        }
    }

    #[test]
    fn from_row_major_round_trip() {
        let p = StateCovariance::<Inertial>::diagonal_from_sigmas(
            [Kilometers::new(1.0); 3],
            [Quantity::<KmPerSecond>::new(1e-3); 3],
        );
        let m = p.to_row_major();
        let p2 = StateCovariance::<Inertial>::from_row_major(m);
        let m2 = p2.to_row_major();
        for (i, row) in m.iter().enumerate() {
            for (j, value) in row.iter().enumerate() {
                assert!((*value - m2[i][j]).abs() < 1e-30);
            }
        }
    }

    #[test]
    fn from_block_components_same_as_from_row_major() {
        let p = StateCovariance::<Inertial>::diagonal_from_sigmas(
            [Kilometers::new(1.0); 3],
            [Quantity::<KmPerSecond>::new(1e-3); 3],
        );
        let p2 = StateCovariance::from_block_components(*p.rr(), *p.rv(), *p.vv());
        let m1 = p.to_row_major();
        let m2 = p2.to_row_major();
        for (i, row) in m1.iter().enumerate() {
            for (j, value) in row.iter().enumerate() {
                assert!((*value - m2[i][j]).abs() < 1e-30);
            }
        }
    }

    #[test]
    fn block_getters_rr_rv_vr_vv() {
        let p = StateCovariance::<Inertial>::diagonal_from_sigmas(
            [Kilometers::new(1.0); 3],
            [Quantity::<KmPerSecond>::new(1e-3); 3],
        );
        let m = p.to_row_major();
        // rr block
        assert!((p.rr().as_array()[0][0] - m[0][0]).abs() < 1e-30);
        // vv block
        assert!((p.vv().as_array()[0][0] - m[3][3]).abs() < 1e-30);
        // rv block
        assert!((p.rv().as_array()[0][0] - m[0][3]).abs() < 1e-30);
        // vr = rvᵀ
        assert!((p.vr().as_array()[0][0] - m[3][0]).abs() < 1e-30);
    }

    #[test]
    fn is_symmetric_true_for_diagonal() {
        let p = StateCovariance::<Inertial>::diagonal_from_sigmas(
            [Kilometers::new(1.0); 3],
            [Quantity::<KmPerSecond>::new(1e-3); 3],
        );
        assert!(p.is_symmetric(qtty::RelativeTolerance::new(1e-10)));
    }

    #[test]
    fn symmetrise_in_place_makes_diagonal_symmetric() {
        let mut p = StateCovariance::<Inertial>::diagonal_from_sigmas(
            [Kilometers::new(1.0); 3],
            [Quantity::<KmPerSecond>::new(1e-3); 3],
        );
        p.symmetrise_in_place();
        assert!(p.is_symmetric(qtty::RelativeTolerance::new(1e-10)));
    }

    #[test]
    fn rotate_by_identity_is_noop() {
        use affn::ops::Rotation3;
        let p = StateCovariance::<Inertial>::diagonal_from_sigmas(
            [Kilometers::new(1.0); 3],
            [Quantity::<KmPerSecond>::new(1e-3); 3],
        );
        let identity =
            Rotation3::from_matrix_unchecked([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]);
        let p2: StateCovariance<Inertial> = p.rotate_by(&identity);
        let m1 = p.to_row_major();
        let m2 = p2.to_row_major();
        for i in 0..6 {
            for j in 0..6 {
                assert!((m1[i][j] - m2[i][j]).abs() < 1e-20);
            }
        }
    }

    #[test]
    fn relabel_preserves_data() {
        let p = StateCovariance::<Inertial>::diagonal_from_sigmas(
            [Kilometers::new(1.0); 3],
            [Quantity::<KmPerSecond>::new(1e-3); 3],
        );
        let m1 = p.to_row_major();
        let p2: StateCovariance<Inertial> = p.relabel();
        let m2 = p2.to_row_major();
        for i in 0..6 {
            for j in 0..6 {
                assert!((m1[i][j] - m2[i][j]).abs() < 1e-30);
            }
        }
    }

    #[test]
    fn process_noise_zero_has_all_zeros() {
        let pn = ProcessNoise::<Inertial>::zero();
        let m = pn.to_row_major();
        for row in &m {
            for val in row {
                assert_eq!(*val, 0.0);
            }
        }
    }

    #[test]
    fn process_noise_try_diagonal_from_sigmas_rejects_non_positive_dt() {
        let result = ProcessNoise::<Inertial>::try_diagonal_from_sigmas(
            [1e-3, 1e-3, 1e-3, 1e-6, 1e-6, 1e-6],
            Second::new(0.0),
        );
        assert!(matches!(
            result,
            Err(PrincipiaError::NonPositiveValue { .. })
        ));
    }

    #[test]
    fn process_noise_add_to_increases_diagonal() {
        let pn = ProcessNoise::<Inertial>::diagonal_from_sigmas(
            [Quantity::<qtty::KmPerSecond>::new(1e-3); 3],
            [Quantity::<qtty::KmPerSecondSquared>::new(1e-6); 3],
            Second::new(1.0),
        );
        let mut p = StateCovariance::<Inertial>::diagonal_from_sigmas(
            [Kilometers::new(1.0); 3],
            [Quantity::<KmPerSecond>::new(1e-3); 3],
        );
        let before = p.to_row_major()[0][0];
        pn.add_to(&mut p);
        let after = p.to_row_major()[0][0];
        assert!(after > before);
    }

    #[test]
    fn cholesky_negative_definite_returns_not_psd() {
        // Build a matrix with a negative diagonal entry
        let mut m = [[0.0_f64; 6]; 6];
        for (i, row) in m.iter_mut().enumerate() {
            row[i] = if i == 2 { -1.0 } else { 1.0 };
        }
        let p = StateCovariance::<Inertial>::from_row_major(m);
        assert!(!p.is_positive_semidefinite(qtty::RelativeTolerance::new(1e-10)));
    }

    #[test]
    fn try_diagonal_from_sigmas_accepts_valid() {
        let result = StateCovariance::<Inertial>::try_diagonal_from_sigmas([
            1.0, 2.0, 3.0, 0.001, 0.002, 0.003,
        ]);
        assert!(result.is_ok());
    }

    #[test]
    fn try_from_row_major_rejects_invalid_psd() {
        let mut m = [[0.0_f64; 36]; 1][0];
        // Negative diagonal → not PSD
        for i in 0..6_usize {
            m[i * 6 + i] = if i == 0 { -1.0 } else { 1.0 };
        }
        let result = StateCovariance::<Inertial>::try_from_row_major(m);
        assert!(result.is_err());
    }

    #[test]
    fn transported_by_identity_is_noop() {
        use crate::variational::StateTransitionMatrix;
        let p = StateCovariance::<Inertial>::diagonal_from_sigmas(
            [Kilometers::new(1.0); 3],
            [Quantity::<KmPerSecond>::new(1e-3); 3],
        );
        let phi = StateTransitionMatrix::<Inertial>::identity();
        let p2 = p.transported_by(&phi);
        let m1 = p.to_row_major();
        let m2 = p2.to_row_major();
        for i in 0..6 {
            for j in 0..6 {
                assert!((m1[i][j] - m2[i][j]).abs() < 1e-20, "[{i}][{j}]");
            }
        }
    }

    #[cfg(feature = "serde")]
    #[test]
    fn state_covariance_serde_roundtrip() {
        let p = StateCovariance::<Inertial>::diagonal_from_sigmas(
            [Kilometers::new(1.0); 3],
            [Quantity::<KmPerSecond>::new(1e-3); 3],
        );
        let json = serde_json::to_string(&p).expect("serialize");
        let p2: StateCovariance<Inertial> = serde_json::from_str(&json).expect("deserialize");
        let m1 = p.to_row_major();
        let m2 = p2.to_row_major();
        for i in 0..6 {
            for j in 0..6 {
                assert!((m1[i][j] - m2[i][j]).abs() < 1e-30, "[{i}][{j}]");
            }
        }
    }

    #[cfg(feature = "serde")]
    #[test]
    fn process_noise_serde_roundtrip() {
        let pn = ProcessNoise::<Inertial>::diagonal_from_sigmas(
            [Quantity::<qtty::KmPerSecond>::new(1e-3); 3],
            [Quantity::<qtty::KmPerSecondSquared>::new(1e-6); 3],
            Second::new(1.0),
        );
        let json = serde_json::to_string(&pn).expect("serialize");
        let pn2: ProcessNoise<Inertial> = serde_json::from_str(&json).expect("deserialize");
        let m1 = pn.to_row_major();
        let m2 = pn2.to_row_major();
        for i in 0..6 {
            for j in 0..6 {
                assert!((m1[i][j] - m2[i][j]).abs() < 1e-30, "[{i}][{j}]");
            }
        }
    }
}