/** {@inheritDoc} */ public T[][] getData() { final T[][] data = buildArray(field, getRowDimension(), getColumnDimension()); for (int i = 0; i < data.length; ++i) { final T[] dataI = data[i]; for (int j = 0; j < dataI.length; ++j) { dataI[j] = getEntry(i, j); } } return data; }
/** {@inheritDoc} */ public FieldMatrix<T> add(FieldMatrix<T> m) throws IllegalArgumentException { // safety check checkAdditionCompatible(m); final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final FieldMatrix<T> 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).add(m.getEntry(row, col))); } } return out; }
/** {@inheritDoc} */ public void setColumn(final int column, final T[] array) throws MatrixIndexException, InvalidMatrixException { checkColumnIndex(column); final int nRows = getRowDimension(); if (array.length != nRows) { throw new InvalidMatrixException( LocalizedFormats.DIMENSIONS_MISMATCH_2x2, array.length, 1, nRows, 1); } for (int i = 0; i < nRows; ++i) { setEntry(i, column, array[i]); } }
/** {@inheritDoc} */ public T[] getRow(final int row) throws MatrixIndexException { checkRowIndex(row); final int nCols = getColumnDimension(); final T[] out = buildArray(field, nCols); for (int i = 0; i < nCols; ++i) { out[i] = getEntry(row, i); } return out; }
/** {@inheritDoc} */ public T[] getColumn(final int column) throws MatrixIndexException { checkColumnIndex(column); final int nRows = getRowDimension(); final T[] out = buildArray(field, nRows); for (int i = 0; i < nRows; ++i) { out[i] = getEntry(i, column); } return out; }
/** {@inheritDoc} */ public T walkInRowOrder(final FieldMatrixChangingVisitor<T> visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws MatrixIndexException, MatrixVisitorException { checkSubMatrixIndex(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) { final T oldValue = getEntry(row, column); final T newValue = visitor.visit(row, column, oldValue); setEntry(row, column, newValue); } } return visitor.end(); }
/** {@inheritDoc} */ public void setRow(final int row, final T[] array) throws MatrixIndexException, InvalidMatrixException { checkRowIndex(row); final int nCols = getColumnDimension(); if (array.length != nCols) { throw new InvalidMatrixException( LocalizedFormats.DIMENSIONS_MISMATCH_2x2, 1, array.length, 1, nCols); } for (int i = 0; i < nCols; ++i) { setEntry(row, i, array[i]); } }
/** {@inheritDoc} */ public FieldMatrix<T> getRowMatrix(final int row) throws MatrixIndexException { checkRowIndex(row); final int nCols = getColumnDimension(); final FieldMatrix<T> out = createMatrix(1, nCols); for (int i = 0; i < nCols; ++i) { out.setEntry(0, i, getEntry(row, i)); } return out; }
/** {@inheritDoc} */ public FieldMatrix<T> getColumnMatrix(final int column) throws MatrixIndexException { checkColumnIndex(column); final int nRows = getRowDimension(); final FieldMatrix<T> out = createMatrix(nRows, 1); for (int i = 0; i < nRows; ++i) { out.setEntry(i, 0, getEntry(i, column)); } return out; }
/** {@inheritDoc} */ public FieldMatrix<T> transpose() { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); final FieldMatrix<T> out = createMatrix(nCols, nRows); walkInOptimizedOrder(new DefaultFieldMatrixPreservingVisitor<T>(field.getZero()) { /** {@inheritDoc} */ @Override public void visit(final int row, final int column, final T value) { out.setEntry(column, row, value); } }); return out; }
/** {@inheritDoc} */ public FieldMatrix<T> getSubMatrix(final int startRow, final int endRow, final int startColumn, final int endColumn) throws MatrixIndexException { checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); final FieldMatrix<T> 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} */ public void setSubMatrix(final T[][] subMatrix, final int row, final int column) throws MatrixIndexException { final int nRows = subMatrix.length; if (nRows == 0) { throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.AT_LEAST_ONE_ROW); } final int nCols = subMatrix[0].length; if (nCols == 0) { throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.AT_LEAST_ONE_COLUMN); } for (int r = 1; r < nRows; ++r) { if (subMatrix[r].length != nCols) { throw MathRuntimeException.createIllegalArgumentException( LocalizedFormats.DIFFERENT_ROWS_LENGTHS, nCols, subMatrix[r].length); } } checkRowIndex(row); checkColumnIndex(column); checkRowIndex(nRows + row - 1); checkColumnIndex(nCols + column - 1); for (int i = 0; i < nRows; ++i) { for (int j = 0; j < nCols; ++j) { setEntry(row + i, column + j, subMatrix[i][j]); } } }
/** * Check if submatrix ranges indices are valid. * Rows and columns are indicated counting from 0 to n-1. * * @param startRow Initial row index * @param endRow Final row index * @param startColumn Initial column index * @param endColumn Final column index * @exception MatrixIndexException if the indices are not valid */ protected void checkSubMatrixIndex(final int startRow, final int endRow, final int startColumn, final int endColumn) { checkRowIndex(startRow); checkRowIndex(endRow); if (startRow > endRow) { throw new MatrixIndexException(LocalizedFormats.INITIAL_ROW_AFTER_FINAL_ROW, startRow, endRow); } checkColumnIndex(startColumn); checkColumnIndex(endColumn); if (startColumn > endColumn) { throw new MatrixIndexException(LocalizedFormats.INITIAL_COLUMN_AFTER_FINAL_COLUMN, startColumn, endColumn); } }
/** {@inheritDoc} */ public FieldMatrix<T> getSubMatrix(final int[] selectedRows, final int[] selectedColumns) throws MatrixIndexException { // safety checks checkSubMatrixIndex(selectedRows, selectedColumns); // copy entries final FieldMatrix<T> subMatrix = createMatrix(selectedRows.length, selectedColumns.length); subMatrix.walkInOptimizedOrder(new DefaultFieldMatrixChangingVisitor<T>(field.getZero()) { /** {@inheritDoc} */ @Override public T visit(final int row, final int column, final T value) { return getEntry(selectedRows[row], selectedColumns[column]); } }); return subMatrix; }
/** {@inheritDoc} */ public void copySubMatrix(int[] selectedRows, int[] selectedColumns, T[][] destination) throws MatrixIndexException, IllegalArgumentException { // safety checks checkSubMatrixIndex(selectedRows, selectedColumns); if ((destination.length < selectedRows.length) || (destination[0].length < selectedColumns.length)) { throw MathRuntimeException.createIllegalArgumentException( LocalizedFormats.DIMENSIONS_MISMATCH_2x2, destination.length, destination[0].length, selectedRows.length, selectedColumns.length); } // copy entries for (int i = 0; i < selectedRows.length; i++) { final T[] destinationI = destination[i]; for (int j = 0; j < selectedColumns.length; j++) { destinationI[j] = getEntry(selectedRows[i], selectedColumns[j]); } } }
checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); final int rowsCount = endRow + 1 - startRow; final int columnsCount = endColumn + 1 - startColumn; walkInOptimizedOrder(new DefaultFieldMatrixPreservingVisitor<T>(field.getZero()) {
/** {@inheritDoc} */ public FieldVector<T> getColumnVector(final int column) throws MatrixIndexException { return new ArrayFieldVector<T>(getColumn(column), false); }
/** {@inheritDoc} */ @Override public FieldMatrix<T> add(final FieldMatrix<T> m) throws IllegalArgumentException { try { return add((Array2DRowFieldMatrix<T>) m); } catch (ClassCastException cce) { return super.add(m); } }
/** {@inheritDoc} */ public FieldMatrix<T> scalarMultiply(final T d) { final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final FieldMatrix<T> 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).multiply(d)); } } return out; }
/** {@inheritDoc} */ public T walkInColumnOrder(final FieldMatrixChangingVisitor<T> visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws MatrixIndexException, MatrixVisitorException { checkSubMatrixIndex(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) { final T oldValue = getEntry(row, column); final T newValue = visitor.visit(row, column, oldValue); setEntry(row, column, newValue); } } return visitor.end(); }