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
extern crate alloc;
use Vec;
use crateVector;
use crateFloatScalar;
use crateMatrix;
use EstimateError;
/// Record of a single EKF forward-pass step, stored by the user for RTS smoothing.
///
/// Each step captures the state/covariance before and after the measurement
/// update, plus the dynamics Jacobian used in the predict step.
///
/// # Fields
///
/// - `x_predicted` / `p_predicted` — state & covariance after `predict`, before `update`
/// - `x_updated` / `p_updated` — state & covariance after `update`
/// - `f_jacobian` — the `F` matrix used in the predict step
/// Rauch–Tung–Striebel fixed-interval smoother (backward pass).
///
/// Given a sequence of forward EKF steps (predict → update at each timestep),
/// returns smoothed `(x, P)` pairs that incorporate future measurements.
///
/// The smoothed estimates always have equal or smaller covariance than the
/// filtered estimates.
///
/// # Arguments
///
/// - `steps` — forward EKF steps in chronological order
///
/// # Returns
///
/// `Vec<(Vector<T,N>, Matrix<T,N,N>)>` of smoothed (state, covariance)
/// for each timestep, in the same order as `steps`.
///
/// Returns `SingularInnovation` if any predicted covariance is singular
/// (required for smoother gain computation).
///
/// # Example
///
/// ```
/// use numeris::estimate::{Ekf, EkfStep, rts_smooth};
/// use numeris::{Vector, Matrix};
///
/// let dt = 0.1_f64;
/// let q = Matrix::new([[0.01, 0.0], [0.0, 0.01]]);
/// let r = Matrix::new([[0.5]]);
/// let f_jac = Matrix::new([[1.0, dt], [0.0, 1.0]]);
///
/// let mut ekf = Ekf::<f64, 2, 1>::new(
/// Vector::from_array([0.0, 0.0]),
/// Matrix::new([[10.0, 0.0], [0.0, 10.0]]),
/// );
///
/// let measurements = [0.1, 0.22, 0.35, 0.45, 0.58];
/// let mut steps = Vec::new();
///
/// for &z_val in &measurements {
/// // Predict
/// let x_pre = ekf.x;
/// ekf.predict(
/// |x| Vector::from_array([x[0] + dt * x[1], x[1]]),
/// |_| f_jac,
/// Some(&q),
/// );
/// let x_predicted = ekf.x;
/// let p_predicted = ekf.p;
///
/// // Update
/// ekf.update(
/// &Vector::from_array([z_val]),
/// |x| Vector::from_array([x[0]]),
/// |_| Matrix::new([[1.0, 0.0]]),
/// &r,
/// ).unwrap();
///
/// steps.push(EkfStep {
/// x_predicted,
/// p_predicted,
/// x_updated: ekf.x,
/// p_updated: ekf.p,
/// f_jacobian: f_jac,
/// });
/// }
///
/// let smoothed = rts_smooth(&steps).unwrap();
/// assert_eq!(smoothed.len(), steps.len());
/// ```