View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with this
4    * work for additional information regarding copyright ownership. The ASF
5    * licenses this file to You under the Apache License, Version 2.0 (the
6    * "License"); you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * https://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
9    * or agreed to in writing, software distributed under the License is
10   * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11   * KIND, either express or implied. See the License for the specific language
12   * governing permissions and limitations under the License.
13   */
14  package org.hipparchus.util;
15  
16  import org.hipparchus.UnitTestUtils;
17  import org.hipparchus.complex.Complex;
18  import org.hipparchus.exception.LocalizedCoreFormats;
19  import org.hipparchus.exception.MathIllegalArgumentException;
20  import org.hipparchus.exception.MathRuntimeException;
21  import org.hipparchus.exception.NullArgumentException;
22  import org.hipparchus.random.Well1024a;
23  import org.junit.jupiter.api.Assertions;
24  import org.junit.jupiter.api.Test;
25  
26  import java.util.Arrays;
27  
28  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
29  import static org.junit.jupiter.api.Assertions.assertEquals;
30  import static org.junit.jupiter.api.Assertions.assertFalse;
31  import static org.junit.jupiter.api.Assertions.assertNull;
32  import static org.junit.jupiter.api.Assertions.assertSame;
33  import static org.junit.jupiter.api.Assertions.assertThrows;
34  import static org.junit.jupiter.api.Assertions.assertTrue;
35  import static org.junit.jupiter.api.Assertions.fail;
36  
37  /**
38   * Test cases for the {@link MathArrays} class.
39   */
40  public class MathArraysTest {
41  
42      private final double[] testArray = {0, 1, 2, 3, 4, 5};
43      private final double[] testWeightsArray = {0.3, 0.2, 1.3, 1.1, 1.0, 1.8};
44      private final double[] testNegativeWeightsArray = {-0.3, 0.2, -1.3, 1.1, 1.0, 1.8};
45      private final double[] nullArray = null;
46      private final double[] singletonArray = {0};
47  
48      @Test
49      void testScale() {
50          final double[] test = new double[] { -2.5, -1, 0, 1, 2.5 };
51          final double[] correctTest = test.clone();
52          final double[] correctScaled = new double[]{5.25, 2.1, 0, -2.1, -5.25};
53  
54          final double[] scaled = MathArrays.scale(-2.1, test);
55  
56          // Make sure test has not changed
57          for (int i = 0; i < test.length; i++) {
58              assertEquals(correctTest[i], test[i], 0);
59          }
60  
61          // Test scaled values
62          for (int i = 0; i < scaled.length; i++) {
63              assertEquals(correctScaled[i], scaled[i], 0);
64          }
65      }
66  
67      @Test
68      void testScaleInPlace() {
69          final double[] test = new double[] { -2.5, -1, 0, 1, 2.5 };
70          final double[] correctScaled = new double[]{5.25, 2.1, 0, -2.1, -5.25};
71          MathArrays.scaleInPlace(-2.1, test);
72  
73          // Make sure test has changed
74          for (int i = 0; i < test.length; i++) {
75              assertEquals(correctScaled[i], test[i], 0);
76          }
77      }
78  
79      @Test
80      void testEbeAddPrecondition() {
81          assertThrows(MathIllegalArgumentException.class, () -> MathArrays.ebeAdd(new double[3], new double[4]));
82      }
83  
84      @Test
85      void testEbeSubtractPrecondition() {
86          assertThrows(MathIllegalArgumentException.class, () -> MathArrays.ebeSubtract(new double[3], new double[4]));
87      }
88  
89      @Test
90      void testEbeMultiplyPrecondition() {
91          assertThrows(MathIllegalArgumentException.class, () -> MathArrays.ebeMultiply(new double[3], new double[4]));
92      }
93  
94      @Test
95      void testEbeDividePrecondition() {
96          assertThrows(MathIllegalArgumentException.class, () -> MathArrays.ebeDivide(new double[3], new double[4]));
97      }
98  
99      @Test
100     void testEbeAdd() {
101         final double[] a = { 0, 1, 2 };
102         final double[] b = { 3, 5, 7 };
103         final double[] r = MathArrays.ebeAdd(a, b);
104 
105         for (int i = 0; i < a.length; i++) {
106             assertEquals(a[i] + b[i], r[i], 0);
107         }
108     }
109 
110     @Test
111     void testEbeSubtract() {
112         final double[] a = { 0, 1, 2 };
113         final double[] b = { 3, 5, 7 };
114         final double[] r = MathArrays.ebeSubtract(a, b);
115 
116         for (int i = 0; i < a.length; i++) {
117             assertEquals(a[i] - b[i], r[i], 0);
118         }
119     }
120 
121     @Test
122     void testEbeMultiply() {
123         final double[] a = { 0, 1, 2 };
124         final double[] b = { 3, 5, 7 };
125         final double[] r = MathArrays.ebeMultiply(a, b);
126 
127         for (int i = 0; i < a.length; i++) {
128             assertEquals(a[i] * b[i], r[i], 0);
129         }
130     }
131 
132     @Test
133     void testEbeDivide() {
134         final double[] a = { 0, 1, 2 };
135         final double[] b = { 3, 5, 7 };
136         final double[] r = MathArrays.ebeDivide(a, b);
137 
138         for (int i = 0; i < a.length; i++) {
139             assertEquals(a[i] / b[i], r[i], 0);
140         }
141     }
142 
143     @Test
144     void testL1DistanceDouble() {
145         double[] p1 = { 2.5,  0.0 };
146         double[] p2 = { -0.5, 4.0 };
147         assertTrue(Precision.equals(7.0, MathArrays.distance1(p1, p2), 1));
148     }
149 
150     @Test
151     void testL1DistanceInt() {
152         int[] p1 = { 3, 0 };
153         int[] p2 = { 0, 4 };
154         assertEquals(7, MathArrays.distance1(p1, p2));
155     }
156 
157     @Test
158     void testL2DistanceDouble() {
159         double[] p1 = { 2.5,  0.0 };
160         double[] p2 = { -0.5, 4.0 };
161         assertTrue(Precision.equals(5.0, MathArrays.distance(p1, p2), 1));
162     }
163 
164     @Test
165     void testL2DistanceInt() {
166         int[] p1 = { 3, 0 };testArrayEquals();
167         int[] p2 = { 0, 4 };
168         assertTrue(Precision.equals(5, MathArrays.distance(p1, p2), 1));
169     }
170 
171     @Test
172     void testLInfDistanceDouble() {
173         double[] p1 = { 2.5,  0.0 };
174         double[] p2 = { -0.5, 4.0 };
175         assertTrue(Precision.equals(4.0, MathArrays.distanceInf(p1, p2), 1));
176     }
177 
178     @Test
179     void testLInfDistanceInt() {
180         int[] p1 = { 3, 0 };
181         int[] p2 = { 0, 4 };
182         assertEquals(4, MathArrays.distanceInf(p1, p2));
183     }
184 
185     @Test
186     void testCosAngle2D() {
187         double expected;
188 
189         final double[] v1 = { 1, 0 };
190         expected = 1;
191         assertEquals(expected, MathArrays.cosAngle(v1, v1), 0d);
192 
193         final double[] v2 = { 0, 1 };
194         expected = 0;
195         assertEquals(expected, MathArrays.cosAngle(v1, v2), 0d);
196 
197         final double[] v3 = { 7, 7 };
198         expected = Math.sqrt(2) / 2;
199         assertEquals(expected, MathArrays.cosAngle(v1, v3), 1e-15);
200         assertEquals(expected, MathArrays.cosAngle(v3, v2), 1e-15);
201 
202         final double[] v4 = { -5, 0 };
203         expected = -1;
204         assertEquals(expected, MathArrays.cosAngle(v1, v4), 0);
205 
206         final double[] v5 = { -100, 100 };
207         expected = 0;
208         assertEquals(expected, MathArrays.cosAngle(v3, v5), 0);
209     }
210 
211     @Test
212     void testCosAngle3D() {
213         double expected;
214 
215         final double[] v1 = { 1, 1, 0 };
216         expected = 1;
217         assertEquals(expected, MathArrays.cosAngle(v1, v1), 1e-15);
218 
219         final double[] v2 = { 1, 1, 1 };
220         expected = Math.sqrt(2) / Math.sqrt(3);
221         assertEquals(expected, MathArrays.cosAngle(v1, v2), 1e-15);
222     }
223 
224     @Test
225     void testCosAngleExtreme() {
226         double expected;
227 
228         final double tiny = 1e-200;
229         final double[] v1 = { tiny, tiny };
230         final double big = 1e200;
231         final double[] v2 = { -big, -big };
232         expected = -1;
233         assertEquals(expected, MathArrays.cosAngle(v1, v2), 1e-15);
234 
235         final double[] v3 = { big, -big };
236         expected = 0;
237         assertEquals(expected, MathArrays.cosAngle(v1, v3), 1e-15);
238     }
239 
240     @Test
241     void testCheckOrder() {
242         MathArrays.checkOrder(new double[] {-15, -5.5, -1, 2, 15},
243                              MathArrays.OrderDirection.INCREASING, true);
244         MathArrays.checkOrder(new double[] {-15, -5.5, -1, 2, 2},
245                              MathArrays.OrderDirection.INCREASING, false);
246         MathArrays.checkOrder(new double[] {3, -5.5, -11, -27.5},
247                              MathArrays.OrderDirection.DECREASING, true);
248         MathArrays.checkOrder(new double[] {3, 0, 0, -5.5, -11, -27.5},
249                              MathArrays.OrderDirection.DECREASING, false);
250 
251         try {
252             MathArrays.checkOrder(new double[] {-15, -5.5, -1, -1, 2, 15},
253                                  MathArrays.OrderDirection.INCREASING, true);
254             fail("an exception should have been thrown");
255         } catch (MathIllegalArgumentException e) {
256             // Expected
257         }
258         try {
259             MathArrays.checkOrder(new double[] {-15, -5.5, -1, -2, 2},
260                                  MathArrays.OrderDirection.INCREASING, false);
261             fail("an exception should have been thrown");
262         } catch (MathIllegalArgumentException e) {
263             // Expected
264         }
265         try {
266             MathArrays.checkOrder(new double[] {3, 3, -5.5, -11, -27.5},
267                                  MathArrays.OrderDirection.DECREASING, true);
268             fail("an exception should have been thrown");
269         } catch (MathIllegalArgumentException e) {
270             // Expected
271         }
272         try {
273             MathArrays.checkOrder(new double[] {3, -1, 0, -5.5, -11, -27.5},
274                                  MathArrays.OrderDirection.DECREASING, false);
275             fail("an exception should have been thrown");
276         } catch (MathIllegalArgumentException e) {
277             // Expected
278         }
279         try {
280             MathArrays.checkOrder(new double[] {3, 0, -5.5, -11, -10},
281                                  MathArrays.OrderDirection.DECREASING, false);
282             fail("an exception should have been thrown");
283         } catch (MathIllegalArgumentException e) {
284             // Expected
285         }
286     }
287 
288     @Test
289     void testIsMonotonic() {
290         assertFalse(MathArrays.isMonotonic(new double[] { -15, -5.5, -1, -1, 2, 15 },
291                                                   MathArrays.OrderDirection.INCREASING, true));
292         assertTrue(MathArrays.isMonotonic(new double[] { -15, -5.5, -1, 0, 2, 15 },
293                                                  MathArrays.OrderDirection.INCREASING, true));
294         assertFalse(MathArrays.isMonotonic(new double[] { -15, -5.5, -1, -2, 2 },
295                                                   MathArrays.OrderDirection.INCREASING, false));
296         assertTrue(MathArrays.isMonotonic(new double[] { -15, -5.5, -1, -1, 2 },
297                                                  MathArrays.OrderDirection.INCREASING, false));
298         assertFalse(MathArrays.isMonotonic(new double[] { 3, 3, -5.5, -11, -27.5 },
299                                                   MathArrays.OrderDirection.DECREASING, true));
300         assertTrue(MathArrays.isMonotonic(new double[] { 3, 2, -5.5, -11, -27.5 },
301                                                  MathArrays.OrderDirection.DECREASING, true));
302         assertFalse(MathArrays.isMonotonic(new double[] { 3, -1, 0, -5.5, -11, -27.5 },
303                                                   MathArrays.OrderDirection.DECREASING, false));
304         assertTrue(MathArrays.isMonotonic(new double[] { 3, 0, 0, -5.5, -11, -27.5 },
305                                                  MathArrays.OrderDirection.DECREASING, false));
306     }
307 
308     @Test
309     void testIsMonotonicComparable() {
310         assertFalse(MathArrays.isMonotonic(new Double[] { -15.0, -5.5, -1.0, -1.0, 2.0, 15.0 },
311                 MathArrays.OrderDirection.INCREASING, true));
312         assertTrue(MathArrays.isMonotonic(new Double[] { -15.0, -5.5, -1.0, 0.0, 2.0, 15.0 },
313                 MathArrays.OrderDirection.INCREASING, true));
314         assertFalse(MathArrays.isMonotonic(new Double[] { -15.0, -5.5, -1.0, -2.0, 2.0 },
315                 MathArrays.OrderDirection.INCREASING, false));
316         assertTrue(MathArrays.isMonotonic(new Double[] { -15.0, -5.5, -1.0, -1.0, 2.0 },
317                 MathArrays.OrderDirection.INCREASING, false));
318         assertFalse(MathArrays.isMonotonic(new Double[] { 3.0, 3.0, -5.5, -11.0, -27.5 },
319                 MathArrays.OrderDirection.DECREASING, true));
320         assertTrue(MathArrays.isMonotonic(new Double[] { 3.0, 2.0, -5.5, -11.0, -27.5 },
321                 MathArrays.OrderDirection.DECREASING, true));
322         assertFalse(MathArrays.isMonotonic(new Double[] { 3.0, -1.0, 0.0, -5.5, -11.0, 27.5 },
323                 MathArrays.OrderDirection.DECREASING, false));
324         assertTrue(MathArrays.isMonotonic(new Double[] { 3.0, 0.0, 0.0, -5.5, -11.0, -27.5 },
325                 MathArrays.OrderDirection.DECREASING, false));
326     }
327 
328     @Test
329     void testCheckRectangular() {
330         final long[][] rect = new long[][] {{0, 1}, {2, 3}};
331         final long[][] ragged = new long[][] {{0, 1}, {2}};
332         final long[][] nullArray = null;
333         final long[][] empty = new long[][] {};
334         MathArrays.checkRectangular(rect);
335         MathArrays.checkRectangular(empty);
336         try {
337             MathArrays.checkRectangular(ragged);
338             fail("Expecting MathIllegalArgumentException");
339         } catch (MathIllegalArgumentException ex) {
340             // Expected
341         }
342         try {
343             MathArrays.checkRectangular(nullArray);
344             fail("Expecting NullArgumentException");
345         } catch (NullArgumentException ex) {
346             // Expected
347         }
348     }
349 
350     @Test
351     void testCheckPositive() {
352         final double[] positive = new double[] {1, 2, 3};
353         final double[] nonNegative = new double[] {0, 1, 2};
354         final double[] nullArray = null;
355         final double[] empty = new double[] {};
356         MathArrays.checkPositive(positive);
357         MathArrays.checkPositive(empty);
358         try {
359             MathArrays.checkPositive(nullArray);
360             fail("Expecting NullPointerException");
361         } catch (NullPointerException ex) {
362             // Expected
363         }
364         try {
365             MathArrays.checkPositive(nonNegative);
366             fail("Expecting MathIllegalArgumentException");
367         } catch (MathIllegalArgumentException ex) {
368             // Expected
369         }
370     }
371 
372     @Test
373     void testCheckNonNegative() {
374         final long[] nonNegative = new long[] {0, 1};
375         final long[] hasNegative = new long[] {-1};
376         final long[] nullArray = null;
377         final long[] empty = new long[] {};
378         MathArrays.checkNonNegative(nonNegative);
379         MathArrays.checkNonNegative(empty);
380         try {
381             MathArrays.checkNonNegative(nullArray);
382             fail("Expecting NullPointerException");
383         } catch (NullPointerException ex) {
384             // Expected
385         }
386         try {
387             MathArrays.checkNonNegative(hasNegative);
388             fail("Expecting MathIllegalArgumentException");
389         } catch (MathIllegalArgumentException ex) {
390             // Expected
391         }
392     }
393 
394     @Test
395     void testCheckNonNegative2D() {
396         final long[][] nonNegative = new long[][] {{0, 1}, {1, 0}};
397         final long[][] hasNegative = new long[][] {{-1}, {0}};
398         final long[][] nullArray = null;
399         final long[][] empty = new long[][] {};
400         MathArrays.checkNonNegative(nonNegative);
401         MathArrays.checkNonNegative(empty);
402         try {
403             MathArrays.checkNonNegative(nullArray);
404             fail("Expecting NullPointerException");
405         } catch (NullPointerException ex) {
406             // Expected
407         }
408         try {
409             MathArrays.checkNonNegative(hasNegative);
410             fail("Expecting MathIllegalArgumentException");
411         } catch (MathIllegalArgumentException ex) {
412             // Expected
413         }
414     }
415 
416     @Test
417     void testCheckNotNaN() {
418         final double[] withoutNaN = { Double.NEGATIVE_INFINITY,
419                                       -Double.MAX_VALUE,
420                                       -1, 0,
421                                       Double.MIN_VALUE,
422                                       FastMath.ulp(1d),
423                                       1, 3, 113, 4769,
424                                       Double.MAX_VALUE,
425                                       Double.POSITIVE_INFINITY };
426 
427         final double[] withNaN = { Double.NEGATIVE_INFINITY,
428                                    -Double.MAX_VALUE,
429                                    -1, 0,
430                                    Double.MIN_VALUE,
431                                    FastMath.ulp(1d),
432                                    1, 3, 113, 4769,
433                                    Double.MAX_VALUE,
434                                    Double.POSITIVE_INFINITY,
435                                    Double.NaN };
436 
437 
438         final double[] nullArray = null;
439         final double[] empty = new double[] {};
440         MathArrays.checkNotNaN(withoutNaN);
441         MathArrays.checkNotNaN(empty);
442         try {
443             MathArrays.checkNotNaN(nullArray);
444             fail("Expecting NullPointerException");
445         } catch (NullPointerException ex) {
446             // Expected
447         }
448         try {
449             MathArrays.checkNotNaN(withNaN);
450             fail("Expecting MathIllegalArgumentException");
451         } catch (MathIllegalArgumentException ex) {
452             // Expected
453         }
454     }
455 
456     @Test
457     void testCheckEqualLength1() {
458         assertThrows(MathIllegalArgumentException.class, () -> MathArrays.checkEqualLength(new double[]{1, 2, 3},
459             new double[]{1, 2, 3, 4}));
460     }
461 
462     @Test
463     void testCheckEqualLength2() {
464         final double[] a = new double[] {-1, -12, -23, -34};
465         final double[] b = new double[] {56, 67, 78, 89};
466         assertTrue(MathArrays.checkEqualLength(a, b, false));
467     }
468 
469     @Test
470     void testSortInPlace() {
471         final double[] x1 = {2,   5,  -3, 1,  4};
472         final double[] x2 = {4,  25,   9, 1, 16};
473         final double[] x3 = {8, 125, -27, 1, 64};
474 
475         MathArrays.sortInPlace(x1, x2, x3);
476 
477         assertEquals(-3,  x1[0], FastMath.ulp(1d));
478         assertEquals(9,   x2[0], FastMath.ulp(1d));
479         assertEquals(-27, x3[0], FastMath.ulp(1d));
480 
481         assertEquals(1, x1[1], FastMath.ulp(1d));
482         assertEquals(1, x2[1], FastMath.ulp(1d));
483         assertEquals(1, x3[1], FastMath.ulp(1d));
484 
485         assertEquals(2, x1[2], FastMath.ulp(1d));
486         assertEquals(4, x2[2], FastMath.ulp(1d));
487         assertEquals(8, x3[2], FastMath.ulp(1d));
488 
489         assertEquals(4,  x1[3], FastMath.ulp(1d));
490         assertEquals(16, x2[3], FastMath.ulp(1d));
491         assertEquals(64, x3[3], FastMath.ulp(1d));
492 
493         assertEquals(5,   x1[4], FastMath.ulp(1d));
494         assertEquals(25,  x2[4], FastMath.ulp(1d));
495         assertEquals(125, x3[4], FastMath.ulp(1d));
496     }
497 
498     @Test
499     void testSortInPlaceDecresasingOrder() {
500         final double[] x1 = {2,   5,  -3, 1,  4};
501         final double[] x2 = {4,  25,   9, 1, 16};
502         final double[] x3 = {8, 125, -27, 1, 64};
503 
504         MathArrays.sortInPlace(x1,
505                                MathArrays.OrderDirection.DECREASING,
506                                x2, x3);
507 
508         assertEquals(-3,  x1[4], FastMath.ulp(1d));
509         assertEquals(9,   x2[4], FastMath.ulp(1d));
510         assertEquals(-27, x3[4], FastMath.ulp(1d));
511 
512         assertEquals(1, x1[3], FastMath.ulp(1d));
513         assertEquals(1, x2[3], FastMath.ulp(1d));
514         assertEquals(1, x3[3], FastMath.ulp(1d));
515 
516         assertEquals(2, x1[2], FastMath.ulp(1d));
517         assertEquals(4, x2[2], FastMath.ulp(1d));
518         assertEquals(8, x3[2], FastMath.ulp(1d));
519 
520         assertEquals(4,  x1[1], FastMath.ulp(1d));
521         assertEquals(16, x2[1], FastMath.ulp(1d));
522         assertEquals(64, x3[1], FastMath.ulp(1d));
523 
524         assertEquals(5,   x1[0], FastMath.ulp(1d));
525         assertEquals(25,  x2[0], FastMath.ulp(1d));
526         assertEquals(125, x3[0], FastMath.ulp(1d));
527     }
528 
529     /** Example in javadoc */
530     @Test
531     void testSortInPlaceExample() {
532         final double[] x = {3, 1, 2};
533         final double[] y = {1, 2, 3};
534         final double[] z = {0, 5, 7};
535         MathArrays.sortInPlace(x, y, z);
536         final double[] sx = {1, 2, 3};
537         final double[] sy = {2, 3, 1};
538         final double[] sz = {5, 7, 0};
539         assertArrayEquals(sx, x);
540         assertArrayEquals(sy, y);
541         assertArrayEquals(sz, z);
542     }
543 
544     @Test
545     void testSortInPlaceFailures() {
546         final double[] nullArray = null;
547         final double[] one = {1};
548         final double[] two = {1, 2};
549         final double[] onep = {2};
550         try {
551             MathArrays.sortInPlace(one, two);
552             fail("Expecting MathIllegalArgumentException");
553         } catch (MathIllegalArgumentException ex) {
554             // expected
555         }
556         try {
557             MathArrays.sortInPlace(one, nullArray);
558             fail("Expecting NullArgumentException");
559         } catch (NullArgumentException ex) {
560             // expected
561         }
562         try {
563             MathArrays.sortInPlace(one, onep, nullArray);
564             fail("Expecting NullArgumentException");
565         } catch (NullArgumentException ex) {
566             // expected
567         }
568     }
569 
570     // MATH-1005
571     @Test
572     void testLinearCombinationWithSingleElementArray() {
573         final double[] a = { 1.23456789 };
574         final double[] b = { 98765432.1 };
575 
576         assertEquals(a[0] * b[0], MathArrays.linearCombination(a, b), 0d);
577     }
578 
579     @Test
580     void testLinearCombination1() {
581         final double[] a = new double[] {
582             -1321008684645961.0 / 268435456.0,
583             -5774608829631843.0 / 268435456.0,
584             -7645843051051357.0 / 8589934592.0
585         };
586         final double[] b = new double[] {
587             -5712344449280879.0 / 2097152.0,
588             -4550117129121957.0 / 2097152.0,
589             8846951984510141.0 / 131072.0
590         };
591 
592         final double abSumInline = MathArrays.linearCombination(a[0], b[0],
593                                                                 a[1], b[1],
594                                                                 a[2], b[2]);
595         final double abSumArray = MathArrays.linearCombination(a, b);
596 
597         assertEquals(abSumInline, abSumArray, 0);
598         assertEquals(-1.8551294182586248737720779899, abSumInline, 1.0e-15);
599 
600         final double naive = a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
601         assertTrue(FastMath.abs(naive - abSumInline) > 1.5);
602 
603     }
604 
605     @Test
606     void testLinearCombinationSignedZero() {
607         assertTrue(FastMath.copySign(1, MathArrays.linearCombination(+0.0, 1.0, +0.0, 1.0)) > 0);
608         assertTrue(FastMath.copySign(1, MathArrays.linearCombination(-0.0, 1.0, -0.0, 1.0)) < 0);
609         assertTrue(FastMath.copySign(1, MathArrays.linearCombination(+0.0, 1.0, +0.0, 1.0, +0.0, 1.0)) > 0);
610         assertTrue(FastMath.copySign(1, MathArrays.linearCombination(-0.0, 1.0, -0.0, 1.0, -0.0, 1.0)) < 0);
611         assertTrue(FastMath.copySign(1, MathArrays.linearCombination(+0.0, 1.0, +0.0, 1.0, +0.0, 1.0)) > 0);
612         assertTrue(FastMath.copySign(1, MathArrays.linearCombination(-0.0, 1.0, -0.0, 1.0, -0.0, 1.0)) < 0);
613         assertTrue(FastMath.copySign(1, MathArrays.linearCombination(+0.0, 1.0, +0.0, 1.0, +0.0, 1.0, +0.0, 1.0)) > 0);
614         assertTrue(FastMath.copySign(1, MathArrays.linearCombination(-0.0, 1.0, -0.0, 1.0, -0.0, 1.0, -0.0, 1.0)) < 0);
615         assertTrue(FastMath.copySign(1, MathArrays.linearCombination(new double[] {+0.0, +0.0}, new double[] {1.0, 1.0})) > 0);
616         assertTrue(FastMath.copySign(1, MathArrays.linearCombination(new double[] {-0.0, -0.0}, new double[] {1.0, 1.0})) < 0);
617     }
618 
619     @Test
620     void testLinearCombination2() {
621         // we compare accurate versus naive dot product implementations
622         // on regular vectors (i.e. not extreme cases like in the previous test)
623         Well1024a random = new Well1024a(553267312521321234L);
624 
625         for (int i = 0; i < 10000; ++i) {
626             final double ux = 1e17 * random.nextDouble();
627             final double uy = 1e17 * random.nextDouble();
628             final double uz = 1e17 * random.nextDouble();
629             final double vx = 1e17 * random.nextDouble();
630             final double vy = 1e17 * random.nextDouble();
631             final double vz = 1e17 * random.nextDouble();
632             final double sInline = MathArrays.linearCombination(ux, vx,
633                                                                 uy, vy,
634                                                                 uz, vz);
635             final double sArray = MathArrays.linearCombination(new double[] {ux, uy, uz},
636                                                                new double[] {vx, vy, vz});
637             assertEquals(sInline, sArray, 0);
638         }
639     }
640 
641     @Test
642     void testLinearCombinationHuge() {
643         int scale = 971;
644         final double[] a = new double[] {
645                                          -1321008684645961.0 / 268435456.0,
646                                          -5774608829631843.0 / 268435456.0,
647                                          -7645843051051357.0 / 8589934592.0
648                                      };
649         final double[] b = new double[] {
650                                          -5712344449280879.0 / 2097152.0,
651                                          -4550117129121957.0 / 2097152.0,
652                                           8846951984510141.0 / 131072.0
653                                      };
654 
655         double[] scaledA = new double[a.length];
656         double[] scaledB = new double[b.length];
657         for (int i = 0; i < scaledA.length; ++i) {
658             scaledA[i] = FastMath.scalb(a[i], -scale);
659             scaledB[i] = FastMath.scalb(b[i], +scale);
660         }
661         final double abSumInline = MathArrays.linearCombination(scaledA[0], scaledB[0],
662                                                                 scaledA[1], scaledB[1],
663                                                                 scaledA[2], scaledB[2]);
664         final double abSumArray = MathArrays.linearCombination(scaledA, scaledB);
665 
666         assertEquals(abSumInline, abSumArray, 0);
667         assertEquals(-1.8551294182586248737720779899, abSumInline, 1.0e-15);
668 
669         final double naive = scaledA[0] * scaledB[0] + scaledA[1] * scaledB[1] + scaledA[2] * scaledB[2];
670         assertTrue(FastMath.abs(naive - abSumInline) > 1.5);
671 
672     }
673 
674     @Test
675     void testLinearCombinationInfinite() {
676         final double[][] a = new double[][] {
677             { 1, 2, 3, 4},
678             { 1, Double.POSITIVE_INFINITY, 3, 4},
679             { 1, 2, Double.POSITIVE_INFINITY, 4},
680             { 1, Double.POSITIVE_INFINITY, 3, Double.NEGATIVE_INFINITY},
681             { 1, 2, 3, 4},
682             { 1, 2, 3, 4},
683             { 1, 2, 3, 4},
684             { 1, 2, 3, 4}
685         };
686         final double[][] b = new double[][] {
687             { 1, -2, 3, 4},
688             { 1, -2, 3, 4},
689             { 1, -2, 3, 4},
690             { 1, -2, 3, 4},
691             { 1, Double.POSITIVE_INFINITY, 3, 4},
692             { 1, -2, Double.POSITIVE_INFINITY, 4},
693             { 1, Double.POSITIVE_INFINITY, 3, Double.NEGATIVE_INFINITY},
694             { Double.NaN, -2, 3, 4}
695         };
696 
697         assertEquals(-3,
698                             MathArrays.linearCombination(a[0][0], b[0][0],
699                                                          a[0][1], b[0][1]),
700                             1.0e-10);
701         assertEquals(6,
702                             MathArrays.linearCombination(a[0][0], b[0][0],
703                                                          a[0][1], b[0][1],
704                                                          a[0][2], b[0][2]),
705                             1.0e-10);
706         assertEquals(22,
707                             MathArrays.linearCombination(a[0][0], b[0][0],
708                                                          a[0][1], b[0][1],
709                                                          a[0][2], b[0][2],
710                                                          a[0][3], b[0][3]),
711                             1.0e-10);
712         assertEquals(22, MathArrays.linearCombination(a[0], b[0]), 1.0e-10);
713 
714         assertEquals(Double.NEGATIVE_INFINITY,
715                             MathArrays.linearCombination(a[1][0], b[1][0],
716                                                          a[1][1], b[1][1]),
717                             1.0e-10);
718         assertEquals(Double.NEGATIVE_INFINITY,
719                             MathArrays.linearCombination(a[1][0], b[1][0],
720                                                          a[1][1], b[1][1],
721                                                          a[1][2], b[1][2]),
722                             1.0e-10);
723         assertEquals(Double.NEGATIVE_INFINITY,
724                             MathArrays.linearCombination(a[1][0], b[1][0],
725                                                          a[1][1], b[1][1],
726                                                          a[1][2], b[1][2],
727                                                          a[1][3], b[1][3]),
728                             1.0e-10);
729         assertEquals(Double.NEGATIVE_INFINITY, MathArrays.linearCombination(a[1], b[1]), 1.0e-10);
730 
731         assertEquals(-3,
732                             MathArrays.linearCombination(a[2][0], b[2][0],
733                                                          a[2][1], b[2][1]),
734                             1.0e-10);
735         assertEquals(Double.POSITIVE_INFINITY,
736                             MathArrays.linearCombination(a[2][0], b[2][0],
737                                                          a[2][1], b[2][1],
738                                                          a[2][2], b[2][2]),
739                             1.0e-10);
740         assertEquals(Double.POSITIVE_INFINITY,
741                             MathArrays.linearCombination(a[2][0], b[2][0],
742                                                          a[2][1], b[2][1],
743                                                          a[2][2], b[2][2],
744                                                          a[2][3], b[2][3]),
745                             1.0e-10);
746         assertEquals(Double.POSITIVE_INFINITY, MathArrays.linearCombination(a[2], b[2]), 1.0e-10);
747 
748         assertEquals(Double.NEGATIVE_INFINITY,
749                             MathArrays.linearCombination(a[3][0], b[3][0],
750                                                          a[3][1], b[3][1]),
751                             1.0e-10);
752         assertEquals(Double.NEGATIVE_INFINITY,
753                             MathArrays.linearCombination(a[3][0], b[3][0],
754                                                          a[3][1], b[3][1],
755                                                          a[3][2], b[3][2]),
756                             1.0e-10);
757         assertEquals(Double.NEGATIVE_INFINITY,
758                             MathArrays.linearCombination(a[3][0], b[3][0],
759                                                          a[3][1], b[3][1],
760                                                          a[3][2], b[3][2],
761                                                          a[3][3], b[3][3]),
762                             1.0e-10);
763         assertEquals(Double.NEGATIVE_INFINITY, MathArrays.linearCombination(a[3], b[3]), 1.0e-10);
764 
765         assertEquals(Double.POSITIVE_INFINITY,
766                             MathArrays.linearCombination(a[4][0], b[4][0],
767                                                          a[4][1], b[4][1]),
768                             1.0e-10);
769         assertEquals(Double.POSITIVE_INFINITY,
770                             MathArrays.linearCombination(a[4][0], b[4][0],
771                                                          a[4][1], b[4][1],
772                                                          a[4][2], b[4][2]),
773                             1.0e-10);
774         assertEquals(Double.POSITIVE_INFINITY,
775                             MathArrays.linearCombination(a[4][0], b[4][0],
776                                                          a[4][1], b[4][1],
777                                                          a[4][2], b[4][2],
778                                                          a[4][3], b[4][3]),
779                             1.0e-10);
780         assertEquals(Double.POSITIVE_INFINITY, MathArrays.linearCombination(a[4], b[4]), 1.0e-10);
781 
782         assertEquals(-3,
783                             MathArrays.linearCombination(a[5][0], b[5][0],
784                                                          a[5][1], b[5][1]),
785                             1.0e-10);
786         assertEquals(Double.POSITIVE_INFINITY,
787                             MathArrays.linearCombination(a[5][0], b[5][0],
788                                                          a[5][1], b[5][1],
789                                                          a[5][2], b[5][2]),
790                             1.0e-10);
791         assertEquals(Double.POSITIVE_INFINITY,
792                             MathArrays.linearCombination(a[5][0], b[5][0],
793                                                          a[5][1], b[5][1],
794                                                          a[5][2], b[5][2],
795                                                          a[5][3], b[5][3]),
796                             1.0e-10);
797         assertEquals(Double.POSITIVE_INFINITY, MathArrays.linearCombination(a[5], b[5]), 1.0e-10);
798 
799         assertEquals(Double.POSITIVE_INFINITY,
800                             MathArrays.linearCombination(a[6][0], b[6][0],
801                                                          a[6][1], b[6][1]),
802                             1.0e-10);
803         assertEquals(Double.POSITIVE_INFINITY,
804                             MathArrays.linearCombination(a[6][0], b[6][0],
805                                                          a[6][1], b[6][1],
806                                                          a[6][2], b[6][2]),
807                             1.0e-10);
808         assertTrue(Double.isNaN(MathArrays.linearCombination(a[6][0], b[6][0],
809                                                                     a[6][1], b[6][1],
810                                                                     a[6][2], b[6][2],
811                                                                     a[6][3], b[6][3])));
812         assertTrue(Double.isNaN(MathArrays.linearCombination(a[6], b[6])));
813 
814         assertTrue(Double.isNaN(MathArrays.linearCombination(a[7][0], b[7][0],
815                                                                     a[7][1], b[7][1])));
816         assertTrue(Double.isNaN(MathArrays.linearCombination(a[7][0], b[7][0],
817                                                                     a[7][1], b[7][1],
818                                                                     a[7][2], b[7][2])));
819         assertTrue(Double.isNaN(MathArrays.linearCombination(a[7][0], b[7][0],
820                                                                     a[7][1], b[7][1],
821                                                                     a[7][2], b[7][2],
822                                                                     a[7][3], b[7][3])));
823         assertTrue(Double.isNaN(MathArrays.linearCombination(a[7], b[7])));
824     }
825 
826     @Test
827     void testArrayEquals() {
828         assertFalse(MathArrays.equals(new double[] { 1d }, null));
829         assertFalse(MathArrays.equals(null, new double[] { 1d }));
830         assertTrue(MathArrays.equals(null, (double[]) null));
831 
832         assertFalse(MathArrays.equals(new double[] { 1d }, new double[0]));
833         assertTrue(MathArrays.equals(new double[] { 1d }, new double[] { 1d }));
834         assertTrue(MathArrays.equals(new double[] { Double.POSITIVE_INFINITY,
835                                                            Double.NEGATIVE_INFINITY, 1d, 0d },
836                                             new double[] { Double.POSITIVE_INFINITY,
837                                                            Double.NEGATIVE_INFINITY, 1d, 0d }));
838         assertFalse(MathArrays.equals(new double[] { Double.NaN },
839                                              new double[] { Double.NaN }));
840         assertFalse(MathArrays.equals(new double[] { Double.POSITIVE_INFINITY },
841                                              new double[] { Double.NEGATIVE_INFINITY }));
842         assertFalse(MathArrays.equals(new double[] { 1d },
843                                              new double[] { FastMath.nextAfter(FastMath.nextAfter(1d, 2d), 2d) }));
844 
845     }
846 
847     @Test
848     void testArrayEqualsIncludingNaN() {
849         assertFalse(MathArrays.equalsIncludingNaN(new double[] { 1d }, null));
850         assertFalse(MathArrays.equalsIncludingNaN(null, new double[] { 1d }));
851         assertTrue(MathArrays.equalsIncludingNaN(null, (double[]) null));
852 
853         assertFalse(MathArrays.equalsIncludingNaN(new double[] { 1d }, new double[0]));
854         assertTrue(MathArrays.equalsIncludingNaN(new double[] { 1d }, new double[] { 1d }));
855         assertTrue(MathArrays.equalsIncludingNaN(new double[] { Double.NaN, Double.POSITIVE_INFINITY,
856                                                                        Double.NEGATIVE_INFINITY, 1d, 0d },
857                                                         new double[] { Double.NaN, Double.POSITIVE_INFINITY,
858                                                                        Double.NEGATIVE_INFINITY, 1d, 0d }));
859         assertFalse(MathArrays.equalsIncludingNaN(new double[] { Double.POSITIVE_INFINITY },
860                                                          new double[] { Double.NEGATIVE_INFINITY }));
861         assertFalse(MathArrays.equalsIncludingNaN(new double[] { 1d },
862                                                          new double[] { FastMath.nextAfter(FastMath.nextAfter(1d, 2d), 2d) }));
863     }
864 
865     @Test
866     void testLongArrayEquals() {
867         assertFalse(MathArrays.equals(new long[] { 1L }, null));
868         assertFalse(MathArrays.equals(null, new long[] { 1L }));
869         assertTrue(MathArrays.equals(null, (long[]) null));
870 
871         assertFalse(MathArrays.equals(new long[] { 1L }, new long[0]));
872         assertTrue(MathArrays.equals(new long[] { 1L }, new long[] { 1L }));
873         assertTrue(MathArrays.equals(new long[] { 0x7ff0000000000000L,
874                                                            0xfff0000000000000L, 1L, 0L },
875                                             new long[] { 0x7ff0000000000000L,
876                                                            0xfff0000000000000L, 1L, 0L }));
877         assertFalse(MathArrays.equals(new long[] { 0x7ff0000000000000L },
878                                              new long[] { 0xfff0000000000000L }));
879 
880     }
881 
882     @Test
883     void testIntArrayEquals() {
884         assertFalse(MathArrays.equals(new int[] { 1 }, null));
885         assertFalse(MathArrays.equals(null, new int[] { 1 }));
886         assertTrue(MathArrays.equals(null, (int[]) null));
887 
888         assertFalse(MathArrays.equals(new int[] { 1 }, new int[0]));
889         assertTrue(MathArrays.equals(new int[] { 1 }, new int[] { 1 }));
890         assertTrue(MathArrays.equals(new int[] { Integer.MAX_VALUE,
891                                                         Integer.MIN_VALUE, 1, 0 },
892                                             new int[] { Integer.MAX_VALUE,
893                                                         Integer.MIN_VALUE, 1, 0 }));
894         assertFalse(MathArrays.equals(new int[] { Integer.MAX_VALUE },
895                                              new int[] { Integer.MIN_VALUE }));
896 
897     }
898 
899     @Test
900     void testByteArrayEquals() {
901         assertFalse(MathArrays.equals(new byte[] { 1 }, null));
902         assertFalse(MathArrays.equals(null, new byte[] { 1 }));
903         assertTrue(MathArrays.equals(null, (byte[]) null));
904 
905         assertFalse(MathArrays.equals(new byte[] { 1 }, new byte[0]));
906         assertTrue(MathArrays.equals(new byte[] { 1 }, new byte[] { 1 }));
907         assertTrue(MathArrays.equals(new byte[] { Byte.MAX_VALUE,
908                                                          Byte.MIN_VALUE, 1, 0 },
909                                             new byte[] { Byte.MAX_VALUE,
910                                                          Byte.MIN_VALUE, 1, 0 }));
911         assertFalse(MathArrays.equals(new byte[] { Byte.MAX_VALUE },
912                                              new byte[] { Byte.MIN_VALUE }));
913 
914     }
915 
916     @Test
917     void testShortArrayEquals() {
918         assertFalse(MathArrays.equals(new short[] { 1 }, null));
919         assertFalse(MathArrays.equals(null, new short[] { 1 }));
920         assertTrue(MathArrays.equals(null, (short[]) null));
921 
922         assertFalse(MathArrays.equals(new short[] { 1 }, new short[0]));
923         assertTrue(MathArrays.equals(new short[] { 1 }, new short[] { 1 }));
924         assertTrue(MathArrays.equals(new short[] { Short.MAX_VALUE,
925                                                           Short.MIN_VALUE, 1, 0 },
926                                             new short[] { Short.MAX_VALUE,
927                                                           Short.MIN_VALUE, 1, 0 }));
928         assertFalse(MathArrays.equals(new short[] { Short.MAX_VALUE },
929                                              new short[] { Short.MIN_VALUE }));
930 
931     }
932 
933     @Test
934     void testFloatArrayEquals() {
935         assertFalse(MathArrays.equals(new float[] { 1 }, null));
936         assertFalse(MathArrays.equals(null, new float[] { 1 }));
937         assertTrue(MathArrays.equals(null, (float[]) null));
938 
939         assertFalse(MathArrays.equals(new float[] { 1 }, new float[0]));
940         assertTrue(MathArrays.equals(new float[] { 1 }, new float[] { 1 }));
941         assertTrue(MathArrays.equals(new float[] { Float.MAX_VALUE,
942                                                           Float.MIN_VALUE, 1, 0 },
943                                             new float[] { Float.MAX_VALUE,
944                                                           Float.MIN_VALUE, 1, 0 }));
945         assertFalse(MathArrays.equals(new float[] { Float.MAX_VALUE },
946                                              new float[] { Float.MIN_VALUE }));
947 
948     }
949 
950     @Test
951     void testDoubleArrayEquals() {
952         assertFalse(MathArrays.equals(new double[] { 1 }, null));
953         assertFalse(MathArrays.equals(null, new double[] { 1 }));
954         assertTrue(MathArrays.equals(null, (double[]) null));
955 
956         assertFalse(MathArrays.equals(new double[] { 1 }, new double[0]));
957         assertTrue(MathArrays.equals(new double[] { 1 }, new double[] { 1 }));
958         assertTrue(MathArrays.equals(new double[] { Double.MAX_VALUE,
959                                                           Double.MIN_VALUE, 1, 0 },
960                                             new double[] { Double.MAX_VALUE,
961                                                           Double.MIN_VALUE, 1, 0 }));
962         assertFalse(MathArrays.equals(new double[] { Double.MAX_VALUE },
963                                              new double[] { Double.MIN_VALUE }));
964 
965     }
966 
967     @Test
968     void testFieldArrayEquals() {
969         assertFalse(MathArrays.equals(new Complex[] { Complex.I }, null));
970         assertFalse(MathArrays.equals(null, new Complex[] { Complex.I }));
971         assertTrue(MathArrays.equals(null, (Complex[]) null));
972 
973         assertFalse(MathArrays.equals(new Complex[] { Complex.I }, new Complex[0]));
974         assertTrue(MathArrays.equals(new Complex[] { Complex.I }, new Complex[] { Complex.I }));
975         assertTrue(MathArrays.equals(new Complex[] { new Complex(Double.MAX_VALUE, 0),
976                                                      new Complex(Double.MIN_VALUE, 0),
977                                                      Complex.ONE, Complex.ZERO },
978                                      new Complex[] { new Complex(Double.MAX_VALUE, 0),
979                                                      new Complex(Double.MIN_VALUE, 0),
980                                                      Complex.ONE, Complex.ZERO }));
981         assertFalse(MathArrays.equals(new Complex[] { new Complex(Double.MAX_VALUE, 0) },
982                                              new Complex[] { new Complex(Double.MIN_VALUE, 0) }));
983 
984     }
985 
986     @Test
987     void testNormalizeArray() {
988         double[] testValues1 = new double[] {1, 1, 2};
989         UnitTestUtils.customAssertEquals(new double[] { .25, .25, .5},
990                                          MathArrays.normalizeArray(testValues1, 1),
991                                          Double.MIN_VALUE);
992 
993         double[] testValues2 = new double[] {-1, -1, 1};
994         UnitTestUtils.customAssertEquals(new double[] { 1, 1, -1},
995                                          MathArrays.normalizeArray(testValues2, 1),
996                                          Double.MIN_VALUE);
997 
998         // Ignore NaNs
999         double[] testValues3 = new double[] {-1, -1, Double.NaN, 1, Double.NaN};
1000         UnitTestUtils.customAssertEquals(new double[] { 1, 1, Double.NaN, -1, Double.NaN},
1001                                          MathArrays.normalizeArray(testValues3, 1),
1002                                          Double.MIN_VALUE);
1003 
1004         // Zero sum -> MathRuntimeException
1005         double[] zeroSum = new double[] {-1, 1};
1006         try {
1007             MathArrays.normalizeArray(zeroSum, 1);
1008             fail("expecting MathRuntimeException");
1009         } catch (MathRuntimeException ex) {
1010             Assertions.assertEquals(LocalizedCoreFormats.ARRAY_SUMS_TO_ZERO, ex.getSpecifier());
1011         }
1012 
1013         // Infinite elements -> MathRuntimeException
1014         double[] hasInf = new double[] {1, 2, 1, Double.NEGATIVE_INFINITY};
1015         try {
1016             MathArrays.normalizeArray(hasInf, 1);
1017             fail("expecting MathIllegalArgumentException");
1018         } catch (MathIllegalArgumentException ex) {
1019             Assertions.assertEquals(LocalizedCoreFormats.INFINITE_ARRAY_ELEMENT, ex.getSpecifier());
1020         }
1021 
1022         // Infinite target -> MathIllegalArgumentException
1023         try {
1024             MathArrays.normalizeArray(testValues1, Double.POSITIVE_INFINITY);
1025             fail("expecting MathIllegalArgumentException");
1026         } catch (MathIllegalArgumentException ex) {
1027             Assertions.assertEquals(LocalizedCoreFormats.NORMALIZE_INFINITE, ex.getSpecifier());
1028         }
1029 
1030         // NaN target -> MathIllegalArgumentException
1031         try {
1032             MathArrays.normalizeArray(testValues1, Double.NaN);
1033             fail("expecting MathIllegalArgumentException");
1034         } catch (MathIllegalArgumentException ex) {
1035             Assertions.assertEquals(LocalizedCoreFormats.NORMALIZE_NAN, ex.getSpecifier());
1036         }
1037     }
1038 
1039     @Test
1040     void testConvolve() {
1041         /* Test Case (obtained via SciPy)
1042          * x=[1.2,-1.8,1.4]
1043          * h=[1,0.8,0.5,0.3]
1044          * convolve(x,h) -> array([ 1.2 , -0.84,  0.56,  0.58,  0.16,  0.42])
1045          */
1046         double[] x1 = { 1.2, -1.8, 1.4 };
1047         double[] h1 = { 1, 0.8, 0.5, 0.3 };
1048         double[] y1 = { 1.2, -0.84, 0.56, 0.58, 0.16, 0.42 };
1049         double tolerance = 1e-13;
1050 
1051         double[] yActual = MathArrays.convolve(x1, h1);
1052         assertArrayEquals(y1, yActual, tolerance);
1053 
1054         double[] x2 = { 1, 2, 3 };
1055         double[] h2 = { 0, 1, 0.5 };
1056         double[] y2 = { 0, 1, 2.5, 4, 1.5 };
1057 
1058         yActual = MathArrays.convolve(x2, h2);
1059         assertArrayEquals(y2, yActual, tolerance);
1060 
1061         try {
1062             MathArrays.convolve(new double[]{1, 2}, null);
1063             fail("an exception should have been thrown");
1064         } catch (NullArgumentException e) {
1065             // expected behavior
1066         }
1067 
1068         try {
1069             MathArrays.convolve(null, new double[]{1, 2});
1070             fail("an exception should have been thrown");
1071         } catch (NullArgumentException e) {
1072             // expected behavior
1073         }
1074 
1075         try {
1076             MathArrays.convolve(new double[]{1, 2}, new double[]{});
1077             fail("an exception should have been thrown");
1078         } catch (MathIllegalArgumentException e) {
1079             // expected behavior
1080         }
1081 
1082         try {
1083             MathArrays.convolve(new double[]{}, new double[]{1, 2});
1084             fail("an exception should have been thrown");
1085         } catch (MathIllegalArgumentException e) {
1086             // expected behavior
1087         }
1088 
1089         try {
1090             MathArrays.convolve(new double[]{}, new double[]{});
1091             fail("an exception should have been thrown");
1092         } catch (MathIllegalArgumentException e) {
1093             // expected behavior
1094         }
1095     }
1096 
1097     @Test
1098     void testShuffleTail() {
1099         final int[] orig = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
1100         final int[] list = orig.clone();
1101         final int start = 4;
1102         MathArrays.shuffle(list, start, MathArrays.Position.TAIL, new Well1024a(7654321L));
1103 
1104         // Ensure that all entries below index "start" did not move.
1105         for (int i = 0; i < start; i++) {
1106             assertEquals(orig[i], list[i]);
1107         }
1108 
1109         // Ensure that at least one entry has moved.
1110         boolean ok = false;
1111         for (int i = start; i < orig.length - 1; i++) {
1112             if (orig[i] != list[i]) {
1113                 ok = true;
1114                 break;
1115             }
1116         }
1117         assertTrue(ok);
1118     }
1119 
1120     @Test
1121     void testShuffleHead() {
1122         final int[] orig = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
1123         final int[] list = orig.clone();
1124         final int start = 4;
1125         MathArrays.shuffle(list, start, MathArrays.Position.HEAD, new Well1024a(1234567L));
1126 
1127         // Ensure that all entries above index "start" did not move.
1128         for (int i = start + 1; i < orig.length; i++) {
1129             assertEquals(orig[i], list[i]);
1130         }
1131 
1132         // Ensure that at least one entry has moved.
1133         boolean ok = false;
1134         for (int i = 0; i <= start; i++) {
1135             if (orig[i] != list[i]) {
1136                 ok = true;
1137                 break;
1138             }
1139         }
1140         assertTrue(ok);
1141     }
1142 
1143     @Test
1144     void testNatural() {
1145         final int n = 4;
1146         final int[] expected = {0, 1, 2, 3};
1147 
1148         final int[] natural = MathArrays.natural(n);
1149         for (int i = 0; i < n; i++) {
1150             assertEquals(expected[i], natural[i]);
1151         }
1152     }
1153 
1154     @Test
1155     void testNaturalZero() {
1156         final int[] natural = MathArrays.natural(0);
1157         assertEquals(0, natural.length);
1158     }
1159 
1160     @Test
1161     void testSequence() {
1162         final int size = 4;
1163         final int start = 5;
1164         final int stride = 2;
1165         final int[] expected = {5, 7, 9, 11};
1166 
1167         final int[] seq = MathArrays.sequence(size, start, stride);
1168         for (int i = 0; i < size; i++) {
1169             assertEquals(expected[i], seq[i]);
1170         }
1171     }
1172 
1173     @Test
1174     void testSequenceZero() {
1175         final int[] seq = MathArrays.sequence(0, 12345, 6789);
1176         assertEquals(0, seq.length);
1177     }
1178 
1179     @Test
1180     void testVerifyValuesPositive() {
1181         for (int j = 0; j < 6; j++) {
1182             for (int i = 1; i < (7 - j); i++) {
1183                 assertTrue(MathArrays.verifyValues(testArray, 0, i));
1184             }
1185         }
1186         assertTrue(MathArrays.verifyValues(singletonArray, 0, 1));
1187         assertTrue(MathArrays.verifyValues(singletonArray, 0, 0, true));
1188     }
1189 
1190     @Test
1191     void testVerifyValuesNegative() {
1192         assertFalse(MathArrays.verifyValues(singletonArray, 0, 0));
1193         assertFalse(MathArrays.verifyValues(testArray, 0, 0));
1194         try {
1195             MathArrays.verifyValues(singletonArray, 2, 1);  // start past end
1196             fail("Expecting MathIllegalArgumentException");
1197         } catch (MathIllegalArgumentException ex) {
1198             // expected
1199         }
1200         try {
1201             MathArrays.verifyValues(testArray, 0, 7);  // end past end
1202             fail("Expecting MathIllegalArgumentException");
1203         } catch (MathIllegalArgumentException ex) {
1204             // expected
1205         }
1206         try {
1207             MathArrays.verifyValues(testArray, -1, 1);  // start negative
1208             fail("Expecting MathIllegalArgumentException");
1209         } catch (MathIllegalArgumentException ex) {
1210             // expected
1211         }
1212         try {
1213             MathArrays.verifyValues(testArray, 0, -1);  // length negative
1214             fail("Expecting MathIllegalArgumentException");
1215         } catch (MathIllegalArgumentException ex) {
1216             // expected
1217         }
1218         try {
1219             MathArrays.verifyValues(nullArray, 0, 1);  // null array
1220             fail("Expecting NullArgumentException");
1221         } catch (NullArgumentException ex) {
1222             // expected
1223         }
1224         try {
1225             MathArrays.verifyValues(testArray, nullArray, 0, 1);  // null weights array
1226             fail("Expecting NullArgumentException");
1227         } catch (NullArgumentException ex) {
1228             // expected
1229         }
1230         try {
1231             MathArrays.verifyValues(singletonArray, testWeightsArray, 0, 1);  // weights.length != value.length
1232             fail("Expecting MathIllegalArgumentException");
1233         } catch (MathIllegalArgumentException ex) {
1234             // expected
1235         }
1236         try {
1237             MathArrays.verifyValues(testArray, testNegativeWeightsArray, 0, 6);  // can't have negative weights
1238             fail("Expecting MathIllegalArgumentException");
1239         } catch (MathIllegalArgumentException ex) {
1240             // expected
1241         }
1242     }
1243 
1244     @Test
1245     void testConcatenate() {
1246         final double[] u = new double[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
1247         final double[] x = new double[] {0, 1, 2};
1248         final double[] y = new double[] {3, 4, 5, 6, 7, 8};
1249         final double[] z = new double[] {9};
1250         assertArrayEquals(u, MathArrays.concatenate(x, y, z), 0);
1251     }
1252 
1253     @Test
1254     void testConcatentateSingle() {
1255         final double[] x = new double[] {0, 1, 2};
1256         assertArrayEquals(x, MathArrays.concatenate(x), 0);
1257     }
1258 
1259     @Test
1260     void testConcatenateNullArguments() {
1261         assertThrows(NullPointerException.class, () -> {
1262             final double[] x = new double[]{0, 1, 2};
1263             MathArrays.concatenate(x, null);
1264         });
1265     }
1266 
1267     @Test
1268     void testUnique() {
1269         final double[] x = {0, 9, 3, 0, 11, 7, 3, 5, -1, -2};
1270         final double[] values = {11, 9, 7, 5, 3, 0, -1, -2};
1271         assertArrayEquals(values, MathArrays.unique(x), 0);
1272     }
1273 
1274     @Test
1275     void testUniqueInfiniteValues() {
1276         final double [] x = {0, Double.NEGATIVE_INFINITY, 3, Double.NEGATIVE_INFINITY,
1277             3, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY};
1278         final double[] u = {Double.POSITIVE_INFINITY, 3, 0, Double.NEGATIVE_INFINITY};
1279         assertArrayEquals(u , MathArrays.unique(x), 0);
1280     }
1281 
1282     @Test
1283     void testUniqueNaNValues() {
1284         final double[] x = new double[] {10, 2, Double.NaN, Double.NaN, Double.NaN,
1285             Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY};
1286         final double[] u = MathArrays.unique(x);
1287         assertEquals(5, u.length);
1288         assertTrue(Double.isNaN(u[0]));
1289         assertEquals(Double.POSITIVE_INFINITY, u[1], 0);
1290         assertEquals(10, u[2], 0);
1291         assertEquals(2, u[3], 0);
1292         assertEquals(Double.NEGATIVE_INFINITY, u[4], 0);
1293     }
1294 
1295     @Test
1296     void testUniqueNullArgument() {
1297         assertThrows(NullPointerException.class, () -> MathArrays.unique(null));
1298     }
1299 
1300     @Test
1301     void testBuildArray1() {
1302         Binary64Field field = Binary64Field.getInstance();
1303         Binary64[] array = MathArrays.buildArray(field, 3);
1304         assertEquals(3, array.length);
1305         for (Binary64 d : array) {
1306             assertSame(field.getZero(), d);
1307         }
1308     }
1309 
1310     @Test
1311     void testBuildArray2AllIndices() {
1312         Binary64Field field = Binary64Field.getInstance();
1313         Binary64[][] array = MathArrays.buildArray(field, 3, 2);
1314         assertEquals(3, array.length);
1315         for (Binary64[] a1 : array) {
1316             assertEquals(2, a1.length);
1317             for (Binary64 d : a1) {
1318                 assertSame(field.getZero(), d);
1319             }
1320         }
1321     }
1322 
1323     @Test
1324     void testBuildArray2MissingLastIndex() {
1325         Binary64Field field = Binary64Field.getInstance();
1326         Binary64[][] array = MathArrays.buildArray(field, 3, -1);
1327         assertEquals(3, array.length);
1328         for (Binary64[] a1 : array) {
1329             assertNull(a1);
1330         }
1331     }
1332 
1333     @Test
1334     void testBuildArray3AllIndices() {
1335         Binary64Field field = Binary64Field.getInstance();
1336         Binary64[][][] array = MathArrays.buildArray(field, 3, 2, 4);
1337         assertEquals(3, array.length);
1338         for (Binary64[][] a1 : array) {
1339             assertEquals(2, a1.length);
1340             for (Binary64[] a2 : a1) {
1341                 assertEquals(4, a2.length);
1342                 for (Binary64 d : a2) {
1343                     assertSame(field.getZero(), d);
1344                 }
1345             }
1346         }
1347     }
1348 
1349     @Test
1350     void testBuildArray3MissingLastIndex() {
1351         Binary64Field field = Binary64Field.getInstance();
1352         Binary64[][][] array = MathArrays.buildArray(field, 3, 2, -1);
1353         assertEquals(3, array.length);
1354         for (Binary64[][] a1 : array) {
1355             assertEquals(2, a1.length);
1356             for (Binary64[] a2 : a1) {
1357                 assertNull(a2);
1358             }
1359         }
1360     }
1361 
1362     @Test
1363     void testIssue471() {
1364 
1365         checkOverflow(new int[] { Integer.MAX_VALUE }, new int[] { -1 });
1366         checkOverflow(new int[] { Integer.MIN_VALUE }, new int[] {  1 });
1367 
1368         checkOverflow(filled(3, 1_000_000_000), new int[3]);
1369         checkOverflow(filled(4, 1_000_000_000), new int[4]);
1370         checkOverflow(filled(10, 1_000_000_000), new int[10]);
1371 
1372         Assertions.assertEquals(100000000, MathArrays.distance1( filled(100_000, 1000), new int[100_000]));
1373         checkOverflow(filled(2_200_000, 1000), new int[2_200_000]);
1374 
1375         Assertions.assertEquals(      9, MathArrays.distance1(new int[] { 1, 2, 3}, new int[] { 4, 5, 6 }));
1376         Assertions.assertEquals(1000000, MathArrays.distance1(filled(100_000, 10), new int[100_000]));
1377 
1378     }
1379 
1380     private int[] filled(int n, int v) {
1381         int[] a = new int[n];
1382         Arrays.fill(a, v);
1383         return a;
1384     }
1385 
1386     private void checkOverflow(final int[] p1, final int[] p2) {
1387         try {
1388             MathArrays.distance1(p1, p2);
1389             Assertions.fail("an exception should have been thrown");
1390         } catch (MathRuntimeException mre) {
1391             Assertions.assertEquals(LocalizedCoreFormats.OVERFLOW, mre.getSpecifier());
1392         }
1393     }
1394 
1395 }