if (!isSquare()) { throw new NonSquareMatrixException(getRowDimension(), getColumnDimension()); return MatrixUtils.createRealIdentityMatrix(this.getRowDimension()); return this.copy(); results[0] = this.copy(); RealMatrix result = this.copy();
/** {@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 RealVector getColumnVector(final int column) throws OutOfRangeException { return new ArrayRealVector(getColumn(column), false); }
/** {@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 boolean isSquare() { return getColumnDimension() == getRowDimension(); }
/** {@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 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 RealVector preMultiply(final RealVector v) throws DimensionMismatchException { try { return new ArrayRealVector(preMultiply(((ArrayRealVector) v).getDataRef()), false); } catch (ClassCastException cce) { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (v.getDimension() != nRows) { throw new DimensionMismatchException(v.getDimension(), 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.getEntry(i); } out[col] = sum; } return new ArrayRealVector(out, false); } }
/** {@inheritDoc} */ @Override public RealVector operate(final RealVector v) throws DimensionMismatchException { try { return new ArrayRealVector(operate(((ArrayRealVector) v).getDataRef()), false); } catch (ClassCastException cce) { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (v.getDimension() != nCols) { throw new DimensionMismatchException(v.getDimension(), nCols); } final double[] out = new double[nRows]; for (int row = 0; row < nRows; ++row) { double sum = 0; for (int i = 0; i < nCols; ++i) { sum += getEntry(row, i) * v.getEntry(i); } out[row] = sum; } return new ArrayRealVector(out, false); } }
/** {@inheritDoc} */ public void setColumn(final int column, final double[] array) throws OutOfRangeException, MatrixDimensionMismatchException { MatrixUtils.checkColumnIndex(this, column); final int nRows = getRowDimension(); if (array.length != nRows) { throw new MatrixDimensionMismatchException(array.length, 1, nRows, 1); } for (int i = 0; i < nRows; ++i) { setEntry(i, column, array[i]); } }
/** {@inheritDoc} */ public RealMatrix getColumnMatrix(final int column) throws OutOfRangeException { MatrixUtils.checkColumnIndex(this, column); final int nRows = getRowDimension(); final RealMatrix out = createMatrix(nRows, 1); for (int i = 0; i < nRows; ++i) { out.setEntry(i, 0, getEntry(i, column)); } 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 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[] 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[] getColumn(final int column) throws OutOfRangeException { MatrixUtils.checkColumnIndex(this, column); final int nRows = getRowDimension(); final double[] out = new double[nRows]; for (int i = 0; i < nRows; ++i) { out[i] = getEntry(i, column); } return out; }
/** {@inheritDoc} */ public RealMatrix getSubMatrix(final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); final RealMatrix subMatrix = createMatrix(endRow - startRow + 1, endColumn - startColumn + 1); for (int i = startRow; i <= endRow; ++i) { for (int j = startColumn; j <= endColumn; ++j) { subMatrix.setEntry(i - startRow, j - startColumn, getEntry(i, j)); } } return subMatrix; }
/** {@inheritDoc} */ @Override public double visit(final int row, final int column, final double value) { return getEntry(selectedRows[row], selectedColumns[column]); }
/** {@inheritDoc} */ public RealVector getRowVector(final int row) throws OutOfRangeException { return new ArrayRealVector(getRow(row), false); }
/** {@inheritDoc} */ public RealMatrix getSubMatrix(final int[] selectedRows, final int[] selectedColumns) throws NullArgumentException, NoDataException, OutOfRangeException { MatrixUtils.checkSubMatrixIndex(this, selectedRows, selectedColumns); final RealMatrix subMatrix = createMatrix(selectedRows.length, selectedColumns.length); subMatrix.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() { /** {@inheritDoc} */ @Override public double visit(final int row, final int column, final double value) { return getEntry(selectedRows[row], selectedColumns[column]); } }); return subMatrix; }