-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.java
222 lines (195 loc) · 4.78 KB
/
math.java
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
package StreamClustering.ReSSL;
import weka.core.Instance;
/**
* Arimithetics computations
* @author Administrator
*
*/
public class math {
public static double[] sub(double[] a, double[] b) {
double[] r = new double[a.length];
for (int i=0; i<r.length; ++i) {
r[i] = a[i]-b[i];
}
return r;
}
public static double[] sub(Instance a, double[] b) {
double[] r = new double[b.length];
for (int i=0; i<r.length; ++i) {
r[i] = a.value(i)-b[i];
}
return r;
}
public static double squareSum(double[] a) {
double r = 0;
for (int i=0; i<a.length; ++i) {
r += a[i]*a[i];
}
return r;
}
public static double euclideanDist(double[] a, double[] b) {
return Math.sqrt(squareSum(sub(a, b)));
}
public static double euclideanDist(Instance sa, Instance sb) {
// TODO Auto-generated method stub
double r = 0;
for (int i=0; i<sa.numAttributes()-1; ++i) {
r += Math.pow(sa.value(i)-sb.value(i), 2);
}
return Math.sqrt(r);
}
public static double min(double[] ds) {
// TODO Auto-generated method stub
double minV = ds[0];
for (int i=1; i<ds.length; ++i) {
if (ds[i] < minV) {
minV = ds[i];
}
}
return minV;
}
public static int minIndex(double[] v) {
int r = 0;
double minValue = v[0];
for (int i=1; i<v.length; ++i) {
if (v[i] < minValue) {
minValue = v[i];
r = i;
}
}
return r;
}
public static double sum(double[] minRVal) {
// TODO Auto-generated method stub
double sum = 0;
for (int i=0; i<minRVal.length; ++i) {
sum += minRVal[i];
}
return sum;
}
public static int[] minRVIndex(double[][] distM) {
// TODO Auto-generated method stub
int[] belongs = new int[distM.length];
for (int i=0; i<distM.length; ++i) {
belongs[i] = minIndex(distM[i]);
}
return belongs;
}
public static int maxIndex(int[] v) {
// TODO Auto-generated method stub
int maxIndex = 0;
int maxValue = v[0];
for (int i=1; i<v.length; ++i) {
if (v[i] > maxValue) {
maxIndex = i;
maxValue = v[i];
}
}
return maxIndex;
}
public static int sum(int[] v) {
// TODO Auto-generated method stub
int r = 0;
for (int i=0; i<v.length; ++i) {
r += v[i];
}
return r;
}
public static double[] normalize(double[] a) {
double[] r = new double[a.length];
double ret = sum(a);
if (ret!=0) {
for (int i=0; i<a.length; ++i) {
r[i] = a[i] / ret;
}
}
return r;
}
public static double[] normalize(int[] v) {
double[] r = new double[v.length];
double sumr = sum(v);
for (int i=0; i<v.length; ++i) {
r[i] = (double)r[i] / sumr;
}
return r;
}
public static double log2(double v) {
return Math.log(v)/Math.log(2);
}
public static boolean belongs(int v, int[] lIndex) {
// TODO Auto-generated method stub
for (int i=0; i<lIndex.length; ++i) {
if (lIndex[i] == v) {
return true;
}
}
return false;
}
public static int minPositive(int[] counts) {
// TODO Auto-generated method stub
int minP = 200;
for (int i=0; i<counts.length; ++i) {
if (counts[i]>0 && counts[i]<minP) {
minP = counts[i];
}
}
return minP;
}
public static double minPositive(double[] v) {
// TODO Auto-generated method stub
double minP = 200;
for (int i=0; i<v.length; ++i) {
if (v[i] > 0 && v[i] < minP) {
minP = v[i];
}
}
assert(minP!=200);
return minP;
}
public static double euclideanDist(double[] ins, Instance instance) {
double r = 0;// TODO Auto-generated method stub
assert(ins.length==instance.numAttributes()-1);
for (int i=0; i<ins.length; ++i) {
r += Math.pow(ins[i], instance.value(i));
}
return Math.sqrt(r);
}
public static int maxIndex(double[] weights) {
// TODO Auto-generated method stub
int id = 0;
double mw = weights[0];
for (int i=1; i<weights.length; ++i) {
if (weights[i] > mw) {
id = i;
mw = weights[i];
}
}
return id;
}
public static double max(double[] weights) {
// TODO Auto-generated method stub
double r = weights[0];
for (int i=1; i<weights.length; ++i) {
if (weights[i] > r) {
r = weights[i];
}
}
return r;
}
public static double mean(double[] val) {
// TODO Auto-generated method stub
double ret = 0;
for (int i=0; i<val.length; ++i) {
ret += val[i];
}
return ret/val.length;
}
public static double std(double[] vals, double bias) {
// TODO Auto-generated method stub
double ret = 0;
for (int i=0; i<vals.length; ++i) {
ret += Math.pow((vals[i]-bias), 2);
}
return Math.sqrt(ret/vals.length);
}
}