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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
extern crate libc;
use array::Array;
use defines::{AfError, TopkFn};
use error::HANDLE_ERROR;
use self::libc::{c_int, c_uint};
use util::{AfArray, DimT, MutAfArray, MutDouble};
use util::{RealNumber, CovarianceComputable};
use util::{HasAfEnum, MedianComputable, RealFloating};
#[allow(dead_code)]
extern {
fn af_mean(out: MutAfArray, arr: AfArray, dim: DimT) -> c_int;
fn af_stdev(out: MutAfArray, arr: AfArray, dim: DimT) -> c_int;
fn af_median(out: MutAfArray, arr: AfArray, dim: DimT) -> c_int;
fn af_mean_weighted(out: MutAfArray, arr: AfArray, wts: AfArray, dim: DimT) -> c_int;
fn af_var_weighted(out: MutAfArray, arr: AfArray, wts: AfArray, dim: DimT) -> c_int;
fn af_var(out: MutAfArray, arr: AfArray, isbiased: c_int, dim: DimT) -> c_int;
fn af_cov(out: MutAfArray, X: AfArray, Y: AfArray, isbiased: c_int) -> c_int;
fn af_var_all(real: MutDouble, imag: MutDouble, arr: AfArray, isbiased: c_int) -> c_int;
fn af_mean_all(real: MutDouble, imag: MutDouble, arr: AfArray) -> c_int;
fn af_stdev_all(real: MutDouble, imag: MutDouble, arr: AfArray) -> c_int;
fn af_median_all(real: MutDouble, imag: MutDouble, arr: AfArray) -> c_int;
fn af_mean_all_weighted(real: MutDouble, imag: MutDouble, arr: AfArray, wts: AfArray) -> c_int;
fn af_var_all_weighted(real: MutDouble, imag: MutDouble, arr: AfArray, wts: AfArray) -> c_int;
fn af_corrcoef(real: MutDouble, imag: MutDouble, X: AfArray, Y: AfArray) -> c_int;
fn af_topk(vals: MutAfArray, idxs: MutAfArray, arr: AfArray, k: c_int,
dim: c_int, order: c_uint) -> c_int;
}
#[allow(unused_mut)]
pub fn median<T>(input: &Array<T>, dim: i64) -> Array<T>
where T: HasAfEnum + MedianComputable
{
let mut temp: i64 = 0;
unsafe {
let err_val = af_median(&mut temp as MutAfArray,
input.get() as AfArray, dim as DimT);
HANDLE_ERROR(AfError::from(err_val));
}
temp.into()
}
macro_rules! stat_func_def {
($doc_str: expr, $fn_name: ident, $ffi_fn: ident) => (
#[doc=$doc_str]
#[allow(unused_mut)]
pub fn $fn_name<T>(input: &Array<T>, dim: i64) -> Array< T::MeanOutType >
where T: HasAfEnum, T::MeanOutType: HasAfEnum
{
let mut temp: i64 = 0;
unsafe {
let err_val = $ffi_fn(&mut temp as MutAfArray,
input.get() as AfArray, dim as DimT);
HANDLE_ERROR(AfError::from(err_val));
}
temp.into()
}
)
}
stat_func_def!("Mean along specified dimension", mean, af_mean);
stat_func_def!("Standard deviation along specified dimension", stdev, af_stdev);
macro_rules! stat_wtd_func_def {
($doc_str: expr, $fn_name: ident, $ffi_fn: ident) => (
#[doc=$doc_str]
#[allow(unused_mut)]
pub fn $fn_name<T, W>(input: &Array<T>,
weights: &Array<W>,
dim: i64) -> Array< T::MeanOutType >
where T: HasAfEnum,
T::MeanOutType: HasAfEnum,
W: HasAfEnum + RealFloating
{
let mut temp: i64 = 0;
unsafe {
let err_val = $ffi_fn(&mut temp as MutAfArray, input.get() as AfArray,
weights.get() as AfArray, dim as DimT);
HANDLE_ERROR(AfError::from(err_val));
}
temp.into()
}
)
}
stat_wtd_func_def!("Weighted mean along specified dimension", mean_weighted, af_mean_weighted);
stat_wtd_func_def!("Weight variance along specified dimension", var_weighted, af_var_weighted);
#[allow(unused_mut)]
pub fn var<T>(arr: &Array<T>, isbiased: bool, dim: i64) -> Array< T::MeanOutType >
where T: HasAfEnum, T::MeanOutType: HasAfEnum
{
let mut temp: i64 = 0;
unsafe {
let err_val = af_var(&mut temp as MutAfArray, arr.get() as AfArray,
isbiased as c_int, dim as DimT);
HANDLE_ERROR(AfError::from(err_val));
}
temp.into()
}
#[allow(unused_mut)]
pub fn cov<T>(x: &Array<T>, y: &Array<T>, isbiased: bool) -> Array< T::MeanOutType >
where T: HasAfEnum + CovarianceComputable,
T::MeanOutType: HasAfEnum
{
let mut temp: i64 = 0;
unsafe {
let err_val = af_cov(&mut temp as MutAfArray, x.get() as AfArray,
y.get() as AfArray, isbiased as c_int);
HANDLE_ERROR(AfError::from(err_val));
}
temp.into()
}
#[allow(unused_mut)]
pub fn var_all<T:HasAfEnum>(input: &Array<T>, isbiased: bool) -> (f64, f64)
{
let mut real: f64 = 0.0;
let mut imag: f64 = 0.0;
unsafe {
let err_val = af_var_all(&mut real as MutDouble, &mut imag as MutDouble,
input.get() as AfArray, isbiased as c_int);
HANDLE_ERROR(AfError::from(err_val));
}
(real, imag)
}
macro_rules! stat_all_func_def {
($doc_str: expr, $fn_name: ident, $ffi_fn: ident) => (
#[doc=$doc_str]
#[allow(unused_mut)]
pub fn $fn_name<T:HasAfEnum>(input: &Array<T>) -> (f64, f64) {
let mut real: f64 = 0.0;
let mut imag: f64 = 0.0;
unsafe {
let err_val = $ffi_fn(&mut real as MutDouble, &mut imag as MutDouble,
input.get() as AfArray);
HANDLE_ERROR(AfError::from(err_val));
}
(real, imag)
}
)
}
stat_all_func_def!("Compute mean of all data", mean_all, af_mean_all);
stat_all_func_def!("Compute standard deviation of all data", stdev_all, af_stdev_all);
#[allow(unused_mut)]
pub fn median_all<T>(input: &Array<T>) -> (f64, f64)
where T: HasAfEnum + MedianComputable
{
let mut real: f64 = 0.0;
let mut imag: f64 = 0.0;
unsafe {
let err_val = af_median_all(&mut real as MutDouble,
&mut imag as MutDouble,
input.get() as AfArray);
HANDLE_ERROR(AfError::from(err_val));
}
(real, imag)
}
macro_rules! stat_wtd_all_func_def {
($doc_str: expr, $fn_name: ident, $ffi_fn: ident) => (
#[doc=$doc_str]
#[allow(unused_mut)]
pub fn $fn_name<T, W>(input: &Array<T>, weights: &Array<W>) -> (f64, f64)
where T: HasAfEnum,
W: HasAfEnum + RealFloating
{
let mut real: f64 = 0.0;
let mut imag: f64 = 0.0;
unsafe {
let err_val = $ffi_fn(&mut real as MutDouble, &mut imag as MutDouble,
input.get() as AfArray, weights.get() as AfArray);
HANDLE_ERROR(AfError::from(err_val));
}
(real, imag)
}
)
}
stat_wtd_all_func_def!("Compute weighted mean of all data",
mean_all_weighted, af_mean_all_weighted);
stat_wtd_all_func_def!("Compute weighted variance of all data",
var_all_weighted, af_var_all_weighted);
#[allow(unused_mut)]
pub fn corrcoef<T>(x: &Array<T>, y: &Array<T>) -> (f64, f64)
where T: HasAfEnum + RealNumber
{
let mut real: f64 = 0.0;
let mut imag: f64 = 0.0;
unsafe {
let err_val = af_corrcoef(&mut real as MutDouble, &mut imag as MutDouble,
x.get() as AfArray, y.get() as AfArray);
HANDLE_ERROR(AfError::from(err_val));
}
(real, imag)
}
pub fn topk<T>(input: &Array<T>, k: u32, dim: i32, order: TopkFn) -> (Array<T>, Array<u32>)
where T: HasAfEnum
{
let mut t0: i64 = 0;
let mut t1: i64 = 0;
unsafe {
let err_val = af_topk(&mut t0 as MutAfArray, &mut t1 as MutAfArray,
input.get() as AfArray, k as c_int, dim as c_int,
order as c_uint);
HANDLE_ERROR(AfError::from(err_val));
}
(t0.into(), t1.into())
}