Repository navigation
Expand file tree
/
Copy pathUtil.java
More file actions
227 lines (191 loc) · 4.78 KB
/
Copy pathUtil.java
File metadata and controls
227 lines (191 loc) · 4.78 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
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
/*
* Created on Aug 24, 2003 by Ravi Mohan
*
*/
package aima.util;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Random;
public class Util {
public static final String NO = "No";
public static final String YES = "Yes";
private static Random r = new Random();
public static <T> T first(List<T> l) {
List<T> newList = new ArrayList<T>();
for (T element : l) {
newList.add(element);
}
return newList.get(0);
}
public static <T> List<T> rest(List<T> l) {
List<T> newList = new ArrayList<T>();
for (T element : l) {
newList.add(element);
}
newList.remove(0);
return newList;
}
public static boolean randomBoolean() {
int trueOrFalse = r.nextInt(2);
return (!(trueOrFalse == 0));
}
public static double[] normalize(double[] probDist) {
int len = probDist.length;
double total = 0.0;
for (double d : probDist) {
total = total + d;
}
double[] normalized = new double[len];
if (total != 0) {
for (int i = 0; i < len; i++) {
normalized[i] = probDist[i] / total;
}
}
double totalN = 0.0;
for (double d : normalized) {
totalN = totalN + d;
}
return normalized;
}
public static List<Double> normalize(List<Double> values) {
double[] valuesAsArray = new double[values.size()];
for (int i = 0; i < valuesAsArray.length; i++) {
valuesAsArray[i] = values.get(i);
}
double[] normalized = normalize(valuesAsArray);
List<Double> results = new ArrayList<Double>();
for (int i = 0; i < normalized.length; i++) {
results.add(normalized[i]);
}
return results;
}
public static int min(int i, int j) {
return (i > j ? j : i);
}
public static int max(int i, int j) {
return (i < j ? j : i);
}
public static int max(int i, int j, int k) {
return max(max(i, j), k);
}
public static int min(int i, int j, int k) {
return min(min(i, j), k);
}
public static <T> T selectRandomlyFromList(List<T> l) {
int index = r.nextInt(l.size());
return l.get(index);
}
public static <T> T mode(List<T> l) {
Hashtable<T, Integer> hash = new Hashtable<T, Integer>();
for (T obj : l) {
if (hash.containsKey(obj)) {
hash.put(obj, hash.get(obj).intValue() + 1);
} else {
hash.put(obj, 1);
}
}
T maxkey = hash.keySet().iterator().next();
for (T key : hash.keySet()) {
if (hash.get(key) > hash.get(maxkey)) {
maxkey = key;
}
}
return maxkey;
}
public static String[] yesno() {
return new String[] { YES, NO };
}
public static double log2(double d) {
return Math.log(d) / Math.log(2);
}
public static double information(double[] probabilities) {
double total = 0.0;
for (double d : probabilities) {
total += (-1.0 * log2(d) * d);
}
return total;
}
public static <T> List<T> removeFrom(List<T> list, T member) {
List<T> newList = new ArrayList<T>();
for (T s : list) {
if (!(s.equals(member))) {
newList.add(s);
}
}
return newList;
}
public static <T extends Number> double sumOfSquares(List<T> list) {
double accum = 0;
for (T item : list) {
accum = accum + (item.doubleValue() * item.doubleValue());
}
return accum;
}
public static String ntimes(String s, int n) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < n; i++) {
buf.append(s);
}
return buf.toString();
}
public static void checkForNanOrInfinity(double d) {
if (Double.isNaN(d)) {
throw new RuntimeException("Not a Number");
}
if (Double.isInfinite(d)) {
throw new RuntimeException("Infinite Number");
}
}
public static int randomNumberBetween(int i, int j) {
/* i,j both **inclusive** */
return r.nextInt(j - i + 1) + i;
}
public static double calculateMean(List<Double> lst) {
Double sum = 0.0;
for (Double d : lst) {
sum = sum + d.doubleValue();
}
return sum / lst.size();
}
public static double calculateStDev(List<Double> values, double mean) {
int listSize = values.size();
Double sumOfDiffSquared = 0.0;
for (Double value : values) {
double diffFromMean = value - mean;
sumOfDiffSquared += ((diffFromMean * diffFromMean) / (listSize - 1));// division
// moved
// here
// to
// avoid
// sum
// becoming
// too
// big
// if
// this
// doesn't
// work
// use
// incremental
// formulation
}
double variance = sumOfDiffSquared; // (listSize - 1); // assumes at
// least 2
// members in
// list
return Math.sqrt(variance);
}
public static List<Double> normalizeFromMeanAndStdev(List<Double> values,
double mean, double stdev) {
List<Double> normalized = new ArrayList<Double>();
for (Double d : values) {
normalized.add((d - mean) / stdev);
}
return normalized;
}
public static double generateRandomDoubleBetween(double lowerLimit,
double upperLimit) {
return lowerLimit + ((upperLimit - lowerLimit) * r.nextDouble());
}
}