1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.math; |
22 |
|
|
23 |
|
import jalview.util.Format; |
24 |
|
|
25 |
|
import java.lang.Math; |
26 |
|
import java.util.Arrays; |
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
|
|
| 6.7% |
Uncovered Elements: 167 (179) |
Complexity: 50 |
Complexity Density: 0.49 |
|
33 |
|
public class MiscMath |
34 |
|
{ |
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
@param |
39 |
|
|
40 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
41 |
0 |
public static void print(double[] m, String format)... |
42 |
|
{ |
43 |
0 |
System.out.print("[ "); |
44 |
0 |
for (double a : m) |
45 |
|
{ |
46 |
0 |
Format.print(System.out, format + " ", a); |
47 |
|
} |
48 |
0 |
System.out.println("]"); |
49 |
|
} |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
@param |
55 |
|
|
56 |
|
@return |
57 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
58 |
0 |
public static double mean(double[] m)... |
59 |
|
{ |
60 |
0 |
double sum = 0; |
61 |
0 |
int nanCount = 0; |
62 |
0 |
for (int i = 0; i < m.length; i++) |
63 |
|
{ |
64 |
0 |
if (!Double.isNaN(m[i])) |
65 |
|
{ |
66 |
0 |
sum += m[i]; |
67 |
|
} |
68 |
|
else |
69 |
|
{ |
70 |
0 |
nanCount++; |
71 |
|
} |
72 |
|
} |
73 |
0 |
return sum / (double) (m.length - nanCount); |
74 |
|
} |
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
@param |
80 |
|
|
81 |
|
@return |
82 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
83 |
0 |
public static double sum(double[] m)... |
84 |
|
{ |
85 |
0 |
double sum = 0; |
86 |
0 |
for (int i = 0; i < m.length; i++) |
87 |
|
{ |
88 |
0 |
if (!Double.isNaN(m[i])) |
89 |
|
{ |
90 |
0 |
sum += m[i]; |
91 |
|
} |
92 |
|
} |
93 |
0 |
return sum; |
94 |
|
} |
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
@param |
100 |
|
|
101 |
|
|
102 |
|
@return |
103 |
|
|
104 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
105 |
0 |
public static double[] sqrt(double[] m)... |
106 |
|
{ |
107 |
0 |
double[] sqrts = new double[m.length]; |
108 |
0 |
for (int i = 0; i < m.length; i++) |
109 |
|
{ |
110 |
0 |
sqrts[i] = Math.sqrt(m[i]); |
111 |
|
} |
112 |
0 |
return sqrts; |
113 |
|
} |
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
@param |
119 |
|
|
120 |
|
@param |
121 |
|
|
122 |
|
|
123 |
|
@return |
124 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
125 |
0 |
public static double[] elementwiseMultiply(byte[] a, double[] b)... |
126 |
|
throws RuntimeException |
127 |
|
{ |
128 |
0 |
if (a.length != b.length) |
129 |
|
|
130 |
|
{ |
131 |
0 |
throw new SameLengthException(a.length, b.length); |
132 |
|
} |
133 |
0 |
double[] result = new double[a.length]; |
134 |
0 |
for (int i = 0; i < a.length; i++) |
135 |
|
{ |
136 |
0 |
result[i] = a[i] * b[i]; |
137 |
|
} |
138 |
0 |
return result; |
139 |
|
} |
140 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
141 |
0 |
public static double[] elementwiseMultiply(double[] a, double[] b)... |
142 |
|
throws RuntimeException |
143 |
|
{ |
144 |
0 |
if (a.length != b.length) |
145 |
|
|
146 |
|
{ |
147 |
0 |
throw new SameLengthException(a.length, b.length); |
148 |
|
} |
149 |
0 |
double[] result = new double[a.length]; |
150 |
0 |
for (int i = 0; i < a.length; i++) |
151 |
|
{ |
152 |
0 |
result[i] = a[i] * b[i]; |
153 |
|
} |
154 |
0 |
return result; |
155 |
|
} |
156 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
157 |
0 |
public static byte[] elementwiseMultiply(byte[] a, byte[] b)... |
158 |
|
throws RuntimeException |
159 |
|
{ |
160 |
0 |
if (a.length != b.length) |
161 |
|
|
162 |
|
{ |
163 |
0 |
throw new SameLengthException(a.length, b.length); |
164 |
|
} |
165 |
0 |
byte[] result = new byte[a.length]; |
166 |
0 |
for (int i = 0; i < a.length; i++) |
167 |
|
{ |
168 |
0 |
result[i] = (byte) (a[i] * b[i]); |
169 |
|
} |
170 |
0 |
return result; |
171 |
|
} |
172 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
173 |
0 |
public static double[] elementwiseMultiply(double[] a, double b)... |
174 |
|
{ |
175 |
0 |
double[] result = new double[a.length]; |
176 |
0 |
for (int i = 0; i < a.length; i++) |
177 |
|
{ |
178 |
0 |
result[i] = a[i] * b; |
179 |
|
} |
180 |
0 |
return result; |
181 |
|
} |
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
|
186 |
|
@param |
187 |
|
|
188 |
|
@param |
189 |
|
|
190 |
|
|
191 |
|
@return |
192 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
193 |
0 |
public static double[] elementwiseDivide(double[] a, double[] b)... |
194 |
|
throws RuntimeException |
195 |
|
{ |
196 |
0 |
if (a.length != b.length) |
197 |
|
|
198 |
|
{ |
199 |
0 |
throw new SameLengthException(a.length, b.length); |
200 |
|
} |
201 |
0 |
double[] result = new double[a.length]; |
202 |
0 |
for (int i = 0; i < a.length; i++) |
203 |
|
{ |
204 |
0 |
result[i] = a[i] / b[i]; |
205 |
|
} |
206 |
0 |
return result; |
207 |
|
} |
208 |
|
|
209 |
|
|
210 |
|
|
211 |
|
|
212 |
|
@param |
213 |
|
|
214 |
|
@param |
215 |
|
|
216 |
|
|
217 |
|
@return |
218 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
219 |
0 |
public static double[] elementwiseAdd(double[] a, double[] b)... |
220 |
|
throws RuntimeException |
221 |
|
{ |
222 |
0 |
if (a.length != b.length) |
223 |
|
|
224 |
|
{ |
225 |
0 |
throw new SameLengthException(a.length, b.length); |
226 |
|
} |
227 |
0 |
double[] result = new double[a.length]; |
228 |
|
|
229 |
0 |
for (int i = 0; i < a.length; i++) |
230 |
|
{ |
231 |
0 |
result[i] += a[i] + b[i]; |
232 |
|
} |
233 |
0 |
return result; |
234 |
|
} |
235 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
236 |
0 |
public static double[] elementwiseAdd(double[] a, double b)... |
237 |
|
{ |
238 |
0 |
double[] result = new double[a.length]; |
239 |
0 |
for (int i = 0; i < a.length; i++) |
240 |
|
{ |
241 |
0 |
result[i] = a[i] + b; |
242 |
|
} |
243 |
0 |
return result; |
244 |
|
} |
245 |
|
|
246 |
|
|
247 |
|
|
248 |
|
|
249 |
|
@param |
250 |
|
|
251 |
|
@param |
252 |
|
|
253 |
|
@param |
254 |
|
|
255 |
|
@param |
256 |
|
|
257 |
|
@param |
258 |
|
|
259 |
|
|
260 |
|
@return |
261 |
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 6 |
Complexity Density: 0.75 |
|
262 |
0 |
public static boolean allClose(double[] a, double[] b, double rtol,... |
263 |
|
double atol, boolean equalNAN) |
264 |
|
{ |
265 |
0 |
boolean areEqual = true; |
266 |
0 |
for (int i = 0; i < a.length; i++) |
267 |
|
{ |
268 |
0 |
if (equalNAN && (Double.isNaN(a[i]) && Double.isNaN(b[i]))) |
269 |
|
|
270 |
|
|
271 |
|
|
272 |
|
{ |
273 |
0 |
continue; |
274 |
|
} |
275 |
0 |
if (Math.abs(a[i] - b[i]) > (atol + rtol * Math.abs(b[i]))) |
276 |
|
|
277 |
|
|
278 |
|
|
279 |
|
|
280 |
|
|
281 |
|
|
282 |
|
|
283 |
|
|
284 |
|
{ |
285 |
0 |
areEqual = false; |
286 |
0 |
break; |
287 |
|
} |
288 |
|
} |
289 |
0 |
return areEqual; |
290 |
|
} |
291 |
|
|
292 |
|
|
293 |
|
|
294 |
|
|
295 |
|
@param |
296 |
|
|
297 |
|
|
298 |
|
@return |
299 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
300 |
2 |
public static int[] findMax(int[] a)... |
301 |
|
{ |
302 |
2 |
int max = 0; |
303 |
2 |
int maxIndex = 0; |
304 |
6 |
for (int i = 0; i < a.length; i++) |
305 |
|
{ |
306 |
4 |
if (a[i] > max) |
307 |
|
{ |
308 |
2 |
max = a[i]; |
309 |
2 |
maxIndex = i; |
310 |
|
} |
311 |
|
} |
312 |
2 |
return new int[] { maxIndex, max }; |
313 |
|
} |
314 |
|
|
315 |
|
|
316 |
|
|
317 |
|
|
318 |
|
@param |
319 |
|
|
320 |
|
@param |
321 |
|
|
322 |
|
|
323 |
|
@return |
324 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
325 |
0 |
public static double dot(double[] a, double[] b)... |
326 |
|
{ |
327 |
0 |
if (a.length != b.length) |
328 |
|
{ |
329 |
0 |
throw new IllegalArgumentException( |
330 |
|
String.format("Vectors do not have the same length (%d, %d)!", |
331 |
|
a.length, b.length)); |
332 |
|
} |
333 |
|
|
334 |
0 |
double aibi = 0; |
335 |
0 |
for (int i = 0; i < a.length; i++) |
336 |
|
{ |
337 |
0 |
aibi += a[i] * b[i]; |
338 |
|
} |
339 |
0 |
return aibi; |
340 |
|
} |
341 |
|
|
342 |
|
|
343 |
|
|
344 |
|
|
345 |
|
@param |
346 |
|
|
347 |
|
|
348 |
|
@return |
349 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
350 |
0 |
public static double norm(double[] v)... |
351 |
|
{ |
352 |
0 |
double result = 0; |
353 |
0 |
for (double i : v) |
354 |
|
{ |
355 |
0 |
result += Math.pow(i, 2); |
356 |
|
} |
357 |
0 |
return Math.sqrt(result); |
358 |
|
} |
359 |
|
|
360 |
|
|
361 |
|
|
362 |
|
|
363 |
|
@param |
364 |
|
|
365 |
|
|
366 |
|
@return |
367 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
368 |
0 |
public static int countNaN(double[] v)... |
369 |
|
{ |
370 |
0 |
int cnt = 0; |
371 |
0 |
for (double i : v) |
372 |
|
{ |
373 |
0 |
if (Double.isNaN(i)) |
374 |
|
{ |
375 |
0 |
cnt++; |
376 |
|
} |
377 |
|
} |
378 |
0 |
return cnt; |
379 |
|
} |
380 |
|
|
381 |
|
|
382 |
|
|
383 |
|
|
384 |
|
|
385 |
|
@param |
386 |
|
@param |
387 |
|
|
388 |
|
@return |
389 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
390 |
0 |
public static long permutations(int n, int r)... |
391 |
|
{ |
392 |
0 |
if (n < r) |
393 |
0 |
return permutations(r, n); |
394 |
|
|
395 |
0 |
long result = 1l; |
396 |
0 |
for (int i = 0; i < r; i++) |
397 |
|
{ |
398 |
0 |
result *= (n - i); |
399 |
|
} |
400 |
0 |
return result; |
401 |
|
} |
402 |
|
|
403 |
|
|
404 |
|
|
405 |
|
|
406 |
|
@param |
407 |
|
@param |
408 |
|
|
409 |
|
@return |
410 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
411 |
0 |
public static int combinations(int n, int r)... |
412 |
|
{ |
413 |
0 |
int result = 1; |
414 |
0 |
for (int i = 0; i < r; i++) |
415 |
|
{ |
416 |
0 |
result *= (n - 1); |
417 |
|
} |
418 |
0 |
return (int) (result / MiscMath.factorial(r)); |
419 |
|
} |
420 |
|
|
421 |
|
|
422 |
|
|
423 |
|
|
424 |
|
@param |
425 |
|
|
426 |
|
@return |
427 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
428 |
0 |
public static int factorial(int n)... |
429 |
|
{ |
430 |
0 |
int result = 1; |
431 |
0 |
for (int i = 0; i < n; i++) |
432 |
|
{ |
433 |
0 |
result *= (n - i); |
434 |
|
} |
435 |
0 |
return result; |
436 |
|
} |
437 |
|
|
438 |
|
} |