usls 0.1.11

A Rust library integrated with ONNXRuntime, providing a collection of ML 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
/// Kalman Filter for 8D state space (x, y, a, h, vx, vy, va, vh)
///
/// Implements a Kalman filter for tracking objects with position (x, y), aspect ratio (a),
/// height (h), and their corresponding velocities. Used in multi-object tracking systems.
#[derive(Debug, Clone)]
pub struct KalmanFilterXYAH {
    std_weight_position: f32,
    std_weight_velocity: f32,
    motion_mat: [[f32; 8]; 8], // 8x8
    update_mat: [[f32; 8]; 4], // 4x8
}

impl Default for KalmanFilterXYAH {
    fn default() -> Self {
        Self::new(1.0 / 20.0, 1.0 / 160.0)
    }
}

impl KalmanFilterXYAH {
    /// Creates a new Kalman filter with custom noise parameters
    pub fn new(std_weight_position: f32, std_weight_velocity: f32) -> Self {
        let ndim = 4;
        let dt = 1.0;
        let mut motion_mat = mat8x8_identity();
        let mut update_mat = mat4x8_zeros();
        update_mat[0][0] = 1.0;
        update_mat[1][1] = 1.0;
        update_mat[2][2] = 1.0;
        update_mat[3][3] = 1.0;

        for (i, row) in motion_mat.iter_mut().enumerate().take(ndim) {
            row[i + ndim] = dt;
        }

        Self {
            std_weight_position,
            std_weight_velocity,
            motion_mat,
            update_mat,
        }
    }

    /// Initializes a new track from a detection measurement
    pub fn initiate(&self, measurement: &DetectBox) -> (StateMean, StateCov) {
        let mut mean = [0.0; 8];

        let mean_vel = [0.0; 4];
        let mean_pos = measurement;
        mean[0..4].copy_from_slice(mean_pos);
        mean[4..8].copy_from_slice(&mean_vel);

        let mut std = [0.0; 8];
        let mesure_val = measurement[3];
        std[0] = 2.0 * self.std_weight_position * mesure_val;
        std[1] = 2.0 * self.std_weight_position * mesure_val;
        std[2] = 1e-2;
        std[3] = 2.0 * self.std_weight_position * mesure_val;
        std[4] = 10.0 * self.std_weight_velocity * mesure_val;
        std[5] = 10.0 * self.std_weight_velocity * mesure_val;
        std[6] = 1e-5;
        std[7] = 10.0 * self.std_weight_velocity * mesure_val;

        let tmp = vec8_mul(&std, &std);
        let covariance = mat8x8_from_diagonal(&tmp);

        (mean, covariance)
    }

    /// Predicts the next state based on current state and motion model
    pub fn predict(&self, mean: &StateMean, covariance: &StateCov) -> (StateMean, StateCov) {
        let mut new_mean = *mean;
        let mut new_covariance = *covariance;
        let mut std = [0.0; 8];
        std[0] = self.std_weight_position * new_mean[3];
        std[1] = self.std_weight_position * new_mean[3];
        std[2] = 1e-1;
        std[3] = self.std_weight_position * new_mean[3];
        std[4] = self.std_weight_velocity * new_mean[3];
        std[5] = self.std_weight_velocity * new_mean[3];
        std[6] = 1e-5;
        std[7] = self.std_weight_velocity * new_mean[3];

        let tmp = vec8_mul(&std, &std);
        let motion_cov = mat8x8_from_diagonal(&tmp);
        new_mean = mat8x8_mul_vec8(&self.motion_mat, &new_mean);

        let tmp1 = mat8x8_mul(&self.motion_mat, &new_covariance);
        let tmp2 = mat8x8_transpose(&self.motion_mat);
        let tmp = mat8x8_mul(&tmp1, &tmp2);
        new_covariance = mat8x8_add(&tmp, &motion_cov);

        (new_mean, new_covariance)
    }

    /// Updates the state estimate using a new measurement
    pub fn update(
        &self,
        mean: &StateMean,
        covariance: &StateCov,
        measurement: &DetectBox,
    ) -> (StateMean, StateCov) {
        let mut new_mean = *mean;
        let mut new_covariance = *covariance;
        let (projected_mean, projected_covariance) = self.project(&new_mean, &new_covariance);

        let update_mat_t = mat4x8_transpose(&self.update_mat);
        let b_t = mat8x8_mul_mat8x4(&new_covariance, &update_mat_t);
        let b = mat8x4_transpose(&b_t);

        // kalman_gain: 4x8
        let kalman_gain = cholesky_solve_4x4(&projected_covariance, &b).unwrap();

        // innovation: 1x4
        let innovation = vec4_sub(measurement, &projected_mean);

        // tmp: 1x8
        let tmp = vec4_mul_mat4x8(&innovation, &kalman_gain);
        new_mean = vec8_add(&new_mean, &tmp);

        let kg_t = mat4x8_transpose(&kalman_gain);
        let tmp1 = mat8x4_mul_mat4x4(&kg_t, &projected_covariance);
        let tmp2 = mat8x4_mul_mat4x8(&tmp1, &kalman_gain);
        new_covariance = mat8x8_sub(&new_covariance, &tmp2);

        (new_mean, new_covariance)
    }

    /// Projects the state distribution to measurement space
    pub fn project(&self, mean: &StateMean, covariance: &StateCov) -> (StateHMean, StateHCov) {
        let std = [
            self.std_weight_position * mean[3],
            self.std_weight_position * mean[3],
            1e-1,
            self.std_weight_position * mean[3],
        ];

        // update_mat: 4x8, mean: 1x8
        // projected_mean: 1x4
        let update_mat_t = mat4x8_transpose(&self.update_mat);
        let projected_mean = vec8_mul_mat8x4(mean, &update_mat_t);

        // 4x4
        let diag = mat4x4_from_diagonal(&std);
        let innovation_cov = mat4x4_component_mul(&diag, &diag);
        let tmp1 = mat4x8_mul_mat8x8(&self.update_mat, covariance);
        let update_mat_t = mat4x8_transpose(&self.update_mat);
        let cov = mat4x8_mul_mat8x4(&tmp1, &update_mat_t);
        let projected_covariance = mat4x4_add(&cov, &innovation_cov);

        (projected_mean, projected_covariance)
    }
}

// 1x4
pub type DetectBox = [f32; 4];
// 1x8
pub type StateMean = [f32; 8];
// 8x8
pub type StateCov = [[f32; 8]; 8];
// 1x4
pub type StateHMean = [f32; 4];
// 4x4
pub type StateHCov = [[f32; 4]; 4];

#[inline]
fn mat8x8_identity() -> [[f32; 8]; 8] {
    let mut m = [[0.0; 8]; 8];
    for (i, row) in m.iter_mut().enumerate() {
        row[i] = 1.0;
    }
    m
}

#[inline]
fn mat4x8_zeros() -> [[f32; 8]; 4] {
    [[0.0; 8]; 4]
}

// 8x8 diagonal matrix from 1x8 vector
#[inline]
fn mat8x8_from_diagonal(v: &[f32; 8]) -> [[f32; 8]; 8] {
    let mut m = [[0.0; 8]; 8];
    for i in 0..8 {
        m[i][i] = v[i];
    }
    m
}

// 4x4 diagonal matrix from 1x4 vector
#[inline]
fn mat4x4_from_diagonal(v: &[f32; 4]) -> [[f32; 4]; 4] {
    let mut m = [[0.0; 4]; 4];
    for i in 0..4 {
        m[i][i] = v[i];
    }
    m
}

// Element-wise multiplication of two 1x8 vectors
#[inline]
fn vec8_mul(a: &[f32; 8], b: &[f32; 8]) -> [f32; 8] {
    let mut result = [0.0; 8];
    for i in 0..8 {
        result[i] = a[i] * b[i];
    }
    result
}

// Element-wise multiplication of two 4x4 matrices
#[inline]
fn mat4x4_component_mul(a: &[[f32; 4]; 4], b: &[[f32; 4]; 4]) -> [[f32; 4]; 4] {
    let mut result = [[0.0; 4]; 4];
    for i in 0..4 {
        for j in 0..4 {
            result[i][j] = a[i][j] * b[i][j];
        }
    }
    result
}

// 8x8 matrix multiplication
#[inline]
fn mat8x8_mul(a: &[[f32; 8]; 8], b: &[[f32; 8]; 8]) -> [[f32; 8]; 8] {
    let mut result = [[0.0; 8]; 8];
    for (i, row) in result.iter_mut().enumerate() {
        for (j, cell) in row.iter_mut().enumerate() {
            for (k, &a_val) in a[i].iter().enumerate() {
                *cell += a_val * b[k][j];
            }
        }
    }
    result
}

// 8x8 matrix + 8x8 matrix
#[inline]
fn mat8x8_add(a: &[[f32; 8]; 8], b: &[[f32; 8]; 8]) -> [[f32; 8]; 8] {
    let mut result = [[0.0; 8]; 8];
    for i in 0..8 {
        for j in 0..8 {
            result[i][j] = a[i][j] + b[i][j];
        }
    }
    result
}

// 8x8 matrix - 8x8 matrix
#[inline]
fn mat8x8_sub(a: &[[f32; 8]; 8], b: &[[f32; 8]; 8]) -> [[f32; 8]; 8] {
    let mut result = [[0.0; 8]; 8];
    for i in 0..8 {
        for j in 0..8 {
            result[i][j] = a[i][j] - b[i][j];
        }
    }
    result
}

// 8x8 matrix transpose
#[inline]
fn mat8x8_transpose(m: &[[f32; 8]; 8]) -> [[f32; 8]; 8] {
    let mut result = [[0.0; 8]; 8];
    for (i, row) in m.iter().enumerate() {
        for (j, &val) in row.iter().enumerate() {
            result[j][i] = val;
        }
    }
    result
}

// 4x8 matrix transpose to 8x4
#[inline]
fn mat4x8_transpose(m: &[[f32; 8]; 4]) -> [[f32; 4]; 8] {
    let mut result = [[0.0; 4]; 8];
    for (i, row) in m.iter().enumerate() {
        for (j, &val) in row.iter().enumerate() {
            result[j][i] = val;
        }
    }
    result
}

// 8x8 matrix * 1x8 vector (returns 1x8)
#[inline]
fn mat8x8_mul_vec8(m: &[[f32; 8]; 8], v: &[f32; 8]) -> [f32; 8] {
    let mut result = [0.0; 8];
    for (i, row) in m.iter().enumerate() {
        for (j, &val) in row.iter().enumerate() {
            result[i] += val * v[j];
        }
    }
    result
}

// 1x8 vector * 8x4 matrix (returns 1x4)
#[inline]
fn vec8_mul_mat8x4(v: &[f32; 8], m: &[[f32; 4]; 8]) -> [f32; 4] {
    let mut result = [0.0; 4];
    for (j, cell) in result.iter_mut().enumerate() {
        for (i, &val) in m.iter().enumerate() {
            *cell += v[i] * val[j];
        }
    }
    result
}

// 4x8 matrix * 8x8 matrix (returns 4x8)
#[inline]
fn mat4x8_mul_mat8x8(a: &[[f32; 8]; 4], b: &[[f32; 8]; 8]) -> [[f32; 8]; 4] {
    let mut result = [[0.0; 8]; 4];
    for (i, row) in result.iter_mut().enumerate() {
        for (j, cell) in row.iter_mut().enumerate() {
            for (k, &a_val) in a[i].iter().enumerate() {
                *cell += a_val * b[k][j];
            }
        }
    }
    result
}

// 4x8 matrix * 8x4 matrix (returns 4x4)
#[inline]
fn mat4x8_mul_mat8x4(a: &[[f32; 8]; 4], b: &[[f32; 4]; 8]) -> [[f32; 4]; 4] {
    let mut result = [[0.0; 4]; 4];
    for (i, row) in result.iter_mut().enumerate() {
        for (j, cell) in row.iter_mut().enumerate() {
            for (k, &a_val) in a[i].iter().enumerate() {
                *cell += a_val * b[k][j];
            }
        }
    }
    result
}

// 8x8 matrix * 8x4 matrix (returns 8x4)
#[inline]
fn mat8x8_mul_mat8x4(a: &[[f32; 8]; 8], b: &[[f32; 4]; 8]) -> [[f32; 4]; 8] {
    let mut result = [[0.0; 4]; 8];
    for (i, row) in result.iter_mut().enumerate() {
        for (j, cell) in row.iter_mut().enumerate() {
            for (k, &a_val) in a[i].iter().enumerate() {
                *cell += a_val * b[k][j];
            }
        }
    }
    result
}

// 1x4 vector - 1x4 vector
#[inline]
fn vec4_sub(a: &[f32; 4], b: &[f32; 4]) -> [f32; 4] {
    let mut result = [0.0; 4];
    for (i, &a_val) in a.iter().enumerate() {
        result[i] = a_val - b[i];
    }
    result
}

// 1x8 vector + 1x8 vector
#[inline]
fn vec8_add(a: &[f32; 8], b: &[f32; 8]) -> [f32; 8] {
    let mut result = [0.0; 8];
    for i in 0..8 {
        result[i] = a[i] + b[i];
    }
    result
}

// 1x4 vector * 4x8 matrix (returns 1x8)
#[inline]
fn vec4_mul_mat4x8(v: &[f32; 4], m: &[[f32; 8]; 4]) -> [f32; 8] {
    let mut result = [0.0; 8];
    for (j, cell) in result.iter_mut().enumerate() {
        for (i, &val) in m.iter().enumerate() {
            *cell += v[i] * val[j];
        }
    }
    result
}

// 8x4 matrix * 4x4 matrix (returns 8x4)
#[inline]
fn mat8x4_mul_mat4x4(a: &[[f32; 4]; 8], b: &[[f32; 4]; 4]) -> [[f32; 4]; 8] {
    let mut result = [[0.0; 4]; 8];
    for (i, row) in result.iter_mut().enumerate() {
        for (j, cell) in row.iter_mut().enumerate() {
            for (k, &a_val) in a[i].iter().enumerate() {
                *cell += a_val * b[k][j];
            }
        }
    }
    result
}

// 8x4 matrix transpose to 4x8
#[inline]
fn mat8x4_transpose(m: &[[f32; 4]; 8]) -> [[f32; 8]; 4] {
    let mut result = [[0.0; 8]; 4];
    for (i, row) in m.iter().enumerate() {
        for (j, &val) in row.iter().enumerate() {
            result[j][i] = val;
        }
    }
    result
}

// 8x4 matrix * 4x8 matrix (returns 8x8)
#[inline]
fn mat8x4_mul_mat4x8(a: &[[f32; 4]; 8], b: &[[f32; 8]; 4]) -> [[f32; 8]; 8] {
    let mut result = [[0.0; 8]; 8];
    for (i, row) in result.iter_mut().enumerate() {
        for (j, cell) in row.iter_mut().enumerate() {
            for (k, &a_val) in a[i].iter().enumerate() {
                *cell += a_val * b[k][j];
            }
        }
    }
    result
}

// 4x4 matrix + 4x4 matrix
#[inline]
fn mat4x4_add(a: &[[f32; 4]; 4], b: &[[f32; 4]; 4]) -> [[f32; 4]; 4] {
    let mut result = [[0.0; 4]; 4];
    for i in 0..4 {
        for j in 0..4 {
            result[i][j] = a[i][j] + b[i][j];
        }
    }
    result
}

// Cholesky decomposition for 4x4 positive definite matrix
// Returns lower triangular matrix L such that A = L * L^T
#[inline]
fn cholesky_4x4(a: &[[f32; 4]; 4]) -> Option<[[f32; 4]; 4]> {
    let mut l = [[0.0; 4]; 4];

    for i in 0..4 {
        for j in 0..=i {
            let mut sum = 0.0;
            for (k, &l_val) in l[i].iter().enumerate().take(j) {
                sum += l_val * l[j][k];
            }

            if i == j {
                let val = a[i][i] - sum;
                if val <= 0.0 {
                    return None; // Not positive definite
                }
                l[i][j] = val.sqrt();
            } else {
                if l[j][j].abs() < 1e-10 {
                    return None;
                }
                l[i][j] = (a[i][j] - sum) / l[j][j];
            }
        }
    }

    Some(l)
}

// Solve L * y = b for y (forward substitution)
#[inline]
fn forward_substitution_4x4(l: &[[f32; 4]; 4], b: &[[f32; 8]; 4]) -> [[f32; 8]; 4] {
    let mut y = [[0.0; 8]; 4];

    for i in 0..4 {
        for j in 0..8 {
            let mut sum = 0.0;
            for (k, &l_val) in l[i].iter().enumerate().take(i) {
                sum += l_val * y[k][j];
            }
            y[i][j] = (b[i][j] - sum) / l[i][i];
        }
    }

    y
}

// Solve L^T * x = y for x (backward substitution)
#[inline]
fn backward_substitution_4x4(l: &[[f32; 4]; 4], y: &[[f32; 8]; 4]) -> [[f32; 8]; 4] {
    let mut x = [[0.0; 8]; 4];

    for i in (0..4).rev() {
        for j in 0..8 {
            let mut sum = 0.0;
            for (k, &l_val) in l.iter().enumerate().skip(i + 1) {
                sum += l_val[i] * x[k][j];
            }
            x[i][j] = (y[i][j] - sum) / l[i][i];
        }
    }

    x
}

// Solve A * X = B using Cholesky decomposition, where A is 4x4 and B is 4x8
// Returns X (4x8 matrix)
#[inline]
fn cholesky_solve_4x4(a: &[[f32; 4]; 4], b: &[[f32; 8]; 4]) -> Option<[[f32; 8]; 4]> {
    let l = cholesky_4x4(a)?;
    let y = forward_substitution_4x4(&l, b);
    let x = backward_substitution_4x4(&l, &y);
    Some(x)
}

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

    #[test]
    fn test_initiate() {
        let kf = KalmanFilterXYAH::default();
        let measurement = [1.0, 2.0, 3.0, 4.0];
        let (mean, covariance) = kf.initiate(&measurement);

        // Check mean initialization
        assert_eq!(mean[0..4], measurement);
        assert_eq!(mean[4..8], [0.0; 4]);

        // Check covariance is positive definite
        for (i, row) in covariance.iter().enumerate() {
            assert!(row[i] > 0.0);
        }
    }

    #[test]
    fn test_predict() {
        let kf = KalmanFilterXYAH::default();
        let mean = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
        let covariance = [
            [0.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
            [0.0, 0.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 0.01, 0.0, 0.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 0.0, 0.2, 0.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 0.0, 0.0, 4.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 0.0, 0.0, 0.0, 4.0, 0.0, 0.0],
            [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.000001, 0.0],
            [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 4.0],
        ];

        let (new_mean, new_covariance) = kf.predict(&mean, &covariance);

        // Check position update with velocity
        assert_eq!(new_mean[0], mean[0] + mean[4]); // x + vx
        assert_eq!(new_mean[1], mean[1] + mean[5]); // y + vy
        assert_eq!(new_mean[2], mean[2] + mean[6]); // a + va
        assert_eq!(new_mean[3], mean[3] + mean[7]); // h + vh

        // Check covariance remains positive definite
        for (i, row) in new_covariance.iter().enumerate() {
            assert!(row[i] > 0.0);
        }
    }

    #[test]
    fn test_project() {
        let kf = KalmanFilterXYAH::default();
        let mean = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
        let covariance = [
            [4.24, 0.0, 0.0, 0.0, 4.0, 0.0, 0.0, 0.0],
            [0.0, 4.24, 0.0, 0.0, 0.0, 4.0, 0.0, 0.0],
            [0.0, 0.0, 1.01e-2, 0.0, 0.0, 0.0, 1.0e-6, 0.0],
            [0.0, 0.0, 0.0, 4.24, 0.0, 0.0, 0.0, 4.0],
            [4.0, 0.0, 0.0, 0.0, 4.000625, 0.0, 0.0, 0.0],
            [0.0, 4.0, 0.0, 0.0, 0.0, 4.000625, 0.0, 0.0],
            [0.0, 0.0, 1.0e-6, 0.0, 0.0, 0.0, 1.0e-6, 0.0],
            [0.0, 0.0, 0.0, 4.0, 0.0, 0.0, 0.0, 4.000625],
        ];

        let (projected_mean, projected_covariance) = kf.project(&mean, &covariance);

        // Check projection extracts position components
        assert_eq!(projected_mean, [1.0, 2.0, 3.0, 4.0]);

        // Check projected covariance is positive definite
        for (i, row) in projected_covariance.iter().enumerate() {
            assert!(row[i] > 0.0);
        }
    }

    #[test]
    fn test_update() {
        let kf = KalmanFilterXYAH::default();
        let mean = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
        let covariance = [
            [4.24, 0.0, 0.0, 0.0, 4.0, 0.0, 0.0, 0.0],
            [0.0, 4.24, 0.0, 0.0, 0.0, 4.0, 0.0, 0.0],
            [0.0, 0.0, 1.01e-2, 0.0, 0.0, 0.0, 1.0e-6, 0.0],
            [0.0, 0.0, 0.0, 4.24, 0.0, 0.0, 0.0, 4.0],
            [4.0, 0.0, 0.0, 0.0, 4.000625, 0.0, 0.0, 0.0],
            [0.0, 4.0, 0.0, 0.0, 0.0, 4.000625, 0.0, 0.0],
            [0.0, 0.0, 1.0e-6, 0.0, 0.0, 0.0, 1.0e-6, 0.0],
            [0.0, 0.0, 0.0, 4.0, 0.0, 0.0, 0.0, 4.000625],
        ];

        let measurement = [1.0, 2.0, 3.0, 4.0];
        let (new_mean, new_covariance) = kf.update(&mean, &covariance, &measurement);

        // Check covariance remains positive definite after update
        for (i, row) in new_covariance.iter().enumerate() {
            assert!(row[i] > 0.0);
        }

        // Check that update doesn't cause numerical instability
        for val in &new_mean {
            assert!(val.is_finite());
        }
    }

    #[test]
    fn test_full_cycle() {
        let kf = KalmanFilterXYAH::default();
        let measurement = [100.0, 200.0, 1.5, 50.0];

        // Initialize track
        let (mut mean, mut covariance) = kf.initiate(&measurement);

        // Run multiple predict-update cycles
        for i in 0..5 {
            let (new_mean, new_covariance) = kf.predict(&mean, &covariance);
            mean = new_mean;
            covariance = new_covariance;

            let new_measurement = [
                100.0 + (i as f32) * 2.0,
                200.0 + (i as f32) * 2.0,
                1.5,
                50.0,
            ];
            let (new_mean, new_covariance) = kf.update(&mean, &covariance, &new_measurement);
            mean = new_mean;
            covariance = new_covariance;
        }

        // Check convergence properties
        assert!((mean[0] - 108.0).abs() < 10.0);
        assert!((mean[1] - 208.0).abs() < 10.0);

        // Check covariance remains positive definite
        for (i, row) in covariance.iter().enumerate() {
            assert!(row[i] > 0.0);
        }
    }

    #[test]
    fn test_constructor() {
        let kf = KalmanFilterXYAH::default();
        assert_eq!(kf.std_weight_position, 0.05);
        assert_eq!(kf.std_weight_velocity, 0.00625);
        assert_eq!(kf.motion_mat.len(), 8);
        assert_eq!(kf.update_mat.len(), 4);
    }
}