/** {@inheritDoc} */ public boolean isSquare() { return getColumnDimension() == getRowDimension(); }
/** {@inheritDoc} */ public double[] getRow(final int row) throws OutOfRangeException { MatrixUtils.checkRowIndex(this, row); final int nCols = getColumnDimension(); final double[] out = new double[nCols]; for (int i = 0; i < nCols; ++i) { out[i] = getEntry(row, i); } return out; }
/** {@inheritDoc} */ public double[][] getData() { final double[][] data = new double[getRowDimension()][getColumnDimension()]; for (int i = 0; i < data.length; ++i) { final double[] dataI = data[i]; for (int j = 0; j < dataI.length; ++j) { dataI[j] = getEntry(i, j); } } return data; }
/** {@inheritDoc} */ public void setRow(final int row, final double[] array) throws OutOfRangeException, MatrixDimensionMismatchException { MatrixUtils.checkRowIndex(this, row); final int nCols = getColumnDimension(); if (array.length != nCols) { throw new MatrixDimensionMismatchException(1, array.length, 1, nCols); } for (int i = 0; i < nCols; ++i) { setEntry(row, i, array[i]); } }
/** {@inheritDoc} */ public double getTrace() throws NonSquareMatrixException { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (nRows != nCols) { throw new NonSquareMatrixException(nRows, nCols); } double trace = 0; for (int i = 0; i < nRows; ++i) { trace += getEntry(i, i); } return trace; }
/** {@inheritDoc} */ public RealMatrix transpose() { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); final RealMatrix out = createMatrix(nCols, nRows); walkInOptimizedOrder(new DefaultRealMatrixPreservingVisitor() { /** {@inheritDoc} */ @Override public void visit(final int row, final int column, final double value) { out.setEntry(column, row, value); } }); return out; }
/** {@inheritDoc} */ public double[] preMultiply(final double[] v) throws DimensionMismatchException { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (v.length != nRows) { throw new DimensionMismatchException(v.length, nRows); } final double[] out = new double[nCols]; for (int col = 0; col < nCols; ++col) { double sum = 0; for (int i = 0; i < nRows; ++i) { sum += getEntry(i, col) * v[i]; } out[col] = sum; } return out; }
/** {@inheritDoc} */ public RealMatrix getRowMatrix(final int row) throws OutOfRangeException { MatrixUtils.checkRowIndex(this, row); final int nCols = getColumnDimension(); final RealMatrix out = createMatrix(1, nCols); for (int i = 0; i < nCols; ++i) { out.setEntry(0, i, getEntry(row, i)); } return out; }
/** {@inheritDoc} */ public RealMatrix scalarMultiply(final double d) { final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final RealMatrix out = createMatrix(rowCount, columnCount); for (int row = 0; row < rowCount; ++row) { for (int col = 0; col < columnCount; ++col) { out.setEntry(row, col, getEntry(row, col) * d); } } return out; }
/** {@inheritDoc} */ public RealMatrix scalarAdd(final double d) { final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final RealMatrix out = createMatrix(rowCount, columnCount); for (int row = 0; row < rowCount; ++row) { for (int col = 0; col < columnCount; ++col) { out.setEntry(row, col, getEntry(row, col) + d); } } return out; }
/** {@inheritDoc} */ public double walkInRowOrder(final RealMatrixPreservingVisitor visitor) { final int rows = getRowDimension(); final int columns = getColumnDimension(); visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int row = 0; row < rows; ++row) { for (int column = 0; column < columns; ++column) { visitor.visit(row, column, getEntry(row, column)); } } return visitor.end(); }
/** {@inheritDoc} */ public double walkInColumnOrder(final RealMatrixPreservingVisitor visitor) { final int rows = getRowDimension(); final int columns = getColumnDimension(); visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int column = 0; column < columns; ++column) { for (int row = 0; row < rows; ++row) { visitor.visit(row, column, getEntry(row, column)); } } return visitor.end(); }
/** {@inheritDoc} */ public void setRowVector(final int row, final RealVector vector) throws OutOfRangeException, MatrixDimensionMismatchException { MatrixUtils.checkRowIndex(this, row); final int nCols = getColumnDimension(); if (vector.getDimension() != nCols) { throw new MatrixDimensionMismatchException(1, vector.getDimension(), 1, nCols); } for (int i = 0; i < nCols; ++i) { setEntry(row, i, vector.getEntry(i)); } }
/** {@inheritDoc} */ public RealMatrix add(RealMatrix m) throws MatrixDimensionMismatchException { MatrixUtils.checkAdditionCompatible(this, m); final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final RealMatrix out = createMatrix(rowCount, columnCount); for (int row = 0; row < rowCount; ++row) { for (int col = 0; col < columnCount; ++col) { out.setEntry(row, col, getEntry(row, col) + m.getEntry(row, col)); } } return out; }
/** {@inheritDoc} */ public double walkInColumnOrder(final RealMatrixChangingVisitor visitor) { final int rows = getRowDimension(); final int columns = getColumnDimension(); visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int column = 0; column < columns; ++column) { for (int row = 0; row < rows; ++row) { final double oldValue = getEntry(row, column); final double newValue = visitor.visit(row, column, oldValue); setEntry(row, column, newValue); } } return visitor.end(); }
/** {@inheritDoc} */ public double walkInRowOrder(final RealMatrixChangingVisitor visitor) { final int rows = getRowDimension(); final int columns = getColumnDimension(); visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int row = 0; row < rows; ++row) { for (int column = 0; column < columns; ++column) { final double oldValue = getEntry(row, column); final double newValue = visitor.visit(row, column, oldValue); setEntry(row, column, newValue); } } return visitor.end(); }
/** {@inheritDoc} */ public RealMatrix subtract(final RealMatrix m) throws MatrixDimensionMismatchException { MatrixUtils.checkSubtractionCompatible(this, m); final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final RealMatrix out = createMatrix(rowCount, columnCount); for (int row = 0; row < rowCount; ++row) { for (int col = 0; col < columnCount; ++col) { out.setEntry(row, col, getEntry(row, col) - m.getEntry(row, col)); } } return out; }
/** {@inheritDoc} */ public double walkInColumnOrder(final RealMatrixPreservingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); visitor.start(getRowDimension(), getColumnDimension(), startRow, endRow, startColumn, endColumn); for (int column = startColumn; column <= endColumn; ++column) { for (int row = startRow; row <= endRow; ++row) { visitor.visit(row, column, getEntry(row, column)); } } return visitor.end(); }
/** {@inheritDoc} */ public double walkInRowOrder(final RealMatrixPreservingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); visitor.start(getRowDimension(), getColumnDimension(), startRow, endRow, startColumn, endColumn); for (int row = startRow; row <= endRow; ++row) { for (int column = startColumn; column <= endColumn; ++column) { visitor.visit(row, column, getEntry(row, column)); } } return visitor.end(); }
/** {@inheritDoc} */ public void setRowMatrix(final int row, final RealMatrix matrix) throws OutOfRangeException, MatrixDimensionMismatchException { MatrixUtils.checkRowIndex(this, row); final int nCols = getColumnDimension(); if ((matrix.getRowDimension() != 1) || (matrix.getColumnDimension() != nCols)) { throw new MatrixDimensionMismatchException(matrix.getRowDimension(), matrix.getColumnDimension(), 1, nCols); } for (int i = 0; i < nCols; ++i) { setEntry(row, i, matrix.getEntry(0, i)); } }