deimos_numerics 0.16.1

Numerical methods and control systems analysis
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
//! Simplified dynamic-size discrete-time extended Kalman filtering.
//!
//! # Glossary
//!
//! - **Jacobian:** First derivative of the state or output map with respect to
//!   the state.
//! - **Innovation:** Measurement residual between the actual and predicted
//!   outputs.
//! - **Process-noise covariance:** Covariance assigned to state evolution
//!   uncertainty.
//! - **Measurement-noise covariance:** Covariance assigned to sensor
//!   uncertainty.

use super::core::{normalized_innovation_norm, predict_covariance, updated_covariance};
use super::dense::{
    llt_solve, mat_mul, mat_mul_vec, vec_add, vec_as_slice, vec_as_slice_mut, vec_norm, vec_sub,
    vector_as_column_matrix, vector_from_slice, zero_matrix, zero_vector,
};
use super::{validate_input_dim, validate_output_dim};
use crate::control::estimation::CovarianceUpdate;
use crate::embedded::EmbeddedError;
use crate::embedded::alloc::{Matrix, Vector};
use crate::sparse::compensated::CompensatedField;
use faer_traits::RealField;
use num_traits::Float;

/// Dynamic nonlinear discrete-time model.
pub trait DiscreteNonlinearModel<T> {
    /// State dimension.
    fn state_dim(&self) -> usize;
    /// Input dimension.
    fn input_dim(&self) -> usize;
    /// Output dimension.
    fn output_dim(&self) -> usize;
    /// Evaluates the transition `x[k+1] = f(x[k], u[k])`.
    ///
    /// Args:
    ///   state: Current state slice with length `state_dim()`.
    ///   input: Current input slice with length `input_dim()`.
    ///   next_state: Output buffer with length `state_dim()` written with the
    ///     next state.
    fn transition(&self, state: &[T], input: &[T], next_state: &mut [T]);
    /// Evaluates the output `y[k] = h(x[k], u[k])`.
    ///
    /// Args:
    ///   state: Current state slice with length `state_dim()`.
    ///   input: Current input slice with length `input_dim()`.
    ///   output: Output buffer with length `output_dim()` written with the
    ///     model output in measurement units.
    fn output(&self, state: &[T], input: &[T], output: &mut [T]);
}

/// EKF-specific nonlinear model with explicit Jacobians.
pub trait DiscreteExtendedKalmanModel<T>: DiscreteNonlinearModel<T> {
    /// Evaluates the transition Jacobian `∂f/∂x`.
    ///
    /// Args:
    ///   state: Current state slice with length `state_dim()`.
    ///   input: Current input slice with length `input_dim()`.
    ///   jacobian: Output matrix with shape `(state_dim(), state_dim())`.
    fn transition_jacobian(&self, state: &[T], input: &[T], jacobian: &mut Matrix<T>);
    /// Evaluates the output Jacobian `∂h/∂x`.
    ///
    /// Args:
    ///   state: Current state slice with length `state_dim()`.
    ///   input: Current input slice with length `input_dim()`.
    ///   jacobian: Output matrix with shape `(output_dim(), state_dim())`.
    fn output_jacobian(&self, state: &[T], input: &[T], jacobian: &mut Matrix<T>);
}

/// One EKF prediction stage.
///
/// Each field is a prediction-stage quantity with state shape `(nx, 1)`,
/// covariance shape `(nx, nx)`, or output shape `(ny, 1)`.
#[derive(Clone, Debug, PartialEq)]
pub struct ExtendedKalmanPrediction<T> {
    /// Predicted state estimate.
    pub state: Vector<T>,
    /// Predicted covariance.
    pub covariance: Matrix<T>,
    /// Predicted output.
    pub output: Vector<T>,
}

/// One EKF update stage.
///
/// Each field is an update-stage quantity with innovation shape `(ny, 1)`,
/// gain shape `(nx, ny)`, state shape `(nx, 1)`, covariance shape `(nx, nx)`,
/// or output shape `(ny, 1)`.
#[derive(Clone, Debug, PartialEq)]
pub struct ExtendedKalmanUpdate<T> {
    /// Innovation `y - h(x^-, u)`.
    pub innovation: Vector<T>,
    /// Euclidean innovation norm.
    pub innovation_norm: T,
    /// Innovation covariance.
    pub innovation_covariance: Matrix<T>,
    /// Normalized innovation norm.
    pub normalized_innovation_norm: T,
    /// Kalman gain.
    pub gain: Matrix<T>,
    /// Posterior state estimate.
    pub state: Vector<T>,
    /// Posterior covariance.
    pub covariance: Matrix<T>,
    /// Posterior output estimate.
    pub output: Vector<T>,
}

#[derive(Clone, Debug, PartialEq)]
struct EkfScratch<T> {
    f: Matrix<T>,
    h: Matrix<T>,
    state: Vector<T>,
    output: Vector<T>,
}

impl<T> EkfScratch<T>
where
    T: Float,
{
    fn new(state_dim: usize, output_dim: usize) -> Self {
        Self {
            f: zero_matrix(state_dim, state_dim),
            h: zero_matrix(output_dim, state_dim),
            state: zero_vector(state_dim),
            output: zero_vector(output_dim),
        }
    }

    fn ensure_dims(&mut self, state_dim: usize, output_dim: usize) {
        if self.f.nrows() != state_dim || self.f.ncols() != state_dim {
            self.f = zero_matrix(state_dim, state_dim);
        }
        if self.h.nrows() != output_dim || self.h.ncols() != state_dim {
            self.h = zero_matrix(output_dim, state_dim);
        }
        if self.state.nrows() != state_dim {
            self.state = zero_vector(state_dim);
        }
        if self.output.nrows() != output_dim {
            self.output = zero_vector(output_dim);
        }
    }
}

/// Dynamic-size extended Kalman filter runtime.
#[derive(Clone, Debug, PartialEq)]
pub struct ExtendedKalmanFilter<T, M> {
    /// Nonlinear model.
    pub model: M,
    /// Process-noise covariance.
    pub w: Matrix<T>,
    /// Measurement-noise covariance.
    pub v: Matrix<T>,
    /// Current posterior state estimate.
    pub x_hat: Vector<T>,
    /// Current posterior covariance.
    pub p: Matrix<T>,
    scratch: EkfScratch<T>,
}

impl<T, M> ExtendedKalmanFilter<T, M>
where
    T: CompensatedField + RealField,
    T::Real: Float,
    M: DiscreteExtendedKalmanModel<T>,
{
    /// Creates a validated dynamic-size EKF runtime.
    ///
    /// Args:
    ///   model: Nonlinear model with `nx = state_dim()` states and `ny =
    ///     output_dim()` outputs.
    ///   w: Process-noise covariance with shape `(nx, nx)`.
    ///   v: Measurement-noise covariance with shape `(ny, ny)`.
    ///   x_hat: Initial posterior state estimate with shape `(nx, 1)`.
    ///   p: Initial posterior covariance with shape `(nx, nx)`.
    ///
    /// Returns:
    ///   A validated EKF runtime with reusable internal scratch for `step()`.
    pub fn new(
        model: M,
        w: Matrix<T>,
        v: Matrix<T>,
        x_hat: Vector<T>,
        p: Matrix<T>,
    ) -> Result<Self, EmbeddedError> {
        let nx = model.state_dim();
        let ny = model.output_dim();
        if x_hat.nrows() != nx {
            return Err(EmbeddedError::LengthMismatch {
                which: "embedded.alloc.ekf.x_hat",
                expected: nx,
                actual: x_hat.nrows(),
            });
        }
        if w.nrows() != nx || w.ncols() != nx {
            return Err(EmbeddedError::DimensionMismatch {
                which: "embedded.alloc.ekf.w",
                expected_rows: nx,
                expected_cols: nx,
                actual_rows: w.nrows(),
                actual_cols: w.ncols(),
            });
        }
        if v.nrows() != ny || v.ncols() != ny {
            return Err(EmbeddedError::DimensionMismatch {
                which: "embedded.alloc.ekf.v",
                expected_rows: ny,
                expected_cols: ny,
                actual_rows: v.nrows(),
                actual_cols: v.ncols(),
            });
        }
        if p.nrows() != nx || p.ncols() != nx {
            return Err(EmbeddedError::DimensionMismatch {
                which: "embedded.alloc.ekf.p",
                expected_rows: nx,
                expected_cols: nx,
                actual_rows: p.nrows(),
                actual_cols: p.ncols(),
            });
        }

        Ok(Self {
            model,
            w,
            v,
            x_hat,
            p,
            scratch: EkfScratch::new(nx, ny),
        })
    }

    /// Returns the current posterior state estimate with shape `(nx, 1)`.
    #[must_use]
    pub fn state_estimate(&self) -> &Vector<T> {
        &self.x_hat
    }

    /// Returns the current posterior covariance with shape `(nx, nx)`.
    #[must_use]
    pub fn covariance(&self) -> &Matrix<T> {
        &self.p
    }

    /// Computes the non-mutating prediction stage.
    ///
    /// Args:
    ///   input: Input slice with length `input_dim()`.
    ///
    /// Returns:
    ///   Prediction-stage state `(nx, 1)`, covariance `(nx, nx)`, and output
    ///   `(ny, 1)`.
    pub fn predict(&self, input: &[T]) -> Result<ExtendedKalmanPrediction<T>, EmbeddedError> {
        validate_input_dim(&self.model, input)?;

        let nx = self.model.state_dim();
        let mut f = zero_matrix(nx, nx);
        self.model
            .transition_jacobian(vec_as_slice(&self.x_hat), input, &mut f);

        let mut state = zero_vector(nx);
        self.model.transition(
            vec_as_slice(&self.x_hat),
            input,
            vec_as_slice_mut(&mut state),
        );

        let covariance = predict_covariance(f.as_ref(), self.p.as_ref(), self.w.as_ref());
        let mut output = zero_vector(self.model.output_dim());
        self.model
            .output(vec_as_slice(&state), input, vec_as_slice_mut(&mut output));

        Ok(ExtendedKalmanPrediction {
            state,
            covariance,
            output,
        })
    }

    /// Computes the non-mutating measurement update.
    ///
    /// Args:
    ///   prediction: Prediction-stage result computed from the same sample.
    ///   input: Input slice with length `input_dim()` used by the output map.
    ///   measurement: Measurement slice with length `output_dim()` in the same
    ///     units as the model output.
    ///
    /// Returns:
    ///   Update-stage innovation `(ny, 1)`, gain `(nx, ny)`, posterior state
    ///   `(nx, 1)`, posterior covariance `(nx, nx)`, and posterior output
    ///   `(ny, 1)`.
    pub fn update(
        &self,
        prediction: &ExtendedKalmanPrediction<T>,
        input: &[T],
        measurement: &[T],
    ) -> Result<ExtendedKalmanUpdate<T>, EmbeddedError> {
        validate_input_dim(&self.model, input)?;
        validate_output_dim(&self.model, measurement)?;

        let ny = self.model.output_dim();
        let mut h = zero_matrix(ny, self.model.state_dim());
        self.model
            .output_jacobian(vec_as_slice(&prediction.state), input, &mut h);

        let innovation = vec_sub(&vector_from_slice(measurement), &prediction.output)?;
        let innovation_covariance =
            predict_covariance(h.as_ref(), prediction.covariance.as_ref(), self.v.as_ref());
        let h_t = h.transpose().to_owned();
        let cross_covariance = mat_mul(&prediction.covariance, &h_t)?;
        let cross_t = cross_covariance.transpose().to_owned();
        let gain = llt_solve(
            &innovation_covariance,
            &cross_t,
            "embedded.alloc.ekf.innovation_covariance",
        )?
        .transpose()
        .to_owned();
        let state = vec_add(&prediction.state, &mat_mul_vec(&gain, &innovation)?)?;
        let covariance = updated_covariance(
            CovarianceUpdate::Joseph,
            prediction.covariance.as_ref(),
            gain.as_ref(),
            h.as_ref(),
            self.v.as_ref(),
            innovation_covariance.as_ref(),
        );

        let mut output = zero_vector(ny);
        self.model
            .output(vec_as_slice(&state), input, vec_as_slice_mut(&mut output));

        Ok(ExtendedKalmanUpdate {
            innovation_norm: vec_norm(&innovation),
            normalized_innovation_norm: normalized_innovation_norm(
                vector_as_column_matrix(&innovation).as_ref(),
                innovation_covariance.as_ref(),
            )?,
            innovation,
            innovation_covariance,
            gain,
            state,
            covariance,
            output,
        })
    }

    /// Runs one full EKF step and stores the posterior estimate.
    ///
    /// Args:
    ///   input: Input slice with length `input_dim()`.
    ///   measurement: Measurement slice with length `output_dim()` in output
    ///     units.
    ///
    /// Returns:
    ///   The full update-stage result for the sample.
    pub fn step(
        &mut self,
        input: &[T],
        measurement: &[T],
    ) -> Result<ExtendedKalmanUpdate<T>, EmbeddedError> {
        validate_input_dim(&self.model, input)?;
        validate_output_dim(&self.model, measurement)?;

        let nx = self.model.state_dim();
        let ny = self.model.output_dim();
        self.scratch.ensure_dims(nx, ny);

        self.model
            .transition_jacobian(vec_as_slice(&self.x_hat), input, &mut self.scratch.f);
        self.model.transition(
            vec_as_slice(&self.x_hat),
            input,
            vec_as_slice_mut(&mut self.scratch.state),
        );

        let prediction_covariance =
            predict_covariance(self.scratch.f.as_ref(), self.p.as_ref(), self.w.as_ref());

        self.model.output(
            vec_as_slice(&self.scratch.state),
            input,
            vec_as_slice_mut(&mut self.scratch.output),
        );
        let innovation = vec_sub(&vector_from_slice(measurement), &self.scratch.output)?;

        self.model.output_jacobian(
            vec_as_slice(&self.scratch.state),
            input,
            &mut self.scratch.h,
        );
        let innovation_covariance = predict_covariance(
            self.scratch.h.as_ref(),
            prediction_covariance.as_ref(),
            self.v.as_ref(),
        );
        let h_t = self.scratch.h.transpose().to_owned();
        let cross_covariance = mat_mul(&prediction_covariance, &h_t)?;
        let cross_t = cross_covariance.transpose().to_owned();
        let gain = llt_solve(
            &innovation_covariance,
            &cross_t,
            "embedded.alloc.ekf.innovation_covariance",
        )?
        .transpose()
        .to_owned();
        let state = vec_add(&self.scratch.state, &mat_mul_vec(&gain, &innovation)?)?;
        let covariance = updated_covariance(
            CovarianceUpdate::Joseph,
            prediction_covariance.as_ref(),
            gain.as_ref(),
            self.scratch.h.as_ref(),
            self.v.as_ref(),
            innovation_covariance.as_ref(),
        );

        self.model.output(
            vec_as_slice(&state),
            input,
            vec_as_slice_mut(&mut self.scratch.output),
        );
        let output = self.scratch.output.clone();

        self.x_hat = state.clone();
        self.p = covariance.clone();

        Ok(ExtendedKalmanUpdate {
            innovation_norm: vec_norm(&innovation),
            normalized_innovation_norm: normalized_innovation_norm(
                vector_as_column_matrix(&innovation).as_ref(),
                innovation_covariance.as_ref(),
            )?,
            innovation,
            innovation_covariance,
            gain,
            state,
            covariance,
            output,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::super::dense::{identity_matrix, scale_matrix, vector_from_slice};
    use super::*;

    #[derive(Clone, Debug, PartialEq)]
    struct QuadraticSensor;

    impl DiscreteNonlinearModel<f64> for QuadraticSensor {
        fn state_dim(&self) -> usize {
            1
        }

        fn input_dim(&self) -> usize {
            1
        }

        fn output_dim(&self) -> usize {
            1
        }

        fn transition(&self, state: &[f64], input: &[f64], next_state: &mut [f64]) {
            next_state[0] = state[0] + input[0];
        }

        fn output(&self, state: &[f64], _input: &[f64], output: &mut [f64]) {
            output[0] = state[0] * state[0];
        }
    }

    impl DiscreteExtendedKalmanModel<f64> for QuadraticSensor {
        fn transition_jacobian(&self, _state: &[f64], _input: &[f64], jacobian: &mut Matrix<f64>) {
            jacobian[(0, 0)] = 1.0;
        }

        fn output_jacobian(&self, state: &[f64], _input: &[f64], jacobian: &mut Matrix<f64>) {
            jacobian[(0, 0)] = 2.0 * state[0];
        }
    }

    #[test]
    fn alloc_ekf_step_runs() {
        let mut filter = ExtendedKalmanFilter::new(
            QuadraticSensor,
            scale_matrix(&identity_matrix(1), 1.0e-3),
            scale_matrix(&identity_matrix(1), 1.0e-2),
            vector_from_slice(&[0.5]),
            identity_matrix(1),
        )
        .unwrap();

        let update = filter.step(&[0.1], &[0.49]).unwrap();
        assert!(update.output[0].is_finite());
        assert!(filter.state_estimate()[0].is_finite());
    }
}