I think I learned this morning the hard way of making test cases, something I do often, but not always.......

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

--
http://www.kimvdlinde.com

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to