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.Collections;
29 import java.util.Comparator;
30 import java.util.Iterator;
31 import java.util.List;
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 (int i = 0; i < in.length; i++) {
711 if (in[i] <= 0) {
712 throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_SMALL_BOUND_EXCLUDED,
713 in[i], 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 (int i = 0; i < in.length; i++) {
742 if (in[i] < 0) {
743 throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_SMALL, in[i], 0);
744 }
745 }
746 }
747
748
749
750
751
752
753
754 public static void checkNonNegative(final long[][] in)
755 throws MathIllegalArgumentException {
756 for (int i = 0; i < in.length; i ++) {
757 for (int j = 0; j < in[i].length; j++) {
758 if (in[i][j] < 0) {
759 throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_SMALL, in[i][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 (int i = 0; i < v.length; i++) {
838 double xabs = FastMath.abs(v[i]);
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 yListLen = yList.length;
958 final int len = x.length;
959
960 for (int j = 0; j < yListLen; j++) {
961 final double[] y = yList[j];
962 if (y == null) {
963 throw new NullArgumentException();
964 }
965 if (y.length != len) {
966 throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
967 y.length, len);
968 }
969 }
970
971
972 final List<PairDoubleInteger> list = new ArrayList<>(len);
973 for (int i = 0; i < len; i++) {
974 list.add(new PairDoubleInteger(x[i], i));
975 }
976
977
978 final Comparator<PairDoubleInteger> comp =
979 dir == MathArrays.OrderDirection.INCREASING ?
980 new Comparator<PairDoubleInteger>() {
981
982 @Override
983 public int compare(PairDoubleInteger o1,
984 PairDoubleInteger o2) {
985 return Double.compare(o1.getKey(), o2.getKey());
986 }
987 } :
988 new Comparator<PairDoubleInteger>() {
989
990 @Override
991 public int compare(PairDoubleInteger o1,
992 PairDoubleInteger o2) {
993 return Double.compare(o2.getKey(), o1.getKey());
994 }
995 };
996
997
998 Collections.sort(list, comp);
999
1000
1001
1002
1003 final int[] indices = new int[len];
1004 for (int i = 0; i < len; i++) {
1005 final PairDoubleInteger e = list.get(i);
1006 x[i] = e.getKey();
1007 indices[i] = e.getValue();
1008 }
1009
1010
1011
1012 for (int j = 0; j < yListLen; j++) {
1013
1014 final double[] yInPlace = yList[j];
1015 final double[] yOrig = yInPlace.clone();
1016
1017 for (int i = 0; i < len; i++) {
1018 yInPlace[i] = yOrig[indices[i]];
1019 }
1020 }
1021 }
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040 public static double linearCombination(final double[] a, final double[] b)
1041 throws MathIllegalArgumentException {
1042 checkEqualLength(a, b);
1043 final int len = a.length;
1044
1045 if (len == 1) {
1046
1047 return a[0] * b[0];
1048 }
1049
1050 final double[] prodHigh = new double[len];
1051 double prodLowSum = 0;
1052
1053 for (int i = 0; i < len; i++) {
1054 final double ai = a[i];
1055 final double aHigh = Double.longBitsToDouble(Double.doubleToRawLongBits(ai) & ((-1L) << 27));
1056 final double aLow = ai - aHigh;
1057
1058 final double bi = b[i];
1059 final double bHigh = Double.longBitsToDouble(Double.doubleToRawLongBits(bi) & ((-1L) << 27));
1060 final double bLow = bi - bHigh;
1061 prodHigh[i] = ai * bi;
1062 final double prodLow = aLow * bLow - (((prodHigh[i] -
1063 aHigh * bHigh) -
1064 aLow * bHigh) -
1065 aHigh * bLow);
1066 prodLowSum += prodLow;
1067 }
1068
1069
1070 final double prodHighCur = prodHigh[0];
1071 double prodHighNext = prodHigh[1];
1072 double sHighPrev = prodHighCur + prodHighNext;
1073 double sPrime = sHighPrev - prodHighNext;
1074 double sLowSum = (prodHighNext - (sHighPrev - sPrime)) + (prodHighCur - sPrime);
1075
1076 final int lenMinusOne = len - 1;
1077 for (int i = 1; i < lenMinusOne; i++) {
1078 prodHighNext = prodHigh[i + 1];
1079 final double sHighCur = sHighPrev + prodHighNext;
1080 sPrime = sHighCur - prodHighNext;
1081 sLowSum += (prodHighNext - (sHighCur - sPrime)) + (sHighPrev - sPrime);
1082 sHighPrev = sHighCur;
1083 }
1084
1085 double result = sHighPrev + (prodLowSum + sLowSum);
1086
1087 if (Double.isNaN(result) || result == 0.0) {
1088
1089
1090
1091 result = a[0] * b[0];
1092 for (int i = 1; i < len; ++i) {
1093 result += a[i] * b[i];
1094 }
1095 }
1096
1097 return result;
1098 }
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121 public static double linearCombination(final double a1, final double b1,
1122 final double a2, final double b2) {
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135 final double a1High = Double.longBitsToDouble(Double.doubleToRawLongBits(a1) & ((-1L) << 27));
1136 final double a1Low = a1 - a1High;
1137 final double b1High = Double.longBitsToDouble(Double.doubleToRawLongBits(b1) & ((-1L) << 27));
1138 final double b1Low = b1 - b1High;
1139
1140
1141 final double prod1High = a1 * b1;
1142 final double prod1Low = a1Low * b1Low - (((prod1High - a1High * b1High) - a1Low * b1High) - a1High * b1Low);
1143
1144
1145 final double a2High = Double.longBitsToDouble(Double.doubleToRawLongBits(a2) & ((-1L) << 27));
1146 final double a2Low = a2 - a2High;
1147 final double b2High = Double.longBitsToDouble(Double.doubleToRawLongBits(b2) & ((-1L) << 27));
1148 final double b2Low = b2 - b2High;
1149
1150
1151 final double prod2High = a2 * b2;
1152 final double prod2Low = a2Low * b2Low - (((prod2High - a2High * b2High) - a2Low * b2High) - a2High * b2Low);
1153
1154
1155 final double s12High = prod1High + prod2High;
1156 final double s12Prime = s12High - prod2High;
1157 final double s12Low = (prod2High - (s12High - s12Prime)) + (prod1High - s12Prime);
1158
1159
1160
1161 double result = s12High + (prod1Low + prod2Low + s12Low);
1162
1163 if (Double.isNaN(result) || result == 0.0) {
1164
1165
1166
1167 result = a1 * b1 + a2 * b2;
1168 }
1169
1170 return result;
1171 }
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196 public static double linearCombination(final double a1, final double b1,
1197 final double a2, final double b2,
1198 final double a3, final double b3) {
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211 final double a1High = Double.longBitsToDouble(Double.doubleToRawLongBits(a1) & ((-1L) << 27));
1212 final double a1Low = a1 - a1High;
1213 final double b1High = Double.longBitsToDouble(Double.doubleToRawLongBits(b1) & ((-1L) << 27));
1214 final double b1Low = b1 - b1High;
1215
1216
1217 final double prod1High = a1 * b1;
1218 final double prod1Low = a1Low * b1Low - (((prod1High - a1High * b1High) - a1Low * b1High) - a1High * b1Low);
1219
1220
1221 final double a2High = Double.longBitsToDouble(Double.doubleToRawLongBits(a2) & ((-1L) << 27));
1222 final double a2Low = a2 - a2High;
1223 final double b2High = Double.longBitsToDouble(Double.doubleToRawLongBits(b2) & ((-1L) << 27));
1224 final double b2Low = b2 - b2High;
1225
1226
1227 final double prod2High = a2 * b2;
1228 final double prod2Low = a2Low * b2Low - (((prod2High - a2High * b2High) - a2Low * b2High) - a2High * b2Low);
1229
1230
1231 final double a3High = Double.longBitsToDouble(Double.doubleToRawLongBits(a3) & ((-1L) << 27));
1232 final double a3Low = a3 - a3High;
1233 final double b3High = Double.longBitsToDouble(Double.doubleToRawLongBits(b3) & ((-1L) << 27));
1234 final double b3Low = b3 - b3High;
1235
1236
1237 final double prod3High = a3 * b3;
1238 final double prod3Low = a3Low * b3Low - (((prod3High - a3High * b3High) - a3Low * b3High) - a3High * b3Low);
1239
1240
1241 final double s12High = prod1High + prod2High;
1242 final double s12Prime = s12High - prod2High;
1243 final double s12Low = (prod2High - (s12High - s12Prime)) + (prod1High - s12Prime);
1244
1245
1246 final double s123High = s12High + prod3High;
1247 final double s123Prime = s123High - prod3High;
1248 final double s123Low = (prod3High - (s123High - s123Prime)) + (s12High - s123Prime);
1249
1250
1251
1252 double result = s123High + (prod1Low + prod2Low + prod3Low + s12Low + s123Low);
1253
1254 if (Double.isNaN(result) || result == 0.0) {
1255
1256
1257
1258 result = a1 * b1 + a2 * b2 + a3 * b3;
1259 }
1260
1261 return result;
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
1289
1290
1291 public static double linearCombination(final double a1, final double b1,
1292 final double a2, final double b2,
1293 final double a3, final double b3,
1294 final double a4, final double b4) {
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307 final double a1High = Double.longBitsToDouble(Double.doubleToRawLongBits(a1) & ((-1L) << 27));
1308 final double a1Low = a1 - a1High;
1309 final double b1High = Double.longBitsToDouble(Double.doubleToRawLongBits(b1) & ((-1L) << 27));
1310 final double b1Low = b1 - b1High;
1311
1312
1313 final double prod1High = a1 * b1;
1314 final double prod1Low = a1Low * b1Low - (((prod1High - a1High * b1High) - a1Low * b1High) - a1High * b1Low);
1315
1316
1317 final double a2High = Double.longBitsToDouble(Double.doubleToRawLongBits(a2) & ((-1L) << 27));
1318 final double a2Low = a2 - a2High;
1319 final double b2High = Double.longBitsToDouble(Double.doubleToRawLongBits(b2) & ((-1L) << 27));
1320 final double b2Low = b2 - b2High;
1321
1322
1323 final double prod2High = a2 * b2;
1324 final double prod2Low = a2Low * b2Low - (((prod2High - a2High * b2High) - a2Low * b2High) - a2High * b2Low);
1325
1326
1327 final double a3High = Double.longBitsToDouble(Double.doubleToRawLongBits(a3) & ((-1L) << 27));
1328 final double a3Low = a3 - a3High;
1329 final double b3High = Double.longBitsToDouble(Double.doubleToRawLongBits(b3) & ((-1L) << 27));
1330 final double b3Low = b3 - b3High;
1331
1332
1333 final double prod3High = a3 * b3;
1334 final double prod3Low = a3Low * b3Low - (((prod3High - a3High * b3High) - a3Low * b3High) - a3High * b3Low);
1335
1336
1337 final double a4High = Double.longBitsToDouble(Double.doubleToRawLongBits(a4) & ((-1L) << 27));
1338 final double a4Low = a4 - a4High;
1339 final double b4High = Double.longBitsToDouble(Double.doubleToRawLongBits(b4) & ((-1L) << 27));
1340 final double b4Low = b4 - b4High;
1341
1342
1343 final double prod4High = a4 * b4;
1344 final double prod4Low = a4Low * b4Low - (((prod4High - a4High * b4High) - a4Low * b4High) - a4High * b4Low);
1345
1346
1347 final double s12High = prod1High + prod2High;
1348 final double s12Prime = s12High - prod2High;
1349 final double s12Low = (prod2High - (s12High - s12Prime)) + (prod1High - s12Prime);
1350
1351
1352 final double s123High = s12High + prod3High;
1353 final double s123Prime = s123High - prod3High;
1354 final double s123Low = (prod3High - (s123High - s123Prime)) + (s12High - s123Prime);
1355
1356
1357 final double s1234High = s123High + prod4High;
1358 final double s1234Prime = s1234High - prod4High;
1359 final double s1234Low = (prod4High - (s1234High - s1234Prime)) + (s123High - s1234Prime);
1360
1361
1362
1363 double result = s1234High + (prod1Low + prod2Low + prod3Low + prod4Low + s12Low + s123Low + s1234Low);
1364
1365 if (Double.isNaN(result) || result == 0.0) {
1366
1367
1368
1369 result = a1 * b1 + a2 * b2 + a3 * b3 + a4 * b4;
1370 }
1371
1372 return result;
1373 }
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385 public static boolean equals(float[] x, float[] y) {
1386 if ((x == null) || (y == null)) {
1387 return !((x == null) ^ (y == null));
1388 }
1389 if (x.length != y.length) {
1390 return false;
1391 }
1392 for (int i = 0; i < x.length; ++i) {
1393 if (!Precision.equals(x[i], y[i])) {
1394 return false;
1395 }
1396 }
1397 return true;
1398 }
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410 public static boolean equalsIncludingNaN(float[] x, float[] y) {
1411 if ((x == null) || (y == null)) {
1412 return !((x == null) ^ (y == null));
1413 }
1414 if (x.length != y.length) {
1415 return false;
1416 }
1417 for (int i = 0; i < x.length; ++i) {
1418 if (!Precision.equalsIncludingNaN(x[i], y[i])) {
1419 return false;
1420 }
1421 }
1422 return true;
1423 }
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435 public static boolean equals(double[] x, double[] y) {
1436 if ((x == null) || (y == null)) {
1437 return !((x == null) ^ (y == null));
1438 }
1439 if (x.length != y.length) {
1440 return false;
1441 }
1442 for (int i = 0; i < x.length; ++i) {
1443 if (!Precision.equals(x[i], y[i])) {
1444 return false;
1445 }
1446 }
1447 return true;
1448 }
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460 public static boolean equalsIncludingNaN(double[] x, double[] y) {
1461 if ((x == null) || (y == null)) {
1462 return !((x == null) ^ (y == null));
1463 }
1464 if (x.length != y.length) {
1465 return false;
1466 }
1467 for (int i = 0; i < x.length; ++i) {
1468 if (!Precision.equalsIncludingNaN(x[i], y[i])) {
1469 return false;
1470 }
1471 }
1472 return true;
1473 }
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484 public static boolean equals(long[] x, long[] 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 (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(int[] x, int[] 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(byte[] x, byte[] 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(short[] x, short[] 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
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594 public static double[] normalizeArray(double[] values, double normalizedSum)
1595 throws MathIllegalArgumentException, MathRuntimeException {
1596 if (Double.isInfinite(normalizedSum)) {
1597 throw new MathIllegalArgumentException(LocalizedCoreFormats.NORMALIZE_INFINITE);
1598 }
1599 if (Double.isNaN(normalizedSum)) {
1600 throw new MathIllegalArgumentException(LocalizedCoreFormats.NORMALIZE_NAN);
1601 }
1602 double sum = 0d;
1603 final int len = values.length;
1604 double[] out = new double[len];
1605 for (int i = 0; i < len; i++) {
1606 if (Double.isInfinite(values[i])) {
1607 throw new MathIllegalArgumentException(LocalizedCoreFormats.INFINITE_ARRAY_ELEMENT, values[i], i);
1608 }
1609 if (!Double.isNaN(values[i])) {
1610 sum += values[i];
1611 }
1612 }
1613 if (sum == 0) {
1614 throw new MathRuntimeException(LocalizedCoreFormats.ARRAY_SUMS_TO_ZERO);
1615 }
1616 for (int i = 0; i < len; i++) {
1617 if (Double.isNaN(values[i])) {
1618 out[i] = Double.NaN;
1619 } else {
1620 out[i] = values[i] * normalizedSum / sum;
1621 }
1622 }
1623 return out;
1624 }
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635 public static <T extends FieldElement<T>> T[] buildArray(final Field<T> field, final int length) {
1636 @SuppressWarnings("unchecked")
1637 T[] array = (T[]) Array.newInstance(field.getRuntimeClass(), length);
1638 Arrays.fill(array, field.getZero());
1639 return array;
1640 }
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653 @SuppressWarnings("unchecked")
1654 public static <T extends FieldElement<T>> T[][] buildArray(final Field<T> field, final int rows, final int columns) {
1655 final T[][] array;
1656 if (columns < 0) {
1657 T[] dummyRow = buildArray(field, 0);
1658 array = (T[][]) Array.newInstance(dummyRow.getClass(), rows);
1659 } else {
1660 array = (T[][]) Array.newInstance(field.getRuntimeClass(),
1661 new int[] {
1662 rows, columns
1663 });
1664 for (int i = 0; i < rows; ++i) {
1665 Arrays.fill(array[i], field.getZero());
1666 }
1667 }
1668 return array;
1669 }
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684 @SuppressWarnings("unchecked")
1685 public static <T extends FieldElement<T>> T[][][] buildArray(final Field<T> field, final int l1, final int l2, final int l3) {
1686 final T[][][] array;
1687 if (l3 < 0) {
1688 T[] dummyRow = buildArray(field, 0);
1689 array = (T[][][]) Array.newInstance(dummyRow.getClass(), l1, l2);
1690 } else {
1691 array = (T[][][]) Array.newInstance(field.getRuntimeClass(),
1692 new int[] {
1693 l1, l2, l3
1694 });
1695 for (int i = 0; i < l1; ++i) {
1696 for (int j = 0; j < l2; ++j) {
1697 Arrays.fill(array[i][j], field.getZero());
1698 }
1699 }
1700 }
1701 return array;
1702 }
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722 public static double[] convolve(double[] x, double[] h)
1723 throws MathIllegalArgumentException, NullArgumentException {
1724 MathUtils.checkNotNull(x);
1725 MathUtils.checkNotNull(h);
1726
1727 final int xLen = x.length;
1728 final int hLen = h.length;
1729
1730 if (xLen == 0 || hLen == 0) {
1731 throw new MathIllegalArgumentException(LocalizedCoreFormats.NO_DATA);
1732 }
1733
1734
1735 final int totalLength = xLen + hLen - 1;
1736 final double[] y = new double[totalLength];
1737
1738
1739 for (int n = 0; n < totalLength; n++) {
1740 double yn = 0;
1741 int k = FastMath.max(0, n + 1 - xLen);
1742 int j = n - k;
1743 while (k < hLen && j >= 0) {
1744 yn += x[j--] * h[k++];
1745 }
1746 y[n] = yn;
1747 }
1748
1749 return y;
1750 }
1751
1752
1753
1754
1755
1756 public enum Position {
1757
1758 HEAD,
1759
1760 TAIL
1761 }
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776 public static void shuffle(int[] list,
1777 int start,
1778 Position pos) {
1779 shuffle(list, start, pos, new Well19937c());
1780 }
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796 public static void shuffle(int[] list,
1797 int start,
1798 Position pos,
1799 RandomGenerator rng) {
1800 switch (pos) {
1801 case TAIL:
1802 for (int i = list.length - 1; i > start; i--) {
1803 final int target = start + rng.nextInt(i - start + 1);
1804 final int temp = list[target];
1805 list[target] = list[i];
1806 list[i] = temp;
1807 }
1808 break;
1809
1810 case HEAD:
1811 for (int i = 0; i < start; i++) {
1812 final int target = i + rng.nextInt(start - i + 1);
1813 final int temp = list[target];
1814 list[target] = list[i];
1815 list[i] = temp;
1816 }
1817 break;
1818
1819 default:
1820 throw MathRuntimeException.createInternalError();
1821 }
1822 }
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832 public static void shuffle(int[] list,
1833 RandomGenerator rng) {
1834 shuffle(list, 0, Position.TAIL, rng);
1835 }
1836
1837
1838
1839
1840
1841
1842
1843
1844 public static void shuffle(int[] list) {
1845 shuffle(list, new Well19937c());
1846 }
1847
1848
1849
1850
1851
1852
1853
1854
1855 public static int[] natural(int n) {
1856 return sequence(n, 0, 1);
1857 }
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870 public static int[] sequence(int size,
1871 int start,
1872 int stride) {
1873 final int[] a = new int[size];
1874 for (int i = 0; i < size; i++) {
1875 a[i] = start + i * stride;
1876 }
1877 return a;
1878 }
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898 public static boolean verifyValues(final double[] values, final int begin, final int length)
1899 throws MathIllegalArgumentException {
1900 return verifyValues(values, begin, length, false);
1901 }
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922 public static boolean verifyValues(final double[] values, final int begin,
1923 final int length, final boolean allowEmpty) throws MathIllegalArgumentException {
1924
1925 MathUtils.checkNotNull(values, LocalizedCoreFormats.INPUT_ARRAY);
1926
1927 if (begin < 0) {
1928 throw new MathIllegalArgumentException(LocalizedCoreFormats.START_POSITION, Integer.valueOf(begin));
1929 }
1930
1931 if (length < 0) {
1932 throw new MathIllegalArgumentException(LocalizedCoreFormats.LENGTH, Integer.valueOf(length));
1933 }
1934
1935 if (begin + length > values.length) {
1936 throw new MathIllegalArgumentException(LocalizedCoreFormats.SUBARRAY_ENDS_AFTER_ARRAY_END,
1937 Integer.valueOf(begin + length), Integer.valueOf(values.length), true);
1938 }
1939
1940 if (length == 0 && !allowEmpty) {
1941 return false;
1942 }
1943
1944 return true;
1945 }
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973 public static boolean verifyValues(
1974 final double[] values,
1975 final double[] weights,
1976 final int begin,
1977 final int length) throws MathIllegalArgumentException {
1978 return verifyValues(values, weights, begin, length, false);
1979 }
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012 public static boolean verifyValues(final double[] values, final double[] weights,
2013 final int begin, final int length, final boolean allowEmpty) throws MathIllegalArgumentException {
2014
2015 MathUtils.checkNotNull(weights, LocalizedCoreFormats.INPUT_ARRAY);
2016 MathUtils.checkNotNull(values, LocalizedCoreFormats.INPUT_ARRAY);
2017
2018 checkEqualLength(weights, values);
2019
2020 boolean containsPositiveWeight = false;
2021 for (int i = begin; i < begin + length; i++) {
2022 final double weight = weights[i];
2023 if (Double.isNaN(weight)) {
2024 throw new MathIllegalArgumentException(LocalizedCoreFormats.NAN_ELEMENT_AT_INDEX, Integer.valueOf(i));
2025 }
2026 if (Double.isInfinite(weight)) {
2027 throw new MathIllegalArgumentException(LocalizedCoreFormats.INFINITE_ARRAY_ELEMENT, Double.valueOf(weight), Integer.valueOf(i));
2028 }
2029 if (weight < 0) {
2030 throw new MathIllegalArgumentException(LocalizedCoreFormats.NEGATIVE_ELEMENT_AT_INDEX, Integer.valueOf(i), Double.valueOf(weight));
2031 }
2032 if (!containsPositiveWeight && weight > 0.0) {
2033 containsPositiveWeight = true;
2034 }
2035 }
2036
2037 if (!containsPositiveWeight) {
2038 throw new MathIllegalArgumentException(LocalizedCoreFormats.WEIGHT_AT_LEAST_ONE_NON_ZERO);
2039 }
2040
2041 return verifyValues(values, begin, length, allowEmpty);
2042 }
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054 public static double[] concatenate(double[]... x) {
2055 int combinedLength = 0;
2056 for (double[] a : x) {
2057 combinedLength += a.length;
2058 }
2059 int offset = 0;
2060 final double[] combined = new double[combinedLength];
2061 for (int i = 0; i < x.length; i++) {
2062 final int curLength = x[i].length;
2063 System.arraycopy(x[i], 0, combined, offset, curLength);
2064 offset += curLength;
2065 }
2066 return combined;
2067 }
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082 public static double[] unique(double[] data) {
2083 TreeSet<Double> values = new TreeSet<>();
2084 for (int i = 0; i < data.length; i++) {
2085 values.add(data[i]);
2086 }
2087 final int count = values.size();
2088 final double[] out = new double[count];
2089 Iterator<Double> iterator = values.descendingIterator();
2090 int i = 0;
2091 while (iterator.hasNext()) {
2092 out[i++] = iterator.next();
2093 }
2094 return out;
2095 }
2096 }