psteitz 2004/10/09 15:39:22 Modified: math/src/java/org/apache/commons/math/linear RealMatrix.java RealMatrixImpl.java math/src/test/org/apache/commons/math/linear RealMatrixImplTest.java Log: Added row and column matrix accessors. Pr #30897. Revision Changes Path 1.24 +24 -4 jakarta-commons/math/src/java/org/apache/commons/math/linear/RealMatrix.java Index: RealMatrix.java =================================================================== RCS file: /home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/linear/RealMatrix.java,v retrieving revision 1.23 retrieving revision 1.24 diff -u -r1.23 -r1.24 --- RealMatrix.java 9 Oct 2004 21:15:56 -0000 1.23 +++ RealMatrix.java 9 Oct 2004 22:39:22 -0000 1.24 @@ -128,15 +128,35 @@ * Gets a submatrix. Rows and columns are indicated * counting from 0 to n-1. * - * @param rows Array of row indices. - * @param columns Array of column indices. + * @param selectedRows Array of row indices. + * @param selectedColumns Array of column indices. * @return The subMatrix containing the data in the * specified rows and columns * @exception MatrixIndexException if row or column selections are not valid */ RealMatrix getSubMatrix(int[] selectedRows, int[] selectedColumns) throws MatrixIndexException; - + + /** + * Returns the entries in row number <code>row</code> + * as a row matrix. Row indices start at 0. + * + * @param row the row to be fetched + * @return row matrix + * @throws MatrixIndexException if the specified row index is invalid + */ + RealMatrix getRowMatrix(int row) throws MatrixIndexException; + + /** + * Returns the entries in column number <code>column</code> + * as a column matrix. Column indices start at 0. + * + * @param column the column to be fetched + * @return column matrix + * @throws MatrixIndexException if the specified column index is invalid + */ + RealMatrix getColumnMatrix(int column) throws MatrixIndexException; + /** * Returns the entries in row number <code>row</code> as an array. * <p> 1.30 +41 -3 jakarta-commons/math/src/java/org/apache/commons/math/linear/RealMatrixImpl.java Index: RealMatrixImpl.java =================================================================== RCS file: /home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/linear/RealMatrixImpl.java,v retrieving revision 1.29 retrieving revision 1.30 diff -u -r1.29 -r1.30 --- RealMatrixImpl.java 9 Oct 2004 21:15:56 -0000 1.29 +++ RealMatrixImpl.java 9 Oct 2004 22:39:22 -0000 1.30 @@ -343,8 +343,8 @@ * Gets a submatrix. Rows and columns are indicated * counting from 0 to n-1. * - * @param rows Array of row indices must be non-empty - * @param columns Array of column indices must be non-empty + * @param selectedRows Array of row indices must be non-empty + * @param selectedColumns Array of column indices must be non-empty * @return The subMatrix containing the data in the * specified rows and columns * @exception MatrixIndexException if supplied row or column index arrays @@ -371,6 +371,44 @@ } return subMatrix; } + + /** + * Returns the entries in row number <code>row</code> + * as a row matrix. Row indices start at 0. + * + * @param row the row to be fetched + * @return row matrix + * @throws MatrixIndexException if the specified row index is invalid + */ + public RealMatrix getRowMatrix(int row) throws MatrixIndexException { + if ( !isValidCoordinate( row, 0)) { + throw new MatrixIndexException("illegal row argument"); + } + int ncols = this.getColumnDimension(); + double[][] out = new double[1][ncols]; + System.arraycopy(data[row], 0, out[0], 0, ncols); + return new RealMatrixImpl(out); + } + + /** + * Returns the entries in column number <code>column</code> + * as a column matrix. Column indices start at 0. + * + * @param column the column to be fetched + * @return column matrix + * @throws MatrixIndexException if the specified column index is invalid + */ + public RealMatrix getColumnMatrix(int column) throws MatrixIndexException { + if ( !isValidCoordinate( 0, column)) { + throw new MatrixIndexException("illegal column argument"); + } + int nRows = this.getRowDimension(); + double[][] out = new double[nRows][1]; + for (int row = 0; row < nRows; row++) { + out[row][0] = data[row][column]; + } + return new RealMatrixImpl(out); + } /** * Returns the entries in row number <code>row</code> as an array. 1.17 +51 -1 jakarta-commons/math/src/test/org/apache/commons/math/linear/RealMatrixImplTest.java Index: RealMatrixImplTest.java =================================================================== RCS file: /home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/linear/RealMatrixImplTest.java,v retrieving revision 1.16 retrieving revision 1.17 diff -u -r1.16 -r1.17 --- RealMatrixImplTest.java 9 Oct 2004 21:15:56 -0000 1.16 +++ RealMatrixImplTest.java 9 Oct 2004 22:39:22 -0000 1.17 @@ -76,6 +76,12 @@ protected double[][] subRows01Cols23 = {{3,4} , {3.5, 4.5}}; protected double[][] subRows23Cols00 = {{2} , {4}}; protected double[][] subRows00Cols33 = {{4}}; + // row matrices + protected double[][] subRow0 = {{1,2,3,4}}; + protected double[][] subRow3 = {{4,5,6,7}}; + // column matrices + protected double[][] subColumn1 = {{2}, {2.5}, {4}, {5}}; + protected double[][] subColumn3 = {{4}, {4.5}, {8}, {7}}; // tolerances protected double entryTolerance = 10E-16; @@ -550,6 +556,50 @@ } try { m.getSubMatrix(new int[] {0}, new int[] {4}); + fail("Expecting MatrixIndexException"); + } catch (MatrixIndexException ex) { + // expected + } + } + + public void testGetRowMatrix() { + RealMatrix m = new RealMatrixImpl(subTestData); + RealMatrix mRow0 = new RealMatrixImpl(subRow0); + RealMatrix mRow3 = new RealMatrixImpl(subRow3); + assertClose("Row0", mRow0, + m.getRowMatrix(0), normTolerance ); + assertClose("Row3", mRow3, + m.getRowMatrix(3), normTolerance ); + try { + m.getRowMatrix(-1); + fail("Expecting MatrixIndexException"); + } catch (MatrixIndexException ex) { + // expected + } + try { + m.getRowMatrix(4); + fail("Expecting MatrixIndexException"); + } catch (MatrixIndexException ex) { + // expected + } + } + + public void testGetColumnMatrix() { + RealMatrix m = new RealMatrixImpl(subTestData); + RealMatrix mColumn1 = new RealMatrixImpl(subColumn1); + RealMatrix mColumn3 = new RealMatrixImpl(subColumn3); + assertClose("Column1", mColumn1, + m.getColumnMatrix(1), normTolerance ); + assertClose("Column3", mColumn3, + m.getColumnMatrix(3), normTolerance ); + try { + m.getColumnMatrix(-1); + fail("Expecting MatrixIndexException"); + } catch (MatrixIndexException ex) { + // expected + } + try { + m.getColumnMatrix(4); fail("Expecting MatrixIndexException"); } catch (MatrixIndexException ex) { // expected
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]