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
extern crate dual_num;
extern crate hifitime;
extern crate nalgebra as na;
extern crate serde;
use self::dual_num::Dual;
use self::hifitime::instant::Instant;
use self::na::allocator::Allocator;
use self::na::{DefaultAllocator, DimName, MatrixMN, VectorN};
use celestia::{CoordinateFrame, State};
use dynamics::Dynamics;
pub mod kalman;
pub mod ranging;
pub trait Linearization
where
Self: Sized,
{
type StateSize: DimName;
fn gradient(&self, t: f64, state: &VectorN<f64, Self::StateSize>) -> MatrixMN<f64, Self::StateSize, Self::StateSize>
where
DefaultAllocator: Allocator<f64, Self::StateSize> + Allocator<f64, Self::StateSize, Self::StateSize>;
}
pub trait Measurement
where
Self: Sized,
DefaultAllocator: Allocator<f64, Self::MeasurementSize> + Allocator<f64, Self::MeasurementSize, Self::StateSize>,
{
type StateSize: DimName;
type MeasurementSize: DimName;
fn new<F: CoordinateFrame>(dt: Instant, tx: State<F>, rx: State<F>, visible: bool) -> Self;
fn observation(&self) -> &VectorN<f64, Self::MeasurementSize>
where
DefaultAllocator: Allocator<f64, Self::MeasurementSize>;
fn sensitivity(&self) -> &MatrixMN<f64, Self::MeasurementSize, Self::StateSize>
where
DefaultAllocator: Allocator<f64, Self::StateSize, Self::MeasurementSize>;
fn visible(&self) -> bool;
fn at(&self) -> Instant;
}
pub trait AutoDiffDynamics: Dynamics
where
Self: Sized,
{
type HyperStateSize: DimName;
fn dual_eom(
&self,
t: f64,
state: &MatrixMN<Dual<f64>, Self::HyperStateSize, Self::HyperStateSize>,
) -> MatrixMN<Dual<f64>, Self::HyperStateSize, Self::HyperStateSize>
where
DefaultAllocator: Allocator<Dual<f64>, Self::HyperStateSize>
+ Allocator<Dual<f64>, Self::HyperStateSize, Self::HyperStateSize>
+ Allocator<f64, Self::HyperStateSize>
+ Allocator<f64, Self::HyperStateSize, Self::HyperStateSize>;
fn compute(
&self,
t: f64,
state: &VectorN<f64, Self::HyperStateSize>,
) -> (
VectorN<f64, Self::HyperStateSize>,
MatrixMN<f64, Self::HyperStateSize, Self::HyperStateSize>,
)
where
DefaultAllocator: Allocator<Dual<f64>, Self::HyperStateSize>
+ Allocator<Dual<f64>, Self::HyperStateSize, Self::HyperStateSize>
+ Allocator<f64, Self::HyperStateSize>
+ Allocator<f64, Self::HyperStateSize, Self::HyperStateSize>,
{
let mut hyperdual_space = MatrixMN::<Dual<f64>, Self::HyperStateSize, Self::HyperStateSize>::zeros();
for i in 0..Self::HyperStateSize::dim() {
let mut v_i = VectorN::<Dual<f64>, Self::HyperStateSize>::zeros();
for j in 0..Self::HyperStateSize::dim() {
v_i[(j, 0)] = Dual::new(state[(j, 0)], if i == j { 1.0 } else { 0.0 });
}
hyperdual_space.set_column(i, &v_i);
}
let state_n_grad = self.dual_eom(t, &hyperdual_space);
let mut state = VectorN::<f64, Self::HyperStateSize>::zeros();
let mut grad = MatrixMN::<f64, Self::HyperStateSize, Self::HyperStateSize>::zeros();
for i in 0..Self::HyperStateSize::dim() {
for j in 0..Self::HyperStateSize::dim() {
if j == 0 {
state[(i, 0)] = state_n_grad[(i, 0)].real();
}
grad[(i, j)] = state_n_grad[(i, j)].dual();
}
}
(state, grad)
}
}
impl<T: AutoDiffDynamics> Linearization for T
where
DefaultAllocator: Allocator<Dual<f64>, T::StateSize> + Allocator<Dual<f64>, T::StateSize, T::StateSize>,
{
type StateSize = T::HyperStateSize;
fn gradient(&self, _t: f64, _state: &VectorN<f64, Self::StateSize>) -> MatrixMN<f64, Self::StateSize, Self::StateSize>
where
DefaultAllocator: Allocator<f64, Self::StateSize> + Allocator<f64, Self::StateSize, Self::StateSize>,
{
panic!("retrieve the gradient by calling self.compute(...)");
}
}