diff --git a/src/java/org/apache/commons/math/linear/BigMatrixImpl.java b/src/java/org/apache/commons/math/linear/BigMatrixImpl.java
index 273955cc3..a1302df12 100644
--- a/src/java/org/apache/commons/math/linear/BigMatrixImpl.java
+++ b/src/java/org/apache/commons/math/linear/BigMatrixImpl.java
@@ -50,12 +50,6 @@ public class BigMatrixImpl implements BigMatrix, Serializable {
/** Serialization id */
static final long serialVersionUID = -1011428905656140431L;
- /** The number zero. */
- private static final BigDecimal ZERO = new BigDecimal(0);
-
- /** The number one. */
- private static final BigDecimal ONE = new BigDecimal(1);
-
/** Entries of the matrix */
private BigDecimal data[][] = null;
@@ -79,6 +73,11 @@ public class BigMatrixImpl implements BigMatrix, Serializable {
/** Bound to determine effective singularity in LU decomposition */
protected static BigDecimal TOO_SMALL = new BigDecimal(10E-12);
+ /** BigDecimal 0 */
+ static final BigDecimal ZERO = new BigDecimal(0);
+ /** BigDecimal 1 */
+ static final BigDecimal ONE = new BigDecimal(1);
+
/**
* Creates a matrix with no data
*/
@@ -90,8 +89,14 @@ public class BigMatrixImpl implements BigMatrix, Serializable {
*
* @param rowDimension the number of rows in the new matrix
* @param columnDimension the number of columns in the new matrix
+ * @throws IllegalArgumentException if row or column dimension is not
+ * positive
*/
public BigMatrixImpl(int rowDimension, int columnDimension) {
+ if (rowDimension <=0 || columnDimension <=0) {
+ throw new IllegalArgumentException
+ ("row and column dimensions must be positive");
+ }
data = new BigDecimal[rowDimension][columnDimension];
lu = null;
}
@@ -692,7 +697,8 @@ public class BigMatrixImpl implements BigMatrix, Serializable {
* @throws InvalidMatrixException if this is not invertible
*/
public BigMatrix inverse() throws InvalidMatrixException {
- return solve(getIdentity(this.getRowDimension()));
+ return solve(MatrixUtils.createBigIdentityMatrix
+ (this.getRowDimension()));
}
/**
@@ -1121,16 +1127,11 @@ public class BigMatrixImpl implements BigMatrix, Serializable {
*
* @param dimension dimension of identity matrix to generate
* @return identity matrix
+ * @throws IllegalArgumentException if dimension is not positive
+ * @deprecated use {@link MatrixUtils#createBigIdentityMatrix}
*/
protected BigMatrix getIdentity(int dimension) {
- BigMatrixImpl out = new BigMatrixImpl(dimension, dimension);
- BigDecimal[][] d = out.getDataRef();
- for (int row = 0; row < dimension; row++) {
- for (int col = 0; col < dimension; col++) {
- d[row][col] = row == col ? ONE : ZERO;
- }
- }
- return out;
+ return MatrixUtils.createBigIdentityMatrix(dimension);
}
/**
diff --git a/src/java/org/apache/commons/math/linear/MatrixUtils.java b/src/java/org/apache/commons/math/linear/MatrixUtils.java
index ed075a341..9ba7dde05 100644
--- a/src/java/org/apache/commons/math/linear/MatrixUtils.java
+++ b/src/java/org/apache/commons/math/linear/MatrixUtils.java
@@ -46,6 +46,24 @@ public class MatrixUtils {
return new RealMatrixImpl(data);
}
+ /**
+ * Returns dimension x dimension
identity matrix.
+ *
+ * @param dimension dimension of identity matrix to generate
+ * @return identity matrix
+ * @throws IllegalArgumentException if dimension is not positive
+ */
+ public static RealMatrix createRealIdentityMatrix(int dimension) {
+ RealMatrixImpl out = new RealMatrixImpl(dimension, dimension);
+ double[][] d = out.getDataRef();
+ for (int row = 0; row < dimension; row++) {
+ for (int col = 0; col < dimension; col++) {
+ d[row][col] = row == col ? 1d : 0d;
+ }
+ }
+ return out;
+ }
+
/**
* Returns a {@link BigMatrix} whose entries are the the values in the
* the input array. The input array is copied, not referenced.
@@ -223,6 +241,27 @@ public class MatrixUtils {
}
return new BigMatrixImpl(data);
}
+ /** BigDecimal constants */
+ //private static final BigDecimal ZERO = new BigDecimal(0);
+ //private static final BigDecimal ONE = new BigDecimal(1);
+
+ /**
+ * Returns dimension x dimension
identity matrix.
+ *
+ * @param dimension dimension of identity matrix to generate
+ * @return identity matrix
+ * @throws IllegalArgumentException if dimension is not positive
+ */
+ public static BigMatrix createBigIdentityMatrix(int dimension) {
+ BigMatrixImpl out = new BigMatrixImpl(dimension, dimension);
+ BigDecimal[][] d = out.getDataRef();
+ for (int row = 0; row < dimension; row++) {
+ for (int col = 0; col < dimension; col++) {
+ d[row][col] = row == col ? BigMatrixImpl.ONE : BigMatrixImpl.ZERO;
+ }
+ }
+ return out;
+ }
}
diff --git a/src/java/org/apache/commons/math/linear/RealMatrixImpl.java b/src/java/org/apache/commons/math/linear/RealMatrixImpl.java
index 3de84ca9d..e7f370752 100644
--- a/src/java/org/apache/commons/math/linear/RealMatrixImpl.java
+++ b/src/java/org/apache/commons/math/linear/RealMatrixImpl.java
@@ -78,10 +78,16 @@ public class RealMatrixImpl implements RealMatrix, Serializable {
/**
* Create a new RealMatrix with the supplied row and column dimensions.
*
- * @param rowDimension the number of rows in the new matrix
- * @param columnDimension the number of columns in the new matrix
+ * @param rowDimension the number of rows in the new matrix
+ * @param columnDimension the number of columns in the new matrix
+ * @throws IllegalArgumentException if row or column dimension is not
+ * positive
*/
public RealMatrixImpl(int rowDimension, int columnDimension) {
+ if (rowDimension <= 0 || columnDimension <= 0) {
+ throw new IllegalArgumentException(
+ "row and column dimensions must be postive");
+ }
data = new double[rowDimension][columnDimension];
lu = null;
}
@@ -494,7 +500,8 @@ public class RealMatrixImpl implements RealMatrix, Serializable {
* @throws InvalidMatrixException if this is not invertible
*/
public RealMatrix inverse() throws InvalidMatrixException {
- return solve(getIdentity(this.getRowDimension()));
+ return solve(MatrixUtils.createRealIdentityMatrix
+ (this.getRowDimension()));
}
/**
@@ -870,16 +877,11 @@ public class RealMatrixImpl implements RealMatrix, Serializable {
*
* @param dimension dimension of identity matrix to generate
* @return identity matrix
+ * @throws IllegalArgumentException if dimension is not positive
+ * @deprecated use {@link MatrixUtils#createRealIdentityMatrix}
*/
protected RealMatrix getIdentity(int dimension) {
- RealMatrixImpl out = new RealMatrixImpl(dimension, dimension);
- double[][] d = out.getDataRef();
- for (int row = 0; row < dimension; row++) {
- for (int col = 0; col < dimension; col++) {
- d[row][col] = row == col ? 1d : 0d;
- }
- }
- return out;
+ return MatrixUtils.createRealIdentityMatrix(dimension);
}
/**
diff --git a/src/test/org/apache/commons/math/linear/MatrixUtilsTest.java b/src/test/org/apache/commons/math/linear/MatrixUtilsTest.java
index d8a6f5b7e..2052bf0da 100644
--- a/src/test/org/apache/commons/math/linear/MatrixUtilsTest.java
+++ b/src/test/org/apache/commons/math/linear/MatrixUtilsTest.java
@@ -187,6 +187,58 @@ public final class MatrixUtilsTest extends TestCase {
// expected
}
}
+
+ /**
+ * Verifies that the matrix is an identity matrix
+ */
+ protected void checkIdentityMatrix(RealMatrix m) {
+ for (int i = 0; i < m.getRowDimension(); i++) {
+ for (int j =0; j < m.getColumnDimension(); j++) {
+ if (i == j) {
+ assertEquals(m.getEntry(i, j), 1d, 0);
+ } else {
+ assertEquals(m.getEntry(i, j), 0d, 0);
+ }
+ }
+ }
+ }
+
+ public void testCreateIdentityMatrix() {
+ checkIdentityMatrix(MatrixUtils.createRealIdentityMatrix(3));
+ checkIdentityMatrix(MatrixUtils.createRealIdentityMatrix(2));
+ checkIdentityMatrix(MatrixUtils.createRealIdentityMatrix(1));
+ try {
+ MatrixUtils.createRealIdentityMatrix(0);
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ }
+
+ /**
+ * Verifies that the matrix is an identity matrix
+ */
+ protected void checkIdentityBigMatrix(BigMatrix m) {
+ for (int i = 0; i < m.getRowDimension(); i++) {
+ for (int j =0; j < m.getColumnDimension(); j++) {
+ if (i == j) {
+ assertEquals(m.getEntry(i, j), BigMatrixImpl.ONE);
+ } else {
+ assertEquals(m.getEntry(i, j), BigMatrixImpl.ZERO);
+ }
+ }
+ }
+ }
+
+ public void testCreateBigIdentityMatrix() {
+ checkIdentityBigMatrix(MatrixUtils.createBigIdentityMatrix(3));
+ checkIdentityBigMatrix(MatrixUtils.createBigIdentityMatrix(2));
+ checkIdentityBigMatrix(MatrixUtils.createBigIdentityMatrix(1));
+ try {
+ MatrixUtils.createRealIdentityMatrix(0);
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ }
}
diff --git a/xdocs/changes.xml b/xdocs/changes.xml
index f65e2962f..5007dd5ce 100644
--- a/xdocs/changes.xml
+++ b/xdocs/changes.xml
@@ -39,6 +39,13 @@ The type attribute can be add,update,fix,remove.
+
+ Added createXIdentityMatrix methods to MatrixUtils and deprecated
+ getIdentity methods in RealMatrixImpl, BigMatrixImpl.
+ Modified RealMatrixImpl, BigMatrixImpl constructors to throw
+ IllegalArgumentExceptions instead of ArrayIndexOutOfBounds when
+ dimension arguments are not positive.
+
Made PRNG pluggable for classes in the random package. Added
RandomGenerator interface extracted from java.util.random and abstract