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
#![allow(non_snake_case)]
use nalgebra::{OMatrix, OVector, QR, RealField};
use nalgebra::{allocator::Allocator, DefaultAllocator, Const, U1, Dim, DimMin, DimMinimum, DimSum, DimAdd};
use crate::linalg::cholesky;
use crate::linalg::cholesky::UDU;
use crate::matrix::check_positive;
use crate::models::{Estimator, ExtendedLinearObserver, InformationState, KalmanEstimator, KalmanState};
use crate::noise::{CorrelatedNoise, CoupledNoise};
#[derive(PartialEq, Clone)]
pub struct InformationRootState<N: RealField, D: Dim>
where
DefaultAllocator: Allocator<N, D, D> + Allocator<N, D>,
{
pub r: OVector<N, D>,
pub R: OMatrix<N, D, D>,
}
impl<N: Copy + RealField, D: Dim> InformationRootState<N, D>
where
DefaultAllocator: Allocator<N, D, D> + Allocator<N, D>,
{
pub fn new_zero(d: D) -> InformationRootState<N, D> {
InformationRootState {
r: OVector::zeros_generic(d, Const::<1>),
R: OMatrix::zeros_generic(d, d),
}
}
pub fn init_information(&mut self, state: &InformationState<N, D>) -> Result<N, &'static str> {
self.R = state.I.clone().cholesky().ok_or("I not PD")?.l().transpose();
let shape = self.R.shape_generic();
let mut RI = OMatrix::identity_generic(shape.0, shape.1);
self.R.solve_upper_triangular_mut(&mut RI);
self.r = RI.tr_mul(&state.i);
Result::Ok(cholesky::UDU::UdUrcond(&state.I))
}
pub fn information_state(&self) -> Result<InformationState<N, D>, &'static str> {
let I = self.R.tr_mul(&self.R);
let x = self.state()?;
let i = &I * x;
Result::Ok(InformationState { i, I })
}
}
impl<N: Copy + RealField, D: Dim> Estimator<N, D> for InformationRootState<N, D>
where
DefaultAllocator: Allocator<N, D, D> + Allocator<N, D>,
{
fn state(&self) -> Result<OVector<N, D>, &'static str> {
self.kalman_state().map(|res| res.x)
}
}
impl<N: Copy + RealField, D: Dim> KalmanEstimator<N, D> for InformationRootState<N, D>
where
DefaultAllocator: Allocator<N, D, D> + Allocator<N, D>,
{
fn init(&mut self, state: &KalmanState<N, D>) -> Result<(), &'static str> {
let udu = UDU::new();
self.R.copy_from(&state.X);
let rcond = udu.UCfactor_n(&mut self.R, state.X.nrows());
check_positive(rcond, "X not PD")?;
let singular = udu.UTinverse(&mut self.R);
assert!(!singular, "singular R");
self.r = &self.R * &state.x;
Result::Ok(())
}
fn kalman_state(&self) -> Result<KalmanState<N, D>, &'static str> {
let shape = self.R.shape_generic();
let mut RI = OMatrix::identity_generic(shape.0, shape.1);
self.R.solve_upper_triangular_mut(&mut RI);
let X = &RI * &RI.transpose();
let x = RI * &self.r;
Result::Ok(KalmanState { x, X })
}
}
impl<N: Copy + RealField, D: Dim, ZD: Dim> ExtendedLinearObserver<N, D, ZD> for InformationRootState<N, D>
where
DefaultAllocator: Allocator<N, D, D> + Allocator<N, ZD, D> + Allocator<N, ZD, ZD> + Allocator<N, D> + Allocator<N, ZD>,
D: DimAdd<ZD> + DimAdd<U1>,
DefaultAllocator: Allocator<N, DimSum<D, ZD>, DimSum<D, U1>> + Allocator<N, DimSum<D, ZD>>,
DimSum<D, ZD>: DimMin<DimSum<D, U1>>,
DefaultAllocator: Allocator<N, DimMinimum<DimSum<D, ZD>, DimSum<D, U1>>> + Allocator<N, DimMinimum<DimSum<D, ZD>, DimSum<D, U1>>, DimSum<D, U1>>
{
fn observe_innovation(
&mut self,
s: &OVector<N, ZD>,
hx: &OMatrix<N, ZD, D>,
noise: &CorrelatedNoise<N, ZD>,
) -> Result<(), &'static str>
{
let udu = UDU::new();
let mut QI = noise.Q.clone();
let rcond = udu.UCfactor_n(&mut QI, s.nrows());
check_positive(rcond, "Q not PD")?;
let singular = udu.UTinverse(&mut QI);
assert!(!singular, "singular QI");
let x = self.state()?;
self.observe_info(&(s + hx * x), hx, &QI)
}
}
impl<N: Copy + RealField, D: Dim> InformationRootState<N, D>
where
DefaultAllocator: Allocator<N, D, D> + Allocator<N, D>
{
pub fn predict<QD: Dim>(
&mut self,
x_pred: &OVector<N, D>,
fx: &OMatrix<N, D, D>,
noise: &CoupledNoise<N, D, QD>,
) -> Result<(), &'static str>
where
D: DimAdd<QD>,
DefaultAllocator: Allocator<N, DimSum<D, QD>, DimSum<D, QD>> + Allocator<N, DimSum<D, QD>> + Allocator<N, D, QD> + Allocator<N, QD>,
DimSum<D, QD>: DimMin<DimSum<D, QD>>,
DefaultAllocator: Allocator<N, DimMinimum<DimSum<D, QD>, DimSum<D, QD>>> + Allocator<N, DimMinimum<DimSum<D, QD>, DimSum<D, QD>>, DimSum<D, QD>>,
{
let mut Fx_inv = fx.clone();
let invertable = Fx_inv.try_inverse_mut();
if !invertable {
return Err("Fx not invertable")?;
}
self.predict_inv_model(x_pred, &Fx_inv, noise).map(|_rcond| {})
}
pub fn predict_inv_model<QD: Dim>(
&mut self,
x_pred: &OVector<N, D>,
fx_inv: &OMatrix<N, D, D>,
noise: &CoupledNoise<N, D, QD>,
) -> Result<N, &'static str>
where
D: DimAdd<QD>,
DefaultAllocator: Allocator<N, DimSum<D, QD>, DimSum<D, QD>> + Allocator<N, DimSum<D, QD>> + Allocator<N, D, QD> + Allocator<N, QD>,
DimSum<D, QD>: DimMin<DimSum<D, QD>>,
DefaultAllocator: Allocator<N, DimMinimum<DimSum<D, QD>, DimSum<D, QD>>> + Allocator<N, DimMinimum<DimSum<D, QD>, DimSum<D, QD>>, DimSum<D, QD>>,
{
let mut Gqr = noise.G.clone();
for qi in 0..noise.q.nrows() {
let mut ZZ = Gqr.column_mut(qi);
ZZ *= noise.q[qi].sqrt();
}
let dqd = noise.G.shape_generic().0.add(noise.q.shape_generic().0);
let mut A = OMatrix::identity_generic(dqd, dqd);
let RFxI: OMatrix<N, D, D> = &self.R * fx_inv;
let x: OMatrix<N, D, QD> = &RFxI * &Gqr;
let x_size = x_pred.shape_generic().0;
let q_size = noise.q.shape_generic().0;
A.generic_slice_mut((q_size.value(), 0), (x_size, q_size)).copy_from(&x);
A.generic_slice_mut((q_size.value(), q_size.value()), (x_size, x_size)).copy_from(&RFxI);
A.generic_slice_mut((q_size.value(), 0), (x_size, q_size)).copy_from(&x);
A.generic_slice_mut((q_size.value(), q_size.value()), (x_size, x_size)).copy_from(&RFxI);
let qr = QR::new(A);
let r = qr.r();
self.R.copy_from(&r.generic_slice((q_size.value(), q_size.value()), (x_size, x_size)));
self.r = &self.R * x_pred;
return Result::Ok(UDU::new().UCrcond(&self.R));
}
pub fn observe_info<ZD: Dim>(
&mut self,
z: &OVector<N, ZD>,
hx: &OMatrix<N, ZD, D>,
noise_inv: &OMatrix<N, ZD, ZD>,
) -> Result<(), &'static str>
where
DefaultAllocator: Allocator<N, D, D> + Allocator<N, ZD, D> + Allocator<N, ZD, ZD> + Allocator<N, D> + Allocator<N, ZD>,
D: DimAdd<ZD> + DimAdd<U1>,
DefaultAllocator: Allocator<N, DimSum<D, ZD>, DimSum<D, U1>> + Allocator<N, DimSum<D, ZD>>,
DimSum<D, ZD>: DimMin<DimSum<D, U1>>,
DefaultAllocator: Allocator<N, DimMinimum<DimSum<D, ZD>, DimSum<D, U1>>> + Allocator<N, DimMinimum<DimSum<D, ZD>, DimSum<D, U1>>, DimSum<D, U1>>
{
let x_size = self.r.shape_generic().0;
let z_size = z.shape_generic().0;
if z_size != hx.shape_generic().0 {
return Result::Err("observation and model size inconsistent");
}
let xd = self.r.shape_generic().0;
let mut A = OMatrix::identity_generic(xd.add(z.shape_generic().0), xd.add(Const::<1>));
A.generic_slice_mut((0, 0), (x_size, x_size)).copy_from(&self.R);
A.generic_slice_mut((0, x_size.value()), (x_size, Const::<1>)).copy_from(&self.r);
A.generic_slice_mut((x_size.value(), 0), (z_size, x_size)).copy_from(&(noise_inv * hx));
A.generic_slice_mut((x_size.value(), x_size.value()), (z_size, Const::<1>)).copy_from(&(noise_inv * z));
let qr = QR::new(A);
let r = qr.r();
self.R.copy_from(&r.generic_slice((0, 0), (x_size, x_size)));
self.r.copy_from(&r.generic_slice((0, x_size.value()), (x_size, Const::<1>)));
Result::Ok(())
}
}