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
use std::ops::Add;
use libnum::{self, Zero, Float};
use itertools::free::enumerate;
use imp_prelude::*;
use numeric_util;
use {
LinalgScalar,
FoldWhile,
Zip,
};
impl<A, S, D> ArrayBase<S, D>
where S: Data<Elem=A>,
D: Dimension,
{
pub fn scalar_sum(&self) -> A
where A: Clone + Add<Output=A> + libnum::Zero,
{
if let Some(slc) = self.as_slice_memory_order() {
return numeric_util::unrolled_sum(slc);
}
let mut sum = A::zero();
for row in self.inner_rows() {
if let Some(slc) = row.as_slice() {
sum = sum + numeric_util::unrolled_sum(slc);
} else {
sum = sum + row.iter().fold(A::zero(), |acc, elt| acc + elt.clone());
}
}
sum
}
pub fn sum_axis(&self, axis: Axis) -> Array<A, D::Smaller>
where A: Clone + Zero + Add<Output=A>,
D: RemoveAxis,
{
let n = self.len_of(axis);
let mut res = self.subview(axis, 0).to_owned();
let stride = self.strides()[axis.index()];
if self.ndim() == 2 && stride == 1 {
let ax = axis.index();
for (i, elt) in enumerate(&mut res) {
*elt = self.subview(Axis(1 - ax), i).scalar_sum();
}
} else {
for i in 1..n {
let view = self.subview(axis, i);
res = res + &view;
}
}
res
}
pub fn mean_axis(&self, axis: Axis) -> Array<A, D::Smaller>
where A: LinalgScalar,
D: RemoveAxis,
{
let n = self.len_of(axis);
let sum = self.sum_axis(axis);
let mut cnt = A::one();
for _ in 1..n {
cnt = cnt + A::one();
}
sum / &aview0(&cnt)
}
pub fn var_axis(&self, axis: Axis, ddof: A) -> Array<A, D::Smaller>
where
A: Float,
D: RemoveAxis,
{
let mut count = A::zero();
let mut mean = Array::<A, _>::zeros(self.dim.remove_axis(axis));
let mut sum_sq = Array::<A, _>::zeros(self.dim.remove_axis(axis));
for subview in self.axis_iter(axis) {
count = count + A::one();
azip!(mut mean, mut sum_sq, x (subview) in {
let delta = x - *mean;
*mean = *mean + delta / count;
*sum_sq = (x - *mean).mul_add(delta, *sum_sq);
});
}
if ddof >= count {
panic!("`ddof` needs to be strictly smaller than the length \
of the axis you are computing the variance for!")
} else {
let dof = count - ddof;
sum_sq.mapv(|s| s / dof)
}
}
pub fn all_close<S2, E>(&self, rhs: &ArrayBase<S2, E>, tol: A) -> bool
where A: Float,
S2: Data<Elem=A>,
E: Dimension,
{
!Zip::from(self)
.and(rhs.broadcast_unwrap(self.raw_dim()))
.fold_while((), |_, x, y| {
if (*x - *y).abs() <= tol {
FoldWhile::Continue(())
} else {
FoldWhile::Done(())
}
}).is_done()
}
}