-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathTools.java
More file actions
370 lines (340 loc) · 9.36 KB
/
Tools.java
File metadata and controls
370 lines (340 loc) · 9.36 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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package bartMachine;
import java.util.ArrayList;
import java.util.Collection;
import gnu.trove.list.array.TDoubleArrayList;
import gnu.trove.list.array.TIntArrayList;
/**
* A class that contains many generally useful convenience methods.
*
* @author Adam Kapelner and Justin Bleich
*/
public class Tools {
/**
* Joins a collection of strings into one string
*
* @param all the collection of substrings
* @param joinby the token that joins the substrings
* @return the final product: str1 + joinby + str2 + . . . + strN
*/
@SuppressWarnings("rawtypes")
public static String StringJoin(ArrayList all, String joinby){
if (all == null){
return " NULL ARRAY ";
}
return StringJoin(all.toArray(), joinby);
}
/**
* Joins a collection of strings into one string
*
* @param all the collection of substrings
* @param joinby the token that joins the substrings
* @return the final product: str1 + joinby + str2 + . . . + strN
*/
public static String StringJoin(TIntArrayList all, String joinby){
if (all == null){
return " NULL ARRAY ";
}
return StringJoin(all.toArray(), joinby);
}
/**
* Joins a collection of strings into one string
*
* @param all the collection of substrings
* @param joinby the token that joins the substrings
* @return the final product: str1 + joinby + str2 + . . . + strN
*/
public static String StringJoin(double[] all, String joinby){
if (all == null){
return " NULL ARRAY ";
}
String joined = "";
for (int i = 0; i < all.length; i++){
joined += all[i];
if (i < all.length - 1)
joined += joinby;
}
return joined;
}
/**
* Joins a collection of strings into one string
*
* @param all the collection of substrings
* @param joinby the token that joins the substrings
* @return the final product: str1 + joinby + str2 + . . . + strN
*/
public static String StringJoin(int[] all, String joinby){
if (all == null){
return " NULL ARRAY ";
}
String joined = "";
for (int i = 0; i < all.length; i++){
joined += all[i];
if (i < all.length - 1)
joined += joinby;
}
return joined;
}
/**
* Joins a collection of strings into one string with commas
*
* @param all the collection of substrings
* @return the final product: str1 + joinby + str2 + . . . + strN
*/
public static String StringJoin(TIntArrayList all){
return StringJoin(all.toArray(), ", ");
}
/**
* Joins a collection of strings into one string with commas
*
* @param all the collection of substrings
* @return the final product: str1 + joinby + str2 + . . . + strN
*/
public static String StringJoin(boolean[] all){
int[] all_ints = new int[all.length];
for (int i = 0; i < all.length; i++){
all_ints[i] = all[i] ? 1 : 0;
}
return StringJoin(all_ints, ", ");
}
/**
* Joins a collection of strings into one string with commas
*
* @param all the collection of substrings
* @return the final product: str1 + joinby + str2 + . . . + strN
*/
public static String StringJoin(int[] all){
return StringJoin(all, ", ");
}
/**
* Joins a collection of strings into one string with commas
*
* @param all the collection of substrings
* @return the final product: str1 + joinby + str2 + . . . + strN
*/
public static String StringJoin(TDoubleArrayList all){
return StringJoin(all.toArray(), ", ");
}
/**
* Joins a collection of strings into one string with commas
*
* @param all the collection of substrings
* @return the final product: str1 + joinby + str2 + . . . + strN
*/
public static String StringJoin(double[] all){
return StringJoin(all, ", ");
}
/**
* Joins a collection of strings into one string with commas
*
* @param all the collection of substrings
* @return the final product: str1 + joinby + str2 + . . . + strN
*/
public static String StringJoin(ArrayList<Object> all){
return StringJoin(all, ", ");
}
/**
* Joins a collection of strings into one string
*
* @param all the collection of substrings
* @param joinby the token that joins the substrings
* @return the final product: str1 + joinby + str2 + . . . + strN
*/
public static String StringJoin(Object[] all, String joinby){
String joined = "";
for (int i = 0; i < all.length; i++){
joined += all[i];
if (i < all.length - 1)
joined += joinby;
}
return joined;
}
/**
* Joins a collection of strings into one string with commas
*
* @param all the collection of substrings
* @return the final product: str1 + joinby + str2 + . . . + strN
*/
public static String StringJoin(Object[] all){
return StringJoin(all, ", ");
}
/**
* Joins a collection of strings into one string
*
* @param all the collection of substrings
* @param joinby the token that joins the substrings
* @return the final product: str1 + joinby + str2 + . . . + strN
*/
public static String StringJoinStrings(Collection<String> all, String joinby){
Object[] arr = all.toArray();
String joined = "";
for (int i = 0; i < arr.length; i++){
joined += (String)arr[i];
if (i < arr.length - 1)
joined += joinby;
}
return joined;
}
/**
* Joins a collection of strings into one string with commas
*
* @param all the collection of substrings
* @return the final product: str1 + joinby + str2 + . . . + strN
*/
public static String StringJoin(Collection<String> all){
return StringJoinStrings(all, ", ");
}
/**
* Returns the max of a vector
*
* @param values The values of interest
* @return The maximum of those values
*/
public static double max(double[] values) {
double max = Double.NEGATIVE_INFINITY;
for (double value : values) {
if (value > max){
max = value;
}
}
return max;
}
/**
* Sums an array of doubles
*
* @param arr The values of interest
* @return The sum of those values
*/
public static double sum_array(double[] arr){
double sum = 0;
for (int i = 0; i < arr.length; i++){
sum += arr[i];
}
return sum;
}
/**
* Sums an array of booleans
*
* @param arr The values of interest
* @return The sum of those values
*/
public static int sum_array(boolean[] arr){
int sum = 0;
for (int i = 0; i < arr.length; i++){
if (arr[i]) {
sum++;
}
}
return sum;
}
/**
* Sums the inverse values of an array
*
* @param arr The values of interest
* @return The sum of the inverses of those values
*/
public static double sum_inv_array(double[] arr) {
double sum = 0;
for (int i = 0; i < arr.length; i++){
sum += 1 / arr[i];
}
return sum;
}
/**
* Normalizes an array by dividing each value by the array's sum
*
* @param arr The values of interest
*/
public static void normalize_array(double[] arr){
double weight = sum_array(arr);
for (int i = 0; i < arr.length; i++){
arr[i] = arr[i] / weight;
}
}
/**
* Weights an array by dividing each value by a specified value
*
* @param weight The value to divide each value in the array by
* @param arr The values of interest
*/
public static void weight_arr(double[] arr, double weight){
for (int i = 0; i < arr.length; i++){
arr[i] = arr[i] / weight;
}
}
/**
* Subtracts one array from the other
*
* @param arr1 The array of minuends
* @param arr2 The array of subtrahends
* @return The array of differences
*/
public static double[] subtract_arrays(double[] arr1, double[] arr2) {
int n = arr1.length;
double[] diff = new double[n];
for (int i = 0; i < n; i++){
diff[i] = arr1[i] - arr2[i];
}
return diff;
}
/**
* Adds one array to another
*
* @param arr1 The array of first addends
* @param arr2 The array of seconds addends
* @return The array of sums
*/
public static double[] add_arrays(double[] arr1, double[] arr2) {
int n = arr1.length;
double[] sum = new double[n];
for (int i = 0; i < n; i++){
sum[i] = arr1[i] + arr2[i];
}
return sum;
}
/**
* Adds one array to another
*
* @param arr1 The array of first addends
* @param arr2 The array of seconds addends
* @return The array of sums
*/
public static double[] add_arrays(double[] arr1, int[] arr2) {
int n = arr1.length;
double[] sum = new double[n];
for (int i = 0; i < n; i++){
sum[i] = arr1[i] + arr2[i];
}
return sum;
}
/**
* Adds one array to another
*
* @param arr1 The array of first addends
* @param arr2 The array of seconds addends
* @return The array of sums
*/
public static int[] add_arrays(int[] arr1, int[] arr2) {
int n = arr1.length;
int[] sum = new int[n];
for (int i = 0; i < n; i++){
sum[i] = arr1[i] + arr2[i];
}
return sum;
}
/**
* Adds one array to another after first converting each addend to binary
* (1 if the value > 0, 0 otherwise)
*
* @param arr1 The array of first addends
* @param arr2 The array of seconds addends
* @return The array of sums of binary valus
*/
public static int[] binary_add_arrays(int[] arr1, int[] arr2) {
int n = arr1.length;
int[] sum = new int[n];
for (int i = 0; i < n; i++){
sum[i] = (arr1[i] >= 1 ? 1 : 0) + (arr2[i] >= 1 ? 1 : 0);
}
return sum;
}
}