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