kshana 0.12.0

Open, reproducible PNT-resilience simulator with quantum-sensor performance models
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
// SPDX-License-Identifier: Apache-2.0
//! Loosely-coupled GNSS/INS extended Kalman filter (15-state error-state).
//!
//! This is the estimator that replaces the hybrid pack's open-loop
//! dead-reckoning-with-truth-snap with a real closed-loop blend of a strapdown
//! inertial solution and GNSS position/velocity fixes. It is an **error-state**
//! (indirect) EKF: rather than estimating the full navigation state, it
//! estimates the *error* in a strapdown solution and feeds those corrections
//! back, which keeps the linearisation valid and the numerics well-conditioned
//! (Groves 2013 §14.1–14.2).
//!
//! The 15-element error state is
//!
//! ```text
//! δx = [ δp(3)  δv(3)  ψ(3)  b_a(3)  b_g(3) ]ᵀ
//! ```
//!
//! position error (NED, m), velocity error (NED, m/s), attitude error angles
//! (rad), accelerometer bias (m/s²), gyro bias (rad/s). The continuous
//! error-state dynamics (Groves eq. 14.48–14.49, local-navigation frame,
//! dropping the second-order gravity-gradient term) are
//!
//! ```text
//! δṗ = δv
//! δv̇ = −[f_n×] ψ − 2[ω_ie×] δv + C_b^n b_a
//! ψ̇  = −[ω_in×] ψ − C_b^n b_g
//! ḃ_a = −(1/τ_a) b_a            (first-order Gauss–Markov; τ→∞ ⇒ random walk)
//! ḃ_g = −(1/τ_g) b_g
//! ```
//!
//! The loosely-coupled measurement is the difference between the INS and GNSS
//! position and velocity, `z = H δx + ν` with `H = [I₃ 0 0 0 0; 0 I₃ 0 0 0]`.
//! The covariance update uses the Joseph form for numerical stability.
//!
//! Tightly-coupled operation forms the innovation in the **range domain**: the
//! predicted range from the INS position to each satellite versus the measured
//! pseudorange, `z_i = ê_i · δp + ν_i` with the line-of-sight unit vector `ê_i`.
//! Because each satellite contributes its own scalar measurement, the filter keeps
//! correcting with **fewer than four satellites**, where a loosely-coupled PVT
//! solution does not exist — see [`GnssInsEkf::update_tightly_coupled`]. (The
//! pseudoranges are taken receiver-clock-corrected, i.e. single-differenced; an
//! explicit receiver-clock state is a future extension.)
//!
//! No external linear-algebra dependency: the small dense matrices are handled
//! by the helpers in this module.

use crate::frames::Vec3;

const N: usize = 15; // error-state dimension

/// A dense `rows × cols` matrix stored row-major.
#[derive(Clone, Debug)]
struct Mat {
    rows: usize,
    cols: usize,
    d: Vec<f64>,
}

impl Mat {
    fn zeros(rows: usize, cols: usize) -> Self {
        Self {
            rows,
            cols,
            d: vec![0.0; rows * cols],
        }
    }
    fn identity(n: usize) -> Self {
        let mut m = Self::zeros(n, n);
        for i in 0..n {
            m.set(i, i, 1.0);
        }
        m
    }
    #[inline]
    fn at(&self, r: usize, c: usize) -> f64 {
        self.d[r * self.cols + c]
    }
    #[inline]
    fn set(&mut self, r: usize, c: usize, v: f64) {
        self.d[r * self.cols + c] = v;
    }
    /// Write a 3×3 block with top-left corner at (r0, c0).
    fn set_block3(&mut self, r0: usize, c0: usize, b: [[f64; 3]; 3]) {
        for (i, row) in b.iter().enumerate() {
            for (j, &v) in row.iter().enumerate() {
                self.set(r0 + i, c0 + j, v);
            }
        }
    }
    fn transpose(&self) -> Mat {
        let mut t = Mat::zeros(self.cols, self.rows);
        for r in 0..self.rows {
            for c in 0..self.cols {
                t.set(c, r, self.at(r, c));
            }
        }
        t
    }
    fn mul(&self, o: &Mat) -> Mat {
        assert_eq!(self.cols, o.rows);
        let mut m = Mat::zeros(self.rows, o.cols);
        for r in 0..self.rows {
            for k in 0..self.cols {
                let a = self.at(r, k);
                if a == 0.0 {
                    continue;
                }
                for c in 0..o.cols {
                    let v = m.at(r, c) + a * o.at(k, c);
                    m.set(r, c, v);
                }
            }
        }
        m
    }
    fn mul_vec(&self, v: &[f64]) -> Vec<f64> {
        assert_eq!(self.cols, v.len());
        let mut out = vec![0.0; self.rows];
        for (r, o) in out.iter_mut().enumerate() {
            let mut s = 0.0;
            for (c, &vc) in v.iter().enumerate() {
                s += self.at(r, c) * vc;
            }
            *o = s;
        }
        out
    }
    fn sub(&self, o: &Mat) -> Mat {
        let mut m = self.clone();
        for i in 0..self.d.len() {
            m.d[i] -= o.d[i];
        }
        m
    }
    fn add(&self, o: &Mat) -> Mat {
        let mut m = self.clone();
        for i in 0..self.d.len() {
            m.d[i] += o.d[i];
        }
        m
    }
    /// Symmetrise in place (averages with the transpose) to suppress numerical drift.
    fn symmetrize(&mut self) {
        for r in 0..self.rows {
            for c in (r + 1)..self.cols {
                let avg = 0.5 * (self.at(r, c) + self.at(c, r));
                self.set(r, c, avg);
                self.set(c, r, avg);
            }
        }
    }
    /// Dense inverse via Gauss–Jordan with partial pivoting (small matrices only).
    fn inverse(&self) -> Option<Mat> {
        assert_eq!(self.rows, self.cols);
        let n = self.rows;
        let mut a = self.clone();
        let mut inv = Mat::identity(n);
        for col in 0..n {
            // Pivot: largest magnitude in this column at/below the diagonal.
            let mut piv = col;
            let mut best = a.at(col, col).abs();
            for r in (col + 1)..n {
                let v = a.at(r, col).abs();
                if v > best {
                    best = v;
                    piv = r;
                }
            }
            if best < 1e-300 {
                return None; // singular
            }
            if piv != col {
                for c in 0..n {
                    let (x, y) = (a.at(col, c), a.at(piv, c));
                    a.set(col, c, y);
                    a.set(piv, c, x);
                    let (x, y) = (inv.at(col, c), inv.at(piv, c));
                    inv.set(col, c, y);
                    inv.set(piv, c, x);
                }
            }
            let d = a.at(col, col);
            for c in 0..n {
                a.set(col, c, a.at(col, c) / d);
                inv.set(col, c, inv.at(col, c) / d);
            }
            for r in 0..n {
                if r == col {
                    continue;
                }
                let factor = a.at(r, col);
                if factor == 0.0 {
                    continue;
                }
                for c in 0..n {
                    a.set(r, c, a.at(r, c) - factor * a.at(col, c));
                    inv.set(r, c, inv.at(r, c) - factor * inv.at(col, c));
                }
            }
        }
        Some(inv)
    }
}

/// Skew-symmetric (cross-product) matrix `[v×]` such that `[v×] w = v × w`.
fn skew(v: Vec3) -> [[f64; 3]; 3] {
    [[0.0, -v[2], v[1]], [v[2], 0.0, -v[0]], [-v[1], v[0], 0.0]]
}

fn neg3(m: [[f64; 3]; 3]) -> [[f64; 3]; 3] {
    let mut o = [[0.0; 3]; 3];
    for i in 0..3 {
        for j in 0..3 {
            o[i][j] = -m[i][j];
        }
    }
    o
}

/// A loosely-coupled GNSS fix: ECEF/NED position and velocity with 1-σ accuracies.
#[derive(Clone, Copy, Debug)]
pub struct GnssMeasurement {
    /// Position in the working frame (NED, m).
    pub pos: Vec3,
    /// Velocity in the working frame (NED, m/s).
    pub vel: Vec3,
    /// 1-σ position accuracy (m), applied isotropically.
    pub sigma_pos_m: f64,
    /// 1-σ velocity accuracy (m/s), applied isotropically.
    pub sigma_vel_mps: f64,
}

/// Process-noise spectral densities driving the error state.
#[derive(Clone, Copy, Debug)]
pub struct EkfNoise {
    /// Velocity random walk — accelerometer white-noise PSD ((m/s²)²/Hz).
    pub vrw_psd: f64,
    /// Angle random walk — gyro white-noise PSD ((rad/s)²/Hz).
    pub arw_psd: f64,
    /// Accelerometer bias random-walk PSD ((m/s²)²·Hz).
    pub accel_bias_rw_psd: f64,
    /// Gyro bias random-walk PSD ((rad/s)²·Hz).
    pub gyro_bias_rw_psd: f64,
    /// Accelerometer bias Gauss–Markov correlation time (s); `f64::INFINITY` ⇒ random walk.
    pub accel_bias_tau: f64,
    /// Gyro bias Gauss–Markov correlation time (s); `f64::INFINITY` ⇒ random walk.
    pub gyro_bias_tau: f64,
}

impl Default for EkfNoise {
    fn default() -> Self {
        // Tactical-grade-ish defaults; callers should set from the IMU datasheet.
        Self {
            vrw_psd: 1e-4,
            arw_psd: 1e-8,
            accel_bias_rw_psd: 1e-10,
            gyro_bias_rw_psd: 1e-14,
            accel_bias_tau: f64::INFINITY,
            gyro_bias_tau: f64::INFINITY,
        }
    }
}

/// 15-state error-state EKF for loosely-coupled GNSS/INS fusion.
pub struct GnssInsEkf {
    /// Error-state estimate `δx` (15). Reset to zero after closed-loop feedback.
    x: [f64; N],
    /// Error-state covariance `P` (15×15).
    p: Mat,
    noise: EkfNoise,
}

impl GnssInsEkf {
    /// Construct with diagonal initial covariance from 1-σ uncertainties.
    pub fn new(
        sigma_pos_m: f64,
        sigma_vel_mps: f64,
        sigma_att_rad: f64,
        sigma_accel_bias: f64,
        sigma_gyro_bias: f64,
        noise: EkfNoise,
    ) -> Self {
        let mut p = Mat::zeros(N, N);
        let var = [
            sigma_pos_m.powi(2),
            sigma_vel_mps.powi(2),
            sigma_att_rad.powi(2),
            sigma_accel_bias.powi(2),
            sigma_gyro_bias.powi(2),
        ];
        for (block, &v) in var.iter().enumerate() {
            for k in 0..3 {
                let i = block * 3 + k;
                p.set(i, i, v);
            }
        }
        Self {
            x: [0.0; N],
            p,
            noise,
        }
    }

    /// The current error-state estimate (15).
    pub fn error_state(&self) -> [f64; N] {
        self.x
    }

    /// Trace of the position-error covariance block (m²) — a scalar uncertainty proxy.
    pub fn position_cov_trace(&self) -> f64 {
        self.p.at(0, 0) + self.p.at(1, 1) + self.p.at(2, 2)
    }

    /// Diagonal of `P` (15) — per-state variance.
    pub fn covariance_diagonal(&self) -> [f64; N] {
        let mut d = [0.0; N];
        for (i, di) in d.iter_mut().enumerate() {
            *di = self.p.at(i, i);
        }
        d
    }

    /// Build the discrete transition `Φ ≈ I + F·dt` from the current navigation
    /// context: specific force in nav frame `f_n`, body→nav DCM `c_bn`, Earth-rate
    /// `omega_ie_n`, and total inertial-to-nav rate `omega_in_n` (all in the NED frame).
    fn transition(
        &self,
        f_n: Vec3,
        c_bn: [[f64; 3]; 3],
        omega_ie_n: Vec3,
        omega_in_n: Vec3,
        dt: f64,
    ) -> Mat {
        let mut f = Mat::zeros(N, N);
        // δṗ = δv
        f.set_block3(0, 3, [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]);
        // δv̇ = −[f_n×] ψ − 2[ω_ie×] δv + C_b^n b_a
        f.set_block3(3, 6, neg3(skew(f_n)));
        let two_wie = [
            2.0 * omega_ie_n[0],
            2.0 * omega_ie_n[1],
            2.0 * omega_ie_n[2],
        ];
        f.set_block3(3, 3, neg3(skew(two_wie)));
        f.set_block3(3, 9, c_bn);
        // ψ̇ = −[ω_in×] ψ − C_b^n b_g
        f.set_block3(6, 6, neg3(skew(omega_in_n)));
        f.set_block3(6, 12, neg3(c_bn));
        // Bias Gauss–Markov: ḃ = −(1/τ) b
        let ka = if self.noise.accel_bias_tau.is_finite() {
            -1.0 / self.noise.accel_bias_tau
        } else {
            0.0
        };
        let kg = if self.noise.gyro_bias_tau.is_finite() {
            -1.0 / self.noise.gyro_bias_tau
        } else {
            0.0
        };
        for k in 0..3 {
            f.set(9 + k, 9 + k, ka);
            f.set(12 + k, 12 + k, kg);
        }
        // Φ = I + F·dt.
        let mut phi = Mat::identity(N);
        for i in 0..N {
            for j in 0..N {
                phi.set(i, j, phi.at(i, j) + f.at(i, j) * dt);
            }
        }
        phi
    }

    /// Discrete process-noise covariance `Q` (first-order, diagonal blocks).
    fn process_noise(&self, dt: f64) -> Mat {
        let mut q = Mat::zeros(N, N);
        let qv = self.noise.vrw_psd * dt;
        let qpsi = self.noise.arw_psd * dt;
        let qba = self.noise.accel_bias_rw_psd * dt;
        let qbg = self.noise.gyro_bias_rw_psd * dt;
        for k in 0..3 {
            q.set(3 + k, 3 + k, qv);
            q.set(6 + k, 6 + k, qpsi);
            q.set(9 + k, 9 + k, qba);
            q.set(12 + k, 12 + k, qbg);
        }
        q
    }

    /// Time-propagate the error state and covariance one step:
    /// `δx ← Φ δx`, `P ← Φ P Φᵀ + Q`.
    pub fn predict(
        &mut self,
        f_n: Vec3,
        c_bn: [[f64; 3]; 3],
        omega_ie_n: Vec3,
        omega_in_n: Vec3,
        dt: f64,
    ) {
        let phi = self.transition(f_n, c_bn, omega_ie_n, omega_in_n, dt);
        self.x = {
            let xv = phi.mul_vec(&self.x);
            let mut a = [0.0; N];
            a.copy_from_slice(&xv);
            a
        };
        let q = self.process_noise(dt);
        let mut p = phi.mul(&self.p).mul(&phi.transpose()).add(&q);
        p.symmetrize();
        self.p = p;
    }

    /// Loosely-coupled measurement update from a GNSS position+velocity fix and
    /// the current INS position+velocity (same NED frame). Returns the 15-element
    /// error-state correction that should be fed back to the strapdown solution
    /// (after which the caller resets the error state via [`reset_error_state`]).
    ///
    /// The innovation is `z = (INS − GNSS)`; the measurement matrix selects the
    /// position and velocity error blocks.
    pub fn update_loosely_coupled(
        &mut self,
        ins_pos: Vec3,
        ins_vel: Vec3,
        gnss: GnssMeasurement,
    ) -> [f64; N] {
        // Measurement matrix H (6×15): observes δp and δv.
        let mut h = Mat::zeros(6, N);
        for k in 0..3 {
            h.set(k, k, 1.0); // δp
            h.set(3 + k, 3 + k, 1.0); // δv
        }
        // Innovation z − H x̂.
        let z = [
            ins_pos[0] - gnss.pos[0],
            ins_pos[1] - gnss.pos[1],
            ins_pos[2] - gnss.pos[2],
            ins_vel[0] - gnss.vel[0],
            ins_vel[1] - gnss.vel[1],
            ins_vel[2] - gnss.vel[2],
        ];
        let hx = h.mul_vec(&self.x);
        let nu: Vec<f64> = (0..6).map(|i| z[i] - hx[i]).collect();

        // R (6×6 diagonal).
        let mut r = Mat::zeros(6, 6);
        let rp = gnss.sigma_pos_m.powi(2);
        let rv = gnss.sigma_vel_mps.powi(2);
        for k in 0..3 {
            r.set(k, k, rp);
            r.set(3 + k, 3 + k, rv);
        }

        let ht = h.transpose();
        let pht = self.p.mul(&ht); // 15×6
        let s = h.mul(&pht).add(&r); // 6×6 innovation covariance
        let s_inv = match s.inverse() {
            Some(si) => si,
            None => return [0.0; N], // ill-conditioned; skip update
        };
        let k_gain = pht.mul(&s_inv); // 15×6

        // δx ← δx + K ν.
        let dk = k_gain.mul_vec(&nu);
        for (xi, &dki) in self.x.iter_mut().zip(dk.iter()) {
            *xi += dki;
        }

        // Joseph form: P ← (I − K H) P (I − K H)ᵀ + K R Kᵀ.
        let kh = k_gain.mul(&h); // 15×15
        let i_kh = Mat::identity(N).sub(&kh);
        let mut p = i_kh
            .mul(&self.p)
            .mul(&i_kh.transpose())
            .add(&k_gain.mul(&r).mul(&k_gain.transpose()));
        p.symmetrize();
        self.p = p;

        self.x
    }

    /// Reset the error-state mean to zero after its correction has been applied to
    /// the strapdown solution (closed-loop feedback). The covariance is retained.
    pub fn reset_error_state(&mut self) {
        self.x = [0.0; N];
    }

    /// Tightly-coupled pseudorange update. The innovation is formed in the range
    /// domain: for each satellite the predicted range from the INS position
    /// `ins_pos` to `sat_positions[i]` is compared to the measured (receiver-clock
    /// corrected) `pseudoranges_m[i]`, and the line-of-sight unit vector `ê_i`
    /// gives the measurement row `H_i = [ê_iᵀ, 0, 0, 0, 0]` mapping the position
    /// error `δp` to the range residual. All positions must be in the same working
    /// frame (the local NED tangent plane used by [`super::closed_loop`]).
    ///
    /// Unlike [`update_loosely_coupled`](Self::update_loosely_coupled) this needs
    /// no PVT fix and so keeps correcting with **as few as one satellite** —
    /// constraining the error along that line of sight. `sigma_range_m` is the 1-σ
    /// pseudorange accuracy, applied isotropically. Returns the updated error state,
    /// or an error if the inputs are empty, mismatched, or `sigma_range_m ≤ 0`.
    pub fn update_tightly_coupled(
        &mut self,
        ins_pos: Vec3,
        sat_positions: &[Vec3],
        pseudoranges_m: &[f64],
        sigma_range_m: f64,
    ) -> Result<[f64; N], &'static str> {
        let m = sat_positions.len();
        if m == 0 || m != pseudoranges_m.len() {
            return Err("need one pseudorange per satellite, at least one");
        }
        if sigma_range_m.is_nan() || sigma_range_m <= 0.0 {
            return Err("sigma_range_m must be positive");
        }
        // Build H (m×N) and the innovation ν = (z − H x̂), with
        //   z_i = ρ̂_i(INS) − ρ_meas_i ≈ ê_i · δp.
        let mut h = Mat::zeros(m, N);
        let mut nu = vec![0.0; m];
        for i in 0..m {
            let d = [
                ins_pos[0] - sat_positions[i][0],
                ins_pos[1] - sat_positions[i][1],
                ins_pos[2] - sat_positions[i][2],
            ];
            let range = (d[0] * d[0] + d[1] * d[1] + d[2] * d[2]).sqrt();
            if range <= 0.0 {
                return Err("degenerate geometry: user coincides with a satellite");
            }
            let e = [d[0] / range, d[1] / range, d[2] / range]; // LOS, sat→user
            for (k, &ek) in e.iter().enumerate() {
                h.set(i, k, ek); // observes δp
            }
            let z = range - pseudoranges_m[i];
            let hx = e[0] * self.x[0] + e[1] * self.x[1] + e[2] * self.x[2];
            nu[i] = z - hx;
        }

        // R (m×m diagonal), then the standard Joseph-form Kalman update.
        let rr = sigma_range_m * sigma_range_m;
        let mut r = Mat::zeros(m, m);
        for i in 0..m {
            r.set(i, i, rr);
        }
        let ht = h.transpose();
        let pht = self.p.mul(&ht); // N×m
        let s = h.mul(&pht).add(&r); // m×m innovation covariance
        let s_inv = match s.inverse() {
            Some(si) => si,
            None => return Err("ill-conditioned innovation covariance"),
        };
        let k_gain = pht.mul(&s_inv); // N×m

        let dk = k_gain.mul_vec(&nu);
        for (xi, &dki) in self.x.iter_mut().zip(dk.iter()) {
            *xi += dki;
        }

        let kh = k_gain.mul(&h); // N×N
        let i_kh = Mat::identity(N).sub(&kh);
        let mut p = i_kh
            .mul(&self.p)
            .mul(&i_kh.transpose())
            .add(&k_gain.mul(&r).mul(&k_gain.transpose()));
        p.symmetrize();
        self.p = p;

        Ok(self.x)
    }
}

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

    fn default_ekf() -> GnssInsEkf {
        GnssInsEkf::new(10.0, 1.0, 0.01, 0.1, 0.01, EkfNoise::default())
    }

    #[test]
    fn skew_matrix_reproduces_the_cross_product() {
        let a = [1.0, 2.0, 3.0];
        let b = [-0.5, 0.7, 0.2];
        let s = skew(a);
        let sb = [
            s[0][0] * b[0] + s[0][1] * b[1] + s[0][2] * b[2],
            s[1][0] * b[0] + s[1][1] * b[1] + s[1][2] * b[2],
            s[2][0] * b[0] + s[2][1] * b[1] + s[2][2] * b[2],
        ];
        let cross = [
            a[1] * b[2] - a[2] * b[1],
            a[2] * b[0] - a[0] * b[2],
            a[0] * b[1] - a[1] * b[0],
        ];
        for i in 0..3 {
            assert!((sb[i] - cross[i]).abs() < 1e-15);
        }
    }

    #[test]
    fn gauss_jordan_inverse_is_correct() {
        let mut m = Mat::zeros(3, 3);
        m.set_block3(0, 0, [[2.0, 1.0, 0.0], [1.0, 3.0, 1.0], [0.0, 1.0, 2.0]]);
        let inv = m.inverse().unwrap();
        let prod = m.mul(&inv);
        for i in 0..3 {
            for j in 0..3 {
                let expect = if i == j { 1.0 } else { 0.0 };
                assert!((prod.at(i, j) - expect).abs() < 1e-12);
            }
        }
    }

    #[test]
    fn predict_keeps_covariance_symmetric_and_psd() {
        let mut ekf = default_ekf();
        let c_bn = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];
        let f_n = [0.1, -0.2, -9.81];
        let w_ie = [5e-5, 0.0, -5e-5];
        for _ in 0..50 {
            ekf.predict(f_n, c_bn, w_ie, w_ie, 0.1);
        }
        // Symmetric.
        for i in 0..N {
            for j in 0..N {
                assert!((ekf.p.at(i, j) - ekf.p.at(j, i)).abs() < 1e-12);
            }
        }
        // Diagonal non-negative (necessary PSD condition).
        for v in ekf.covariance_diagonal() {
            assert!(v >= 0.0, "negative variance {v}");
        }
        // Position uncertainty grows without aiding.
        assert!(ekf.position_cov_trace() > 3.0 * 10.0_f64.powi(2));
    }

    #[test]
    fn position_update_reduces_position_covariance() {
        let mut ekf = default_ekf();
        let before = ekf.position_cov_trace();
        let gnss = GnssMeasurement {
            pos: [0.0, 0.0, 0.0],
            vel: [0.0, 0.0, 0.0],
            sigma_pos_m: 1.0,
            sigma_vel_mps: 0.1,
        };
        ekf.update_loosely_coupled([0.0; 3], [0.0; 3], gnss);
        let after = ekf.position_cov_trace();
        assert!(
            after < before,
            "covariance did not shrink: {before} -> {after}"
        );
        // A 1 m GNSS sigma should pull the 10 m prior well below the prior.
        assert!(after < before * 0.1);
    }

    #[test]
    fn filter_recovers_a_known_position_error() {
        // Diagonal prior (from new): per-axis δp correction = P_p/(P_p+R_p)·z.
        let mut ekf = default_ekf();
        let true_pos_err = [3.0, -4.0, 1.5];
        // INS reads truth + error; GNSS reads truth ⇒ innovation = error.
        let ins_pos = true_pos_err;
        let gnss = GnssMeasurement {
            pos: [0.0; 3],
            vel: [0.0; 3],
            sigma_pos_m: 1.0,
            sigma_vel_mps: 0.1,
        };
        let dx = ekf.update_loosely_coupled(ins_pos, [0.0; 3], gnss);
        let pp = 10.0_f64.powi(2); // initial position variance
        let rp = 1.0_f64.powi(2);
        let gain = pp / (pp + rp);
        for k in 0..3 {
            assert!(
                (dx[k] - gain * true_pos_err[k]).abs() < 1e-9,
                "axis {k}: {} vs {}",
                dx[k],
                gain * true_pos_err[k]
            );
        }
    }

    #[test]
    fn larger_measurement_noise_means_smaller_correction() {
        let innov = [5.0, 0.0, 0.0];
        let mut tight = default_ekf();
        let mut loose = default_ekf();
        let dx_tight = tight.update_loosely_coupled(
            innov,
            [0.0; 3],
            GnssMeasurement {
                pos: [0.0; 3],
                vel: [0.0; 3],
                sigma_pos_m: 0.5,
                sigma_vel_mps: 0.1,
            },
        );
        let dx_loose = loose.update_loosely_coupled(
            innov,
            [0.0; 3],
            GnssMeasurement {
                pos: [0.0; 3],
                vel: [0.0; 3],
                sigma_pos_m: 50.0,
                sigma_vel_mps: 0.1,
            },
        );
        assert!(dx_tight[0] > dx_loose[0]);
        assert!(dx_loose[0] > 0.0 && dx_loose[0] < dx_tight[0]);
    }

    #[test]
    fn reset_zeros_the_error_state_but_keeps_covariance() {
        let mut ekf = default_ekf();
        ekf.update_loosely_coupled(
            [5.0, 0.0, 0.0],
            [0.0; 3],
            GnssMeasurement {
                pos: [0.0; 3],
                vel: [0.0; 3],
                sigma_pos_m: 1.0,
                sigma_vel_mps: 0.1,
            },
        );
        let cov_before = ekf.position_cov_trace();
        assert!(ekf.error_state()[0].abs() > 0.0);
        ekf.reset_error_state();
        assert_eq!(ekf.error_state(), [0.0; N]);
        assert_eq!(ekf.position_cov_trace(), cov_before);
    }

    #[test]
    fn tightly_coupled_observes_the_along_line_of_sight_error_only() {
        // One satellite directly overhead (NED down = up). Its line of sight is
        // purely vertical, so a pseudorange update can observe the DOWN component
        // of the position error but not the horizontal — the range-domain
        // selectivity that lets a tightly-coupled filter use one satellite.
        let mut ekf = default_ekf();
        let sat: Vec3 = [0.0, 0.0, -20_000_000.0];
        let ins_pos: Vec3 = [0.0, 0.0, 0.0];
        // True error is 10 m down: truth = ins − δp ⇒ measured range = |truth − sat|.
        let truth: Vec3 = [0.0, 0.0, -10.0];
        let dsat = [truth[0] - sat[0], truth[1] - sat[1], truth[2] - sat[2]];
        let rho_meas = (dsat[0] * dsat[0] + dsat[1] * dsat[1] + dsat[2] * dsat[2]).sqrt();
        let dx = ekf
            .update_tightly_coupled(ins_pos, &[sat], &[rho_meas], 1.0)
            .unwrap();
        // The down error is recovered; the horizontal stays ~unobserved.
        assert!((dx[2] - 10.0).abs() < 1.0, "down error estimate {}", dx[2]);
        assert!(
            dx[0].abs() < 1e-6 && dx[1].abs() < 1e-6,
            "horizontal leaked"
        );
    }

    #[test]
    fn tightly_coupled_rejects_malformed_input() {
        let mut ekf = default_ekf();
        let sat = [0.0, 0.0, -2.0e7];
        assert!(ekf.update_tightly_coupled([0.0; 3], &[], &[], 1.0).is_err());
        assert!(ekf
            .update_tightly_coupled([0.0; 3], &[sat], &[], 1.0)
            .is_err());
        assert!(ekf
            .update_tightly_coupled([0.0; 3], &[sat], &[2.0e7], -1.0)
            .is_err());
    }
}