diff --git a/src/java/org/apache/commons/math/linear/BigMatrix.java b/src/java/org/apache/commons/math/linear/BigMatrix.java index a1b3ff222..0473c17f7 100644 --- a/src/java/org/apache/commons/math/linear/BigMatrix.java +++ b/src/java/org/apache/commons/math/linear/BigMatrix.java @@ -21,8 +21,11 @@ import java.math.BigDecimal; /** * Interface defining a real-valued matrix with basic algebraic operations, using * BigDecimal representations for the entries. + *

+ * Matrix element indexing is 0-based -- e.g., getEntry(0, 0) + * returns the element in the first row, first column of the matrix. * - * @version $Revision: 1.6 $ $Date: 2004/09/01 21:26:11 $ + * @version $Revision: 1.7 $ $Date: 2004/09/05 01:19:23 $ */ public interface BigMatrix { @@ -151,8 +154,8 @@ public interface BigMatrix { /** * Returns the entries in row number row as an array. *

- * Row indices start at 1. A MatrixIndexException is thrown - * unless 0 < row <= rowDimension. + * Row indices start at 0. A MatrixIndexException is thrown + * unless 0 <= row < rowDimension. * * @param row the row to be fetched * @return array of entries in the row @@ -164,8 +167,8 @@ public interface BigMatrix { * Returns the entries in row number row as an array * of double values. *

- * Row indices start at 1. A MatrixIndexException is thrown - * unless 0 < row <= rowDimension. + * Row indices start at 0. A MatrixIndexException is thrown + * unless 0 <= row < rowDimension. * * @param row the row to be fetched * @return array of entries in the row @@ -176,8 +179,8 @@ public interface BigMatrix { /** * Returns the entries in column number col as an array. *

- * Column indices start at 1. A MatrixIndexException is thrown - * unless 0 < column <= columnDimension. + * Column indices start at 0. A MatrixIndexException is thrown + * unless 0 <= column < columnDimension. * * @param col the column to be fetched * @return array of entries in the column @@ -189,8 +192,8 @@ public interface BigMatrix { * Returns the entries in column number col as an array * of double values. *

- * Column indices start at 1. A MatrixIndexException is thrown - * unless 0 < column <= columnDimension. + * Column indices start at 0. A MatrixIndexException is thrown + * unless 0 <= column < columnDimension. * * @param col the column to be fetched * @return array of entries in the column @@ -201,10 +204,10 @@ public interface BigMatrix { /** * Returns the entry in the specified row and column. *

- * Row and column indices start at 1 and must satisfy + * Row and column indices start at 0 and must satisfy *

* otherwise a MatrixIndexException is thrown. * @@ -218,10 +221,10 @@ public interface BigMatrix { /** * Returns the entry in the specified row and column as a double. *

- * Row and column indices start at 1 and must satisfy + * Row and column indices start at 0 and must satisfy *

* otherwise a MatrixIndexException is thrown. * @@ -235,10 +238,10 @@ public interface BigMatrix { /** * Sets the entry in the specified row and column to the specified value. *

- * Row and column indices start at 1 and must satisfy + * Row and column indices start at 0 and must satisfy *

* otherwise a MatrixIndexException is thrown. * @@ -254,10 +257,10 @@ public interface BigMatrix { /** * Sets the entry in the specified row and column to the specified value. *

- * Row and column indices start at 1 and must satisfy + * Row and column indices start at 0 and must satisfy *

* otherwise a MatrixIndexException is thrown. * @@ -274,10 +277,10 @@ public interface BigMatrix { * Sets the entry in the specified row and column to the * BigDecimal value represented by the input string. *

- * Row and column indices start at 1 and must satisfy + * Row and column indices start at 0 and must satisfy *

* otherwise a MatrixIndexException is thrown. * diff --git a/src/java/org/apache/commons/math/linear/BigMatrixImpl.java b/src/java/org/apache/commons/math/linear/BigMatrixImpl.java index d1ee7aca0..9efe3e221 100644 --- a/src/java/org/apache/commons/math/linear/BigMatrixImpl.java +++ b/src/java/org/apache/commons/math/linear/BigMatrixImpl.java @@ -30,16 +30,20 @@ import java.math.BigDecimal; *
  • getDeterminant
  • *
  • inverse
  • *

    - * Usage note:
    +* Usage notes:
    + *

    + * @version $Revision: 1.6 $ $Date: 2004/09/05 01:19:23 $ */ public class BigMatrixImpl implements BigMatrix, Serializable { @@ -428,20 +432,20 @@ public class BigMatrixImpl implements BigMatrix, Serializable { /** * Returns the entries in row number row as an array. *

    - * Row indices start at 1. A MatrixIndexException is thrown - * unless 0 < row <= rowDimension. + * Row indices start at 0. A MatrixIndexException is thrown + * unless 0 <= row < rowDimension. * * @param row the row to be fetched * @return array of entries in the row * @throws MatrixIndexException if the specified row index is not valid */ public BigDecimal[] getRow(int row) throws MatrixIndexException { - if ( !isValidCoordinate( row, 1 ) ) { + if ( !isValidCoordinate( row, 0 ) ) { throw new MatrixIndexException("illegal row argument"); } int ncols = this.getColumnDimension(); BigDecimal[] out = new BigDecimal[ncols]; - System.arraycopy(data[row - 1], 0, out, 0, ncols); + System.arraycopy(data[row], 0, out, 0, ncols); return out; } @@ -449,21 +453,21 @@ public class BigMatrixImpl implements BigMatrix, Serializable { * Returns the entries in row number row as an array * of double values. *

    - * Row indices start at 1. A MatrixIndexException is thrown - * unless 0 < row <= rowDimension. + * Row indices start at 0. A MatrixIndexException is thrown + * unless 0 <= row < rowDimension. * * @param row the row to be fetched * @return array of entries in the row * @throws MatrixIndexException if the specified row index is not valid */ public double[] getRowAsDoubleArray(int row) throws MatrixIndexException { - if ( !isValidCoordinate( row, 1 ) ) { + if ( !isValidCoordinate( row, 0 ) ) { throw new MatrixIndexException("illegal row argument"); } int ncols = this.getColumnDimension(); double[] out = new double[ncols]; for (int i=0;icol as an array. *

    - * Column indices start at 1. A MatrixIndexException is thrown - * unless 0 < column <= columnDimension. + * Column indices start at 0. A MatrixIndexException is thrown + * unless 0 <= column < columnDimension. * * @param col the column to be fetched * @return array of entries in the column * @throws MatrixIndexException if the specified column index is not valid */ public BigDecimal[] getColumn(int col) throws MatrixIndexException { - if ( !isValidCoordinate(1, col) ) { + if ( !isValidCoordinate(0, col) ) { throw new MatrixIndexException("illegal column argument"); } int nRows = this.getRowDimension(); BigDecimal[] out = new BigDecimal[nRows]; for (int i = 0; i < nRows; i++) { - out[i] = data[i][col - 1]; + out[i] = data[i][col]; } return out; } @@ -494,21 +498,21 @@ public class BigMatrixImpl implements BigMatrix, Serializable { * Returns the entries in column number col as an array * of double values. *

    - * Column indices start at 1. A MatrixIndexException is thrown - * unless 0 < column <= columnDimension. + * Column indices start at 0. A MatrixIndexException is thrown + * unless 0 <= column < columnDimension. * * @param col the column to be fetched * @return array of entries in the column * @throws MatrixIndexException if the specified column index is not valid */ public double[] getColumnAsDoubleArray(int col) throws MatrixIndexException { - if ( !isValidCoordinate( 1, col ) ) { + if ( !isValidCoordinate( 0, col ) ) { throw new MatrixIndexException("illegal column argument"); } int nrows = this.getRowDimension(); double[] out = new double[nrows]; for (int i=0;i - * Row and column indices start at 1 and must satisfy + * Row and column indices start at 0 and must satisfy *

    * otherwise a MatrixIndexException is thrown. * @@ -533,16 +537,16 @@ public class BigMatrixImpl implements BigMatrix, Serializable { if (!isValidCoordinate(row,column)) { throw new MatrixIndexException("matrix entry does not exist"); } - return data[row - 1][column - 1]; + return data[row][column]; } /** * Returns the entry in the specified row and column as a double. *

    - * Row and column indices start at 1 and must satisfy + * Row and column indices start at 0 and must satisfy *

    * otherwise a MatrixIndexException is thrown. * @@ -559,10 +563,10 @@ public class BigMatrixImpl implements BigMatrix, Serializable { /** * Sets the entry in the specified row and column to the specified value. *

    - * Row and column indices start at 1 and must satisfy + * Row and column indices start at 0 and must satisfy *

    * otherwise a MatrixIndexException is thrown. * @@ -576,17 +580,17 @@ public class BigMatrixImpl implements BigMatrix, Serializable { if (!isValidCoordinate(row,column)) { throw new MatrixIndexException("matrix entry does not exist"); } - data[row - 1][column - 1] = value; + data[row][column] = value; lu = null; } /** * Sets the entry in the specified row and column to the specified value. *

    - * Row and column indices start at 1 and must satisfy + * Row and column indices start at 0 and must satisfy *

    * otherwise a MatrixIndexException is thrown. * @@ -603,10 +607,10 @@ public class BigMatrixImpl implements BigMatrix, Serializable { * Sets the entry in the specified row and column to the * BigDecimal value represented by the input string. *

    - * Row and column indices start at 1 and must satisfy + * Row and column indices start at 0 and must satisfy *

    * otherwise a MatrixIndexException is thrown. * @@ -1166,7 +1170,7 @@ public class BigMatrixImpl implements BigMatrix, Serializable { int nRows = this.getRowDimension(); int nCols = this.getColumnDimension(); - return !(row < 1 || row > nRows || col < 1 || col > nCols); + return !(row < 0 || row >= nRows || col < 0 || col >= nCols); } } diff --git a/src/java/org/apache/commons/math/linear/RealMatrix.java b/src/java/org/apache/commons/math/linear/RealMatrix.java index 0ef1c8711..bec152b90 100644 --- a/src/java/org/apache/commons/math/linear/RealMatrix.java +++ b/src/java/org/apache/commons/math/linear/RealMatrix.java @@ -17,8 +17,12 @@ package org.apache.commons.math.linear; /** - * Interface defining a real-valued matrix with basic algebraic operations - * @version $Revision: 1.21 $ $Date: 2004/09/01 21:26:11 $ + * Interface defining a real-valued matrix with basic algebraic operations. + *

    + * Matrix element indexing is 0-based -- e.g., getEntry(0, 0) + * returns the element in the first row, first column of the matrix. + * + * @version $Revision: 1.22 $ $Date: 2004/09/05 01:19:23 $ */ public interface RealMatrix { @@ -108,8 +112,8 @@ public interface RealMatrix { /** * Returns the entries in row number row as an array. *

    - * Row indices start at 1. A MatrixIndexException is thrown - * unless 0 < row <= rowDimension. + * Row indices start at 0. A MatrixIndexException is thrown + * unless 0 <= row < rowDimension. * * @param row the row to be fetched * @return array of entries in the row @@ -120,8 +124,8 @@ public interface RealMatrix { /** * Returns the entries in column number col as an array. *

    - * Column indices start at 1. A MatrixIndexException is thrown - * unless 0 < column <= columnDimension. + * Column indices start at 0. A MatrixIndexException is thrown + * unless 0 <= column < columnDimension. * * @param col the column to be fetched * @return array of entries in the column @@ -132,10 +136,10 @@ public interface RealMatrix { /** * Returns the entry in the specified row and column. *

    - * Row and column indices start at 1 and must satisfy + * Row and column indices start at 0 and must satisfy *

    * otherwise a MatrixIndexException is thrown. * @@ -149,10 +153,10 @@ public interface RealMatrix { /** * Sets the entry in the specified row and column to the specified value. *

    - * Row and column indices start at 1 and must satisfy + * Row and column indices start at 0 and must satisfy *

    * otherwise a MatrixIndexException is thrown. * diff --git a/src/java/org/apache/commons/math/linear/RealMatrixImpl.java b/src/java/org/apache/commons/math/linear/RealMatrixImpl.java index 9cf4519e3..b148d4553 100644 --- a/src/java/org/apache/commons/math/linear/RealMatrixImpl.java +++ b/src/java/org/apache/commons/math/linear/RealMatrixImpl.java @@ -29,16 +29,21 @@ import java.io.Serializable; *
  • getDeterminant
  • *
  • inverse
  • *

    - * Usage note:
    + * Usage notes:
    + *

    * - * @version $Revision: 1.27 $ $Date: 2004/09/01 21:26:11 $ + * @version $Revision: 1.28 $ $Date: 2004/09/05 01:19:23 $ */ public class RealMatrixImpl implements RealMatrix, Serializable { @@ -306,41 +311,41 @@ public class RealMatrixImpl implements RealMatrix, Serializable { /** * Returns the entries in row number row as an array. *

    - * Row indices start at 1. A MatrixIndexException is thrown - * unless 0 < row <= rowDimension. + * Row indices start at 0. A MatrixIndexException is thrown + * unless 0 <= row < rowDimension. * * @param row the row to be fetched * @return array of entries in the row * @throws MatrixIndexException if the specified row index is not valid */ public double[] getRow(int row) throws MatrixIndexException { - if ( !isValidCoordinate( row, 1 ) ) { + if ( !isValidCoordinate( row, 0 ) ) { throw new MatrixIndexException("illegal row argument"); } int ncols = this.getColumnDimension(); double[] out = new double[ncols]; - System.arraycopy(data[row - 1], 0, out, 0, ncols); + System.arraycopy(data[row], 0, out, 0, ncols); return out; } /** * Returns the entries in column number col as an array. *

    - * Column indices start at 1. A MatrixIndexException is thrown - * unless 0 < column <= columnDimension. + * Column indices start at 0. A MatrixIndexException is thrown + * unless 0 <= column < columnDimension. * * @param col the column to be fetched * @return array of entries in the column * @throws MatrixIndexException if the specified column index is not valid */ public double[] getColumn(int col) throws MatrixIndexException { - if ( !isValidCoordinate(1, col) ) { + if ( !isValidCoordinate(0, col) ) { throw new MatrixIndexException("illegal column argument"); } int nRows = this.getRowDimension(); double[] out = new double[nRows]; for (int row = 0; row < nRows; row++) { - out[row] = data[row][col - 1]; + out[row] = data[row][col]; } return out; } @@ -348,10 +353,10 @@ public class RealMatrixImpl implements RealMatrix, Serializable { /** * Returns the entry in the specified row and column. *

    - * Row and column indices start at 1 and must satisfy + * Row and column indices start at 0 and must satisfy *

    * otherwise a MatrixIndexException is thrown. * @@ -365,16 +370,16 @@ public class RealMatrixImpl implements RealMatrix, Serializable { if (!isValidCoordinate(row,column)) { throw new MatrixIndexException("matrix entry does not exist"); } - return data[row - 1][column - 1]; + return data[row][column]; } /** * Sets the entry in the specified row and column to the specified value. *

    - * Row and column indices start at 1 and must satisfy + * Row and column indices start at 0 and must satisfy *

    * otherwise a MatrixIndexException is thrown. * @@ -388,7 +393,7 @@ public class RealMatrixImpl implements RealMatrix, Serializable { if (!isValidCoordinate(row,column)) { throw new MatrixIndexException("matrix entry does not exist"); } - data[row - 1][column - 1] = value; + data[row][column] = value; lu = null; } @@ -850,7 +855,7 @@ public class RealMatrixImpl implements RealMatrix, Serializable { int nRows = this.getRowDimension(); int nCols = this.getColumnDimension(); - return !(row < 1 || row > nRows || col < 1 || col > nCols); + return !(row < 0 || row > nRows - 1 || col < 0 || col > nCols -1); } } diff --git a/src/test/org/apache/commons/math/linear/BigMatrixImplTest.java b/src/test/org/apache/commons/math/linear/BigMatrixImplTest.java index c19b74eb2..87028ad38 100644 --- a/src/test/org/apache/commons/math/linear/BigMatrixImplTest.java +++ b/src/test/org/apache/commons/math/linear/BigMatrixImplTest.java @@ -24,7 +24,7 @@ import java.math.BigDecimal; /** * Test cases for the {@link BigMatrixImpl} class. * - * @version $Revision: 1.2 $ $Date: 2004/07/11 04:49:24 $ + * @version $Revision: 1.3 $ $Date: 2004/09/05 01:19:23 $ */ public final class BigMatrixImplTest extends TestCase { @@ -425,8 +425,8 @@ public final class BigMatrixImplTest extends TestCase { public void testGetVectors() { BigMatrix m = new BigMatrixImpl(testData); - assertClose("get row",m.getRowAsDoubleArray(1),testDataRow1,entryTolerance); - assertClose("get col",m.getColumnAsDoubleArray(3),testDataCol3,entryTolerance); + assertClose("get row",m.getRowAsDoubleArray(0),testDataRow1,entryTolerance); + assertClose("get col",m.getColumnAsDoubleArray(2),testDataCol3,entryTolerance); try { double[] x = m.getRowAsDoubleArray(10); fail("expecting MatrixIndexException"); @@ -443,17 +443,17 @@ public final class BigMatrixImplTest extends TestCase { public void testEntryMutators() { BigMatrix m = new BigMatrixImpl(testData); - assertEquals("get entry",m.getEntry(1,2).doubleValue(),2d,entryTolerance); - m.setEntry(1,2,100d); - assertEquals("get entry",m.getEntry(1,2).doubleValue(),100d,entryTolerance); + assertEquals("get entry",m.getEntry(0,1).doubleValue(),2d,entryTolerance); + m.setEntry(0,1,100d); + assertEquals("get entry",m.getEntry(0,1).doubleValue(),100d,entryTolerance); try { - double x = m.getEntry(0,2).doubleValue(); + double x = m.getEntry(-1,2).doubleValue(); fail("expecting MatrixIndexException"); } catch (MatrixIndexException ex) { ; } try { - m.setEntry(1,4,200d); + m.setEntry(1,3,200d); fail("expecting MatrixIndexException"); } catch (MatrixIndexException ex) { ; @@ -529,8 +529,8 @@ public final class BigMatrixImplTest extends TestCase { throw new InvalidMatrixException("incorrect dimensions"); } int n = lu.getRowDimension(); - for (int i = 1; i <= n; i++) { - for (int j = 1; j <= n; j++) { + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { if (j < i) { lower.setEntry(i, j, lu.getEntry(i, j)); upper.setEntry(i, j, 0d); @@ -552,9 +552,9 @@ public final class BigMatrixImplTest extends TestCase { } int n = matrix.getRowDimension(); BigMatrix out = new BigMatrixImpl(n, n); - for (int i =1; i <= n; i++) { - for (int j = 1; j <= n; j++) { - out.setEntry(i, j, matrix.getEntry(permutation[i -1] + 1, j)); + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + out.setEntry(i, j, matrix.getEntry(permutation[i], j)); } } return out; @@ -577,7 +577,7 @@ public final class BigMatrixImplTest extends TestCase { for (int i = 0; i < m.getRowDimension(); i++) { String os = ""; for (int j = 0; j < m.getColumnDimension(); j++) { - os += m.getEntry(i+1, j+1) + " "; + os += m.getEntry(i, j) + " "; } System.out.println(os); } diff --git a/src/test/org/apache/commons/math/linear/RealMatrixImplTest.java b/src/test/org/apache/commons/math/linear/RealMatrixImplTest.java index 46217d333..52a40f964 100644 --- a/src/test/org/apache/commons/math/linear/RealMatrixImplTest.java +++ b/src/test/org/apache/commons/math/linear/RealMatrixImplTest.java @@ -22,7 +22,7 @@ import junit.framework.TestSuite; /** * Test cases for the {@link RealMatrixImpl} class. * - * @version $Revision: 1.14 $ $Date: 2004/05/18 04:08:38 $ + * @version $Revision: 1.15 $ $Date: 2004/09/05 01:19:23 $ */ public final class RealMatrixImplTest extends TestCase { @@ -369,8 +369,8 @@ public final class RealMatrixImplTest extends TestCase { public void testGetVectors() { RealMatrix m = new RealMatrixImpl(testData); - assertClose("get row",m.getRow(1),testDataRow1,entryTolerance); - assertClose("get col",m.getColumn(3),testDataCol3,entryTolerance); + assertClose("get row",m.getRow(0),testDataRow1,entryTolerance); + assertClose("get col",m.getColumn(2),testDataCol3,entryTolerance); try { double[] x = m.getRow(10); fail("expecting MatrixIndexException"); @@ -387,11 +387,11 @@ public final class RealMatrixImplTest extends TestCase { public void testEntryMutators() { RealMatrix m = new RealMatrixImpl(testData); - assertEquals("get entry",m.getEntry(1,2),2d,entryTolerance); + assertEquals("get entry",m.getEntry(0,1),2d,entryTolerance); m.setEntry(1,2,100d); assertEquals("get entry",m.getEntry(1,2),100d,entryTolerance); try { - double x = m.getEntry(0,2); + double x = m.getEntry(-1,2); fail("expecting MatrixIndexException"); } catch (MatrixIndexException ex) { ; @@ -492,8 +492,8 @@ public final class RealMatrixImplTest extends TestCase { throw new InvalidMatrixException("incorrect dimensions"); } int n = lu.getRowDimension(); - for (int i = 1; i <= n; i++) { - for (int j = 1; j <= n; j++) { + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { if (j < i) { lower.setEntry(i, j, lu.getEntry(i, j)); upper.setEntry(i, j, 0d); @@ -515,9 +515,9 @@ public final class RealMatrixImplTest extends TestCase { } int n = matrix.getRowDimension(); RealMatrix out = new RealMatrixImpl(n, n); - for (int i =1; i <= n; i++) { - for (int j = 1; j <= n; j++) { - out.setEntry(i, j, matrix.getEntry(permutation[i -1] + 1, j)); + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + out.setEntry(i, j, matrix.getEntry(permutation[i], j)); } } return out; @@ -540,7 +540,7 @@ public final class RealMatrixImplTest extends TestCase { for (int i = 0; i < m.getRowDimension(); i++) { String os = ""; for (int j = 0; j < m.getColumnDimension(); j++) { - os += m.getEntry(i+1, j+1) + " "; + os += m.getEntry(i, j) + " "; } System.out.println(os); }