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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
use std::cmp;
use libnum::Zero;
use std::ops::{Add, Mul, Sub, Div};
pub fn dot<T: Copy + Zero + Add<T, Output = T> + Mul<T, Output = T>>(u: &[T], v: &[T]) -> T {
let len = cmp::min(u.len(), v.len());
let mut xs = &u[..len];
let mut ys = &v[..len];
let mut s = T::zero();
let (mut p0, mut p1, mut p2, mut p3, mut p4, mut p5, mut p6, mut p7) =
(T::zero(), T::zero(), T::zero(), T::zero(), T::zero(), T::zero(), T::zero(), T::zero());
while xs.len() >= 8 {
p0 = p0 + xs[0] * ys[0];
p1 = p1 + xs[1] * ys[1];
p2 = p2 + xs[2] * ys[2];
p3 = p3 + xs[3] * ys[3];
p4 = p4 + xs[4] * ys[4];
p5 = p5 + xs[5] * ys[5];
p6 = p6 + xs[6] * ys[6];
p7 = p7 + xs[7] * ys[7];
xs = &xs[8..];
ys = &ys[8..];
}
s = s + p0 + p4;
s = s + p1 + p5;
s = s + p2 + p6;
s = s + p3 + p7;
for i in 0..xs.len() {
s = s + xs[i] * ys[i];
}
s
}
pub fn unrolled_sum<T>(mut xs: &[T]) -> T
where T: Clone + Add<Output = T> + Zero
{
let mut sum = T::zero();
let (mut p0, mut p1, mut p2, mut p3, mut p4, mut p5, mut p6, mut p7) =
(T::zero(), T::zero(), T::zero(), T::zero(), T::zero(), T::zero(), T::zero(), T::zero());
while xs.len() >= 8 {
p0 = p0 + xs[0].clone();
p1 = p1 + xs[1].clone();
p2 = p2 + xs[2].clone();
p3 = p3 + xs[3].clone();
p4 = p4 + xs[4].clone();
p5 = p5 + xs[5].clone();
p6 = p6 + xs[6].clone();
p7 = p7 + xs[7].clone();
xs = &xs[8..];
}
sum = sum.clone() + (p0 + p4);
sum = sum.clone() + (p1 + p5);
sum = sum.clone() + (p2 + p6);
sum = sum.clone() + (p3 + p7);
for elt in xs {
sum = sum.clone() + elt.clone();
}
sum
}
pub fn in_place_vec_bin_op<F, T>(mut u: &mut [T], v: &[T], mut f: F)
where F: FnMut(&mut T, &T),
T: Copy
{
debug_assert_eq!(u.len(), v.len());
let len = cmp::min(u.len(), v.len());
let ys = &v[..len];
let xs = &mut u[..len];
for i in 0..len {
f(&mut xs[i], &ys[i])
}
}
pub fn vec_bin_op<F, T>(u: &[T], v: &[T], f: F) -> Vec<T>
where F: Fn(T, T) -> T,
T: Copy
{
debug_assert_eq!(u.len(), v.len());
let len = cmp::min(u.len(), v.len());
let xs = &u[..len];
let ys = &v[..len];
let mut out_vec = Vec::with_capacity(len);
unsafe {
out_vec.set_len(len);
}
{
let out_slice = &mut out_vec[..len];
for i in 0..len {
out_slice[i] = f(xs[i], ys[i]);
}
}
out_vec
}
pub fn vec_sum<T: Copy + Add<T, Output = T>>(u: &[T], v: &[T]) -> Vec<T> {
vec_bin_op(u, v, |x, y| x + y)
}
pub fn vec_sub<T: Copy + Sub<T, Output = T>>(u: &[T], v: &[T]) -> Vec<T> {
vec_bin_op(u, v, |x, y| x - y)
}
pub fn ele_mul<T: Copy + Mul<T, Output = T>>(u: &[T], v: &[T]) -> Vec<T> {
vec_bin_op(u, v, |x, y| x * y)
}
pub fn ele_div<T: Copy + Div<T, Output = T>>(u: &[T], v: &[T]) -> Vec<T> {
vec_bin_op(u, v, |x, y| x / y)
}
pub fn argmax<T>(u: &[T]) -> (usize, T)
where T: Copy + PartialOrd
{
assert!(u.len() != 0);
let mut max_index = 0;
let mut max = u[max_index];
for (i, v) in u.iter().enumerate().skip(1) {
if max < *v {
max_index = i;
max = *v;
}
}
(max_index, max)
}
pub fn argmin<T>(u: &[T]) -> (usize, T)
where T: Copy + PartialOrd
{
assert!(u.len() != 0);
let mut min_index = 0;
let mut min = u[min_index];
for (i, v) in u.iter().enumerate().skip(1) {
if min > *v {
min_index = i;
min = *v;
}
}
(min_index, min)
}
pub fn find<T>(p: &[T], u: T) -> usize
where T: PartialOrd
{
for (i, v) in p.iter().enumerate() {
if *v == u {
return i;
}
}
panic!("Value not found.")
}