-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathStatistics.java
More file actions
124 lines (90 loc) · 4.04 KB
/
Statistics.java
File metadata and controls
124 lines (90 loc) · 4.04 KB
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
package com.arrayfire;
import static com.arrayfire.ArrayFire.*;
class Statistics {
static private native long afMean(long ref, int dim);
static private native long afMeanWeighted(long ref, long weightsRef, int dim);
static private native DoubleComplex afMeanAll(long ref);
static private native DoubleComplex afMeanAllWeighted(long ref, long weightsRef);
static private native long afVar(long ref, boolean isBiased, int dim);
static private native long afVarWeighted(long ref, long weightsRef, int dim);
static private native DoubleComplex afVarAll(long ref, boolean isBiased);
static private native DoubleComplex afVarAllWeighted(long ref, long weightsRef);
static private native long afStdev(long ref, int dim);
static private native DoubleComplex afStdevAll(long ref);
static private native long afMedian(long ref, int dim);
static private native DoubleComplex afMedianAll(long ref);
static private native long afCov(long ref, long ref2, boolean isBiased);
static private native DoubleComplex afCorrcoef(long ref, long ref2);
static private native long[] afTopk(long ref, int k, int dim, int order);
static public Array mean(final Array in, int dim) {
return new Array(afMean(in.ref, dim));
}
static public Array mean(final Array in, final Array weights, int dim) {
return new Array(afMeanWeighted(in.ref, weights.ref, dim));
}
static public <T> T mean(final Array in, Class<T> type) throws Exception {
DoubleComplex res = afMeanAll(in.ref);
return castResult(res, type);
}
static public <T> T mean(final Array in, final Array weights, Class<T> type) throws Exception {
DoubleComplex res = afMeanAllWeighted(in.ref, weights.ref);
return castResult(res, type);
}
static public Array var(final Array in, boolean isBiased, int dim) {
return new Array(afVar(in.ref, isBiased, dim));
}
static public Array var(final Array in, final Array weights, int dim) {
return new Array(afVarWeighted(in.ref, weights.ref, dim));
}
static public <T> T var(final Array in, boolean isBiased, Class<T> type) throws Exception {
DoubleComplex res = afVarAll(in.ref, isBiased);
return castResult(res, type);
}
static public <T> T var(final Array in, final Array weights, Class<T> type) throws Exception {
DoubleComplex res = afVarAllWeighted(in.ref, weights.ref);
return castResult(res, type);
}
static public Array stdev(final Array in, int dim) {
return new Array(afStdev(in.ref, dim));
}
static public <T> T stdev(final Array in, Class<T> type) throws Exception {
DoubleComplex res = afStdevAll(in.ref);
return castResult(res, type);
}
static public Array median(final Array in, int dim) {
return new Array(afMedian(in.ref, dim));
}
static public <T> T median(final Array in, Class<T> type) throws Exception {
DoubleComplex res = afMedianAll(in.ref);
return castResult(res, type);
}
static public Array cov(Array x, Array y, boolean isBiased) {
return new Array(afCov(x.ref, y.ref, isBiased));
}
static public <T extends Number> T corrcoef(final Array x, final Array y, Class<T> type)
throws Exception {
DoubleComplex res = afCorrcoef(x.ref, y.ref);
return castResult(res, type);
}
static public Array[] topk(final Array in, int k, int dim, ArrayFire.TopkOrder order) throws Exception {
long[] refs = afTopk(in.ref, k, dim, order.getOrder());
return new Array[] {new Array(refs[0]), new Array(refs[1])};
}
static public <T> T castResult(DoubleComplex res, Class<T> type) throws Exception {
Object ret;
if (type == Float.class) {
ret = Float.valueOf((float) res.real());
} else if (type == Double.class) {
ret = Double.valueOf((double) res.real());
} else if (type == Integer.class) {
ret = Integer.valueOf((int) res.real());
} else if (type == FloatComplex.class) {
ret = new FloatComplex((float) res.real(), (float) res.imag());
} else if (type == DoubleComplex.class) {
ret = res;
} else {
throw new Exception("Unknown type");
}
return type.cast(ret);
}
}