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.


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@171229 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Phil Steitz 2005-05-21 16:29:19 +00:00
parent a4d35ef08b
commit cd3a81c0bb
5 changed files with 127 additions and 26 deletions

View File

@ -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);
}
/**

View File

@ -46,6 +46,24 @@ public class MatrixUtils {
return new RealMatrixImpl(data);
}
/**
* Returns <code>dimension x dimension</code> 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 <code>dimension x dimension</code> 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;
}
}

View File

@ -80,8 +80,14 @@ public class RealMatrixImpl implements RealMatrix, 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 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);
}
/**

View File

@ -188,5 +188,57 @@ public final class MatrixUtilsTest extends TestCase {
}
}
/**
* 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
}
}
}

View File

@ -39,6 +39,13 @@ The <action> type attribute can be add,update,fix,remove.
<body>
<release version="1.1" date="In Development"
description="Jakarta Commons Math 1.1 - Development">
<action dev="psteitz" type="update">
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.
</action>
<action dev="psteitz" type="update">
Made PRNG pluggable for classes in the random package. Added
RandomGenerator interface extracted from java.util.random and abstract