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
use crate::celestia::Frame;
use crate::dimensions::allocator::Allocator;
use crate::dimensions::{DefaultAllocator, DimName, MatrixMN, VectorN, U3, U6};
use crate::time::{Duration, Epoch};
use std::fmt;
#[allow(clippy::upper_case_acronyms)]
pub type SNC3 = SNC<U3>;
#[allow(clippy::upper_case_acronyms)]
pub type SNC6 = SNC<U6>;
#[derive(Clone)]
#[allow(clippy::upper_case_acronyms)]
pub struct SNC<A: DimName>
where
DefaultAllocator: Allocator<f64, A> + Allocator<f64, A, A>,
{
pub start_time: Option<Epoch>,
pub frame: Option<Frame>,
pub disable_time: Duration,
pub init_epoch: Option<Epoch>,
diag: VectorN<f64, A>,
decay_diag: Option<Vec<f64>>,
pub prev_epoch: Option<Epoch>,
}
impl<A> fmt::Debug for SNC<A>
where
A: DimName,
DefaultAllocator: Allocator<f64, A> + Allocator<f64, A, A>,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(decay) = &self.decay_diag {
let mut fmt_cov = Vec::with_capacity(A::dim());
for (i, dv) in decay.iter().enumerate() {
fmt_cov.push(format!("{:.1e} × exp(- {:.1e} × t)", self.diag[i], dv));
}
write!(
f,
"SNC: diag({}) {}",
fmt_cov.join(", "),
if let Some(start) = self.start_time {
format!("starting at {}", start.as_gregorian_utc_str())
} else {
"".to_string()
}
)
} else {
let mut fmt_cov = Vec::with_capacity(A::dim());
for i in 0..A::dim() {
fmt_cov.push(format!("{:.1e}", self.diag[i]));
}
write!(
f,
"SNC: diag({}) {}",
fmt_cov.join(", "),
if let Some(start) = self.start_time {
format!("starting at {}", start.as_gregorian_utc_str())
} else {
"".to_string()
}
)
}
}
}
impl<A> fmt::Display for SNC<A>
where
A: DimName,
DefaultAllocator: Allocator<f64, A> + Allocator<f64, A, A>,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
impl<A: DimName> SNC<A>
where
DefaultAllocator: Allocator<f64, A> + Allocator<f64, A, A>,
{
pub fn from_diagonal(disable_time: Duration, values: &[f64]) -> Self {
assert_eq!(
values.len(),
A::dim(),
"Not enough values for the size of the SNC matrix"
);
let mut diag = VectorN::zeros();
for (i, v) in values.iter().enumerate() {
diag[i] = *v;
}
Self {
diag,
disable_time,
start_time: None,
frame: None,
decay_diag: None,
init_epoch: None,
prev_epoch: None,
}
}
pub fn with_start_time(disable_time: Duration, values: &[f64], start_time: Epoch) -> Self {
let mut me = Self::from_diagonal(disable_time, values);
me.start_time = Some(start_time);
me
}
pub fn with_decay(
disable_time: Duration,
initial_snc: &[f64],
decay_constants_s: &[f64],
) -> Self {
assert_eq!(
decay_constants_s.len(),
A::dim(),
"Not enough decay constants for the size of the SNC matrix"
);
let mut me = Self::from_diagonal(disable_time, initial_snc);
me.decay_diag = Some(decay_constants_s.to_vec());
me
}
pub fn to_matrix(&self, epoch: Epoch) -> Option<MatrixMN<f64, A, A>> {
if let Some(start_time) = self.start_time {
if start_time > epoch {
return None;
}
}
if let Some(prev_epoch) = self.prev_epoch {
if epoch - prev_epoch > self.disable_time {
return None;
}
}
let mut snc = MatrixMN::<f64, A, A>::zeros();
for i in 0..self.diag.nrows() {
snc[(i, i)] = self.diag[i];
}
if let Some(decay) = &self.decay_diag {
let total_delta_t = (epoch - self.init_epoch.unwrap()).in_seconds();
for i in 0..self.diag.nrows() {
snc[(i, i)] *= (-decay[i] * total_delta_t).exp();
}
}
Some(snc)
}
}
#[test]
fn test_snc_init() {
use crate::time::TimeUnit;
let snc_expo = SNC3::with_decay(
2 * TimeUnit::Minute,
&[1e-6, 1e-6, 1e-6],
&[3600.0, 3600.0, 3600.0],
);
println!("{}", snc_expo);
let snc_std = SNC3::with_start_time(
2 * TimeUnit::Minute,
&[1e-6, 1e-6, 1e-6],
Epoch::from_et_seconds(3600.0),
);
println!("{}", snc_std);
}