commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject cvs commit: jakarta-commons/math/src/test/org/apache/commons/math/linear RealMatrixImplTest.java
Date Sat, 09 Oct 2004 21:15:56 GMT
psteitz     2004/10/09 14:15:56

  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 submatrix accessors.
  Pr #30897
  Submitted by: Kim van der Linde
  Reviewed by: Phil Steitz
  
  Revision  Changes    Path
  1.23      +30 -2     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.22
  retrieving revision 1.23
  diff -u -r1.22 -r1.23
  --- RealMatrix.java	5 Sep 2004 01:19:23 -0000	1.22
  +++ RealMatrix.java	9 Oct 2004 21:15:56 -0000	1.23
  @@ -108,7 +108,35 @@
        * @return norm
        */
       double getNorm();
  -
  +    
  +    /**
  +     * Gets a submatrix. 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
  +     * @return The subMatrix containing the data of the
  +     *         specified rows and columns
  +     * @exception MatrixIndexException  if the indices are not valid
  +     */
  +   RealMatrix getSubMatrix(int startRow, int endRow, int startColumn,
  +            int endColumn) throws MatrixIndexException;
  +   
  +   /**
  +    * 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.
  +    * @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 an array.
        * <p>
  
  
  
  1.29      +65 -1     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.28
  retrieving revision 1.29
  diff -u -r1.28 -r1.29
  --- RealMatrixImpl.java	5 Sep 2004 01:19:23 -0000	1.28
  +++ RealMatrixImpl.java	9 Oct 2004 21:15:56 -0000	1.29
  @@ -307,6 +307,70 @@
           }
           return maxColSum;
       }
  +    
  +    /**
  +     * Gets a submatrix. 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
  +     * @return The subMatrix containing the data of the
  +     *         specified rows and columns
  +     * @exception MatrixIndexException if row or column selections are not valid
  +     */
  +    public RealMatrix getSubMatrix(int startRow, int endRow, int startColumn,
  +            int endColumn) throws MatrixIndexException {
  +        if (startRow < 0 || startRow > endRow || endRow > data.length
  +             || startColumn < 0 || startColumn > endColumn
  +             || endColumn > data[0].length ) {
  +            throw new MatrixIndexException(
  +                    "invalid row or column index selection");
  +        }
  +        RealMatrixImpl subMatrix = new RealMatrixImpl(endRow - startRow+1,
  +                endColumn - startColumn+1);
  +        double[][] subMatrixData = subMatrix.getDataRef();
  +        for (int i = startRow; i <= endRow; i++) {
  +            for (int j = startColumn; j <= endColumn; j++) {
  +                    subMatrixData[i - startRow][j - startColumn] = data[i][j];
  +                }
  +            }
  +        return subMatrix;
  +    }
  +    
  +    /**
  +     * 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
  +     * @return The subMatrix containing the data in the
  +     *     specified rows and columns
  +     * @exception MatrixIndexException  if supplied row or column index arrays
  +     *     are not valid
  +     */
  +    public RealMatrix getSubMatrix(int[] selectedRows, int[] selectedColumns)
  +    throws MatrixIndexException {
  +        if (selectedRows.length * selectedColumns.length == 0) {
  +            throw new MatrixIndexException(
  +                    "selected row and column index arrays must be non-empty");
  +        }
  +        RealMatrixImpl subMatrix = new RealMatrixImpl(selectedRows.length,
  +                selectedColumns.length);
  +        double[][] subMatrixData = subMatrix.getDataRef();
  +        try  {
  +            for (int i = 0; i < selectedRows.length; i++) {
  +                for (int j = 0; j < selectedColumns.length; j++) {
  +                    subMatrixData[i][j] = data[selectedRows[i]][selectedColumns[j]];
  +                }
  +            }
  +        }
  +        catch (ArrayIndexOutOfBoundsException e) {
  +            throw new MatrixIndexException("matrix dimension mismatch");
  +        }
  +        return subMatrix;
  +    } 
   
        /**
        * Returns the entries in row number <code>row</code> as an array.
  
  
  
  1.16      +115 -23   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.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- RealMatrixImplTest.java	5 Sep 2004 01:19:23 -0000	1.15
  +++ RealMatrixImplTest.java	9 Oct 2004 21:15:56 -0000	1.16
  @@ -27,32 +27,59 @@
   
   public final class RealMatrixImplTest extends TestCase {
       
  -    private double[][] testData = { {1d,2d,3d}, {2d,5d,3d}, {1d,0d,8d} };
  -    private double[][] testDataLU = {{2d, 5d, 3d}, {.5d, -2.5d, 6.5d}, {0.5d, 0.2d, .2d}};
  -    private double[][] testDataPlus2 = { {3d,4d,5d}, {4d,7d,5d}, {3d,2d,10d} };
  -    private double[][] testDataMinus = { {-1d,-2d,-3d}, {-2d,-5d,-3d}, 
  +    // 3 x 3 identity matrix
  +    protected double[][] id = { {1d,0d,0d}, {0d,1d,0d}, {0d,0d,1d} };
  +    
  +    // Test data for group operations
  +    protected double[][] testData = { {1d,2d,3d}, {2d,5d,3d}, {1d,0d,8d} };
  +    protected double[][] testDataLU = {{2d, 5d, 3d}, {.5d, -2.5d, 6.5d}, {0.5d, 0.2d, .2d}};
  +    protected double[][] testDataPlus2 = { {3d,4d,5d}, {4d,7d,5d}, {3d,2d,10d} };
  +    protected double[][] testDataMinus = { {-1d,-2d,-3d}, {-2d,-5d,-3d}, 
          {-1d,0d,-8d} };
  -    private double[] testDataRow1 = {1d,2d,3d};
  -    private double[] testDataCol3 = {3d,3d,8d};
  -    private double[][] testDataInv = 
  +    protected double[] testDataRow1 = {1d,2d,3d};
  +    protected double[] testDataCol3 = {3d,3d,8d};
  +    protected double[][] testDataInv = 
           { {-40d,16d,9d}, {13d,-5d,-3d}, {5d,-2d,-1d} };
  -    private double[] preMultTest = {8,12,33};
  -    private double[][] testData2 ={ {1d,2d,3d}, {2d,5d,3d}};
  -    private double[][] testData2T = { {1d,2d}, {2d,5d}, {3d,3d}};
  -    private double[][] testDataPlusInv = 
  +    protected double[] preMultTest = {8,12,33};
  +    protected double[][] testData2 ={ {1d,2d,3d}, {2d,5d,3d}};
  +    protected double[][] testData2T = { {1d,2d}, {2d,5d}, {3d,3d}};
  +    protected double[][] testDataPlusInv = 
           { {-39d,18d,12d}, {15d,0d,0d}, {6d,-2d,7d} };
  -    private double[][] id = { {1d,0d,0d}, {0d,1d,0d}, {0d,0d,1d} };
  -    private double[][] luData = { {2d,3d,3d}, {0d,5d,7d}, {6d,9d,8d} };
  -    private double[][] luDataLUDecomposition = { {6d,9d,8d}, {0d,5d,7d}, {0.33333333333333,0d,0.33333333333333}
};
  -    private double[][] singular = { {2d,3d}, {2d,3d} };
  -    private double[][] bigSingular = {{1d,2d,3d,4d}, {2d,5d,3d,4d},
  +    
  +    // lu decomposition tests
  +    protected double[][] luData = { {2d,3d,3d}, {0d,5d,7d}, {6d,9d,8d} };
  +    protected double[][] luDataLUDecomposition = { {6d,9d,8d}, {0d,5d,7d},
  +            {0.33333333333333,0d,0.33333333333333} };
  +    
  +    // singular matrices
  +    protected double[][] singular = { {2d,3d}, {2d,3d} };
  +    protected double[][] bigSingular = {{1d,2d,3d,4d}, {2d,5d,3d,4d},
           {7d,3d,256d,1930d}, {3d,7d,6d,8d}}; // 4th row = 1st + 2nd
  -    private double[][] detData = { {1d,2d,3d}, {4d,5d,6d}, {7d,8d,10d} };
  -    private double[][] detData2 = { {1d, 3d}, {2d, 4d}};
  -    private double[] testVector = {1,2,3};
  -    private double[] testVector2 = {1,2,3,4};
  -    private double entryTolerance = 10E-16;
  -    private double normTolerance = 10E-14;
  +    protected double[][] detData = { {1d,2d,3d}, {4d,5d,6d}, {7d,8d,10d} };
  +    protected double[][] detData2 = { {1d, 3d}, {2d, 4d}};
  +    
  +    // vectors
  +    protected double[] testVector = {1,2,3};
  +    protected double[] testVector2 = {1,2,3,4};
  +    
  +    // submatrix accessor tests
  +    protected double[][] subTestData = {{1, 2, 3, 4}, {1.5, 2.5, 3.5, 4.5},
  +            {2, 4, 6, 8}, {4, 5, 6, 7}}; 
  +    // array selections
  +    protected double[][] subRows02Cols13 = { {2, 4}, {4, 8}};
  +    protected double[][] subRows03Cols12 = { {2, 3}, {5, 6}};
  +    protected double[][] subRows03Cols123 = { {2, 3, 4} , {5, 6, 7}};
  +    // effective permutations
  +    protected double[][] subRows20Cols123 = { {4, 6, 8} , {2, 3, 4}};
  +    protected double[][] subRows31Cols31 = {{7, 5}, {4.5, 2.5}};
  +    // contiguous ranges
  +    protected double[][] subRows01Cols23 = {{3,4} , {3.5, 4.5}};
  +    protected double[][] subRows23Cols00 = {{2} , {4}};
  +    protected double[][] subRows00Cols33 = {{4}};
  +    
  +    // tolerances
  +    protected double entryTolerance = 10E-16;
  +    protected double normTolerance = 10E-14;
       
       public RealMatrixImplTest(String name) {
           super(name);
  @@ -462,6 +489,71 @@
           assertEquals(-1 * solution[0] + 7 * solution[1] + 6 * solution[2], constants[1],
1E-12);
           assertEquals(4 * solution[0] - 3 * solution[1] -5 * solution[2], constants[2],
1E-12);   
           
  +    }
  +    
  +    // test submatrix accessors
  +    public void testSubMatrix() {
  +        RealMatrix m = new RealMatrixImpl(subTestData);
  +        RealMatrix mRows23Cols00 = new RealMatrixImpl(subRows23Cols00);
  +        RealMatrix mRows00Cols33 = new RealMatrixImpl(subRows00Cols33);
  +        RealMatrix mRows01Cols23 = new RealMatrixImpl(subRows01Cols23);
  +        RealMatrix mRows02Cols13 = new RealMatrixImpl(subRows02Cols13);
  +        RealMatrix mRows03Cols12 = new RealMatrixImpl(subRows03Cols12);
  +        RealMatrix mRows03Cols123 = new RealMatrixImpl(subRows03Cols123);
  +        RealMatrix mRows20Cols123 = new RealMatrixImpl(subRows20Cols123);
  +        RealMatrix mRows31Cols31 = new RealMatrixImpl(subRows31Cols31);
  +        assertClose("Rows23Cols00", mRows23Cols00, 
  +                m.getSubMatrix(2 , 3 , 0, 0), normTolerance );
  +        assertClose("Rows00Cols33", mRows00Cols33, 
  +                m.getSubMatrix(0 , 0 , 3, 3), normTolerance );
  +        assertClose("Rows01Cols23", mRows01Cols23,
  +                m.getSubMatrix(0 , 1 , 2, 3), normTolerance );   
  +        assertClose("Rows02Cols13", mRows02Cols13,
  +                m.getSubMatrix(new int[] {0,2}, new int[] {1,3}), normTolerance);  
  +        assertClose("Rows03Cols12", mRows03Cols12,
  +                m.getSubMatrix(new int[] {0,3}, new int[] {1,2}), normTolerance);  
  +        assertClose("Rows03Cols123", mRows03Cols123,
  +                m.getSubMatrix(new int[] {0,3}, new int[] {1,2,3}), normTolerance); 
  +        assertClose("Rows20Cols123", mRows20Cols123,
  +                m.getSubMatrix(new int[] {2,0}, new int[] {1,2,3}), normTolerance); 
  +        assertClose("Rows31Cols31", mRows31Cols31,
  +                m.getSubMatrix(new int[] {3,1}, new int[] {3,1}), normTolerance); 
  +        try {
  +            m.getSubMatrix(1,0,2,4);
  +            fail("Expecting MatrixIndexException");
  +        } catch (MatrixIndexException ex) {
  +            // expected
  +        }
  +        try {
  +            m.getSubMatrix(-1,1,2,2);
  +            fail("Expecting MatrixIndexException");
  +        } catch (MatrixIndexException ex) {
  +            // expected
  +        }
  +        try {
  +            m.getSubMatrix(1,0,2,2);
  +            fail("Expecting MatrixIndexException");
  +        } catch (MatrixIndexException ex) {
  +            // expected
  +        }
  +        try {
  +            m.getSubMatrix(1,0,2,4);
  +            fail("Expecting MatrixIndexException");
  +        } catch (MatrixIndexException ex) {
  +            // expected
  +        }
  +        try {
  +            m.getSubMatrix(new int[] {}, new int[] {0});
  +            fail("Expecting MatrixIndexException");
  +        } catch (MatrixIndexException ex) {
  +            // expected
  +        }
  +        try {
  +            m.getSubMatrix(new int[] {0}, new int[] {4});
  +            fail("Expecting MatrixIndexException");
  +        } catch (MatrixIndexException ex) {
  +            // expected
  +        }
       }
       
       //--------------- -----------------Protected methods
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message