2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
package org.apache.commons.math.linear;
20
import org.apache.commons.math.MathRuntimeException;
21
import org.apache.commons.math.util.MathUtils;
24
* Basic implementation of RealMatrix methods regardless of the underlying storage.
25
* <p>All the methods implemented here use {@link #getEntry(int, int)} to access
26
* matrix elements. Derived class can provide faster implementations. </p>
28
* @version $Revision: 783702 $ $Date: 2009-06-11 04:54:02 -0400 (Thu, 11 Jun 2009) $
31
public abstract class AbstractRealMatrix implements RealMatrix {
35
* @deprecated as of release 2.0, since all methods using this are deprecated
38
private DecompositionSolver lu;
41
* Creates a matrix with no data
43
protected AbstractRealMatrix() {
48
* Create a new RealMatrix with the supplied row and column dimensions.
50
* @param rowDimension the number of rows in the new matrix
51
* @param columnDimension the number of columns in the new matrix
52
* @throws IllegalArgumentException if row or column dimension is not positive
54
protected AbstractRealMatrix(final int rowDimension, final int columnDimension)
55
throws IllegalArgumentException {
56
if (rowDimension <= 0 ) {
57
throw MathRuntimeException.createIllegalArgumentException(
58
"invalid row dimension {0} (must be positive)",
61
if (columnDimension <= 0) {
62
throw MathRuntimeException.createIllegalArgumentException(
63
"invalid column dimension {0} (must be positive)",
70
public abstract RealMatrix createMatrix(final int rowDimension, final int columnDimension)
71
throws IllegalArgumentException;
74
public abstract RealMatrix copy();
77
public RealMatrix add(RealMatrix m) throws IllegalArgumentException {
80
MatrixUtils.checkAdditionCompatible(this, m);
82
final int rowCount = getRowDimension();
83
final int columnCount = getColumnDimension();
84
final RealMatrix out = createMatrix(rowCount, columnCount);
85
for (int row = 0; row < rowCount; ++row) {
86
for (int col = 0; col < columnCount; ++col) {
87
out.setEntry(row, col, getEntry(row, col) + m.getEntry(row, col));
96
public RealMatrix subtract(final RealMatrix m) throws IllegalArgumentException {
99
MatrixUtils.checkSubtractionCompatible(this, m);
101
final int rowCount = getRowDimension();
102
final int columnCount = getColumnDimension();
103
final RealMatrix out = createMatrix(rowCount, columnCount);
104
for (int row = 0; row < rowCount; ++row) {
105
for (int col = 0; col < columnCount; ++col) {
106
out.setEntry(row, col, getEntry(row, col) - m.getEntry(row, col));
115
public RealMatrix scalarAdd(final double d) {
117
final int rowCount = getRowDimension();
118
final int columnCount = getColumnDimension();
119
final RealMatrix out = createMatrix(rowCount, columnCount);
120
for (int row = 0; row < rowCount; ++row) {
121
for (int col = 0; col < columnCount; ++col) {
122
out.setEntry(row, col, getEntry(row, col) + d);
131
public RealMatrix scalarMultiply(final double d) {
133
final int rowCount = getRowDimension();
134
final int columnCount = getColumnDimension();
135
final RealMatrix out = createMatrix(rowCount, columnCount);
136
for (int row = 0; row < rowCount; ++row) {
137
for (int col = 0; col < columnCount; ++col) {
138
out.setEntry(row, col, getEntry(row, col) * d);
147
public RealMatrix multiply(final RealMatrix m)
148
throws IllegalArgumentException {
151
MatrixUtils.checkMultiplicationCompatible(this, m);
153
final int nRows = getRowDimension();
154
final int nCols = m.getColumnDimension();
155
final int nSum = getColumnDimension();
156
final RealMatrix out = createMatrix(nRows, nCols);
157
for (int row = 0; row < nRows; ++row) {
158
for (int col = 0; col < nCols; ++col) {
160
for (int i = 0; i < nSum; ++i) {
161
sum += getEntry(row, i) * m.getEntry(i, col);
163
out.setEntry(row, col, sum);
172
public RealMatrix preMultiply(final RealMatrix m)
173
throws IllegalArgumentException {
174
return m.multiply(this);
178
public double[][] getData() {
180
final double[][] data = new double[getRowDimension()][getColumnDimension()];
182
for (int i = 0; i < data.length; ++i) {
183
final double[] dataI = data[i];
184
for (int j = 0; j < dataI.length; ++j) {
185
dataI[j] = getEntry(i, j);
194
public double getNorm() {
195
return walkInColumnOrder(new RealMatrixPreservingVisitor() {
197
/** Last row index. */
198
private double endRow;
200
/** Sum of absolute values on one column. */
201
private double columnSum;
203
/** Maximal sum across all columns. */
204
private double maxColSum;
207
public void start(final int rows, final int columns,
208
final int startRow, final int endRow,
209
final int startColumn, final int endColumn) {
210
this.endRow = endRow;
216
public void visit(final int row, final int column, final double value) {
217
columnSum += Math.abs(value);
219
maxColSum = Math.max(maxColSum, columnSum);
225
public double end() {
233
public double getFrobeniusNorm() {
234
return walkInOptimizedOrder(new RealMatrixPreservingVisitor() {
236
/** Sum of squared entries. */
240
public void start(final int rows, final int columns,
241
final int startRow, final int endRow,
242
final int startColumn, final int endColumn) {
247
public void visit(final int row, final int column, final double value) {
248
sum += value * value;
252
public double end() {
253
return Math.sqrt(sum);
260
public RealMatrix getSubMatrix(final int startRow, final int endRow,
261
final int startColumn, final int endColumn)
262
throws MatrixIndexException {
264
MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn);
266
final RealMatrix subMatrix =
267
createMatrix(endRow - startRow + 1, endColumn - startColumn + 1);
268
for (int i = startRow; i <= endRow; ++i) {
269
for (int j = startColumn; j <= endColumn; ++j) {
270
subMatrix.setEntry(i - startRow, j - startColumn, getEntry(i, j));
279
public RealMatrix getSubMatrix(final int[] selectedRows, final int[] selectedColumns)
280
throws MatrixIndexException {
283
MatrixUtils.checkSubMatrixIndex(this, selectedRows, selectedColumns);
286
final RealMatrix subMatrix =
287
createMatrix(selectedRows.length, selectedColumns.length);
288
subMatrix.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
292
public double visit(final int row, final int column, final double value) {
293
return getEntry(selectedRows[row], selectedColumns[column]);
303
public void copySubMatrix(final int startRow, final int endRow,
304
final int startColumn, final int endColumn,
305
final double[][] destination)
306
throws MatrixIndexException, IllegalArgumentException {
309
MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn);
310
final int rowsCount = endRow + 1 - startRow;
311
final int columnsCount = endColumn + 1 - startColumn;
312
if ((destination.length < rowsCount) || (destination[0].length < columnsCount)) {
313
throw MathRuntimeException.createIllegalArgumentException(
314
"dimensions mismatch: got {0}x{1} but expected {2}x{3}",
315
destination.length, destination[0].length,
316
rowsCount, columnsCount);
320
walkInOptimizedOrder(new DefaultRealMatrixPreservingVisitor() {
322
/** Initial row index. */
323
private int startRow;
325
/** Initial column index. */
326
private int startColumn;
330
public void start(final int rows, final int columns,
331
final int startRow, final int endRow,
332
final int startColumn, final int endColumn) {
333
this.startRow = startRow;
334
this.startColumn = startColumn;
339
public void visit(final int row, final int column, final double value) {
340
destination[row - startRow][column - startColumn] = value;
343
}, startRow, endRow, startColumn, endColumn);
348
public void copySubMatrix(int[] selectedRows, int[] selectedColumns, double[][] destination)
349
throws MatrixIndexException, IllegalArgumentException {
352
MatrixUtils.checkSubMatrixIndex(this, selectedRows, selectedColumns);
353
if ((destination.length < selectedRows.length) ||
354
(destination[0].length < selectedColumns.length)) {
355
throw MathRuntimeException.createIllegalArgumentException(
356
"dimensions mismatch: got {0}x{1} but expected {2}x{3}",
357
destination.length, destination[0].length,
358
selectedRows.length, selectedColumns.length);
362
for (int i = 0; i < selectedRows.length; i++) {
363
final double[] destinationI = destination[i];
364
for (int j = 0; j < selectedColumns.length; j++) {
365
destinationI[j] = getEntry(selectedRows[i], selectedColumns[j]);
372
public void setSubMatrix(final double[][] subMatrix, final int row, final int column)
373
throws MatrixIndexException {
375
final int nRows = subMatrix.length;
377
throw MathRuntimeException.createIllegalArgumentException("matrix must have at least one row");
380
final int nCols = subMatrix[0].length;
382
throw MathRuntimeException.createIllegalArgumentException("matrix must have at least one column");
385
for (int r = 1; r < nRows; ++r) {
386
if (subMatrix[r].length != nCols) {
387
throw MathRuntimeException.createIllegalArgumentException(
388
"some rows have length {0} while others have length {1}",
389
nCols, subMatrix[r].length);
393
MatrixUtils.checkRowIndex(this, row);
394
MatrixUtils.checkColumnIndex(this, column);
395
MatrixUtils.checkRowIndex(this, nRows + row - 1);
396
MatrixUtils.checkColumnIndex(this, nCols + column - 1);
398
for (int i = 0; i < nRows; ++i) {
399
for (int j = 0; j < nCols; ++j) {
400
setEntry(row + i, column + j, subMatrix[i][j]);
409
public RealMatrix getRowMatrix(final int row)
410
throws MatrixIndexException {
412
MatrixUtils.checkRowIndex(this, row);
413
final int nCols = getColumnDimension();
414
final RealMatrix out = createMatrix(1, nCols);
415
for (int i = 0; i < nCols; ++i) {
416
out.setEntry(0, i, getEntry(row, i));
424
public void setRowMatrix(final int row, final RealMatrix matrix)
425
throws MatrixIndexException, InvalidMatrixException {
427
MatrixUtils.checkRowIndex(this, row);
428
final int nCols = getColumnDimension();
429
if ((matrix.getRowDimension() != 1) ||
430
(matrix.getColumnDimension() != nCols)) {
431
throw new InvalidMatrixException(
432
"dimensions mismatch: got {0}x{1} but expected {2}x{3}",
433
matrix.getRowDimension(), matrix.getColumnDimension(), 1, nCols);
435
for (int i = 0; i < nCols; ++i) {
436
setEntry(row, i, matrix.getEntry(0, i));
442
public RealMatrix getColumnMatrix(final int column)
443
throws MatrixIndexException {
445
MatrixUtils.checkColumnIndex(this, column);
446
final int nRows = getRowDimension();
447
final RealMatrix out = createMatrix(nRows, 1);
448
for (int i = 0; i < nRows; ++i) {
449
out.setEntry(i, 0, getEntry(i, column));
457
public void setColumnMatrix(final int column, final RealMatrix matrix)
458
throws MatrixIndexException, InvalidMatrixException {
460
MatrixUtils.checkColumnIndex(this, column);
461
final int nRows = getRowDimension();
462
if ((matrix.getRowDimension() != nRows) ||
463
(matrix.getColumnDimension() != 1)) {
464
throw new InvalidMatrixException(
465
"dimensions mismatch: got {0}x{1} but expected {2}x{3}",
466
matrix.getRowDimension(), matrix.getColumnDimension(), nRows, 1);
468
for (int i = 0; i < nRows; ++i) {
469
setEntry(i, column, matrix.getEntry(i, 0));
475
public RealVector getRowVector(final int row)
476
throws MatrixIndexException {
477
return new ArrayRealVector(getRow(row), false);
481
public void setRowVector(final int row, final RealVector vector)
482
throws MatrixIndexException, InvalidMatrixException {
484
MatrixUtils.checkRowIndex(this, row);
485
final int nCols = getColumnDimension();
486
if (vector.getDimension() != nCols) {
487
throw new InvalidMatrixException(
488
"dimensions mismatch: got {0}x{1} but expected {2}x{3}",
489
1, vector.getDimension(), 1, nCols);
491
for (int i = 0; i < nCols; ++i) {
492
setEntry(row, i, vector.getEntry(i));
498
public RealVector getColumnVector(final int column)
499
throws MatrixIndexException {
500
return new ArrayRealVector(getColumn(column), false);
504
public void setColumnVector(final int column, final RealVector vector)
505
throws MatrixIndexException, InvalidMatrixException {
507
MatrixUtils.checkColumnIndex(this, column);
508
final int nRows = getRowDimension();
509
if (vector.getDimension() != nRows) {
510
throw new InvalidMatrixException(
511
"dimensions mismatch: got {0}x{1} but expected {2}x{3}",
512
vector.getDimension(), 1, nRows, 1);
514
for (int i = 0; i < nRows; ++i) {
515
setEntry(i, column, vector.getEntry(i));
521
public double[] getRow(final int row)
522
throws MatrixIndexException {
524
MatrixUtils.checkRowIndex(this, row);
525
final int nCols = getColumnDimension();
526
final double[] out = new double[nCols];
527
for (int i = 0; i < nCols; ++i) {
528
out[i] = getEntry(row, i);
536
public void setRow(final int row, final double[] array)
537
throws MatrixIndexException, InvalidMatrixException {
539
MatrixUtils.checkRowIndex(this, row);
540
final int nCols = getColumnDimension();
541
if (array.length != nCols) {
542
throw new InvalidMatrixException(
543
"dimensions mismatch: got {0}x{1} but expected {2}x{3}",
544
1, array.length, 1, nCols);
546
for (int i = 0; i < nCols; ++i) {
547
setEntry(row, i, array[i]);
553
public double[] getColumn(final int column)
554
throws MatrixIndexException {
556
MatrixUtils.checkColumnIndex(this, column);
557
final int nRows = getRowDimension();
558
final double[] out = new double[nRows];
559
for (int i = 0; i < nRows; ++i) {
560
out[i] = getEntry(i, column);
568
public void setColumn(final int column, final double[] array)
569
throws MatrixIndexException, InvalidMatrixException {
571
MatrixUtils.checkColumnIndex(this, column);
572
final int nRows = getRowDimension();
573
if (array.length != nRows) {
574
throw new InvalidMatrixException(
575
"dimensions mismatch: got {0}x{1} but expected {2}x{3}",
576
array.length, 1, nRows, 1);
578
for (int i = 0; i < nRows; ++i) {
579
setEntry(i, column, array[i]);
585
public abstract double getEntry(int row, int column)
586
throws MatrixIndexException;
589
public abstract void setEntry(int row, int column, double value)
590
throws MatrixIndexException;
593
public abstract void addToEntry(int row, int column, double increment)
594
throws MatrixIndexException;
597
public abstract void multiplyEntry(int row, int column, double factor)
598
throws MatrixIndexException;
601
public RealMatrix transpose() {
603
final int nRows = getRowDimension();
604
final int nCols = getColumnDimension();
605
final RealMatrix out = createMatrix(nCols, nRows);
606
walkInOptimizedOrder(new DefaultRealMatrixPreservingVisitor() {
610
public void visit(final int row, final int column, final double value) {
611
out.setEntry(column, row, value);
622
public RealMatrix inverse()
623
throws InvalidMatrixException {
625
lu = new LUDecompositionImpl(this, MathUtils.SAFE_MIN).getSolver();
627
return lu.getInverse();
632
public double getDeterminant()
633
throws InvalidMatrixException {
634
return new LUDecompositionImpl(this, MathUtils.SAFE_MIN).getDeterminant();
638
public boolean isSquare() {
639
return (getColumnDimension() == getRowDimension());
644
public boolean isSingular() {
646
lu = new LUDecompositionImpl(this, MathUtils.SAFE_MIN).getSolver();
648
return !lu.isNonSingular();
652
public abstract int getRowDimension();
655
public abstract int getColumnDimension();
658
public double getTrace()
659
throws NonSquareMatrixException {
660
final int nRows = getRowDimension();
661
final int nCols = getColumnDimension();
662
if (nRows != nCols) {
663
throw new NonSquareMatrixException(nRows, nCols);
666
for (int i = 0; i < nRows; ++i) {
667
trace += getEntry(i, i);
673
public double[] operate(final double[] v)
674
throws IllegalArgumentException {
676
final int nRows = getRowDimension();
677
final int nCols = getColumnDimension();
678
if (v.length != nCols) {
679
throw MathRuntimeException.createIllegalArgumentException(
680
"vector length mismatch: got {0} but expected {1}",
684
final double[] out = new double[nRows];
685
for (int row = 0; row < nRows; ++row) {
687
for (int i = 0; i < nCols; ++i) {
688
sum += getEntry(row, i) * v[i];
698
public RealVector operate(final RealVector v)
699
throws IllegalArgumentException {
701
return new ArrayRealVector(operate(((ArrayRealVector) v).getDataRef()), false);
702
} catch (ClassCastException cce) {
703
final int nRows = getRowDimension();
704
final int nCols = getColumnDimension();
705
if (v.getDimension() != nCols) {
706
throw MathRuntimeException.createIllegalArgumentException(
707
"vector length mismatch: got {0} but expected {1}",
708
v.getDimension(), nCols);
711
final double[] out = new double[nRows];
712
for (int row = 0; row < nRows; ++row) {
714
for (int i = 0; i < nCols; ++i) {
715
sum += getEntry(row, i) * v.getEntry(i);
720
return new ArrayRealVector(out, false);
725
public double[] preMultiply(final double[] v)
726
throws IllegalArgumentException {
728
final int nRows = getRowDimension();
729
final int nCols = getColumnDimension();
730
if (v.length != nRows) {
731
throw MathRuntimeException.createIllegalArgumentException(
732
"vector length mismatch: got {0} but expected {1}",
736
final double[] out = new double[nCols];
737
for (int col = 0; col < nCols; ++col) {
739
for (int i = 0; i < nRows; ++i) {
740
sum += getEntry(i, col) * v[i];
750
public RealVector preMultiply(final RealVector v)
751
throws IllegalArgumentException {
753
return new ArrayRealVector(preMultiply(((ArrayRealVector) v).getDataRef()), false);
754
} catch (ClassCastException cce) {
756
final int nRows = getRowDimension();
757
final int nCols = getColumnDimension();
758
if (v.getDimension() != nRows) {
759
throw MathRuntimeException.createIllegalArgumentException(
760
"vector length mismatch: got {0} but expected {1}",
761
v.getDimension(), nRows);
764
final double[] out = new double[nCols];
765
for (int col = 0; col < nCols; ++col) {
767
for (int i = 0; i < nRows; ++i) {
768
sum += getEntry(i, col) * v.getEntry(i);
773
return new ArrayRealVector(out);
779
public double walkInRowOrder(final RealMatrixChangingVisitor visitor)
780
throws MatrixVisitorException {
781
final int rows = getRowDimension();
782
final int columns = getColumnDimension();
783
visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
784
for (int row = 0; row < rows; ++row) {
785
for (int column = 0; column < columns; ++column) {
786
final double oldValue = getEntry(row, column);
787
final double newValue = visitor.visit(row, column, oldValue);
788
setEntry(row, column, newValue);
792
return visitor.end();
796
public double walkInRowOrder(final RealMatrixPreservingVisitor visitor)
797
throws MatrixVisitorException {
798
final int rows = getRowDimension();
799
final int columns = getColumnDimension();
800
visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
801
for (int row = 0; row < rows; ++row) {
802
for (int column = 0; column < columns; ++column) {
803
visitor.visit(row, column, getEntry(row, column));
806
return visitor.end();
810
public double walkInRowOrder(final RealMatrixChangingVisitor visitor,
811
final int startRow, final int endRow,
812
final int startColumn, final int endColumn)
813
throws MatrixIndexException, MatrixVisitorException {
814
MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn);
815
visitor.start(getRowDimension(), getColumnDimension(),
816
startRow, endRow, startColumn, endColumn);
817
for (int row = startRow; row <= endRow; ++row) {
818
for (int column = startColumn; column <= endColumn; ++column) {
819
final double oldValue = getEntry(row, column);
820
final double newValue = visitor.visit(row, column, oldValue);
821
setEntry(row, column, newValue);
825
return visitor.end();
829
public double walkInRowOrder(final RealMatrixPreservingVisitor visitor,
830
final int startRow, final int endRow,
831
final int startColumn, final int endColumn)
832
throws MatrixIndexException, MatrixVisitorException {
833
MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn);
834
visitor.start(getRowDimension(), getColumnDimension(),
835
startRow, endRow, startColumn, endColumn);
836
for (int row = startRow; row <= endRow; ++row) {
837
for (int column = startColumn; column <= endColumn; ++column) {
838
visitor.visit(row, column, getEntry(row, column));
841
return visitor.end();
845
public double walkInColumnOrder(final RealMatrixChangingVisitor visitor)
846
throws MatrixVisitorException {
847
final int rows = getRowDimension();
848
final int columns = getColumnDimension();
849
visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
850
for (int column = 0; column < columns; ++column) {
851
for (int row = 0; row < rows; ++row) {
852
final double oldValue = getEntry(row, column);
853
final double newValue = visitor.visit(row, column, oldValue);
854
setEntry(row, column, newValue);
858
return visitor.end();
862
public double walkInColumnOrder(final RealMatrixPreservingVisitor visitor)
863
throws MatrixVisitorException {
864
final int rows = getRowDimension();
865
final int columns = getColumnDimension();
866
visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
867
for (int column = 0; column < columns; ++column) {
868
for (int row = 0; row < rows; ++row) {
869
visitor.visit(row, column, getEntry(row, column));
872
return visitor.end();
876
public double walkInColumnOrder(final RealMatrixChangingVisitor visitor,
877
final int startRow, final int endRow,
878
final int startColumn, final int endColumn)
879
throws MatrixIndexException, MatrixVisitorException {
880
MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn);
881
visitor.start(getRowDimension(), getColumnDimension(),
882
startRow, endRow, startColumn, endColumn);
883
for (int column = startColumn; column <= endColumn; ++column) {
884
for (int row = startRow; row <= endRow; ++row) {
885
final double oldValue = getEntry(row, column);
886
final double newValue = visitor.visit(row, column, oldValue);
887
setEntry(row, column, newValue);
891
return visitor.end();
895
public double walkInColumnOrder(final RealMatrixPreservingVisitor visitor,
896
final int startRow, final int endRow,
897
final int startColumn, final int endColumn)
898
throws MatrixIndexException, MatrixVisitorException {
899
MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn);
900
visitor.start(getRowDimension(), getColumnDimension(),
901
startRow, endRow, startColumn, endColumn);
902
for (int column = startColumn; column <= endColumn; ++column) {
903
for (int row = startRow; row <= endRow; ++row) {
904
visitor.visit(row, column, getEntry(row, column));
907
return visitor.end();
911
public double walkInOptimizedOrder(final RealMatrixChangingVisitor visitor)
912
throws MatrixVisitorException {
913
return walkInRowOrder(visitor);
917
public double walkInOptimizedOrder(final RealMatrixPreservingVisitor visitor)
918
throws MatrixVisitorException {
919
return walkInRowOrder(visitor);
923
public double walkInOptimizedOrder(final RealMatrixChangingVisitor visitor,
924
final int startRow, final int endRow,
925
final int startColumn, final int endColumn)
926
throws MatrixIndexException, MatrixVisitorException {
927
return walkInRowOrder(visitor, startRow, endRow, startColumn, endColumn);
931
public double walkInOptimizedOrder(final RealMatrixPreservingVisitor visitor,
932
final int startRow, final int endRow,
933
final int startColumn, final int endColumn)
934
throws MatrixIndexException, MatrixVisitorException {
935
return walkInRowOrder(visitor, startRow, endRow, startColumn, endColumn);
940
public double[] solve(final double[] b)
941
throws IllegalArgumentException, InvalidMatrixException {
943
lu = new LUDecompositionImpl(this, MathUtils.SAFE_MIN).getSolver();
950
public RealMatrix solve(final RealMatrix b)
951
throws IllegalArgumentException, InvalidMatrixException {
953
lu = new LUDecompositionImpl(this, MathUtils.SAFE_MIN).getSolver();
960
* <a href="http://www.math.gatech.edu/~bourbaki/math2601/Web-notes/2num.pdf">
961
* LU decomposition</a> for this matrix, storing the result for use by other methods.
963
* <strong>Implementation Note</strong>:<br>
964
* Uses <a href="http://www.damtp.cam.ac.uk/user/fdl/people/sd/lectures/nummeth98/linear.htm">
965
* Crout's algorithm</a>, with partial pivoting.</p>
967
* <strong>Usage Note</strong>:<br>
968
* This method should rarely be invoked directly. Its only use is
969
* to force recomputation of the LU decomposition when changes have been
970
* made to the underlying data using direct array references. Changes
971
* made using setXxx methods will trigger recomputation when needed
974
* @throws InvalidMatrixException if the matrix is non-square or singular.
975
* @deprecated as of release 2.0, replaced by {@link LUDecomposition}
978
public void luDecompose()
979
throws InvalidMatrixException {
981
lu = new LUDecompositionImpl(this, MathUtils.SAFE_MIN).getSolver();
986
* Get a string representation for this matrix.
987
* @return a string representation for this matrix
990
public String toString() {
991
final int nRows = getRowDimension();
992
final int nCols = getColumnDimension();
993
final StringBuffer res = new StringBuffer();
994
String fullClassName = getClass().getName();
995
String shortClassName = fullClassName.substring(fullClassName.lastIndexOf('.') + 1);
996
res.append(shortClassName).append("{");
998
for (int i = 0; i < nRows; ++i) {
1003
for (int j = 0; j < nCols; ++j) {
1007
res.append(getEntry(i, j));
1013
return res.toString();
1018
* Returns true iff <code>object</code> is a
1019
* <code>RealMatrix</code> instance with the same dimensions as this
1020
* and all corresponding matrix entries are equal.
1022
* @param object the object to test equality against.
1023
* @return true if object equals this
1026
public boolean equals(final Object object) {
1027
if (object == this ) {
1030
if (object instanceof RealMatrix == false) {
1033
RealMatrix m = (RealMatrix) object;
1034
final int nRows = getRowDimension();
1035
final int nCols = getColumnDimension();
1036
if (m.getColumnDimension() != nCols || m.getRowDimension() != nRows) {
1039
for (int row = 0; row < nRows; ++row) {
1040
for (int col = 0; col < nCols; ++col) {
1041
if (getEntry(row, col) != m.getEntry(row, col)) {
1050
* Computes a hashcode for the matrix.
1052
* @return hashcode for matrix
1055
public int hashCode() {
1057
final int nRows = getRowDimension();
1058
final int nCols = getColumnDimension();
1059
ret = ret * 31 + nRows;
1060
ret = ret * 31 + nCols;
1061
for (int row = 0; row < nRows; ++row) {
1062
for (int col = 0; col < nCols; ++col) {
1063
ret = ret * 31 + (11 * (row+1) + 17 * (col+1)) *
1064
MathUtils.hash(getEntry(row, col));