1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math.linear;
19 import java.io.Serializable;
20 import java.math.BigDecimal;
21
22 import org.apache.commons.math.MathRuntimeException;
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 @Deprecated
55 public class BigMatrixImpl implements BigMatrix, Serializable {
56
57
58 private static final long serialVersionUID = -1011428905656140431L;
59
60
61 protected BigDecimal data[][] = null;
62
63
64
65
66 protected BigDecimal lu[][] = null;
67
68
69 protected int[] permutation = null;
70
71
72 protected int parity = 1;
73
74
75 private int roundingMode = BigDecimal.ROUND_HALF_UP;
76
77
78 private int scale = 64;
79
80
81 private static final BigDecimal TOO_SMALL = new BigDecimal(10E-12);
82
83
84 static final BigDecimal ZERO = new BigDecimal(0);
85
86 static final BigDecimal ONE = new BigDecimal(1);
87
88
89
90
91 public BigMatrixImpl() {
92 }
93
94
95
96
97
98
99
100
101
102 public BigMatrixImpl(int rowDimension, int columnDimension) {
103 if (rowDimension <= 0 ) {
104 throw MathRuntimeException.createIllegalArgumentException(
105 "invalid row dimension {0} (must be positive)",
106 rowDimension);
107 }
108 if (columnDimension <= 0) {
109 throw MathRuntimeException.createIllegalArgumentException(
110 "invalid column dimension {0} (must be positive)",
111 columnDimension);
112 }
113 data = new BigDecimal[rowDimension][columnDimension];
114 lu = null;
115 }
116
117
118
119
120
121
122
123
124
125
126
127
128
129 public BigMatrixImpl(BigDecimal[][] d) {
130 this.copyIn(d);
131 lu = null;
132 }
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149 public BigMatrixImpl(BigDecimal[][] d, boolean copyArray) {
150 if (copyArray) {
151 copyIn(d);
152 } else {
153 if (d == null) {
154 throw new NullPointerException();
155 }
156 final int nRows = d.length;
157 if (nRows == 0) {
158 throw MathRuntimeException.createIllegalArgumentException("matrix must have at least one row");
159 }
160
161 final int nCols = d[0].length;
162 if (nCols == 0) {
163 throw MathRuntimeException.createIllegalArgumentException("matrix must have at least one column");
164 }
165 for (int r = 1; r < nRows; r++) {
166 if (d[r].length != nCols) {
167 throw MathRuntimeException.createIllegalArgumentException(
168 "some rows have length {0} while others have length {1}",
169 nCols, d[r].length);
170 }
171 }
172 data = d;
173 }
174 lu = null;
175 }
176
177
178
179
180
181
182
183
184
185
186
187
188 public BigMatrixImpl(double[][] d) {
189 final int nRows = d.length;
190 if (nRows == 0) {
191 throw MathRuntimeException.createIllegalArgumentException("matrix must have at least one row");
192 }
193
194 final int nCols = d[0].length;
195 if (nCols == 0) {
196 throw MathRuntimeException.createIllegalArgumentException("matrix must have at least one column");
197 }
198 for (int row = 1; row < nRows; row++) {
199 if (d[row].length != nCols) {
200 throw MathRuntimeException.createIllegalArgumentException(
201 "some rows have length {0} while others have length {1}",
202 nCols, d[row].length);
203 }
204 }
205 this.copyIn(d);
206 lu = null;
207 }
208
209
210
211
212
213
214
215
216
217
218 public BigMatrixImpl(String[][] d) {
219 final int nRows = d.length;
220 if (nRows == 0) {
221 throw MathRuntimeException.createIllegalArgumentException("matrix must have at least one row");
222 }
223
224 final int nCols = d[0].length;
225 if (nCols == 0) {
226 throw MathRuntimeException.createIllegalArgumentException("matrix must have at least one column");
227 }
228 for (int row = 1; row < nRows; row++) {
229 if (d[row].length != nCols) {
230 throw MathRuntimeException.createIllegalArgumentException(
231 "some rows have length {0} while others have length {1}",
232 nCols, d[row].length);
233 }
234 }
235 this.copyIn(d);
236 lu = null;
237 }
238
239
240
241
242
243
244
245
246
247
248 public BigMatrixImpl(BigDecimal[] v) {
249 final int nRows = v.length;
250 data = new BigDecimal[nRows][1];
251 for (int row = 0; row < nRows; row++) {
252 data[row][0] = v[row];
253 }
254 }
255
256
257
258
259
260
261 public BigMatrix copy() {
262 return new BigMatrixImpl(this.copyOut(), false);
263 }
264
265
266
267
268
269
270
271
272 public BigMatrix add(BigMatrix m) throws IllegalArgumentException {
273 try {
274 return add((BigMatrixImpl) m);
275 } catch (ClassCastException cce) {
276
277
278 MatrixUtils.checkAdditionCompatible(this, m);
279
280 final int rowCount = getRowDimension();
281 final int columnCount = getColumnDimension();
282 final BigDecimal[][] outData = new BigDecimal[rowCount][columnCount];
283 for (int row = 0; row < rowCount; row++) {
284 final BigDecimal[] dataRow = data[row];
285 final BigDecimal[] outDataRow = outData[row];
286 for (int col = 0; col < columnCount; col++) {
287 outDataRow[col] = dataRow[col].add(m.getEntry(row, col));
288 }
289 }
290 return new BigMatrixImpl(outData, false);
291 }
292 }
293
294
295
296
297
298
299
300
301 public BigMatrixImpl add(BigMatrixImpl m) throws IllegalArgumentException {
302
303
304 MatrixUtils.checkAdditionCompatible(this, m);
305
306 final int rowCount = getRowDimension();
307 final int columnCount = getColumnDimension();
308 final BigDecimal[][] outData = new BigDecimal[rowCount][columnCount];
309 for (int row = 0; row < rowCount; row++) {
310 final BigDecimal[] dataRow = data[row];
311 final BigDecimal[] mRow = m.data[row];
312 final BigDecimal[] outDataRow = outData[row];
313 for (int col = 0; col < columnCount; col++) {
314 outDataRow[col] = dataRow[col].add(mRow[col]);
315 }
316 }
317 return new BigMatrixImpl(outData, false);
318 }
319
320
321
322
323
324
325
326
327 public BigMatrix subtract(BigMatrix m) throws IllegalArgumentException {
328 try {
329 return subtract((BigMatrixImpl) m);
330 } catch (ClassCastException cce) {
331
332
333 MatrixUtils.checkSubtractionCompatible(this, m);
334
335 final int rowCount = getRowDimension();
336 final int columnCount = getColumnDimension();
337 final BigDecimal[][] outData = new BigDecimal[rowCount][columnCount];
338 for (int row = 0; row < rowCount; row++) {
339 final BigDecimal[] dataRow = data[row];
340 final BigDecimal[] outDataRow = outData[row];
341 for (int col = 0; col < columnCount; col++) {
342 outDataRow[col] = dataRow[col].subtract(getEntry(row, col));
343 }
344 }
345 return new BigMatrixImpl(outData, false);
346 }
347 }
348
349
350
351
352
353
354
355
356 public BigMatrixImpl subtract(BigMatrixImpl m) throws IllegalArgumentException {
357
358
359 MatrixUtils.checkSubtractionCompatible(this, m);
360
361 final int rowCount = getRowDimension();
362 final int columnCount = getColumnDimension();
363 final BigDecimal[][] outData = new BigDecimal[rowCount][columnCount];
364 for (int row = 0; row < rowCount; row++) {
365 final BigDecimal[] dataRow = data[row];
366 final BigDecimal[] mRow = m.data[row];
367 final BigDecimal[] outDataRow = outData[row];
368 for (int col = 0; col < columnCount; col++) {
369 outDataRow[col] = dataRow[col].subtract(mRow[col]);
370 }
371 }
372 return new BigMatrixImpl(outData, false);
373 }
374
375
376
377
378
379
380
381 public BigMatrix scalarAdd(BigDecimal d) {
382 final int rowCount = getRowDimension();
383 final int columnCount = getColumnDimension();
384 final BigDecimal[][] outData = new BigDecimal[rowCount][columnCount];
385 for (int row = 0; row < rowCount; row++) {
386 final BigDecimal[] dataRow = data[row];
387 final BigDecimal[] outDataRow = outData[row];
388 for (int col = 0; col < columnCount; col++) {
389 outDataRow[col] = dataRow[col].add(d);
390 }
391 }
392 return new BigMatrixImpl(outData, false);
393 }
394
395
396
397
398
399
400 public BigMatrix scalarMultiply(BigDecimal d) {
401 final int rowCount = getRowDimension();
402 final int columnCount = getColumnDimension();
403 final BigDecimal[][] outData = new BigDecimal[rowCount][columnCount];
404 for (int row = 0; row < rowCount; row++) {
405 final BigDecimal[] dataRow = data[row];
406 final BigDecimal[] outDataRow = outData[row];
407 for (int col = 0; col < columnCount; col++) {
408 outDataRow[col] = dataRow[col].multiply(d);
409 }
410 }
411 return new BigMatrixImpl(outData, false);
412 }
413
414
415
416
417
418
419
420
421 public BigMatrix multiply(BigMatrix m) throws IllegalArgumentException {
422 try {
423 return multiply((BigMatrixImpl) m);
424 } catch (ClassCastException cce) {
425
426
427 MatrixUtils.checkMultiplicationCompatible(this, m);
428
429 final int nRows = this.getRowDimension();
430 final int nCols = m.getColumnDimension();
431 final int nSum = this.getColumnDimension();
432 final BigDecimal[][] outData = new BigDecimal[nRows][nCols];
433 for (int row = 0; row < nRows; row++) {
434 final BigDecimal[] dataRow = data[row];
435 final BigDecimal[] outDataRow = outData[row];
436 for (int col = 0; col < nCols; col++) {
437 BigDecimal sum = ZERO;
438 for (int i = 0; i < nSum; i++) {
439 sum = sum.add(dataRow[i].multiply(m.getEntry(i, col)));
440 }
441 outDataRow[col] = sum;
442 }
443 }
444 return new BigMatrixImpl(outData, false);
445 }
446 }
447
448
449
450
451
452
453
454
455 public BigMatrixImpl multiply(BigMatrixImpl m) throws IllegalArgumentException {
456
457
458 MatrixUtils.checkMultiplicationCompatible(this, m);
459
460 final int nRows = this.getRowDimension();
461 final int nCols = m.getColumnDimension();
462 final int nSum = this.getColumnDimension();
463 final BigDecimal[][] outData = new BigDecimal[nRows][nCols];
464 for (int row = 0; row < nRows; row++) {
465 final BigDecimal[] dataRow = data[row];
466 final BigDecimal[] outDataRow = outData[row];
467 for (int col = 0; col < nCols; col++) {
468 BigDecimal sum = ZERO;
469 for (int i = 0; i < nSum; i++) {
470 sum = sum.add(dataRow[i].multiply(m.data[i][col]));
471 }
472 outDataRow[col] = sum;
473 }
474 }
475 return new BigMatrixImpl(outData, false);
476 }
477
478
479
480
481
482
483
484
485 public BigMatrix preMultiply(BigMatrix m) throws IllegalArgumentException {
486 return m.multiply(this);
487 }
488
489
490
491
492
493
494
495
496 public BigDecimal[][] getData() {
497 return copyOut();
498 }
499
500
501
502
503
504
505
506
507
508 public double[][] getDataAsDoubleArray() {
509 final int nRows = getRowDimension();
510 final int nCols = getColumnDimension();
511 final double d[][] = new double[nRows][nCols];
512 for (int i = 0; i < nRows; i++) {
513 for (int j = 0; j < nCols; j++) {
514 d[i][j] = data[i][j].doubleValue();
515 }
516 }
517 return d;
518 }
519
520
521
522
523
524
525
526
527 public BigDecimal[][] getDataRef() {
528 return data;
529 }
530
531
532
533
534
535
536
537 public int getRoundingMode() {
538 return roundingMode;
539 }
540
541
542
543
544
545
546 public void setRoundingMode(int roundingMode) {
547 this.roundingMode = roundingMode;
548 }
549
550
551
552
553
554
555
556 public int getScale() {
557 return scale;
558 }
559
560
561
562
563
564
565 public void setScale(int scale) {
566 this.scale = scale;
567 }
568
569
570
571
572
573
574
575 public BigDecimal getNorm() {
576 BigDecimal maxColSum = ZERO;
577 for (int col = 0; col < this.getColumnDimension(); col++) {
578 BigDecimal sum = ZERO;
579 for (int row = 0; row < this.getRowDimension(); row++) {
580 sum = sum.add(data[row][col].abs());
581 }
582 maxColSum = maxColSum.max(sum);
583 }
584 return maxColSum;
585 }
586
587
588
589
590
591
592
593
594
595
596
597
598
599 public BigMatrix getSubMatrix(int startRow, int endRow,
600 int startColumn, int endColumn)
601 throws MatrixIndexException {
602
603 MatrixUtils.checkRowIndex(this, startRow);
604 MatrixUtils.checkRowIndex(this, endRow);
605 if (startRow > endRow) {
606 throw new MatrixIndexException("initial row {0} after final row {1}",
607 startRow, endRow);
608 }
609
610 MatrixUtils.checkColumnIndex(this, startColumn);
611 MatrixUtils.checkColumnIndex(this, endColumn);
612 if (startColumn > endColumn) {
613 throw new MatrixIndexException("initial column {0} after final column {1}",
614 startColumn, endColumn);
615 }
616
617 final BigDecimal[][] subMatrixData =
618 new BigDecimal[endRow - startRow + 1][endColumn - startColumn + 1];
619 for (int i = startRow; i <= endRow; i++) {
620 System.arraycopy(data[i], startColumn,
621 subMatrixData[i - startRow], 0,
622 endColumn - startColumn + 1);
623 }
624
625 return new BigMatrixImpl(subMatrixData, false);
626
627 }
628
629
630
631
632
633
634
635
636
637
638
639
640 public BigMatrix getSubMatrix(int[] selectedRows, int[] selectedColumns)
641 throws MatrixIndexException {
642
643 if (selectedRows.length * selectedColumns.length == 0) {
644 if (selectedRows.length == 0) {
645 throw new MatrixIndexException("empty selected row index array");
646 }
647 throw new MatrixIndexException("empty selected column index array");
648 }
649
650 final BigDecimal[][] subMatrixData =
651 new BigDecimal[selectedRows.length][selectedColumns.length];
652 try {
653 for (int i = 0; i < selectedRows.length; i++) {
654 final BigDecimal[] subI = subMatrixData[i];
655 final BigDecimal[] dataSelectedI = data[selectedRows[i]];
656 for (int j = 0; j < selectedColumns.length; j++) {
657 subI[j] = dataSelectedI[selectedColumns[j]];
658 }
659 }
660 } catch (ArrayIndexOutOfBoundsException e) {
661
662
663 for (final int row : selectedRows) {
664 MatrixUtils.checkRowIndex(this, row);
665 }
666 for (final int column : selectedColumns) {
667 MatrixUtils.checkColumnIndex(this, column);
668 }
669 }
670 return new BigMatrixImpl(subMatrixData, false);
671 }
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700 public void setSubMatrix(BigDecimal[][] subMatrix, int row, int column)
701 throws MatrixIndexException {
702
703 final int nRows = subMatrix.length;
704 if (nRows == 0) {
705 throw MathRuntimeException.createIllegalArgumentException("matrix must have at least one row");
706 }
707
708 final int nCols = subMatrix[0].length;
709 if (nCols == 0) {
710 throw MathRuntimeException.createIllegalArgumentException("matrix must have at least one column");
711 }
712
713 for (int r = 1; r < nRows; r++) {
714 if (subMatrix[r].length != nCols) {
715 throw MathRuntimeException.createIllegalArgumentException(
716 "some rows have length {0} while others have length {1}",
717 nCols, subMatrix[r].length);
718 }
719 }
720
721 if (data == null) {
722 if (row > 0) {
723 throw MathRuntimeException.createIllegalStateException(
724 "first {0} rows are not initialized yet",
725 row);
726 }
727 if (column > 0) {
728 throw MathRuntimeException.createIllegalStateException(
729 "first {0} columns are not initialized yet",
730 column);
731 }
732 data = new BigDecimal[nRows][nCols];
733 System.arraycopy(subMatrix, 0, data, 0, subMatrix.length);
734 } else {
735 MatrixUtils.checkRowIndex(this, row);
736 MatrixUtils.checkColumnIndex(this, column);
737 MatrixUtils.checkRowIndex(this, nRows + row - 1);
738 MatrixUtils.checkColumnIndex(this, nCols + column - 1);
739 }
740 for (int i = 0; i < nRows; i++) {
741 System.arraycopy(subMatrix[i], 0, data[row + i], column, nCols);
742 }
743
744 lu = null;
745
746 }
747
748
749
750
751
752
753
754
755
756 public BigMatrix getRowMatrix(int row) throws MatrixIndexException {
757 MatrixUtils.checkRowIndex(this, row);
758 final int ncols = this.getColumnDimension();
759 final BigDecimal[][] out = new BigDecimal[1][ncols];
760 System.arraycopy(data[row], 0, out[0], 0, ncols);
761 return new BigMatrixImpl(out, false);
762 }
763
764
765
766
767
768
769
770
771
772 public BigMatrix getColumnMatrix(int column) throws MatrixIndexException {
773 MatrixUtils.checkColumnIndex(this, column);
774 final int nRows = this.getRowDimension();
775 final BigDecimal[][] out = new BigDecimal[nRows][1];
776 for (int row = 0; row < nRows; row++) {
777 out[row][0] = data[row][column];
778 }
779 return new BigMatrixImpl(out, false);
780 }
781
782
783
784
785
786
787
788
789
790
791
792 public BigDecimal[] getRow(int row) throws MatrixIndexException {
793 MatrixUtils.checkRowIndex(this, row);
794 final int ncols = this.getColumnDimension();
795 final BigDecimal[] out = new BigDecimal[ncols];
796 System.arraycopy(data[row], 0, out, 0, ncols);
797 return out;
798 }
799
800
801
802
803
804
805
806
807
808
809
810
811 public double[] getRowAsDoubleArray(int row) throws MatrixIndexException {
812 MatrixUtils.checkRowIndex(this, row);
813 final int ncols = this.getColumnDimension();
814 final double[] out = new double[ncols];
815 for (int i=0;i<ncols;i++) {
816 out[i] = data[row][i].doubleValue();
817 }
818 return out;
819 }
820
821
822
823
824
825
826
827
828
829
830
831 public BigDecimal[] getColumn(int col) throws MatrixIndexException {
832 MatrixUtils.checkColumnIndex(this, col);
833 final int nRows = this.getRowDimension();
834 final BigDecimal[] out = new BigDecimal[nRows];
835 for (int i = 0; i < nRows; i++) {
836 out[i] = data[i][col];
837 }
838 return out;
839 }
840
841
842
843
844
845
846
847
848
849
850
851
852 public double[] getColumnAsDoubleArray(int col) throws MatrixIndexException {
853 MatrixUtils.checkColumnIndex(this, col);
854 final int nrows = this.getRowDimension();
855 final double[] out = new double[nrows];
856 for (int i=0;i<nrows;i++) {
857 out[i] = data[i][col].doubleValue();
858 }
859 return out;
860 }
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877 public BigDecimal getEntry(int row, int column)
878 throws MatrixIndexException {
879 try {
880 return data[row][column];
881 } catch (ArrayIndexOutOfBoundsException e) {
882 throw new MatrixIndexException(
883 "no entry at indices ({0}, {1}) in a {2}x{3} matrix",
884 row, column, getRowDimension(), getColumnDimension());
885 }
886 }
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904 public double getEntryAsDouble(int row, int column) throws MatrixIndexException {
905 return getEntry(row,column).doubleValue();
906 }
907
908
909
910
911
912
913 public BigMatrix transpose() {
914 final int nRows = this.getRowDimension();
915 final int nCols = this.getColumnDimension();
916 final BigDecimal[][] outData = new BigDecimal[nCols][nRows];
917 for (int row = 0; row < nRows; row++) {
918 final BigDecimal[] dataRow = data[row];
919 for (int col = 0; col < nCols; col++) {
920 outData[col][row] = dataRow[col];
921 }
922 }
923 return new BigMatrixImpl(outData, false);
924 }
925
926
927
928
929
930
931
932 public BigMatrix inverse() throws InvalidMatrixException {
933 return solve(MatrixUtils.createBigIdentityMatrix(getRowDimension()));
934 }
935
936
937
938
939
940
941
942 public BigDecimal getDeterminant() throws InvalidMatrixException {
943 if (!isSquare()) {
944 throw new NonSquareMatrixException(getRowDimension(), getColumnDimension());
945 }
946 if (isSingular()) {
947 return ZERO;
948 } else {
949 BigDecimal det = (parity == 1) ? ONE : ONE.negate();
950 for (int i = 0; i < this.getRowDimension(); i++) {
951 det = det.multiply(lu[i][i]);
952 }
953 return det;
954 }
955 }
956
957
958
959
960
961 public boolean isSquare() {
962 return (this.getColumnDimension() == this.getRowDimension());
963 }
964
965
966
967
968
969 public boolean isSingular() {
970 if (lu == null) {
971 try {
972 luDecompose();
973 return false;
974 } catch (InvalidMatrixException ex) {
975 return true;
976 }
977 } else {
978 return false;
979 }
980 }
981
982
983
984
985
986
987 public int getRowDimension() {
988 return data.length;
989 }
990
991
992
993
994
995
996 public int getColumnDimension() {
997 return data[0].length;
998 }
999
1000
1001
1002
1003
1004
1005
1006
1007
1008 public BigDecimal getTrace() throws IllegalArgumentException {
1009 if (!isSquare()) {
1010 throw new NonSquareMatrixException(getRowDimension(), getColumnDimension());
1011 }
1012 BigDecimal trace = data[0][0];
1013 for (int i = 1; i < this.getRowDimension(); i++) {
1014 trace = trace.add(data[i][i]);
1015 }
1016 return trace;
1017 }
1018
1019
1020
1021
1022
1023
1024
1025
1026 public BigDecimal[] operate(BigDecimal[] v) throws IllegalArgumentException {
1027 if (v.length != getColumnDimension()) {
1028 throw MathRuntimeException.createIllegalArgumentException(
1029 "vector length mismatch: got {0} but expected {1}",
1030 v.length, getColumnDimension() );
1031 }
1032 final int nRows = this.getRowDimension();
1033 final int nCols = this.getColumnDimension();
1034 final BigDecimal[] out = new BigDecimal[nRows];
1035 for (int row = 0; row < nRows; row++) {
1036 BigDecimal sum = ZERO;
1037 for (int i = 0; i < nCols; i++) {
1038 sum = sum.add(data[row][i].multiply(v[i]));
1039 }
1040 out[row] = sum;
1041 }
1042 return out;
1043 }
1044
1045
1046
1047
1048
1049
1050
1051
1052 public BigDecimal[] operate(double[] v) throws IllegalArgumentException {
1053 final BigDecimal bd[] = new BigDecimal[v.length];
1054 for (int i = 0; i < bd.length; i++) {
1055 bd[i] = new BigDecimal(v[i]);
1056 }
1057 return operate(bd);
1058 }
1059
1060
1061
1062
1063
1064
1065
1066
1067 public BigDecimal[] preMultiply(BigDecimal[] v) throws IllegalArgumentException {
1068 final int nRows = this.getRowDimension();
1069 if (v.length != nRows) {
1070 throw MathRuntimeException.createIllegalArgumentException(
1071 "vector length mismatch: got {0} but expected {1}",
1072 v.length, nRows );
1073 }
1074 final int nCols = this.getColumnDimension();
1075 final BigDecimal[] out = new BigDecimal[nCols];
1076 for (int col = 0; col < nCols; col++) {
1077 BigDecimal sum = ZERO;
1078 for (int i = 0; i < nRows; i++) {
1079 sum = sum.add(data[i][col].multiply(v[i]));
1080 }
1081 out[col] = sum;
1082 }
1083 return out;
1084 }
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097 public BigDecimal[] solve(BigDecimal[] b) throws IllegalArgumentException, InvalidMatrixException {
1098 final int nRows = this.getRowDimension();
1099 if (b.length != nRows) {
1100 throw MathRuntimeException.createIllegalArgumentException(
1101 "vector length mismatch: got {0} but expected {1}",
1102 b.length, nRows);
1103 }
1104 final BigMatrix bMatrix = new BigMatrixImpl(b);
1105 final BigDecimal[][] solution = ((BigMatrixImpl) (solve(bMatrix))).getDataRef();
1106 final BigDecimal[] out = new BigDecimal[nRows];
1107 for (int row = 0; row < nRows; row++) {
1108 out[row] = solution[row][0];
1109 }
1110 return out;
1111 }
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124 public BigDecimal[] solve(double[] b) throws IllegalArgumentException, InvalidMatrixException {
1125 final BigDecimal bd[] = new BigDecimal[b.length];
1126 for (int i = 0; i < bd.length; i++) {
1127 bd[i] = new BigDecimal(b[i]);
1128 }
1129 return solve(bd);
1130 }
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143 public BigMatrix solve(BigMatrix b) throws IllegalArgumentException, InvalidMatrixException {
1144 if (b.getRowDimension() != getRowDimension()) {
1145 throw MathRuntimeException.createIllegalArgumentException(
1146 "dimensions mismatch: got {0}x{1} but expected {2}x{3}",
1147 b.getRowDimension(), b.getColumnDimension(), getRowDimension(), "n");
1148 }
1149 if (!isSquare()) {
1150 throw new NonSquareMatrixException(getRowDimension(), getColumnDimension());
1151 }
1152 if (this.isSingular()) {
1153 throw new SingularMatrixException();
1154 }
1155
1156 final int nCol = this.getColumnDimension();
1157 final int nColB = b.getColumnDimension();
1158 final int nRowB = b.getRowDimension();
1159
1160
1161 final BigDecimal[][] bp = new BigDecimal[nRowB][nColB];
1162 for (int row = 0; row < nRowB; row++) {
1163 final BigDecimal[] bpRow = bp[row];
1164 for (int col = 0; col < nColB; col++) {
1165 bpRow[col] = b.getEntry(permutation[row], col);
1166 }
1167 }
1168
1169
1170 for (int col = 0; col < nCol; col++) {
1171 for (int i = col + 1; i < nCol; i++) {
1172 final BigDecimal[] bpI = bp[i];
1173 final BigDecimal[] luI = lu[i];
1174 for (int j = 0; j < nColB; j++) {
1175 bpI[j] = bpI[j].subtract(bp[col][j].multiply(luI[col]));
1176 }
1177 }
1178 }
1179
1180
1181 for (int col = nCol - 1; col >= 0; col--) {
1182 final BigDecimal[] bpCol = bp[col];
1183 final BigDecimal luDiag = lu[col][col];
1184 for (int j = 0; j < nColB; j++) {
1185 bpCol[j] = bpCol[j].divide(luDiag, scale, roundingMode);
1186 }
1187 for (int i = 0; i < col; i++) {
1188 final BigDecimal[] bpI = bp[i];
1189 final BigDecimal[] luI = lu[i];
1190 for (int j = 0; j < nColB; j++) {
1191 bpI[j] = bpI[j].subtract(bp[col][j].multiply(luI[col]));
1192 }
1193 }
1194 }
1195
1196 return new BigMatrixImpl(bp, false);
1197
1198 }
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218 public void luDecompose() throws InvalidMatrixException {
1219
1220 final int nRows = this.getRowDimension();
1221 final int nCols = this.getColumnDimension();
1222 if (nRows != nCols) {
1223 throw new NonSquareMatrixException(getRowDimension(), getColumnDimension());
1224 }
1225 lu = this.getData();
1226
1227
1228 permutation = new int[nRows];
1229 for (int row = 0; row < nRows; row++) {
1230 permutation[row] = row;
1231 }
1232 parity = 1;
1233
1234
1235 for (int col = 0; col < nCols; col++) {
1236
1237 BigDecimal sum = ZERO;
1238
1239
1240 for (int row = 0; row < col; row++) {
1241 final BigDecimal[] luRow = lu[row];
1242 sum = luRow[col];
1243 for (int i = 0; i < row; i++) {
1244 sum = sum.subtract(luRow[i].multiply(lu[i][col]));
1245 }
1246 luRow[col] = sum;
1247 }
1248
1249
1250 int max = col;
1251 BigDecimal largest = ZERO;
1252 for (int row = col; row < nRows; row++) {
1253 final BigDecimal[] luRow = lu[row];
1254 sum = luRow[col];
1255 for (int i = 0; i < col; i++) {
1256 sum = sum.subtract(luRow[i].multiply(lu[i][col]));
1257 }
1258 luRow[col] = sum;
1259
1260
1261 if (sum.abs().compareTo(largest) == 1) {
1262 largest = sum.abs();
1263 max = row;
1264 }
1265 }
1266
1267
1268 if (lu[max][col].abs().compareTo(TOO_SMALL) <= 0) {
1269 lu = null;
1270 throw new SingularMatrixException();
1271 }
1272
1273
1274 if (max != col) {
1275 BigDecimal tmp = ZERO;
1276 for (int i = 0; i < nCols; i++) {
1277 tmp = lu[max][i];
1278 lu[max][i] = lu[col][i];
1279 lu[col][i] = tmp;
1280 }
1281 int temp = permutation[max];
1282 permutation[max] = permutation[col];
1283 permutation[col] = temp;
1284 parity = -parity;
1285 }
1286
1287
1288 final BigDecimal luDiag = lu[col][col];
1289 for (int row = col + 1; row < nRows; row++) {
1290 final BigDecimal[] luRow = lu[row];
1291 luRow[col] = luRow[col].divide(luDiag, scale, roundingMode);
1292 }
1293
1294 }
1295
1296 }
1297
1298
1299
1300
1301
1302 @Override
1303 public String toString() {
1304 StringBuffer res = new StringBuffer();
1305 res.append("BigMatrixImpl{");
1306 if (data != null) {
1307 for (int i = 0; i < data.length; i++) {
1308 if (i > 0) {
1309 res.append(",");
1310 }
1311 res.append("{");
1312 for (int j = 0; j < data[0].length; j++) {
1313 if (j > 0) {
1314 res.append(",");
1315 }
1316 res.append(data[i][j]);
1317 }
1318 res.append("}");
1319 }
1320 }
1321 res.append("}");
1322 return res.toString();
1323 }
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334 @Override
1335 public boolean equals(Object object) {
1336 if (object == this ) {
1337 return true;
1338 }
1339 if (object instanceof BigMatrixImpl == false) {
1340 return false;
1341 }
1342 final BigMatrix m = (BigMatrix) object;
1343 final int nRows = getRowDimension();
1344 final int nCols = getColumnDimension();
1345 if (m.getColumnDimension() != nCols || m.getRowDimension() != nRows) {
1346 return false;
1347 }
1348 for (int row = 0; row < nRows; row++) {
1349 final BigDecimal[] dataRow = data[row];
1350 for (int col = 0; col < nCols; col++) {
1351 if (!dataRow[col].equals(m.getEntry(row, col))) {
1352 return false;
1353 }
1354 }
1355 }
1356 return true;
1357 }
1358
1359
1360
1361
1362
1363
1364 @Override
1365 public int hashCode() {
1366 int ret = 7;
1367 final int nRows = getRowDimension();
1368 final int nCols = getColumnDimension();
1369 ret = ret * 31 + nRows;
1370 ret = ret * 31 + nCols;
1371 for (int row = 0; row < nRows; row++) {
1372 final BigDecimal[] dataRow = data[row];
1373 for (int col = 0; col < nCols; col++) {
1374 ret = ret * 31 + (11 * (row+1) + 17 * (col+1)) *
1375 dataRow[col].hashCode();
1376 }
1377 }
1378 return ret;
1379 }
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410 protected BigMatrix getLUMatrix() throws InvalidMatrixException {
1411 if (lu == null) {
1412 luDecompose();
1413 }
1414 return new BigMatrixImpl(lu);
1415 }
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429 protected int[] getPermutation() {
1430 final int[] out = new int[permutation.length];
1431 System.arraycopy(permutation, 0, out, 0, permutation.length);
1432 return out;
1433 }
1434
1435
1436
1437
1438
1439
1440
1441
1442 private BigDecimal[][] copyOut() {
1443 final int nRows = this.getRowDimension();
1444 final BigDecimal[][] out = new BigDecimal[nRows][this.getColumnDimension()];
1445
1446 for (int i = 0; i < nRows; i++) {
1447 System.arraycopy(data[i], 0, out[i], 0, data[i].length);
1448 }
1449 return out;
1450 }
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462 private void copyIn(BigDecimal[][] in) {
1463 setSubMatrix(in,0,0);
1464 }
1465
1466
1467
1468
1469
1470
1471 private void copyIn(double[][] in) {
1472 final int nRows = in.length;
1473 final int nCols = in[0].length;
1474 data = new BigDecimal[nRows][nCols];
1475 for (int i = 0; i < nRows; i++) {
1476 final BigDecimal[] dataI = data[i];
1477 final double[] inI = in[i];
1478 for (int j = 0; j < nCols; j++) {
1479 dataI[j] = new BigDecimal(inI[j]);
1480 }
1481 }
1482 lu = null;
1483 }
1484
1485
1486
1487
1488
1489
1490
1491 private void copyIn(String[][] in) {
1492 final int nRows = in.length;
1493 final int nCols = in[0].length;
1494 data = new BigDecimal[nRows][nCols];
1495 for (int i = 0; i < nRows; i++) {
1496 final BigDecimal[] dataI = data[i];
1497 final String[] inI = in[i];
1498 for (int j = 0; j < nCols; j++) {
1499 dataI[j] = new BigDecimal(inI[j]);
1500 }
1501 }
1502 lu = null;
1503 }
1504
1505 }