Author: luc Date: Sun Jan 4 04:07:43 2009 New Revision: 731231 URL: http://svn.apache.org/viewvc?rev=731231&view=rev Log: added copySubmatrix methods use matrix visitors to implement some existing methods more efficiently
Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/linear/AbstractRealMatrix.java commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealMatrix.java Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/linear/AbstractRealMatrix.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/linear/AbstractRealMatrix.java?rev=731231&r1=731230&r2=731231&view=diff ============================================================================== --- commons/proper/math/trunk/src/java/org/apache/commons/math/linear/AbstractRealMatrix.java (original) +++ commons/proper/math/trunk/src/java/org/apache/commons/math/linear/AbstractRealMatrix.java Sun Jan 4 04:07:43 2009 @@ -285,24 +285,106 @@ } /** {...@inheritdoc} */ - public RealMatrix getSubMatrix(int[] selectedRows, int[] selectedColumns) + public RealMatrix getSubMatrix(final int[] selectedRows, final int[] selectedColumns) throws MatrixIndexException { + // safety checks checkSubMatrixIndex(selectedRows, selectedColumns); + // copy entries final RealMatrix subMatrix = createMatrix(selectedRows.length, selectedColumns.length); - for (int i = 0; i < selectedRows.length; i++) { - for (int j = 0; j < selectedColumns.length; j++) { - subMatrix.setEntry(i, j, getEntry(selectedRows[i], selectedColumns[j])); + subMatrix.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() { + + /** Serializable version identifier. */ + private static final long serialVersionUID = 4572851009041214720L; + + /** {...@inheritdoc} */ + public double visit(final int row, final int column, final double value) { + return getEntry(selectedRows[row], selectedColumns[column]); } - } + + }); return subMatrix; } /** {...@inheritdoc} */ + public void copySubMatrix(final int startRow, final int endRow, + final int startColumn, final int endColumn, + final double[][] destination) + throws MatrixIndexException, IllegalArgumentException { + + // safety checks + checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); + final int rowsCount = endRow + 1 - startRow; + final int columnsCount = endColumn + 1 - startColumn; + if ((destination.length < rowsCount) || (destination[0].length < columnsCount)) { + throw MathRuntimeException.createIllegalArgumentException( + "dimensions mismatch: got {0}x{1} but expected {2}x{3}", + new Object[] { + destination.length, destination[0].length, + rowsCount, columnsCount + }); + } + + // copy entries + walkInOptimizedOrder(new DefaultRealMatrixPreservingVisitor() { + + /** Serializable version identifier. */ + private static final long serialVersionUID = -6302162622577015104L; + + /** Initial row index. */ + private int startRow; + + /** Initial column index. */ + private int startColumn; + + /** {...@inheritdoc} */ + public void start(final int rows, final int columns, + final int startRow, final int endRow, + final int startColumn, final int endColumn) { + this.startRow = startRow; + this.startColumn = startColumn; + } + + /** {...@inheritdoc} */ + public void visit(final int row, final int column, final double value) { + destination[row - startRow][column - startColumn] = value; + } + + }, startRow, endRow, startColumn, endColumn); + + } + + /** {...@inheritdoc} */ + public void copySubMatrix(int[] selectedRows, int[] selectedColumns, double[][] destination) + throws MatrixIndexException, IllegalArgumentException { + + // safety checks + checkSubMatrixIndex(selectedRows, selectedColumns); + if ((destination.length < selectedRows.length) || + (destination[0].length < selectedColumns.length)) { + throw MathRuntimeException.createIllegalArgumentException( + "dimensions mismatch: got {0}x{1} but expected {2}x{3}", + new Object[] { + destination.length, destination[0].length, + selectedRows.length, selectedColumns.length + }); + } + + // copy entries + for (int i = 0; i < selectedRows.length; i++) { + final double[] destinationI = destination[i]; + for (int j = 0; j < selectedColumns.length; j++) { + destinationI[j] = getEntry(selectedRows[i], selectedColumns[j]); + } + } + + } + + /** {...@inheritdoc} */ public void setSubMatrix(final double[][] subMatrix, final int row, final int column) throws MatrixIndexException { @@ -554,27 +636,16 @@ final int nRows = getRowDimension(); final int nCols = getColumnDimension(); final RealMatrix out = createMatrix(nCols, nRows); - walkInOptimizedOrder(new RealMatrixPreservingVisitor() { + walkInOptimizedOrder(new DefaultRealMatrixPreservingVisitor() { - /** Serializable version identifier */ - private static final long serialVersionUID = 3807296710038754174L; - - /** {...@inheritdoc} */ - public void start(final int rows, final int columns, - final int startRow, final int endRow, - final int startColumn, final int endColumn) { - } + /** Serializable version identifier. */ + private static final long serialVersionUID = 2482589609486637597L; /** {...@inheritdoc} */ public void visit(final int row, final int column, final double value) { out.setEntry(column, row, value); } - /** {...@inheritdoc} */ - public double end() { - return 0; - } - }); return out; Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealMatrix.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealMatrix.java?rev=731231&r1=731230&r2=731231&view=diff ============================================================================== --- commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealMatrix.java (original) +++ commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealMatrix.java Sun Jan 4 04:07:43 2009 @@ -136,8 +136,8 @@ * specified rows and columns * @exception MatrixIndexException if the indices are not valid */ - RealMatrix getSubMatrix(int startRow, int endRow, int startColumn, - int endColumn) throws MatrixIndexException; + RealMatrix getSubMatrix(int startRow, int endRow, int startColumn, int endColumn) + throws MatrixIndexException; /** * Gets a submatrix. Rows and columns are indicated @@ -150,9 +150,40 @@ * @exception MatrixIndexException if row or column selections are not valid */ RealMatrix getSubMatrix(int[] selectedRows, int[] selectedColumns) - throws MatrixIndexException; + throws MatrixIndexException; /** + * Copy a submatrix. Rows and columns are indicated + * counting from 0 to n-1. + * + * @param startRow Initial row index + * @param endRow Final row index (inclusive) + * @param startColumn Initial column index + * @param endColumn Final column index (inclusive) + * @param destination The arrays where the submatrix data should be copied + * (if larger than rows/columns counts, only the upper-left part will be used) + * @exception MatrixIndexException if the indices are not valid + * @exception IllegalArgumentException if the destination array is too small + */ + void copySubMatrix(int startRow, int endRow, int startColumn, int endColumn, + double[][] destination) + throws MatrixIndexException, IllegalArgumentException; + + /** + * Copy a submatrix. Rows and columns are indicated + * counting from 0 to n-1. + * + * @param selectedRows Array of row indices. + * @param selectedColumns Array of column indices. + * @param destination The arrays where the submatrix data should be copied + * (if larger than rows/columns counts, only the upper-left part will be used) + * @exception MatrixIndexException if the indices are not valid + * @exception IllegalArgumentException if the destination array is too small + */ + void copySubMatrix(int[] selectedRows, int[] selectedColumns, double[][] destination) + throws MatrixIndexException, IllegalArgumentException; + + /** * Replace the submatrix starting at <code>row, column</code> using data in * the input <code>subMatrix</code> array. Indexes are 0-based. * <p>