1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.hipparchus.util;
24
25 import java.lang.reflect.Array;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.Comparator;
29 import java.util.Iterator;
30 import java.util.List;
31 import java.util.NavigableSet;
32 import java.util.TreeSet;
33
34 import org.hipparchus.Field;
35 import org.hipparchus.FieldElement;
36 import org.hipparchus.CalculusFieldElement;
37 import org.hipparchus.exception.LocalizedCoreFormats;
38 import org.hipparchus.exception.MathIllegalArgumentException;
39 import org.hipparchus.exception.MathRuntimeException;
40 import org.hipparchus.exception.NullArgumentException;
41 import org.hipparchus.random.RandomGenerator;
42 import org.hipparchus.random.Well19937c;
43
44
45
46
47 public class MathArrays {
48
49
50
51
52 private MathArrays() {}
53
54
55
56
57 public interface Function {
58
59
60
61
62
63 double evaluate(double[] array);
64
65
66
67
68
69
70
71 double evaluate(double[] array,
72 int startIndex,
73 int numElements);
74
75 }
76
77
78
79
80
81
82
83
84 public static double[] scale(double val, final double[] arr) {
85 double[] newArr = new double[arr.length];
86 for (int i = 0; i < arr.length; i++) {
87 newArr[i] = arr[i] * val;
88 }
89 return newArr;
90 }
91
92
93
94
95
96
97
98
99
100 public static void scaleInPlace(double val, final double[] arr) {
101 for (int i = 0; i < arr.length; i++) {
102 arr[i] *= val;
103 }
104 }
105
106
107
108
109
110
111
112
113
114
115 public static double[] ebeAdd(double[] a, double[] b)
116 throws MathIllegalArgumentException {
117 checkEqualLength(a, b);
118
119 final double[] result = a.clone();
120 for (int i = 0; i < a.length; i++) {
121 result[i] += b[i];
122 }
123 return result;
124 }
125
126
127
128
129
130
131
132
133
134 public static double[] ebeSubtract(double[] a, double[] b)
135 throws MathIllegalArgumentException {
136 checkEqualLength(a, b);
137
138 final double[] result = a.clone();
139 for (int i = 0; i < a.length; i++) {
140 result[i] -= b[i];
141 }
142 return result;
143 }
144
145
146
147
148
149
150
151
152
153 public static double[] ebeMultiply(double[] a, double[] b)
154 throws MathIllegalArgumentException {
155 checkEqualLength(a, b);
156
157 final double[] result = a.clone();
158 for (int i = 0; i < a.length; i++) {
159 result[i] *= b[i];
160 }
161 return result;
162 }
163
164
165
166
167
168
169
170
171
172 public static double[] ebeDivide(double[] a, double[] b)
173 throws MathIllegalArgumentException {
174 checkEqualLength(a, b);
175
176 final double[] result = a.clone();
177 for (int i = 0; i < a.length; i++) {
178 result[i] /= b[i];
179 }
180 return result;
181 }
182
183
184
185
186
187
188
189
190
191 public static double distance1(double[] p1, double[] p2)
192 throws MathIllegalArgumentException {
193 checkEqualLength(p1, p2);
194 double sum = 0;
195 for (int i = 0; i < p1.length; i++) {
196 sum += FastMath.abs(p1[i] - p2[i]);
197 }
198 return sum;
199 }
200
201
202
203
204
205
206
207
208
209 public static int distance1(int[] p1, int[] p2)
210 throws MathIllegalArgumentException {
211 checkEqualLength(p1, p2);
212 int sum = 0;
213 for (int i = 0; i < p1.length; i++) {
214 sum += FastMath.abs(p1[i] - p2[i]);
215 }
216 return sum;
217 }
218
219
220
221
222
223
224
225
226
227 public static double distance(double[] p1, double[] p2)
228 throws MathIllegalArgumentException {
229 checkEqualLength(p1, p2);
230 double sum = 0;
231 for (int i = 0; i < p1.length; i++) {
232 final double dp = p1[i] - p2[i];
233 sum += dp * dp;
234 }
235 return FastMath.sqrt(sum);
236 }
237
238
239
240
241
242
243
244
245 public static double cosAngle(double[] v1, double[] v2) {
246 return linearCombination(v1, v2) / (safeNorm(v1) * safeNorm(v2));
247 }
248
249
250
251
252
253
254
255
256
257 public static double distance(int[] p1, int[] p2)
258 throws MathIllegalArgumentException {
259 checkEqualLength(p1, p2);
260 double sum = 0;
261 for (int i = 0; i < p1.length; i++) {
262 final double dp = p1[i] - p2[i];
263 sum += dp * dp;
264 }
265 return FastMath.sqrt(sum);
266 }
267
268
269
270
271
272
273
274
275
276 public static double distanceInf(double[] p1, double[] p2)
277 throws MathIllegalArgumentException {
278 checkEqualLength(p1, p2);
279 double max = 0;
280 for (int i = 0; i < p1.length; i++) {
281 max = FastMath.max(max, FastMath.abs(p1[i] - p2[i]));
282 }
283 return max;
284 }
285
286
287
288
289
290
291
292
293
294 public static int distanceInf(int[] p1, int[] p2)
295 throws MathIllegalArgumentException {
296 checkEqualLength(p1, p2);
297 int max = 0;
298 for (int i = 0; i < p1.length; i++) {
299 max = FastMath.max(max, FastMath.abs(p1[i] - p2[i]));
300 }
301 return max;
302 }
303
304
305
306
307 public enum OrderDirection {
308
309 INCREASING,
310
311 DECREASING
312 }
313
314
315
316
317
318
319
320
321
322
323 public static <T extends Comparable<? super T>> boolean isMonotonic(T[] val,
324 OrderDirection dir,
325 boolean strict) {
326 T previous = val[0];
327 final int max = val.length;
328 for (int i = 1; i < max; i++) {
329 final int comp;
330 switch (dir) {
331 case INCREASING:
332 comp = previous.compareTo(val[i]);
333 if (strict) {
334 if (comp >= 0) {
335 return false;
336 }
337 } else {
338 if (comp > 0) {
339 return false;
340 }
341 }
342 break;
343 case DECREASING:
344 comp = val[i].compareTo(previous);
345 if (strict) {
346 if (comp >= 0) {
347 return false;
348 }
349 } else {
350 if (comp > 0) {
351 return false;
352 }
353 }
354 break;
355 default:
356
357 throw MathRuntimeException.createInternalError();
358 }
359
360 previous = val[i];
361 }
362 return true;
363 }
364
365
366
367
368
369
370
371
372
373 public static boolean isMonotonic(double[] val, OrderDirection dir, boolean strict) {
374 return checkOrder(val, dir, strict, false);
375 }
376
377
378
379
380
381
382
383
384
385
386
387 public static boolean checkEqualLength(double[] a,
388 double[] b,
389 boolean abort) {
390 if (a.length == b.length) {
391 return true;
392 } else {
393 if (abort) {
394 throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
395 a.length, b.length);
396 }
397 return false;
398 }
399 }
400
401
402
403
404
405
406
407
408 public static void checkEqualLength(double[] a,
409 double[] b) {
410 checkEqualLength(a, b, true);
411 }
412
413
414
415
416
417
418
419
420
421
422
423
424
425 public static <T extends CalculusFieldElement<T>> boolean checkEqualLength(final T[] a,
426 final T[] b,
427 boolean abort) {
428 if (a.length == b.length) {
429 return true;
430 } else {
431 if (abort) {
432 throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
433 a.length, b.length);
434 }
435 return false;
436 }
437 }
438
439
440
441
442
443
444
445
446
447
448 public static <T extends CalculusFieldElement<T>> void checkEqualLength(final T[] a, final T[] b) {
449 checkEqualLength(a, b, true);
450 }
451
452
453
454
455
456
457
458
459
460
461
462 public static boolean checkEqualLength(int[] a,
463 int[] b,
464 boolean abort) {
465 if (a.length == b.length) {
466 return true;
467 } else {
468 if (abort) {
469 throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
470 a.length, b.length);
471 }
472 return false;
473 }
474 }
475
476
477
478
479
480
481
482
483 public static void checkEqualLength(int[] a,
484 int[] b) {
485 checkEqualLength(a, b, true);
486 }
487
488
489
490
491
492
493
494
495
496
497
498
499 public static boolean checkOrder(double[] val, OrderDirection dir,
500 boolean strict, boolean abort)
501 throws MathIllegalArgumentException {
502 double previous = val[0];
503 final int max = val.length;
504
505 int index;
506 ITEM:
507 for (index = 1; index < max; index++) {
508 switch (dir) {
509 case INCREASING:
510 if (strict) {
511 if (val[index] <= previous) {
512 break ITEM;
513 }
514 } else {
515 if (val[index] < previous) {
516 break ITEM;
517 }
518 }
519 break;
520 case DECREASING:
521 if (strict) {
522 if (val[index] >= previous) {
523 break ITEM;
524 }
525 } else {
526 if (val[index] > previous) {
527 break ITEM;
528 }
529 }
530 break;
531 default:
532
533 throw MathRuntimeException.createInternalError();
534 }
535
536 previous = val[index];
537 }
538
539 if (index == max) {
540
541 return true;
542 }
543
544
545 if (abort) {
546 throw new MathIllegalArgumentException(dir == MathArrays.OrderDirection.INCREASING ?
547 (strict ?
548 LocalizedCoreFormats.NOT_STRICTLY_INCREASING_SEQUENCE :
549 LocalizedCoreFormats.NOT_INCREASING_SEQUENCE) :
550 (strict ?
551 LocalizedCoreFormats.NOT_STRICTLY_DECREASING_SEQUENCE :
552 LocalizedCoreFormats.NOT_DECREASING_SEQUENCE),
553 val[index], previous, index, index - 1);
554 } else {
555 return false;
556 }
557 }
558
559
560
561
562
563
564
565
566
567 public static void checkOrder(double[] val, OrderDirection dir,
568 boolean strict) throws MathIllegalArgumentException {
569 checkOrder(val, dir, strict, true);
570 }
571
572
573
574
575
576
577
578 public static void checkOrder(double[] val) throws MathIllegalArgumentException {
579 checkOrder(val, OrderDirection.INCREASING, true);
580 }
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595 public static <T extends CalculusFieldElement<T>>boolean checkOrder(T[] val, OrderDirection dir,
596 boolean strict, boolean abort)
597 throws MathIllegalArgumentException {
598 double previous = val[0].getReal();
599 final int max = val.length;
600
601 int index;
602 ITEM:
603 for (index = 1; index < max; index++) {
604 switch (dir) {
605 case INCREASING:
606 if (strict) {
607 if (val[index].getReal() <= previous) {
608 break ITEM;
609 }
610 } else {
611 if (val[index].getReal() < previous) {
612 break ITEM;
613 }
614 }
615 break;
616 case DECREASING:
617 if (strict) {
618 if (val[index].getReal() >= previous) {
619 break ITEM;
620 }
621 } else {
622 if (val[index].getReal() > previous) {
623 break ITEM;
624 }
625 }
626 break;
627 default:
628
629 throw MathRuntimeException.createInternalError();
630 }
631
632 previous = val[index].getReal();
633 }
634
635 if (index == max) {
636
637 return true;
638 }
639
640
641 if (abort) {
642 throw new MathIllegalArgumentException(dir == MathArrays.OrderDirection.INCREASING ?
643 (strict ?
644 LocalizedCoreFormats.NOT_STRICTLY_INCREASING_SEQUENCE :
645 LocalizedCoreFormats.NOT_INCREASING_SEQUENCE) :
646 (strict ?
647 LocalizedCoreFormats.NOT_STRICTLY_DECREASING_SEQUENCE :
648 LocalizedCoreFormats.NOT_DECREASING_SEQUENCE),
649 val[index], previous, index, index - 1);
650 } else {
651 return false;
652 }
653 }
654
655
656
657
658
659
660
661
662
663
664
665 public static <T extends CalculusFieldElement<T>> void checkOrder(T[] val, OrderDirection dir,
666 boolean strict) throws MathIllegalArgumentException {
667 checkOrder(val, dir, strict, true);
668 }
669
670
671
672
673
674
675
676
677
678 public static <T extends CalculusFieldElement<T>> void checkOrder(T[] val) throws MathIllegalArgumentException {
679 checkOrder(val, OrderDirection.INCREASING, true);
680 }
681
682
683
684
685
686
687
688
689 public static void checkRectangular(final long[][] in)
690 throws MathIllegalArgumentException, NullArgumentException {
691 MathUtils.checkNotNull(in);
692 for (int i = 1; i < in.length; i++) {
693 if (in[i].length != in[0].length) {
694 throw new MathIllegalArgumentException(
695 LocalizedCoreFormats.DIFFERENT_ROWS_LENGTHS,
696 in[i].length, in[0].length);
697 }
698 }
699 }
700
701
702
703
704
705
706
707
708 public static void checkPositive(final double[] in)
709 throws MathIllegalArgumentException {
710 for (double v : in) {
711 if (v <= 0) {
712 throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_SMALL_BOUND_EXCLUDED,
713 v, 0);
714 }
715 }
716 }
717
718
719
720
721
722
723
724 public static void checkNotNaN(final double[] in)
725 throws MathIllegalArgumentException {
726 for(int i = 0; i < in.length; i++) {
727 if (Double.isNaN(in[i])) {
728 throw new MathIllegalArgumentException(LocalizedCoreFormats.NAN_ELEMENT_AT_INDEX, i);
729 }
730 }
731 }
732
733
734
735
736
737
738
739 public static void checkNonNegative(final long[] in)
740 throws MathIllegalArgumentException {
741 for (long l : in) {
742 if (l < 0) {
743 throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_SMALL, l, 0);
744 }
745 }
746 }
747
748
749
750
751
752
753
754 public static void checkNonNegative(final long[][] in)
755 throws MathIllegalArgumentException {
756 for (long[] longs : in) {
757 for (int j = 0; j < longs.length; j++) {
758 if (longs[j] < 0) {
759 throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_SMALL, longs[j], 0);
760 }
761 }
762 }
763 }
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827 public static double safeNorm(double[] v) {
828 double rdwarf = 3.834e-20;
829 double rgiant = 1.304e+19;
830 double s1 = 0;
831 double s2 = 0;
832 double s3 = 0;
833 double x1max = 0;
834 double x3max = 0;
835 double floatn = v.length;
836 double agiant = rgiant / floatn;
837 for (double value : v) {
838 double xabs = FastMath.abs(value);
839 if (xabs < rdwarf || xabs > agiant) {
840 if (xabs > rdwarf) {
841 if (xabs > x1max) {
842 double r = x1max / xabs;
843 s1 = 1 + s1 * r * r;
844 x1max = xabs;
845 } else {
846 double r = xabs / x1max;
847 s1 += r * r;
848 }
849 } else {
850 if (xabs > x3max) {
851 double r = x3max / xabs;
852 s3 = 1 + s3 * r * r;
853 x3max = xabs;
854 } else {
855 if (xabs != 0) {
856 double r = xabs / x3max;
857 s3 += r * r;
858 }
859 }
860 }
861 } else {
862 s2 += xabs * xabs;
863 }
864 }
865 double norm;
866 if (s1 != 0) {
867 norm = x1max * Math.sqrt(s1 + (s2 / x1max) / x1max);
868 } else {
869 if (s2 == 0) {
870 norm = x3max * Math.sqrt(s3);
871 } else {
872 if (s2 >= x3max) {
873 norm = Math.sqrt(s2 * (1 + (x3max / s2) * (x3max * s3)));
874 } else {
875 norm = Math.sqrt(x3max * ((s2 / x3max) + (x3max * s3)));
876 }
877 }
878 }
879 return norm;
880 }
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897 public static void sortInPlace(double[] x, double[]... yList)
898 throws MathIllegalArgumentException, NullArgumentException {
899 sortInPlace(x, OrderDirection.INCREASING, yList);
900 }
901
902
903
904
905 private static class PairDoubleInteger {
906
907 private final double key;
908
909 private final int value;
910
911
912
913
914
915 PairDoubleInteger(double key, int value) {
916 this.key = key;
917 this.value = value;
918 }
919
920
921 public double getKey() {
922 return key;
923 }
924
925
926 public int getValue() {
927 return value;
928 }
929 }
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947 public static void sortInPlace(double[] x,
948 final OrderDirection dir,
949 double[]... yList)
950 throws MathIllegalArgumentException, NullArgumentException {
951
952
953 if (x == null) {
954 throw new NullArgumentException();
955 }
956
957 final int len = x.length;
958
959 for (final double[] y : yList) {
960 if (y == null) {
961 throw new NullArgumentException();
962 }
963 if (y.length != len) {
964 throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
965 y.length, len);
966 }
967 }
968
969
970 final List<PairDoubleInteger> list = new ArrayList<>(len);
971 for (int i = 0; i < len; i++) {
972 list.add(new PairDoubleInteger(x[i], i));
973 }
974
975
976 final Comparator<PairDoubleInteger> comp =
977 dir == MathArrays.OrderDirection.INCREASING ?
978 new Comparator<PairDoubleInteger>() {
979
980 @Override
981 public int compare(PairDoubleInteger o1,
982 PairDoubleInteger o2) {
983 return Double.compare(o1.getKey(), o2.getKey());
984 }
985 } :
986 new Comparator<PairDoubleInteger>() {
987
988 @Override
989 public int compare(PairDoubleInteger o1,
990 PairDoubleInteger o2) {
991 return Double.compare(o2.getKey(), o1.getKey());
992 }
993 };
994
995
996 list.sort(comp);
997
998
999
1000
1001 final int[] indices = new int[len];
1002 for (int i = 0; i < len; i++) {
1003 final PairDoubleInteger e = list.get(i);
1004 x[i] = e.getKey();
1005 indices[i] = e.getValue();
1006 }
1007
1008
1009
1010 for (final double[] yInPlace : yList) {
1011
1012 final double[] yOrig = yInPlace.clone();
1013
1014 for (int i = 0; i < len; i++) {
1015 yInPlace[i] = yOrig[indices[i]];
1016 }
1017 }
1018 }
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037 public static double linearCombination(final double[] a, final double[] b)
1038 throws MathIllegalArgumentException {
1039 checkEqualLength(a, b);
1040 final int len = a.length;
1041
1042 if (len == 1) {
1043
1044 return a[0] * b[0];
1045 }
1046
1047 final double[] prodHigh = new double[len];
1048 double prodLowSum = 0;
1049
1050 for (int i = 0; i < len; i++) {
1051 final double ai = a[i];
1052 final double aHigh = Double.longBitsToDouble(Double.doubleToRawLongBits(ai) & ((-1L) << 27));
1053 final double aLow = ai - aHigh;
1054
1055 final double bi = b[i];
1056 final double bHigh = Double.longBitsToDouble(Double.doubleToRawLongBits(bi) & ((-1L) << 27));
1057 final double bLow = bi - bHigh;
1058 prodHigh[i] = ai * bi;
1059 final double prodLow = aLow * bLow - (((prodHigh[i] -
1060 aHigh * bHigh) -
1061 aLow * bHigh) -
1062 aHigh * bLow);
1063 prodLowSum += prodLow;
1064 }
1065
1066
1067 final double prodHighCur = prodHigh[0];
1068 double prodHighNext = prodHigh[1];
1069 double sHighPrev = prodHighCur + prodHighNext;
1070 double sPrime = sHighPrev - prodHighNext;
1071 double sLowSum = (prodHighNext - (sHighPrev - sPrime)) + (prodHighCur - sPrime);
1072
1073 final int lenMinusOne = len - 1;
1074 for (int i = 1; i < lenMinusOne; i++) {
1075 prodHighNext = prodHigh[i + 1];
1076 final double sHighCur = sHighPrev + prodHighNext;
1077 sPrime = sHighCur - prodHighNext;
1078 sLowSum += (prodHighNext - (sHighCur - sPrime)) + (sHighPrev - sPrime);
1079 sHighPrev = sHighCur;
1080 }
1081
1082 double result = sHighPrev + (prodLowSum + sLowSum);
1083
1084 if (Double.isNaN(result) || result == 0.0) {
1085
1086
1087
1088 result = a[0] * b[0];
1089 for (int i = 1; i < len; ++i) {
1090 result += a[i] * b[i];
1091 }
1092 }
1093
1094 return result;
1095 }
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118 public static double linearCombination(final double a1, final double b1,
1119 final double a2, final double b2) {
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132 final double a1High = Double.longBitsToDouble(Double.doubleToRawLongBits(a1) & ((-1L) << 27));
1133 final double a1Low = a1 - a1High;
1134 final double b1High = Double.longBitsToDouble(Double.doubleToRawLongBits(b1) & ((-1L) << 27));
1135 final double b1Low = b1 - b1High;
1136
1137
1138 final double prod1High = a1 * b1;
1139 final double prod1Low = a1Low * b1Low - (((prod1High - a1High * b1High) - a1Low * b1High) - a1High * b1Low);
1140
1141
1142 final double a2High = Double.longBitsToDouble(Double.doubleToRawLongBits(a2) & ((-1L) << 27));
1143 final double a2Low = a2 - a2High;
1144 final double b2High = Double.longBitsToDouble(Double.doubleToRawLongBits(b2) & ((-1L) << 27));
1145 final double b2Low = b2 - b2High;
1146
1147
1148 final double prod2High = a2 * b2;
1149 final double prod2Low = a2Low * b2Low - (((prod2High - a2High * b2High) - a2Low * b2High) - a2High * b2Low);
1150
1151
1152 final double s12High = prod1High + prod2High;
1153 final double s12Prime = s12High - prod2High;
1154 final double s12Low = (prod2High - (s12High - s12Prime)) + (prod1High - s12Prime);
1155
1156
1157
1158 double result = s12High + (prod1Low + prod2Low + s12Low);
1159
1160 if (Double.isNaN(result) || result == 0.0) {
1161
1162
1163
1164 result = a1 * b1 + a2 * b2;
1165 }
1166
1167 return result;
1168 }
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193 public static double linearCombination(final double a1, final double b1,
1194 final double a2, final double b2,
1195 final double a3, final double b3) {
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208 final double a1High = Double.longBitsToDouble(Double.doubleToRawLongBits(a1) & ((-1L) << 27));
1209 final double a1Low = a1 - a1High;
1210 final double b1High = Double.longBitsToDouble(Double.doubleToRawLongBits(b1) & ((-1L) << 27));
1211 final double b1Low = b1 - b1High;
1212
1213
1214 final double prod1High = a1 * b1;
1215 final double prod1Low = a1Low * b1Low - (((prod1High - a1High * b1High) - a1Low * b1High) - a1High * b1Low);
1216
1217
1218 final double a2High = Double.longBitsToDouble(Double.doubleToRawLongBits(a2) & ((-1L) << 27));
1219 final double a2Low = a2 - a2High;
1220 final double b2High = Double.longBitsToDouble(Double.doubleToRawLongBits(b2) & ((-1L) << 27));
1221 final double b2Low = b2 - b2High;
1222
1223
1224 final double prod2High = a2 * b2;
1225 final double prod2Low = a2Low * b2Low - (((prod2High - a2High * b2High) - a2Low * b2High) - a2High * b2Low);
1226
1227
1228 final double a3High = Double.longBitsToDouble(Double.doubleToRawLongBits(a3) & ((-1L) << 27));
1229 final double a3Low = a3 - a3High;
1230 final double b3High = Double.longBitsToDouble(Double.doubleToRawLongBits(b3) & ((-1L) << 27));
1231 final double b3Low = b3 - b3High;
1232
1233
1234 final double prod3High = a3 * b3;
1235 final double prod3Low = a3Low * b3Low - (((prod3High - a3High * b3High) - a3Low * b3High) - a3High * b3Low);
1236
1237
1238 final double s12High = prod1High + prod2High;
1239 final double s12Prime = s12High - prod2High;
1240 final double s12Low = (prod2High - (s12High - s12Prime)) + (prod1High - s12Prime);
1241
1242
1243 final double s123High = s12High + prod3High;
1244 final double s123Prime = s123High - prod3High;
1245 final double s123Low = (prod3High - (s123High - s123Prime)) + (s12High - s123Prime);
1246
1247
1248
1249 double result = s123High + (prod1Low + prod2Low + prod3Low + s12Low + s123Low);
1250
1251 if (Double.isNaN(result) || result == 0.0) {
1252
1253
1254
1255 result = a1 * b1 + a2 * b2 + a3 * b3;
1256 }
1257
1258 return result;
1259 }
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288 public static double linearCombination(final double a1, final double b1,
1289 final double a2, final double b2,
1290 final double a3, final double b3,
1291 final double a4, final double b4) {
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304 final double a1High = Double.longBitsToDouble(Double.doubleToRawLongBits(a1) & ((-1L) << 27));
1305 final double a1Low = a1 - a1High;
1306 final double b1High = Double.longBitsToDouble(Double.doubleToRawLongBits(b1) & ((-1L) << 27));
1307 final double b1Low = b1 - b1High;
1308
1309
1310 final double prod1High = a1 * b1;
1311 final double prod1Low = a1Low * b1Low - (((prod1High - a1High * b1High) - a1Low * b1High) - a1High * b1Low);
1312
1313
1314 final double a2High = Double.longBitsToDouble(Double.doubleToRawLongBits(a2) & ((-1L) << 27));
1315 final double a2Low = a2 - a2High;
1316 final double b2High = Double.longBitsToDouble(Double.doubleToRawLongBits(b2) & ((-1L) << 27));
1317 final double b2Low = b2 - b2High;
1318
1319
1320 final double prod2High = a2 * b2;
1321 final double prod2Low = a2Low * b2Low - (((prod2High - a2High * b2High) - a2Low * b2High) - a2High * b2Low);
1322
1323
1324 final double a3High = Double.longBitsToDouble(Double.doubleToRawLongBits(a3) & ((-1L) << 27));
1325 final double a3Low = a3 - a3High;
1326 final double b3High = Double.longBitsToDouble(Double.doubleToRawLongBits(b3) & ((-1L) << 27));
1327 final double b3Low = b3 - b3High;
1328
1329
1330 final double prod3High = a3 * b3;
1331 final double prod3Low = a3Low * b3Low - (((prod3High - a3High * b3High) - a3Low * b3High) - a3High * b3Low);
1332
1333
1334 final double a4High = Double.longBitsToDouble(Double.doubleToRawLongBits(a4) & ((-1L) << 27));
1335 final double a4Low = a4 - a4High;
1336 final double b4High = Double.longBitsToDouble(Double.doubleToRawLongBits(b4) & ((-1L) << 27));
1337 final double b4Low = b4 - b4High;
1338
1339
1340 final double prod4High = a4 * b4;
1341 final double prod4Low = a4Low * b4Low - (((prod4High - a4High * b4High) - a4Low * b4High) - a4High * b4Low);
1342
1343
1344 final double s12High = prod1High + prod2High;
1345 final double s12Prime = s12High - prod2High;
1346 final double s12Low = (prod2High - (s12High - s12Prime)) + (prod1High - s12Prime);
1347
1348
1349 final double s123High = s12High + prod3High;
1350 final double s123Prime = s123High - prod3High;
1351 final double s123Low = (prod3High - (s123High - s123Prime)) + (s12High - s123Prime);
1352
1353
1354 final double s1234High = s123High + prod4High;
1355 final double s1234Prime = s1234High - prod4High;
1356 final double s1234Low = (prod4High - (s1234High - s1234Prime)) + (s123High - s1234Prime);
1357
1358
1359
1360 double result = s1234High + (prod1Low + prod2Low + prod3Low + prod4Low + s12Low + s123Low + s1234Low);
1361
1362 if (Double.isNaN(result) || result == 0.0) {
1363
1364
1365
1366 result = a1 * b1 + a2 * b2 + a3 * b3 + a4 * b4;
1367 }
1368
1369 return result;
1370 }
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382 public static boolean equals(float[] x, float[] y) {
1383 if ((x == null) || (y == null)) {
1384 return !((x == null) ^ (y == null));
1385 }
1386 if (x.length != y.length) {
1387 return false;
1388 }
1389 for (int i = 0; i < x.length; ++i) {
1390 if (!Precision.equals(x[i], y[i])) {
1391 return false;
1392 }
1393 }
1394 return true;
1395 }
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407 public static boolean equalsIncludingNaN(float[] x, float[] y) {
1408 if ((x == null) || (y == null)) {
1409 return !((x == null) ^ (y == null));
1410 }
1411 if (x.length != y.length) {
1412 return false;
1413 }
1414 for (int i = 0; i < x.length; ++i) {
1415 if (!Precision.equalsIncludingNaN(x[i], y[i])) {
1416 return false;
1417 }
1418 }
1419 return true;
1420 }
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432 public static boolean equals(double[] x, double[] y) {
1433 if ((x == null) || (y == null)) {
1434 return !((x == null) ^ (y == null));
1435 }
1436 if (x.length != y.length) {
1437 return false;
1438 }
1439 for (int i = 0; i < x.length; ++i) {
1440 if (!Precision.equals(x[i], y[i])) {
1441 return false;
1442 }
1443 }
1444 return true;
1445 }
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459 public static <T extends FieldElement<T>> boolean equals(T[] x, T[] y) {
1460 if ((x == null) || (y == null)) {
1461 return !((x == null) ^ (y == null));
1462 }
1463 if (x.length != y.length) {
1464 return false;
1465 }
1466 for (int i = 0; i < x.length; ++i) {
1467 if (!x[i].equals(y[i])) {
1468 return false;
1469 }
1470 }
1471 return true;
1472 }
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484 public static boolean equalsIncludingNaN(double[] x, double[] y) {
1485 if ((x == null) || (y == null)) {
1486 return !((x == null) ^ (y == null));
1487 }
1488 if (x.length != y.length) {
1489 return false;
1490 }
1491 for (int i = 0; i < x.length; ++i) {
1492 if (!Precision.equalsIncludingNaN(x[i], y[i])) {
1493 return false;
1494 }
1495 }
1496 return true;
1497 }
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508 public static boolean equals(long[] x, long[] y) {
1509 if ((x == null) || (y == null)) {
1510 return !((x == null) ^ (y == null));
1511 }
1512 if (x.length != y.length) {
1513 return false;
1514 }
1515 for (int i = 0; i < x.length; ++i) {
1516 if (x[i] != y[i]) {
1517 return false;
1518 }
1519 }
1520 return true;
1521 }
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532 public static boolean equals(int[] x, int[] y) {
1533 if ((x == null) || (y == null)) {
1534 return !((x == null) ^ (y == null));
1535 }
1536 if (x.length != y.length) {
1537 return false;
1538 }
1539 for (int i = 0; i < x.length; ++i) {
1540 if (x[i] != y[i]) {
1541 return false;
1542 }
1543 }
1544 return true;
1545 }
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556 public static boolean equals(byte[] x, byte[] y) {
1557 if ((x == null) || (y == null)) {
1558 return !((x == null) ^ (y == null));
1559 }
1560 if (x.length != y.length) {
1561 return false;
1562 }
1563 for (int i = 0; i < x.length; ++i) {
1564 if (x[i] != y[i]) {
1565 return false;
1566 }
1567 }
1568 return true;
1569 }
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580 public static boolean equals(short[] x, short[] y) {
1581 if ((x == null) || (y == null)) {
1582 return !((x == null) ^ (y == null));
1583 }
1584 if (x.length != y.length) {
1585 return false;
1586 }
1587 for (int i = 0; i < x.length; ++i) {
1588 if (x[i] != y[i]) {
1589 return false;
1590 }
1591 }
1592 return true;
1593 }
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618 public static double[] normalizeArray(double[] values, double normalizedSum)
1619 throws MathIllegalArgumentException, MathRuntimeException {
1620 if (Double.isInfinite(normalizedSum)) {
1621 throw new MathIllegalArgumentException(LocalizedCoreFormats.NORMALIZE_INFINITE);
1622 }
1623 if (Double.isNaN(normalizedSum)) {
1624 throw new MathIllegalArgumentException(LocalizedCoreFormats.NORMALIZE_NAN);
1625 }
1626 double sum = 0d;
1627 final int len = values.length;
1628 double[] out = new double[len];
1629 for (int i = 0; i < len; i++) {
1630 if (Double.isInfinite(values[i])) {
1631 throw new MathIllegalArgumentException(LocalizedCoreFormats.INFINITE_ARRAY_ELEMENT, values[i], i);
1632 }
1633 if (!Double.isNaN(values[i])) {
1634 sum += values[i];
1635 }
1636 }
1637 if (sum == 0) {
1638 throw new MathRuntimeException(LocalizedCoreFormats.ARRAY_SUMS_TO_ZERO);
1639 }
1640 for (int i = 0; i < len; i++) {
1641 if (Double.isNaN(values[i])) {
1642 out[i] = Double.NaN;
1643 } else {
1644 out[i] = values[i] * normalizedSum / sum;
1645 }
1646 }
1647 return out;
1648 }
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659 public static <T extends FieldElement<T>> T[] buildArray(final Field<T> field, final int length) {
1660 @SuppressWarnings("unchecked")
1661 T[] array = (T[]) Array.newInstance(field.getRuntimeClass(), length);
1662 Arrays.fill(array, field.getZero());
1663 return array;
1664 }
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677 @SuppressWarnings("unchecked")
1678 public static <T extends FieldElement<T>> T[][] buildArray(final Field<T> field, final int rows, final int columns) {
1679 final T[][] array;
1680 if (columns < 0) {
1681 T[] dummyRow = buildArray(field, 0);
1682 array = (T[][]) Array.newInstance(dummyRow.getClass(), rows);
1683 } else {
1684 array = (T[][]) Array.newInstance(field.getRuntimeClass(), rows, columns);
1685 for (int i = 0; i < rows; ++i) {
1686 Arrays.fill(array[i], field.getZero());
1687 }
1688 }
1689 return array;
1690 }
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705 @SuppressWarnings("unchecked")
1706 public static <T extends FieldElement<T>> T[][][] buildArray(final Field<T> field, final int l1, final int l2, final int l3) {
1707 final T[][][] array;
1708 if (l3 < 0) {
1709 T[] dummyRow = buildArray(field, 0);
1710 array = (T[][][]) Array.newInstance(dummyRow.getClass(), l1, l2);
1711 } else {
1712 array = (T[][][]) Array.newInstance(field.getRuntimeClass(), l1, l2, l3);
1713 for (int i = 0; i < l1; ++i) {
1714 for (int j = 0; j < l2; ++j) {
1715 Arrays.fill(array[i][j], field.getZero());
1716 }
1717 }
1718 }
1719 return array;
1720 }
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740 public static double[] convolve(double[] x, double[] h)
1741 throws MathIllegalArgumentException, NullArgumentException {
1742 MathUtils.checkNotNull(x);
1743 MathUtils.checkNotNull(h);
1744
1745 final int xLen = x.length;
1746 final int hLen = h.length;
1747
1748 if (xLen == 0 || hLen == 0) {
1749 throw new MathIllegalArgumentException(LocalizedCoreFormats.NO_DATA);
1750 }
1751
1752
1753 final int totalLength = xLen + hLen - 1;
1754 final double[] y = new double[totalLength];
1755
1756
1757 for (int n = 0; n < totalLength; n++) {
1758 double yn = 0;
1759 int k = FastMath.max(0, n + 1 - xLen);
1760 int j = n - k;
1761 while (k < hLen && j >= 0) {
1762 yn += x[j--] * h[k++];
1763 }
1764 y[n] = yn;
1765 }
1766
1767 return y;
1768 }
1769
1770
1771
1772
1773
1774 public enum Position {
1775
1776 HEAD,
1777
1778 TAIL
1779 }
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794 public static void shuffle(int[] list,
1795 int start,
1796 Position pos) {
1797 shuffle(list, start, pos, new Well19937c());
1798 }
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814 public static void shuffle(int[] list,
1815 int start,
1816 Position pos,
1817 RandomGenerator rng) {
1818 switch (pos) {
1819 case TAIL:
1820 for (int i = list.length - 1; i > start; i--) {
1821 final int target = start + rng.nextInt(i - start + 1);
1822 final int temp = list[target];
1823 list[target] = list[i];
1824 list[i] = temp;
1825 }
1826 break;
1827
1828 case HEAD:
1829 for (int i = 0; i < start; i++) {
1830 final int target = i + rng.nextInt(start - i + 1);
1831 final int temp = list[target];
1832 list[target] = list[i];
1833 list[i] = temp;
1834 }
1835 break;
1836
1837 default:
1838 throw MathRuntimeException.createInternalError();
1839 }
1840 }
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850 public static void shuffle(int[] list,
1851 RandomGenerator rng) {
1852 shuffle(list, 0, Position.TAIL, rng);
1853 }
1854
1855
1856
1857
1858
1859
1860
1861
1862 public static void shuffle(int[] list) {
1863 shuffle(list, new Well19937c());
1864 }
1865
1866
1867
1868
1869
1870
1871
1872
1873 public static int[] natural(int n) {
1874 return sequence(n, 0, 1);
1875 }
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888 public static int[] sequence(int size,
1889 int start,
1890 int stride) {
1891 final int[] a = new int[size];
1892 for (int i = 0; i < size; i++) {
1893 a[i] = start + i * stride;
1894 }
1895 return a;
1896 }
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916 public static boolean verifyValues(final double[] values, final int begin, final int length)
1917 throws MathIllegalArgumentException {
1918 return verifyValues(values, begin, length, false);
1919 }
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940 public static boolean verifyValues(final double[] values, final int begin,
1941 final int length, final boolean allowEmpty) throws MathIllegalArgumentException {
1942
1943 MathUtils.checkNotNull(values, LocalizedCoreFormats.INPUT_ARRAY);
1944
1945 if (begin < 0) {
1946 throw new MathIllegalArgumentException(LocalizedCoreFormats.START_POSITION, begin);
1947 }
1948
1949 if (length < 0) {
1950 throw new MathIllegalArgumentException(LocalizedCoreFormats.LENGTH, length);
1951 }
1952
1953 if (begin + length > values.length) {
1954 throw new MathIllegalArgumentException(LocalizedCoreFormats.SUBARRAY_ENDS_AFTER_ARRAY_END,
1955 begin + length, values.length, true);
1956 }
1957
1958 if (length == 0 && !allowEmpty) {
1959 return false;
1960 }
1961
1962 return true;
1963 }
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991 public static boolean verifyValues(
1992 final double[] values,
1993 final double[] weights,
1994 final int begin,
1995 final int length) throws MathIllegalArgumentException {
1996 return verifyValues(values, weights, begin, length, false);
1997 }
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030 public static boolean verifyValues(final double[] values, final double[] weights,
2031 final int begin, final int length, final boolean allowEmpty) throws MathIllegalArgumentException {
2032
2033 MathUtils.checkNotNull(weights, LocalizedCoreFormats.INPUT_ARRAY);
2034 MathUtils.checkNotNull(values, LocalizedCoreFormats.INPUT_ARRAY);
2035
2036 checkEqualLength(weights, values);
2037
2038 boolean containsPositiveWeight = false;
2039 for (int i = begin; i < begin + length; i++) {
2040 final double weight = weights[i];
2041 if (Double.isNaN(weight)) {
2042 throw new MathIllegalArgumentException(LocalizedCoreFormats.NAN_ELEMENT_AT_INDEX, i);
2043 }
2044 if (Double.isInfinite(weight)) {
2045 throw new MathIllegalArgumentException(LocalizedCoreFormats.INFINITE_ARRAY_ELEMENT, weight, i);
2046 }
2047 if (weight < 0) {
2048 throw new MathIllegalArgumentException(LocalizedCoreFormats.NEGATIVE_ELEMENT_AT_INDEX, i, weight);
2049 }
2050 if (!containsPositiveWeight && weight > 0.0) {
2051 containsPositiveWeight = true;
2052 }
2053 }
2054
2055 if (!containsPositiveWeight) {
2056 throw new MathIllegalArgumentException(LocalizedCoreFormats.WEIGHT_AT_LEAST_ONE_NON_ZERO);
2057 }
2058
2059 return verifyValues(values, begin, length, allowEmpty);
2060 }
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072 public static double[] concatenate(double[]... x) {
2073 int combinedLength = 0;
2074 for (double[] a : x) {
2075 combinedLength += a.length;
2076 }
2077 int offset = 0;
2078 final double[] combined = new double[combinedLength];
2079 for (double[] doubles : x) {
2080 final int curLength = doubles.length;
2081 System.arraycopy(doubles, 0, combined, offset, curLength);
2082 offset += curLength;
2083 }
2084 return combined;
2085 }
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100 public static double[] unique(double[] data) {
2101 NavigableSet<Double> values = new TreeSet<>();
2102 for (double datum : data) {
2103 values.add(datum);
2104 }
2105 final int count = values.size();
2106 final double[] out = new double[count];
2107 Iterator<Double> iterator = values.descendingIterator();
2108 int i = 0;
2109 while (iterator.hasNext()) {
2110 out[i++] = iterator.next();
2111 }
2112 return out;
2113 }
2114 }