1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math4.legacy.core;
18
19 import java.util.Arrays;
20
21 import org.junit.Assert;
22 import org.junit.Test;
23
24 import org.apache.commons.numbers.core.Precision;
25 import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
26 import org.apache.commons.math4.legacy.exception.MathArithmeticException;
27 import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
28 import org.apache.commons.math4.legacy.exception.NoDataException;
29 import org.apache.commons.math4.legacy.exception.NonMonotonicSequenceException;
30 import org.apache.commons.math4.legacy.exception.NotANumberException;
31 import org.apache.commons.math4.legacy.exception.NotPositiveException;
32 import org.apache.commons.math4.legacy.exception.NotStrictlyPositiveException;
33 import org.apache.commons.math4.legacy.exception.NullArgumentException;
34 import org.apache.commons.math4.legacy.exception.NotFiniteNumberException;
35 import org.apache.commons.math4.core.jdkmath.JdkMath;
36
37
38
39
40 public class MathArraysTest {
41
42 private double[] testArray = {0, 1, 2, 3, 4, 5};
43 private double[] testWeightsArray = {0.3, 0.2, 1.3, 1.1, 1.0, 1.8};
44 private double[] testNegativeWeightsArray = {-0.3, 0.2, -1.3, 1.1, 1.0, 1.8};
45 private double[] singletonArray = {0};
46
47 @Test
48 public void testScale() {
49 final double[] test = new double[] {-2.5, -1, 0, 1, 2.5};
50 final double[] correctTest = Arrays.copyOf(test, test.length);
51 final double[] correctScaled = new double[]{5.25, 2.1, 0, -2.1, -5.25};
52
53 final double[] scaled = MathArrays.scale(-2.1, test);
54
55
56 for (int i = 0; i < test.length; i++) {
57 Assert.assertEquals(correctTest[i], test[i], 0);
58 }
59
60
61 for (int i = 0; i < scaled.length; i++) {
62 Assert.assertEquals(correctScaled[i], scaled[i], 0);
63 }
64 }
65
66 @Test
67 public void testScaleInPlace() {
68 final double[] test = new double[] {-2.5, -1, 0, 1, 2.5};
69 final double[] correctScaled = new double[]{5.25, 2.1, 0, -2.1, -5.25};
70 MathArrays.scaleInPlace(-2.1, test);
71
72
73 for (int i = 0; i < test.length; i++) {
74 Assert.assertEquals(correctScaled[i], test[i], 0);
75 }
76 }
77
78 @Test(expected = DimensionMismatchException.class)
79 public void testEbeAddPrecondition() {
80 MathArrays.ebeAdd(new double[3], new double[4]);
81 }
82 @Test(expected = DimensionMismatchException.class)
83 public void testEbeSubtractPrecondition() {
84 MathArrays.ebeSubtract(new double[3], new double[4]);
85 }
86 @Test(expected = DimensionMismatchException.class)
87 public void testEbeMultiplyPrecondition() {
88 MathArrays.ebeMultiply(new double[3], new double[4]);
89 }
90 @Test(expected = DimensionMismatchException.class)
91 public void testEbeDividePrecondition() {
92 MathArrays.ebeDivide(new double[3], new double[4]);
93 }
94
95 @Test
96 public void testEbeAdd() {
97 final double[] a = {0, 1, 2};
98 final double[] b = {3, 5, 7};
99 final double[] r = MathArrays.ebeAdd(a, b);
100
101 for (int i = 0; i < a.length; i++) {
102 Assert.assertEquals(a[i] + b[i], r[i], 0);
103 }
104 }
105 @Test
106 public void testEbeSubtract() {
107 final double[] a = {0, 1, 2};
108 final double[] b = {3, 5, 7};
109 final double[] r = MathArrays.ebeSubtract(a, b);
110
111 for (int i = 0; i < a.length; i++) {
112 Assert.assertEquals(a[i] - b[i], r[i], 0);
113 }
114 }
115
116 @Test
117 public void testEbeMultiply() {
118 final double[] a = {0, 1, 2};
119 final double[] b = {3, 5, 7};
120 final double[] r = MathArrays.ebeMultiply(a, b);
121
122 for (int i = 0; i < a.length; i++) {
123 Assert.assertEquals(a[i] * b[i], r[i], 0);
124 }
125 }
126
127 @Test
128 public void testEbeDivide() {
129 final double[] a = {0, 1, 2};
130 final double[] b = {3, 5, 7};
131 final double[] r = MathArrays.ebeDivide(a, b);
132
133 for (int i = 0; i < a.length; i++) {
134 Assert.assertEquals(a[i] / b[i], r[i], 0);
135 }
136 }
137
138 @Test
139 public void testL1DistanceDouble() {
140 double[] p1 = {2.5, 0.0};
141 double[] p2 = {-0.5, 4.0};
142 Assert.assertTrue(Precision.equals(7.0, MathArrays.distance1(p1, p2), 1));
143 }
144
145 @Test
146 public void testL1DistanceInt() {
147 int[] p1 = {3, 0};
148 int[] p2 = {0, 4};
149 Assert.assertEquals(7, MathArrays.distance1(p1, p2));
150 }
151
152 @Test
153 public void testL2DistanceDouble() {
154 double[] p1 = {2.5, 0.0};
155 double[] p2 = {-0.5, 4.0};
156 Assert.assertTrue(Precision.equals(5.0, MathArrays.distance(p1, p2), 1));
157 }
158
159 @Test
160 public void testL2DistanceInt() {
161 int[] p1 = {3, 0};
162 int[] p2 = {0, 4};
163 Assert.assertTrue(Precision.equals(5, MathArrays.distance(p1, p2), 1));
164 }
165
166 @Test
167 public void testLInfDistanceDouble() {
168 double[] p1 = {2.5, 0.0};
169 double[] p2 = {-0.5, 4.0};
170 Assert.assertTrue(Precision.equals(4.0, MathArrays.distanceInf(p1, p2), 1));
171 }
172
173 @Test
174 public void testLInfDistanceInt() {
175 int[] p1 = {3, 0};
176 int[] p2 = {0, 4};
177 Assert.assertEquals(4, MathArrays.distanceInf(p1, p2));
178 }
179
180 @Test
181 public void testCheckOrder() {
182 MathArrays.checkOrder(new double[] {-15, -5.5, -1, 2, 15},
183 MathArrays.OrderDirection.INCREASING, true);
184 MathArrays.checkOrder(new double[] {-15, -5.5, -1, 2, 2},
185 MathArrays.OrderDirection.INCREASING, false);
186 MathArrays.checkOrder(new double[] {3, -5.5, -11, -27.5},
187 MathArrays.OrderDirection.DECREASING, true);
188 MathArrays.checkOrder(new double[] {3, 0, 0, -5.5, -11, -27.5},
189 MathArrays.OrderDirection.DECREASING, false);
190
191 try {
192 MathArrays.checkOrder(new double[] {-15, -5.5, -1, -1, 2, 15},
193 MathArrays.OrderDirection.INCREASING, true);
194 Assert.fail("an exception should have been thrown");
195 } catch (NonMonotonicSequenceException e) {
196
197 }
198 try {
199 MathArrays.checkOrder(new double[] {-15, -5.5, -1, -2, 2},
200 MathArrays.OrderDirection.INCREASING, false);
201 Assert.fail("an exception should have been thrown");
202 } catch (NonMonotonicSequenceException e) {
203
204 }
205 try {
206 MathArrays.checkOrder(new double[] {3, 3, -5.5, -11, -27.5},
207 MathArrays.OrderDirection.DECREASING, true);
208 Assert.fail("an exception should have been thrown");
209 } catch (NonMonotonicSequenceException e) {
210
211 }
212 try {
213 MathArrays.checkOrder(new double[] {3, -1, 0, -5.5, -11, -27.5},
214 MathArrays.OrderDirection.DECREASING, false);
215 Assert.fail("an exception should have been thrown");
216 } catch (NonMonotonicSequenceException e) {
217
218 }
219 try {
220 MathArrays.checkOrder(new double[] {3, 0, -5.5, -11, -10},
221 MathArrays.OrderDirection.DECREASING, false);
222 Assert.fail("an exception should have been thrown");
223 } catch (NonMonotonicSequenceException e) {
224
225 }
226 }
227
228 @Test
229 public void testIsMonotonic() {
230 Assert.assertFalse(MathArrays.isMonotonic(new double[] {-15, -5.5, -1, -1, 2, 15},
231 MathArrays.OrderDirection.INCREASING, true));
232 Assert.assertTrue(MathArrays.isMonotonic(new double[] {-15, -5.5, -1, 0, 2, 15},
233 MathArrays.OrderDirection.INCREASING, true));
234 Assert.assertFalse(MathArrays.isMonotonic(new double[] {-15, -5.5, -1, -2, 2},
235 MathArrays.OrderDirection.INCREASING, false));
236 Assert.assertTrue(MathArrays.isMonotonic(new double[] {-15, -5.5, -1, -1, 2},
237 MathArrays.OrderDirection.INCREASING, false));
238 Assert.assertFalse(MathArrays.isMonotonic(new double[] {3, 3, -5.5, -11, -27.5},
239 MathArrays.OrderDirection.DECREASING, true));
240 Assert.assertTrue(MathArrays.isMonotonic(new double[] {3, 2, -5.5, -11, -27.5},
241 MathArrays.OrderDirection.DECREASING, true));
242 Assert.assertFalse(MathArrays.isMonotonic(new double[] {3, -1, 0, -5.5, -11, -27.5},
243 MathArrays.OrderDirection.DECREASING, false));
244 Assert.assertTrue(MathArrays.isMonotonic(new double[] {3, 0, 0, -5.5, -11, -27.5},
245 MathArrays.OrderDirection.DECREASING, false));
246 }
247
248 @Test
249 public void testIsMonotonicComparable() {
250 Assert.assertFalse(MathArrays.isMonotonic(new Double[] {Double.valueOf(-15),
251 Double.valueOf(-5.5),
252 Double.valueOf(-1),
253 Double.valueOf(-1),
254 Double.valueOf(2),
255 Double.valueOf(15) },
256 MathArrays.OrderDirection.INCREASING, true));
257 Assert.assertTrue(MathArrays.isMonotonic(new Double[] {Double.valueOf(-15),
258 Double.valueOf(-5.5),
259 Double.valueOf(-1),
260 Double.valueOf(0),
261 Double.valueOf(2),
262 Double.valueOf(15) },
263 MathArrays.OrderDirection.INCREASING, true));
264 Assert.assertFalse(MathArrays.isMonotonic(new Double[] {Double.valueOf(-15),
265 Double.valueOf(-5.5),
266 Double.valueOf(-1),
267 Double.valueOf(-2),
268 Double.valueOf(2) },
269 MathArrays.OrderDirection.INCREASING, false));
270 Assert.assertTrue(MathArrays.isMonotonic(new Double[] {Double.valueOf(-15),
271 Double.valueOf(-5.5),
272 Double.valueOf(-1),
273 Double.valueOf(-1),
274 Double.valueOf(2) },
275 MathArrays.OrderDirection.INCREASING, false));
276 Assert.assertFalse(MathArrays.isMonotonic(new Double[] {Double.valueOf(3),
277 Double.valueOf(3),
278 Double.valueOf(-5.5),
279 Double.valueOf(-11),
280 Double.valueOf(-27.5) },
281 MathArrays.OrderDirection.DECREASING, true));
282 Assert.assertTrue(MathArrays.isMonotonic(new Double[] {Double.valueOf(3),
283 Double.valueOf(2),
284 Double.valueOf(-5.5),
285 Double.valueOf(-11),
286 Double.valueOf(-27.5) },
287 MathArrays.OrderDirection.DECREASING, true));
288 Assert.assertFalse(MathArrays.isMonotonic(new Double[] {Double.valueOf(3),
289 Double.valueOf(-1),
290 Double.valueOf(0),
291 Double.valueOf(-5.5),
292 Double.valueOf(-11),
293 Double.valueOf(-27.5) },
294 MathArrays.OrderDirection.DECREASING, false));
295 Assert.assertTrue(MathArrays.isMonotonic(new Double[] {Double.valueOf(3),
296 Double.valueOf(0),
297 Double.valueOf(0),
298 Double.valueOf(-5.5),
299 Double.valueOf(-11),
300 Double.valueOf(-27.5) },
301 MathArrays.OrderDirection.DECREASING, false));
302 }
303
304 @Test
305 public void testCheckRectangular() {
306 final long[][] rect = new long[][] {{0, 1}, {2, 3}};
307 final long[][] ragged = new long[][] {{0, 1}, {2}};
308 final long[][] nullArray = null;
309 final long[][] empty = new long[][] {};
310 MathArrays.checkRectangular(rect);
311 MathArrays.checkRectangular(empty);
312 try {
313 MathArrays.checkRectangular(ragged);
314 Assert.fail("Expecting DimensionMismatchException");
315 } catch (DimensionMismatchException ex) {
316
317 }
318 try {
319 MathArrays.checkRectangular(nullArray);
320 Assert.fail("Expecting NullArgumentException");
321 } catch (NullArgumentException ex) {
322
323 }
324 }
325
326 @Test
327 public void testCheckPositive() {
328 final double[] positive = new double[] {1, 2, 3};
329 final double[] nonNegative = new double[] {0, 1, 2};
330 final double[] nullArray = null;
331 final double[] empty = new double[] {};
332 MathArrays.checkPositive(positive);
333 MathArrays.checkPositive(empty);
334 try {
335 MathArrays.checkPositive(nullArray);
336 Assert.fail("Expecting NullPointerException");
337 } catch (NullPointerException ex) {
338
339 }
340 try {
341 MathArrays.checkPositive(nonNegative);
342 Assert.fail("Expecting NotStrictlyPositiveException");
343 } catch (NotStrictlyPositiveException ex) {
344
345 }
346 }
347
348 @Test
349 public void testCheckNonNegative() {
350 final long[] nonNegative = new long[] {0, 1};
351 final long[] hasNegative = new long[] {-1};
352 final long[] nullArray = null;
353 final long[] empty = new long[] {};
354 MathArrays.checkNonNegative(nonNegative);
355 MathArrays.checkNonNegative(empty);
356 try {
357 MathArrays.checkNonNegative(nullArray);
358 Assert.fail("Expecting NullPointerException");
359 } catch (NullPointerException ex) {
360
361 }
362 try {
363 MathArrays.checkNonNegative(hasNegative);
364 Assert.fail("Expecting NotPositiveException");
365 } catch (NotPositiveException ex) {
366
367 }
368 }
369
370 @Test
371 public void testCheckNonNegative2D() {
372 final long[][] nonNegative = new long[][] {{0, 1}, {1, 0}};
373 final long[][] hasNegative = new long[][] {{-1}, {0}};
374 final long[][] nullArray = null;
375 final long[][] empty = new long[][] {};
376 MathArrays.checkNonNegative(nonNegative);
377 MathArrays.checkNonNegative(empty);
378 try {
379 MathArrays.checkNonNegative(nullArray);
380 Assert.fail("Expecting NullPointerException");
381 } catch (NullPointerException ex) {
382
383 }
384 try {
385 MathArrays.checkNonNegative(hasNegative);
386 Assert.fail("Expecting NotPositiveException");
387 } catch (NotPositiveException ex) {
388
389 }
390 }
391
392 @Test
393 public void testCheckNotNaN() {
394 final double[] withoutNaN = {Double.NEGATIVE_INFINITY,
395 -Double.MAX_VALUE,
396 -1, 0,
397 Double.MIN_VALUE,
398 JdkMath.ulp(1d),
399 1, 3, 113, 4769,
400 Double.MAX_VALUE,
401 Double.POSITIVE_INFINITY };
402
403 final double[] withNaN = {Double.NEGATIVE_INFINITY,
404 -Double.MAX_VALUE,
405 -1, 0,
406 Double.MIN_VALUE,
407 JdkMath.ulp(1d),
408 1, 3, 113, 4769,
409 Double.MAX_VALUE,
410 Double.POSITIVE_INFINITY,
411 Double.NaN };
412
413
414 final double[] nullArray = null;
415 final double[] empty = new double[] {};
416 MathArrays.checkNotNaN(withoutNaN);
417 MathArrays.checkNotNaN(empty);
418 try {
419 MathArrays.checkNotNaN(nullArray);
420 Assert.fail("Expecting NullPointerException");
421 } catch (NullPointerException ex) {
422
423 }
424 try {
425 MathArrays.checkNotNaN(withNaN);
426 Assert.fail("Expecting NotANumberException");
427 } catch (NotANumberException ex) {
428
429 }
430 }
431
432 @Test(expected = DimensionMismatchException.class)
433 public void testCheckEqualLength1() {
434 MathArrays.checkEqualLength(new double[] {1, 2, 3},
435 new double[] {1, 2, 3, 4});
436 }
437
438 @Test
439 public void testCheckEqualLength2() {
440 final double[] a = new double[] {-1, -12, -23, -34};
441 final double[] b = new double[] {56, 67, 78, 89};
442 Assert.assertTrue(MathArrays.checkEqualLength(a, b, false));
443 }
444
445 @Test
446 public void testArrayEquals() {
447 Assert.assertFalse(MathArrays.equals(new double[] {1d}, null));
448 Assert.assertFalse(MathArrays.equals(null, new double[] {1d}));
449 Assert.assertTrue(MathArrays.equals((double[]) null, (double[]) null));
450
451 Assert.assertFalse(MathArrays.equals(new double[] {1d}, new double[0]));
452 Assert.assertTrue(MathArrays.equals(new double[] {1d}, new double[] {1d}));
453 Assert.assertTrue(MathArrays.equals(new double[] {Double.POSITIVE_INFINITY,
454 Double.NEGATIVE_INFINITY, 1d, 0d},
455 new double[] {Double.POSITIVE_INFINITY,
456 Double.NEGATIVE_INFINITY, 1d, 0d}));
457 Assert.assertFalse(MathArrays.equals(new double[] {Double.NaN},
458 new double[] {Double.NaN}));
459 Assert.assertFalse(MathArrays.equals(new double[] {Double.POSITIVE_INFINITY},
460 new double[] {Double.NEGATIVE_INFINITY}));
461 Assert.assertFalse(MathArrays.equals(new double[] {1d},
462 new double[] {JdkMath.nextAfter(JdkMath.nextAfter(1d, 2d), 2d)}));
463 }
464
465 @Test
466 public void testArrayEqualsIncludingNaN() {
467 Assert.assertFalse(MathArrays.equalsIncludingNaN(new double[] {1d}, null));
468 Assert.assertFalse(MathArrays.equalsIncludingNaN(null, new double[] {1d}));
469 Assert.assertTrue(MathArrays.equalsIncludingNaN((double[]) null, (double[]) null));
470
471 Assert.assertFalse(MathArrays.equalsIncludingNaN(new double[] {1d}, new double[0]));
472 Assert.assertTrue(MathArrays.equalsIncludingNaN(new double[] {1d}, new double[] {1d}));
473 Assert.assertTrue(MathArrays.equalsIncludingNaN(new double[] {Double.NaN, Double.POSITIVE_INFINITY,
474 Double.NEGATIVE_INFINITY, 1d, 0d},
475 new double[] {Double.NaN, Double.POSITIVE_INFINITY,
476 Double.NEGATIVE_INFINITY, 1d, 0d}));
477 Assert.assertFalse(MathArrays.equalsIncludingNaN(new double[] {Double.POSITIVE_INFINITY},
478 new double[] {Double.NEGATIVE_INFINITY}));
479 Assert.assertFalse(MathArrays.equalsIncludingNaN(new double[] {1d},
480 new double[] {JdkMath.nextAfter(JdkMath.nextAfter(1d, 2d), 2d)}));
481 }
482
483 @Test
484 public void testNormalizeArray() {
485 double[] testValues1 = new double[] {1, 1, 2};
486 Assert.assertArrayEquals(new double[] {.25, .25, .5},
487 MathArrays.normalizeArray(testValues1, 1),
488 Double.MIN_VALUE);
489
490 double[] testValues2 = new double[] {-1, -1, 1};
491 Assert.assertArrayEquals(new double[] {1, 1, -1},
492 MathArrays.normalizeArray(testValues2, 1),
493 Double.MIN_VALUE);
494
495
496 double[] testValues3 = new double[] {-1, -1, Double.NaN, 1, Double.NaN};
497 Assert.assertArrayEquals(new double[] {1, 1, Double.NaN, -1, Double.NaN},
498 MathArrays.normalizeArray(testValues3, 1),
499 Double.MIN_VALUE);
500
501
502 double[] zeroSum = new double[] {-1, 1};
503 try {
504 MathArrays.normalizeArray(zeroSum, 1);
505 Assert.fail("expecting MathArithmeticException");
506 } catch (MathArithmeticException ex) { }
507
508
509 double[] hasInf = new double[] {1, 2, 1, Double.NEGATIVE_INFINITY};
510 try {
511 MathArrays.normalizeArray(hasInf, 1);
512 Assert.fail("expecting MathIllegalArgumentException");
513 } catch (MathIllegalArgumentException ex) { }
514
515
516 try {
517 MathArrays.normalizeArray(testValues1, Double.POSITIVE_INFINITY);
518 Assert.fail("expecting MathIllegalArgumentException");
519 } catch (MathIllegalArgumentException ex) { }
520
521
522 try {
523 MathArrays.normalizeArray(testValues1, Double.NaN);
524 Assert.fail("expecting MathIllegalArgumentException");
525 } catch (MathIllegalArgumentException ex) { }
526 }
527
528 @Test
529 public void testConvolve() {
530
531
532
533
534
535 double[] x1 = {1.2, -1.8, 1.4};
536 double[] h1 = {1, 0.8, 0.5, 0.3};
537 double[] y1 = {1.2, -0.84, 0.56, 0.58, 0.16, 0.42};
538 double tolerance = 1e-13;
539
540 double[] yActual = MathArrays.convolve(x1, h1);
541 Assert.assertArrayEquals(y1, yActual, tolerance);
542
543 double[] x2 = {1, 2, 3};
544 double[] h2 = {0, 1, 0.5};
545 double[] y2 = {0, 1, 2.5, 4, 1.5};
546
547 yActual = MathArrays.convolve(x2, h2);
548 Assert.assertArrayEquals(y2, yActual, tolerance);
549
550 try {
551 MathArrays.convolve(new double[]{1, 2}, null);
552 Assert.fail("an exception should have been thrown");
553 } catch (NullArgumentException e) {
554
555 }
556
557 try {
558 MathArrays.convolve(null, new double[]{1, 2});
559 Assert.fail("an exception should have been thrown");
560 } catch (NullArgumentException e) {
561
562 }
563
564 try {
565 MathArrays.convolve(new double[]{1, 2}, new double[]{});
566 Assert.fail("an exception should have been thrown");
567 } catch (NoDataException e) {
568
569 }
570
571 try {
572 MathArrays.convolve(new double[]{}, new double[]{1, 2});
573 Assert.fail("an exception should have been thrown");
574 } catch (NoDataException e) {
575
576 }
577
578 try {
579 MathArrays.convolve(new double[]{}, new double[]{});
580 Assert.fail("an exception should have been thrown");
581 } catch (NoDataException e) {
582
583 }
584 }
585
586 @Test
587 public void testNatural() {
588 final int n = 4;
589 final int[] expected = {0, 1, 2, 3};
590
591 final int[] natural = MathArrays.natural(n);
592 for (int i = 0; i < n; i++) {
593 Assert.assertEquals(expected[i], natural[i]);
594 }
595 }
596
597 @Test
598 public void testNaturalZero() {
599 final int[] natural = MathArrays.natural(0);
600 Assert.assertEquals(0, natural.length);
601 }
602
603 @Test
604 public void testSequence() {
605 final int size = 4;
606 final int start = 5;
607 final int stride = 2;
608 final int[] expected = {5, 7, 9, 11};
609
610 final int[] seq = MathArrays.sequence(size, start, stride);
611 for (int i = 0; i < size; i++) {
612 Assert.assertEquals(expected[i], seq[i]);
613 }
614 }
615
616 @Test
617 public void testSequenceZero() {
618 final int[] seq = MathArrays.sequence(0, 12345, 6789);
619 Assert.assertEquals(0, seq.length);
620 }
621
622 @Test
623 public void testVerifyValuesPositive() {
624 for (int j = 0; j < 6; j++) {
625 for (int i = 1; i < (7 - j); i++) {
626 Assert.assertTrue(MathArrays.verifyValues(testArray, 0, i));
627 }
628 }
629 Assert.assertTrue(MathArrays.verifyValues(singletonArray, 0, 1));
630 Assert.assertTrue(MathArrays.verifyValues(singletonArray, 0, 0, true));
631 }
632
633 @Test
634 public void testVerifyValuesNegative() {
635 final double[] nullArray = null;
636 Assert.assertFalse(MathArrays.verifyValues(singletonArray, 0, 0));
637 Assert.assertFalse(MathArrays.verifyValues(testArray, 0, 0));
638 Assert.assertTrue(MathArrays.verifyValues(testArray, 0, 0, true));
639 Assert.assertFalse(MathArrays.verifyValues(testArray, testWeightsArray, 0, 0));
640 Assert.assertTrue(MathArrays.verifyValues(testArray, testWeightsArray, 0, 0, true));
641 try {
642 MathArrays.verifyValues(singletonArray, 2, 1);
643 Assert.fail("Expecting MathIllegalArgumentException");
644 } catch (MathIllegalArgumentException ex) {
645
646 }
647 try {
648 MathArrays.verifyValues(testArray, 0, 7);
649 Assert.fail("Expecting MathIllegalArgumentException");
650 } catch (MathIllegalArgumentException ex) {
651
652 }
653 try {
654 MathArrays.verifyValues(testArray, -1, 1);
655 Assert.fail("Expecting MathIllegalArgumentException");
656 } catch (MathIllegalArgumentException ex) {
657
658 }
659 try {
660 MathArrays.verifyValues(testArray, 0, -1);
661 Assert.fail("Expecting MathIllegalArgumentException");
662 } catch (MathIllegalArgumentException ex) {
663
664 }
665 try {
666 MathArrays.verifyValues(nullArray, 0, 1);
667 Assert.fail("Expecting NullArgumentException");
668 } catch (NullArgumentException ex) {
669
670 }
671 try {
672 MathArrays.verifyValues(testArray, nullArray, 0, 1);
673 Assert.fail("Expecting NullArgumentException");
674 } catch (NullArgumentException ex) {
675
676 }
677 try {
678 MathArrays.verifyValues(singletonArray, testWeightsArray, 0, 1);
679 Assert.fail("Expecting MathIllegalArgumentException");
680 } catch (MathIllegalArgumentException ex) {
681
682 }
683 try {
684 MathArrays.verifyValues(testArray, testNegativeWeightsArray, 0, 6);
685 Assert.fail("Expecting MathIllegalArgumentException");
686 } catch (MathIllegalArgumentException ex) {
687
688 }
689 try {
690 MathArrays.verifyValues(testArray, new double[testArray.length], 0, 6);
691 Assert.fail("Expecting MathIllegalArgumentException");
692 } catch (MathIllegalArgumentException ex) {
693
694 }
695 }
696
697 @Test
698 public void testConcatenate() {
699 final double[] u = new double[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
700 final double[] x = new double[] {0, 1, 2};
701 final double[] y = new double[] {3, 4, 5, 6, 7, 8};
702 final double[] z = new double[] {9};
703 Assert.assertArrayEquals(u, MathArrays.concatenate(x, y, z), 0);
704 }
705
706 @Test
707 public void testConcatenateSingle() {
708 final double[] x = new double[] {0, 1, 2};
709 Assert.assertArrayEquals(x, MathArrays.concatenate(x), 0);
710 }
711
712 public void testConcatenateEmptyArguments() {
713 final double[] x = new double[] {0, 1, 2};
714 final double[] y = new double[] {3};
715 final double[] z = new double[] {};
716 final double[] u = new double[] {0, 1, 2, 3};
717 Assert.assertArrayEquals(u, MathArrays.concatenate(x, z, y), 0);
718 Assert.assertArrayEquals(u, MathArrays.concatenate(x, y, z), 0);
719 Assert.assertArrayEquals(u, MathArrays.concatenate(z, x, y), 0);
720 Assert.assertEquals(0, MathArrays.concatenate(z, z, z).length);
721 }
722
723 @Test(expected = NullPointerException.class)
724 public void testConcatenateNullArguments() {
725 final double[] x = new double[] {0, 1, 2};
726 MathArrays.concatenate(x, null);
727 }
728
729 @Test
730 public void testUnique() {
731 final double[] x = {0, 9, 3, 0, 11, 7, 3, 5, -1, -2};
732 final double[] values = {11, 9, 7, 5, 3, 0, -1, -2};
733 Assert.assertArrayEquals(values, MathArrays.unique(x), 0);
734 }
735
736 @Test
737 public void testUniqueInfiniteValues() {
738 final double[] x = {0, Double.NEGATIVE_INFINITY, 3, Double.NEGATIVE_INFINITY,
739 3, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY};
740 final double[] u = {Double.POSITIVE_INFINITY, 3, 0, Double.NEGATIVE_INFINITY};
741 Assert.assertArrayEquals(u, MathArrays.unique(x), 0);
742 }
743
744 @Test
745 public void testUniqueNaNValues() {
746 final double[] x = new double[] {10, 2, Double.NaN, Double.NaN, Double.NaN,
747 Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY};
748 final double[] u = MathArrays.unique(x);
749 Assert.assertEquals(5, u.length);
750 Assert.assertTrue(Double.isNaN(u[0]));
751 Assert.assertEquals(Double.POSITIVE_INFINITY, u[1], 0);
752 Assert.assertEquals(10, u[2], 0);
753 Assert.assertEquals(2, u[3], 0);
754 Assert.assertEquals(Double.NEGATIVE_INFINITY, u[4], 0);
755 }
756
757 @Test(expected = NullPointerException.class)
758 public void testUniqueNullArgument() {
759 MathArrays.unique(null);
760 }
761
762 @Test
763 public void testCheckFinite() {
764 try {
765 MathArrays.checkFinite(new double[] {0, -1, Double.POSITIVE_INFINITY, -2, 3});
766 Assert.fail("an exception should have been thrown");
767 } catch (NotFiniteNumberException e) {
768
769 }
770 try {
771 MathArrays.checkFinite(new double[] {1, Double.NEGATIVE_INFINITY, -2, 3});
772 Assert.fail("an exception should have been thrown");
773 } catch (NotFiniteNumberException e) {
774
775 }
776 try {
777 MathArrays.checkFinite(new double[] {4, 3, -1, Double.NaN, -2, 1});
778 Assert.fail("an exception should have been thrown");
779 } catch (NotFiniteNumberException e) {
780
781 }
782 }
783 }