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
use crate::{
implementation::{CentralMoments, SealedSupportedOrder, Storage},
Index, Moments, Order, Scalar, Spatial, SupportedIndex, SupportedOrder,
};
#[derive(Debug, Clone, PartialEq)]
pub struct Central<T: Scalar, const ORDER: usize>(
pub(crate) <Order<ORDER> as SealedSupportedOrder<T>>::Storage,
)
where
Order<ORDER>: SupportedOrder<T>;
impl<'a, T: Scalar, const ORDER: usize> From<&'a Spatial<T, ORDER>> for Central<T, ORDER>
where
Order<ORDER>: SupportedOrder<T>,
{
fn from(raw_moments: &'a Spatial<T, ORDER>) -> Self {
let mut central_moments = <Order<ORDER> as SealedSupportedOrder<T>>::Storage::zeros();
Order::<ORDER>::calculate_central_moments(&raw_moments.0, &mut central_moments);
Self(central_moments)
}
}
impl<T: Scalar, const ORDER: usize> Moments<T, ORDER> for Central<T, ORDER>
where
Order<ORDER>: SupportedOrder<T>,
{
#[inline(always)]
fn get<const I: usize, const J: usize>(&self) -> T
where
Index<I, J>: SupportedIndex<ORDER>,
{
self.0.get::<I, J>()
}
}
#[cfg(test)]
mod tests {
use approx::assert_abs_diff_eq;
use crate::{central::Central, Moments, Spatial};
#[test]
fn test_central_moments() {
let points = [
(53, 19),
(52, 20),
(49, 20),
(48, 21),
(47, 21),
(46, 22),
(45, 22),
(44, 23),
(43, 23),
(42, 24),
(41, 24),
(39, 26),
(38, 26),
(34, 30),
(76, 30),
(77, 31),
(79, 31),
(80, 32),
(81, 32),
(83, 34),
(84, 34),
(86, 36),
(86, 37),
(87, 38),
(87, 39),
(88, 40),
(88, 47),
(86, 49),
(86, 50),
(83, 53),
(82, 53),
(81, 54),
(81, 55),
(82, 55),
(84, 57),
(84, 58),
(85, 59),
(85, 60),
(86, 61),
(86, 63),
(87, 64),
(87, 65),
(88, 66),
(93, 66),
(94, 65),
(94, 64),
(95, 63),
(95, 60),
(96, 59),
(99, 59),
(99, 53),
(98, 52),
(97, 52),
(96, 51),
(95, 51),
(94, 50),
(93, 50),
(90, 47),
(90, 46),
(91, 45),
(91, 44),
(92, 43),
(92, 42),
(93, 41),
(93, 39),
(94, 38),
(94, 36),
(91, 33),
(91, 32),
(85, 26),
(84, 26),
(82, 24),
(81, 24),
(80, 23),
(79, 23),
(78, 22),
(77, 22),
(76, 21),
(75, 21),
(74, 20),
(71, 20),
(70, 19),
(69, 19),
(67, 21),
(66, 21),
(62, 25),
(60, 25),
(54, 19),
];
let central_moments = {
let moments: Spatial<f64, 3> = points.iter().collect();
Central::from(&moments)
};
assert_abs_diff_eq!(central_moments.get::<0, 0>(), 703.0);
assert_abs_diff_eq!(central_moments.get::<1, 0>(), 0.0);
assert_abs_diff_eq!(central_moments.get::<0, 1>(), 0.0);
assert_abs_diff_eq!(central_moments.get::<2, 0>(), 212120.628694484);
assert_abs_diff_eq!(central_moments.get::<1, 1>(), 119935.73091512569);
assert_abs_diff_eq!(central_moments.get::<0, 2>(), 134538.24431009952);
assert_abs_diff_eq!(central_moments.get::<3, 0>(), -2035756.4570507407);
assert_abs_diff_eq!(
central_moments.get::<2, 1>(),
-158011.91380318906,
epsilon = 10e-7
);
assert_abs_diff_eq!(
central_moments.get::<1, 2>(),
862112.1277520265,
epsilon = 10e-7
);
assert_abs_diff_eq!(
central_moments.get::<0, 3>(),
1343240.7361632437,
epsilon = 10e-7
);
}
}