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
#![allow(non_snake_case)]
use nalgebra::{allocator::Allocator, Const, DefaultAllocator, Dim, OMatrix, OVector, RealField};
use crate::linalg::rcond;
use crate::matrix::check_positive;
use crate::models::{Estimator, ExtendedLinearObserver, ExtendedLinearPredictor, InformationState, KalmanEstimator, KalmanState};
use crate::noise::{CorrelatedNoise, CoupledNoise};
impl<N: RealField, D: Dim> InformationState<N, D>
where
DefaultAllocator: Allocator<N, D, D> + Allocator<N, D>,
{
pub fn new_zero(d: D) -> InformationState<N, D> {
InformationState {
i: OVector::zeros_generic(d, Const::<1>),
I: OMatrix::zeros_generic(d, d),
}
}
}
impl<N: RealField, D: Dim> Estimator<N, D> for InformationState<N, D>
where
DefaultAllocator: Allocator<N, D, D> + Allocator<N, D>,
{
fn state(&self) -> Result<OVector<N, D>, &'static str> {
KalmanEstimator::kalman_state(self).map(|r| r.x)
}
}
impl<N: RealField, D: Dim> KalmanEstimator<N, D> for InformationState<N, D>
where
DefaultAllocator: Allocator<N, D, D> + Allocator<N, D>,
{
fn init(&mut self, state: &KalmanState<N, D>) -> Result<(), &'static str> {
self.I = state.X.clone().cholesky().ok_or("X not PD")?.inverse();
self.i = &self.I * &state.x;
Ok(())
}
fn kalman_state(&self) -> Result<KalmanState<N, D>, &'static str> {
let X = self.I.clone().cholesky().ok_or("Y not PD")?.inverse();
let x = &X * &self.i;
Ok(KalmanState { x, X })
}
}
impl<N: RealField, D: Dim> ExtendedLinearPredictor<N, D> for InformationState<N, D>
where
DefaultAllocator: Allocator<N, D, D> + Allocator<N, D>
{
fn predict(
&mut self,
x_pred: &OVector<N, D>,
fx: &OMatrix<N, D, D>,
noise: &CorrelatedNoise<N, D>,
) -> Result<(), &'static str> {
let mut X = self.I.clone().cholesky().ok_or("I not PD in predict")?.inverse();
X.quadform_tr(N::one(), &fx, &X.clone(), N::zero());
X += &noise.Q;
self.init(&KalmanState { x: x_pred.clone_owned(), X })?;
return Ok(())
}
}
impl<N: Copy + RealField, D: Dim, ZD: Dim> ExtendedLinearObserver<N, D, ZD> for InformationState<N, D>
where
DefaultAllocator: Allocator<N, D, D> + Allocator<N, D, ZD> + Allocator<N, ZD, D> + Allocator<N, ZD, ZD> + Allocator<N, D> + Allocator<N, ZD>,
{
fn observe_innovation(
&mut self,
s: &OVector<N, ZD>,
hx: &OMatrix<N, ZD, D>,
noise: &CorrelatedNoise<N, ZD>,
) -> Result<(), &'static str>
{
let x = self.state().unwrap();
let noise_inv = noise.Q.clone().cholesky().ok_or("Q not PD in observe")?.inverse();
let info = self.observe_info(hx, &noise_inv, &(s + hx * x));
self.add_information(&info);
return Ok(())
}
}
impl<N: Copy + RealField, D: Dim> InformationState<N, D>
where
DefaultAllocator: Allocator<N, D, D> + Allocator<N, D>,
{
pub fn predict_linear<QD: Dim>(
&mut self,
pred_inv: OMatrix<N, D, D>,
noise: &CoupledNoise<N, D, QD>,
) -> Result<N, &'static str>
where
DefaultAllocator: Allocator<N, QD, QD> + Allocator<N, D, QD> + Allocator<N, QD, D> + Allocator<N, QD>,
{
let I_shape = self.I.shape_generic();
let A = (&self.I * &pred_inv).tr_mul(&pred_inv);
let mut B = (&A * &noise.G).tr_mul(&noise.G);
for i in 0..noise.q.nrows() {
B[(i, i)] += N::one() / noise.q[i];
}
B = B.cholesky().ok_or("B not PD")?.inverse();
let rcond = rcond::rcond_symetric(&B);
check_positive(rcond, "(G'invFx'.I.inv(Fx).G + inv(Q)) not PD")?;
self.I.quadform_tr(N::one(), &noise.G, &B, N::zero());
let ig = OMatrix::identity_generic(I_shape.0, I_shape.1) - &A * &self.I;
self.I = &ig * &A;
let y = pred_inv.tr_mul(&self.i);
self.i = &ig * &y;
Ok(rcond)
}
pub fn add_information(&mut self, information: &InformationState<N, D>) {
self.i += &information.i;
self.I += &information.I;
}
pub fn observe_info<ZD: Dim>(
&self,
hx: &OMatrix<N, ZD, D>,
noise_inv: &OMatrix<N, ZD, ZD>,
z: &OVector<N, ZD>
) -> InformationState<N, D>
where
DefaultAllocator: Allocator<N, ZD, ZD> + Allocator<N, ZD, D> + Allocator<N, D, ZD> + Allocator<N, ZD>
{
let HxTZI = hx.tr_mul(noise_inv);
let ii = &HxTZI * z;
let II = &HxTZI * hx;
InformationState { i: ii, I: II }
}
}