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