commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Phil Steitz <p...@steitz.com>
Subject [math] Addional Matrix methods (was: Re: Messing up more: (was: I messed up
Date Fri, 08 Oct 2004 02:31:10 GMT
Kim van der Linde wrote:
> I think I learned this morning the hard way of making test cases, 
> something I do often, but not always.......

Thanks!  Have a look in the src/test directory of [math] to see how we do 
unit tests in [math] using JUnit <http://www.junit.org/>.

I have been travelling, cut off from CVS, so have not been able to commit 
this stuff, but I will get it in in the next day or two.

Phil
> 
> public static void main(String args[])
> {
>   double[][] data = {{1, 2, 3, 4},
>                      {1.5, 2.5, 3.5, 4.5},
>                      {2, 4, 6, 8},
>                      {4, 5, 6, 7}};
>    Matrix m = new Matrix(data);
>    int[] rows = {0, 3};
>    int[] columns = {1, 2};
>    System.out.println(m.toSquareString());
>    System.out.println("Rows 0 to 2; columns 1 to 3");
>    System.out.println(m.getSubMatrix(0,2,1,3).toSquareString());
>    System.out.println("Rows 0 & 3; columns 1 & 2");
>    System.out.println(m.getSubMatrix(rows,columns).toSquareString());
>    System.out.println("Rows 0 to 2; columns 1 & 2");
>    System.out.println(m.getSubMatrix(0, 2 ,columns).toSquareString());
>    System.out.println("Rows 0 & 3; columns 1 to 3");
>    System.out.println(m.getSubMatrix(rows,1, 3).toSquareString());
>    System.out.println();
> }
> 
> And that showed that this is the correct method:
> 
> /**
>  * Get 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 matrix dimension
>  *                mismatch
>  */
> public Matrix getSubMatrix(int startRow, int endRow, int startColumn,
>             int endColumn) throws MatrixIndexException
> {
>   Matrix subMatrix = new Matrix(endRow - startRow+1,
>         endColumn - startColumn+1);
>   double[][] subMatrixData = subMatrix.getDataRef();
>   try
>   {
>     for (int i = startRow; i <= endRow; i++)
>     {
>       for (int j = startColumn; j <= endColumn; j++)
>       {
>         subMatrixData[i - startRow][j - startColumn] = data[i][j];
>       }
>     }
>   }
>   catch (ArrayIndexOutOfBoundsException e)
>   {
>     throw new MatrixIndexException("matrix dimension mismatch");
>   }
>   return subMatrix;
> }
> 
> O, I do have and do use two additional subMatrix versions as you can 
> see....
> 
> Cheers,
> 
> kim
> 


---------------------------------------------------------------------
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