Matrix exception classes moved back to package "linear". Removed unnecessary
"import" statements, fixed Javadoc and unit tests and upgraded them to
JUnit 4 (MATH-423).


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1079350 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2011-03-08 13:42:16 +00:00
parent dd39f901b2
commit e3fe16402e
42 changed files with 1312 additions and 1173 deletions

View File

@ -22,8 +22,6 @@ import java.util.Arrays;
import org.apache.commons.math.Field;
import org.apache.commons.math.FieldElement;
import org.apache.commons.math.exception.MatrixDimensionMismatchException;
import org.apache.commons.math.exception.NonSquareMatrixException;
import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.NoDataException;
import org.apache.commons.math.exception.OutOfRangeException;

View File

@ -20,8 +20,6 @@ package org.apache.commons.math.linear;
import org.apache.commons.math.exception.NoDataException;
import org.apache.commons.math.exception.NotStrictlyPositiveException;
import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.NonSquareMatrixException;
import org.apache.commons.math.exception.MatrixDimensionMismatchException;
import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.util.MathUtils;
import org.apache.commons.math.util.FastMath;

View File

@ -162,7 +162,7 @@ public class Array2DRowFieldMatrix<T extends FieldElement<T>>
*
* @param m Matrix to be added.
* @return {@code this} + m.
* @throws org.apache.commons.math.exception.MatrixDimensionMismatchException
* @throws MatrixDimensionMismatchException
* if {@code m} is not the same size as this matrix.
*/
public Array2DRowFieldMatrix<T> add(final Array2DRowFieldMatrix<T> m) {
@ -189,7 +189,7 @@ public class Array2DRowFieldMatrix<T extends FieldElement<T>>
*
* @param m Matrix to be subtracted.
* @return {@code this} + m.
* @throws org.apache.commons.math.exception.MatrixDimensionMismatchException
* @throws MatrixDimensionMismatchException
* if {@code m} is not the same size as this matrix.
*/
public Array2DRowFieldMatrix<T> subtract(final Array2DRowFieldMatrix<T> m) {

View File

@ -168,7 +168,7 @@ public class Array2DRowRealMatrix extends AbstractRealMatrix implements Serializ
*
* @param m Matrix to be added.
* @return {@code this} + m.
* @throws org.apache.commons.math.exception.MatrixDimensionMismatchException
* @throws MatrixDimensionMismatchException
* if {@code m} is not the same size as this matrix.
*/
public Array2DRowRealMatrix add(final Array2DRowRealMatrix m) {
@ -195,7 +195,7 @@ public class Array2DRowRealMatrix extends AbstractRealMatrix implements Serializ
*
* @param m Matrix to be subtracted.
* @return {@code this} - m.
* @throws org.apache.commons.math.exception.MatrixDimensionMismatchException
* @throws MatrixDimensionMismatchException
* if {@code m} is not the same size as this matrix.
*/
public Array2DRowRealMatrix subtract(final Array2DRowRealMatrix m) {

View File

@ -23,7 +23,6 @@ import org.apache.commons.math.Field;
import org.apache.commons.math.FieldElement;
import org.apache.commons.math.exception.NoDataException;
import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.MatrixDimensionMismatchException;
import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.util.FastMath;

View File

@ -20,7 +20,6 @@ package org.apache.commons.math.linear;
import java.io.Serializable;
import java.util.Arrays;
import org.apache.commons.math.exception.MatrixDimensionMismatchException;
import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.NoDataException;
import org.apache.commons.math.exception.util.LocalizedFormats;

View File

@ -27,8 +27,7 @@ package org.apache.commons.math.linear;
* <ul>
* <li>a {@link #getLT() getLT} method has been added,</li>
* <li>the <code>isspd</code> method has been removed, the constructors of
* implementation classes being expected to throw {@link
* org.apache.commons.math.exception.NonPositiveDefiniteMatrixException}
* implementation classes being expected to throw {@link NonPositiveDefiniteMatrixException}
* when a matrix cannot be decomposed,</li>
* <li>a {@link #getDeterminant() getDeterminant} method has been added,</li>
* <li>the <code>solve</code> method has been replaced by a {@link
@ -42,7 +41,6 @@ package org.apache.commons.math.linear;
* @since 2.0
*/
public interface CholeskyDecomposition {
/**
* Returns the matrix L of the decomposition.
* <p>L is an lower-triangular matrix</p>
@ -68,5 +66,4 @@ public interface CholeskyDecomposition {
* @return a solver
*/
DecompositionSolver getSolver();
}

View File

@ -18,9 +18,6 @@
package org.apache.commons.math.linear;
import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.NonSquareMatrixException;
import org.apache.commons.math.exception.NonSymmetricMatrixException;
import org.apache.commons.math.exception.NonPositiveDefiniteMatrixException;
import org.apache.commons.math.util.FastMath;
@ -270,8 +267,7 @@ public class CholeskyDecompositionImpl implements CholeskyDecomposition {
* @param b right-hand side of the equation A &times; X = B
* @return a vector X such that A &times; X = B
* @throws DimensionMismatchException if the matrices dimensions do not match.
* @throws org.apache.commons.math.exception.SingularMatrixException if
* the decomposed matrix is singular.
* @throws SingularMatrixException if the decomposed matrix is singular.
*/
public ArrayRealVector solve(ArrayRealVector b) {
return new ArrayRealVector(solve(b.getDataRef()), false);

View File

@ -42,7 +42,7 @@ public interface DecompositionSolver {
* @return a vector X that minimizes the two norm of A &times; X - B
* @throws org.apache.commons.math.exception.DimensionMismatchException
* if the matrices dimensions do not match.
* @throws org.apache.commons.math.exception.SingularMatrixException
* @throws SingularMatrixException
* if the decomposed matrix is singular.
*/
double[] solve(final double[] b);
@ -54,7 +54,7 @@ public interface DecompositionSolver {
* @return a vector X that minimizes the two norm of A &times; X - B
* @throws org.apache.commons.math.exception.DimensionMismatchException
* if the matrices dimensions do not match.
* @throws org.apache.commons.math.exception.SingularMatrixException
* @throws SingularMatrixException
* if the decomposed matrix is singular.
*/
RealVector solve(final RealVector b);
@ -66,7 +66,7 @@ public interface DecompositionSolver {
* @return a matrix X that minimizes the two norm of A &times; X - B
* @throws org.apache.commons.math.exception.DimensionMismatchException
* if the matrices dimensions do not match.
* @throws org.apache.commons.math.exception.SingularMatrixException
* @throws SingularMatrixException
* if the decomposed matrix is singular.
*/
RealMatrix solve(final RealMatrix b);
@ -79,7 +79,7 @@ public interface DecompositionSolver {
/** Get the inverse (or pseudo-inverse) of the decomposed matrix.
* @return inverse matrix
* @throws org.apache.commons.math.exception.SingularMatrixException
* @throws SingularMatrixException
* if the decomposed matrix is singular.
*/
RealMatrix getInverse();

View File

@ -18,8 +18,6 @@
package org.apache.commons.math.linear;
import org.apache.commons.math.exception.MaxCountExceededException;
import org.apache.commons.math.exception.SingularMatrixException;
import org.apache.commons.math.exception.NonSymmetricMatrixException;
import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.util.MathUtils;

View File

@ -44,7 +44,7 @@ public interface FieldDecompositionSolver<T extends FieldElement<T>> {
* @return a vector X that minimizes the two norm of A &times; X - B
* @throws org.apache.commons.math.exception.DimensionMismatchException
* if the matrices dimensions do not match.
* @throws org.apache.commons.math.exception.SingularMatrixException
* @throws SingularMatrixException
* if the decomposed matrix is singular.
*/
T[] solve(final T[] b);
@ -56,7 +56,7 @@ public interface FieldDecompositionSolver<T extends FieldElement<T>> {
* @return a vector X that minimizes the two norm of A &times; X - B
* @throws org.apache.commons.math.exception.DimensionMismatchException
* if the matrices dimensions do not match.
* @throws org.apache.commons.math.exception.SingularMatrixException
* @throws SingularMatrixException
* if the decomposed matrix is singular.
*/
FieldVector<T> solve(final FieldVector<T> b);
@ -68,7 +68,7 @@ public interface FieldDecompositionSolver<T extends FieldElement<T>> {
* @return a matrix X that minimizes the two norm of A &times; X - B
* @throws org.apache.commons.math.exception.DimensionMismatchException
* if the matrices dimensions do not match.
* @throws org.apache.commons.math.exception.SingularMatrixException
* @throws SingularMatrixException
* if the decomposed matrix is singular.
*/
FieldMatrix<T> solve(final FieldMatrix<T> b);
@ -81,7 +81,7 @@ public interface FieldDecompositionSolver<T extends FieldElement<T>> {
/** Get the inverse (or pseudo-inverse) of the decomposed matrix.
* @return inverse matrix
* @throws org.apache.commons.math.exception.SingularMatrixException
* @throws SingularMatrixException
* if the decomposed matrix is singular.
*/
FieldMatrix<T> getInverse();

View File

@ -22,8 +22,6 @@ import java.lang.reflect.Array;
import org.apache.commons.math.Field;
import org.apache.commons.math.FieldElement;
import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.NonSquareMatrixException;
import org.apache.commons.math.exception.SingularMatrixException;
/**
* Calculates the LUP-decomposition of a square matrix.

View File

@ -63,7 +63,7 @@ public interface FieldMatrix<T extends FieldElement<T>> extends AnyMatrix {
*
* @param m Matrix to be added.
* @return {@code this} + {@code m}.
* @throws org.apache.commons.math.exception.MatrixDimensionMismatchException
* @throws MatrixDimensionMismatchException
* if {@code m} is not the same size as this matrix.
*/
FieldMatrix<T> add(FieldMatrix<T> m);
@ -73,7 +73,7 @@ public interface FieldMatrix<T extends FieldElement<T>> extends AnyMatrix {
*
* @param m Matrix to be subtracted.
* @return {@code this} - {@code m}.
* @throws org.apache.commons.math.exception.MatrixDimensionMismatchException
* @throws MatrixDimensionMismatchException
* if {@code m} is not the same size as this matrix.
*/
FieldMatrix<T> subtract(FieldMatrix<T> m);
@ -201,7 +201,7 @@ public interface FieldMatrix<T extends FieldElement<T>> extends AnyMatrix {
* @param subMatrix Array containing the submatrix replacement data.
* @param row Row coordinate of the top-left element to be replaced.
* @param column Column coordinate of the top-left element to be replaced.
* @throws org.apache.commons.math.exception.MatrixDimensionMismatchException
* @throws MatrixDimensionMismatchException
* if {@code subMatrix} does not fit into this matrix from element in
* {@code (row, column)}.
* @throws org.apache.commons.math.exception.ZeroException if a row or column
@ -235,7 +235,7 @@ public interface FieldMatrix<T extends FieldElement<T>> extends AnyMatrix {
* of columns as the instance).
* @throws org.apache.commons.math.exception.OutOfRangeException
* if the specified row index is invalid.
* @throws org.apache.commons.math.exception.MatrixDimensionMismatchException
* @throws MatrixDimensionMismatchException
* if the matrix dimensions do not match one instance row.
*/
void setRowMatrix(int row, FieldMatrix<T> matrix);
@ -260,7 +260,7 @@ public interface FieldMatrix<T extends FieldElement<T>> extends AnyMatrix {
* number of rows as the instance).
* @throws org.apache.commons.math.exception.OutOfRangeException
* if the specified column index is invalid.
* @throws org.apache.commons.math.exception.MatrixDimensionMismatchException
* @throws MatrixDimensionMismatchException
* if the matrix dimensions do not match one instance column.
*/
void setColumnMatrix(int column, FieldMatrix<T> matrix);
@ -285,7 +285,7 @@ public interface FieldMatrix<T extends FieldElement<T>> extends AnyMatrix {
* as the instance).
* @throws org.apache.commons.math.exception.OutOfRangeException
* if the specified row index is invalid.
* @throws org.apache.commons.math.exception.MatrixDimensionMismatchException
* @throws MatrixDimensionMismatchException
* if the vector dimension does not match one instance row.
*/
void setRowVector(int row, FieldVector<T> vector);
@ -310,7 +310,7 @@ public interface FieldMatrix<T extends FieldElement<T>> extends AnyMatrix {
* as the instance).
* @throws org.apache.commons.math.exception.OutOfRangeException
* if the specified column index is invalid.
* @throws org.apache.commons.math.exception.MatrixDimensionMismatchException
* @throws MatrixDimensionMismatchException
* if the vector dimension does not match one instance column.
*/
void setColumnVector(int column, FieldVector<T> vector);
@ -334,7 +334,7 @@ public interface FieldMatrix<T extends FieldElement<T>> extends AnyMatrix {
* the instance).
* @throws org.apache.commons.math.exception.OutOfRangeException
* if the specified row index is invalid.
* @throws org.apache.commons.math.exception.MatrixDimensionMismatchException
* @throws MatrixDimensionMismatchException
* if the array size does not match one instance row.
*/
void setRow(int row, T[] array);
@ -357,7 +357,7 @@ public interface FieldMatrix<T extends FieldElement<T>> extends AnyMatrix {
* @param array column array (must have the same number of rows as the instance)
* @throws org.apache.commons.math.exception.OutOfRangeException
* if the specified column index is invalid.
* @throws org.apache.commons.math.exception.MatrixDimensionMismatchException
* @throws MatrixDimensionMismatchException
* if the array size does not match one instance column.
*/
void setColumn(int column, T[] array);
@ -423,7 +423,7 @@ public interface FieldMatrix<T extends FieldElement<T>> extends AnyMatrix {
* trace</a> of the matrix (the sum of the elements on the main diagonal).
*
* @return trace
* @throws org.apache.commons.math.exception.NonSquareMatrixException
* @throws NonSquareMatrixException
* if the matrix is not square.
*/
T getTrace();

View File

@ -18,8 +18,6 @@
package org.apache.commons.math.linear;
import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.NonSquareMatrixException;
import org.apache.commons.math.exception.SingularMatrixException;
import org.apache.commons.math.util.FastMath;
/**

View File

@ -14,8 +14,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math.exception;
package org.apache.commons.math.linear;
import org.apache.commons.math.exception.MultiDimensionMismatchException;
import org.apache.commons.math.exception.util.LocalizedFormats;
/**

View File

@ -29,7 +29,6 @@ import org.apache.commons.math.exception.OutOfRangeException;
import org.apache.commons.math.exception.NoDataException;
import org.apache.commons.math.exception.NumberIsTooSmallException;
import org.apache.commons.math.exception.NullArgumentException;
import org.apache.commons.math.exception.MatrixDimensionMismatchException;
import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.fraction.BigFraction;

View File

@ -14,8 +14,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math.exception;
package org.apache.commons.math.linear;
import org.apache.commons.math.exception.MathIllegalArgumentException;
import org.apache.commons.math.exception.util.LocalizedFormats;
/**

View File

@ -14,8 +14,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math.exception;
package org.apache.commons.math.linear;
import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.util.LocalizedFormats;
/**

View File

@ -14,8 +14,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math.exception;
package org.apache.commons.math.linear;
import org.apache.commons.math.exception.MathIllegalArgumentException;
import org.apache.commons.math.exception.util.LocalizedFormats;
/**

View File

@ -170,7 +170,7 @@ public class OpenMapRealMatrix extends AbstractRealMatrix
*
* @param m Matrix to postmultiply by.
* @return {@code this} * {@code m}.
* @throws org.apache.commons.math.exception.MatrixDimensionMismatchException
* @throws MatrixDimensionMismatchException
* if the number of rows of {@code m} differ from the number of columns
* of this matrix.
*/

View File

@ -20,7 +20,6 @@ package org.apache.commons.math.linear;
import java.util.Arrays;
import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.SingularMatrixException;
import org.apache.commons.math.util.FastMath;

View File

@ -234,7 +234,7 @@ public interface RealMatrix extends AnyMatrix {
* columns as the instance).
* @throws org.apache.commons.math.exception.OutOfRangeException if the
* specified row index is invalid.
* @throws org.apache.commons.math.exception.MatrixDimensionMismatchException
* @throws MatrixDimensionMismatchException
* if the matrix dimensions do not match one instance row.
*/
void setRowMatrix(int row, RealMatrix matrix);
@ -259,7 +259,7 @@ public interface RealMatrix extends AnyMatrix {
* of rows as the instance).
* @throws org.apache.commons.math.exception.OutOfRangeException if
* the specified column index is invalid.
* @throws org.apache.commons.math.exception.MatrixDimensionMismatchException
* @throws MatrixDimensionMismatchException
* if the {@code matrix} dimensions do not match one instance column.
*/
void setColumnMatrix(int column, RealMatrix matrix);
@ -284,7 +284,7 @@ public interface RealMatrix extends AnyMatrix {
* as the instance).
* @throws org.apache.commons.math.exception.OutOfRangeException if
* the specified row index is invalid.
* @throws org.apache.commons.math.exception.MatrixDimensionMismatchException
* @throws MatrixDimensionMismatchException
* if the vector dimension does not match one instance row.
*/
void setRowVector(int row, RealVector vector);
@ -309,7 +309,7 @@ public interface RealMatrix extends AnyMatrix {
* the instance).
* @throws org.apache.commons.math.exception.OutOfRangeException if the
* specified column index is invalid.
* @throws org.apache.commons.math.exception.MatrixDimensionMismatchException
* @throws MatrixDimensionMismatchException
* if the vector dimension does not match one instance column.
*/
void setColumnVector(int column, RealVector vector);
@ -334,7 +334,7 @@ public interface RealMatrix extends AnyMatrix {
* the instance)
* @throws org.apache.commons.math.exception.OutOfRangeException if the
* specified row index is invalid.
* @throws org.apache.commons.math.exception.MatrixDimensionMismatchException
* @throws MatrixDimensionMismatchException
* if the array size does not match one instance row.
*/
void setRow(int row, double[] array);
@ -359,7 +359,7 @@ public interface RealMatrix extends AnyMatrix {
* the instance).
* @throws org.apache.commons.math.exception.OutOfRangeException if the
* specified column index is invalid.
* @throws org.apache.commons.math.exception.MatrixDimensionMismatchException
* @throws MatrixDimensionMismatchException
* if the array size does not match one instance column.
*/
void setColumn(int column, double[] array);
@ -427,7 +427,7 @@ public interface RealMatrix extends AnyMatrix {
* trace</a> of the matrix (the sum of the elements on the main diagonal).
*
* @return the trace.
* @throws org.apache.commons.math.exception.NonSquareMatrixException
* @throws NonSquareMatrixException
* if the matrix is not square.
*/
double getTrace();

View File

@ -14,8 +14,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math.exception;
package org.apache.commons.math.linear;
import org.apache.commons.math.exception.MathIllegalArgumentException;
import org.apache.commons.math.exception.util.LocalizedFormats;
/**

View File

@ -19,7 +19,6 @@ package org.apache.commons.math.linear;
import java.util.Arrays;
import org.apache.commons.math.exception.NonSquareMatrixException;
import org.apache.commons.math.util.FastMath;
@ -58,11 +57,12 @@ class TriDiagonalTransformer {
* Only the upper triangular part of the matrix is used.</p>
*
* @param matrix Symmetrical matrix to transform.
* @exception NonSquareMatrixException if the matrix is not square.
* @throws NonSquareMatrixException if the matrix is not square.
*/
public TriDiagonalTransformer(RealMatrix matrix) {
if (!matrix.isSquare()) {
throw new NonSquareMatrixException(matrix.getRowDimension(), matrix.getColumnDimension());
throw new NonSquareMatrixException(matrix.getRowDimension(),
matrix.getColumnDimension());
}
final int m = matrix.getRowDimension();

View File

@ -172,10 +172,10 @@ public abstract class AbstractLeastSquaresOptimizer
* Get the covariance matrix of the optimized parameters.
*
* @return the covariance matrix.
* @throws org.apache.commons.math.exception.SingularMatrixException
* @throws org.apache.commons.math.linear.SingularMatrixException
* if the covariance matrix cannot be computed (singular problem).
* @throws org.apache.commons.math.exception.MathUserException if the jacobian
* function throws one.
* @throws org.apache.commons.math.exception.MathUserException if the
* jacobian function throws one.
*/
public double[][] getCovariances() {
// set up the jacobian
@ -205,8 +205,8 @@ public abstract class AbstractLeastSquaresOptimizer
* Guessing is covariance-based: It only gives a rough order of magnitude.
*
* @return errors in optimized parameters
* @throws org.apache.commons.math.exception.SingularMatrixException if
* the covariances matrix cannot be computed.
* @throws org.apache.commons.math.linear.SingularMatrixException
* if the covariances matrix cannot be computed.
* @throws NumberIsTooSmallException if the number of degrees of freedom is not
* positive, i.e. the number of measurements is less or equal to the number of
* parameters.

View File

@ -18,7 +18,6 @@
package org.apache.commons.math.optimization.general;
import org.apache.commons.math.exception.MathUserException;
import org.apache.commons.math.exception.SingularMatrixException;
import org.apache.commons.math.exception.ConvergenceException;
import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.linear.BlockRealMatrix;
@ -26,6 +25,7 @@ import org.apache.commons.math.linear.DecompositionSolver;
import org.apache.commons.math.linear.LUDecompositionImpl;
import org.apache.commons.math.linear.QRDecompositionImpl;
import org.apache.commons.math.linear.RealMatrix;
import org.apache.commons.math.linear.SingularMatrixException;
import org.apache.commons.math.optimization.VectorialPointValuePair;
import org.apache.commons.math.optimization.ConvergenceChecker;

View File

@ -18,7 +18,7 @@
package org.apache.commons.math.random;
import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.NonPositiveDefiniteMatrixException;
import org.apache.commons.math.linear.NonPositiveDefiniteMatrixException;
import org.apache.commons.math.linear.MatrixUtils;
import org.apache.commons.math.linear.RealMatrix;
import org.apache.commons.math.util.FastMath;
@ -73,17 +73,18 @@ public class CorrelatedRandomVectorGenerator
/** Rank of the covariance matrix. */
private int rank;
/** Simple constructor.
* <p>Build a correlated random vector generator from its mean
* vector and covariance matrix.</p>
* @param mean expected mean values for all components
* @param covariance covariance matrix
* @param small diagonal elements threshold under which column are
/**
* Builds a correlated random vector generator from its mean
* vector and covariance matrix.
*
* @param mean Expected mean values for all components.
* @param covariance Covariance matrix.
* @param small Diagonal elements threshold under which column are
* considered to be dependent on previous ones and are discarded
* @param generator underlying generator for uncorrelated normalized
* components
* @throws NonPositiveDefiniteMatrixException if the
* covariance matrix is not strictly positive definite
* components.
* @throws org.apache.commons.math.linear.NonPositiveDefiniteMatrixException
* if the covariance matrix is not strictly positive definite.
* @throws DimensionMismatchException if the mean and covariance
* arrays dimensions do not match.
*/
@ -100,19 +101,19 @@ public class CorrelatedRandomVectorGenerator
this.generator = generator;
normalized = new double[rank];
}
/** Simple constructor.
* <p>Build a null mean random correlated vector generator from its
* covariance matrix.</p>
* @param covariance covariance matrix
* @param small diagonal elements threshold under which column are
* considered to be dependent on previous ones and are discarded
* @param generator underlying generator for uncorrelated normalized
* components
* @exception NonPositiveDefiniteMatrixException if the
* covariance matrix is not strictly positive definite
/**
* Builds a null mean random correlated vector generator from its
* covariance matrix.
*
* @param covariance Covariance matrix.
* @param small Diagonal elements threshold under which column are
* considered to be dependent on previous ones and are discarded.
* @param generator Underlying generator for uncorrelated normalized
* components.
* @throws org.apache.commons.math.linear.NonPositiveDefiniteMatrixException
* if the covariance matrix is not strictly positive definite.
*/
public CorrelatedRandomVectorGenerator(RealMatrix covariance, double small,
NormalizedRandomGenerator generator) {
@ -126,7 +127,6 @@ public class CorrelatedRandomVectorGenerator
this.generator = generator;
normalized = new double[rank];
}
/** Get the underlying normalized components generator.
@ -171,8 +171,8 @@ public class CorrelatedRandomVectorGenerator
* @param covariance covariance matrix
* @param small diagonal elements threshold under which column are
* considered to be dependent on previous ones and are discarded
* @throws NonPositiveDefiniteMatrixException if the
* covariance matrix is not strictly positive definite.
* @throws org.apache.commons.math.linear.NonPositiveDefiniteMatrixException
* if the covariance matrix is not strictly positive definite.
*/
private void decompose(RealMatrix covariance, double small) {
int order = covariance.getRowDimension();

View File

@ -16,17 +16,16 @@
*/
package org.apache.commons.math.linear;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.Assert;
import org.apache.commons.math.TestUtils;
import org.apache.commons.math.util.FastMath;
import org.apache.commons.math.exception.MathUserException;
import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.MatrixDimensionMismatchException;
import org.apache.commons.math.exception.OutOfRangeException;
import org.apache.commons.math.exception.NoDataException;
import org.apache.commons.math.exception.NumberIsTooSmallException;
import org.apache.commons.math.exception.NonSquareMatrixException;
import org.apache.commons.math.exception.MathIllegalArgumentException;
import org.apache.commons.math.exception.MathIllegalStateException;
@ -36,7 +35,7 @@ import org.apache.commons.math.exception.MathIllegalStateException;
* @version $Revision$ $Date$
*/
public final class Array2DRowRealMatrixTest extends TestCase {
public final class Array2DRowRealMatrixTest {
// 3 x 3 identity matrix
protected double[][] id = { {1d,0d,0d}, {0d,1d,0d}, {0d,0d,1d} };
@ -98,33 +97,32 @@ public final class Array2DRowRealMatrixTest extends TestCase {
protected double entryTolerance = 10E-16;
protected double normTolerance = 10E-14;
public Array2DRowRealMatrixTest(String name) {
super(name);
}
/** test dimensions */
@Test
public void testDimensions() {
Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData);
Array2DRowRealMatrix m2 = new Array2DRowRealMatrix(testData2);
assertEquals("testData row dimension",3,m.getRowDimension());
assertEquals("testData column dimension",3,m.getColumnDimension());
assertTrue("testData is square",m.isSquare());
assertEquals("testData2 row dimension",m2.getRowDimension(),2);
assertEquals("testData2 column dimension",m2.getColumnDimension(),3);
assertTrue("testData2 is not square",!m2.isSquare());
Assert.assertEquals("testData row dimension",3,m.getRowDimension());
Assert.assertEquals("testData column dimension",3,m.getColumnDimension());
Assert.assertTrue("testData is square",m.isSquare());
Assert.assertEquals("testData2 row dimension",m2.getRowDimension(),2);
Assert.assertEquals("testData2 column dimension",m2.getColumnDimension(),3);
Assert.assertTrue("testData2 is not square",!m2.isSquare());
}
/** test copy functions */
@Test
public void testCopyFunctions() {
Array2DRowRealMatrix m1 = new Array2DRowRealMatrix(testData);
Array2DRowRealMatrix m2 = new Array2DRowRealMatrix(m1.getData());
assertEquals(m2,m1);
Assert.assertEquals(m2,m1);
Array2DRowRealMatrix m3 = new Array2DRowRealMatrix(testData);
Array2DRowRealMatrix m4 = new Array2DRowRealMatrix(m3.getData(), false);
assertEquals(m4,m3);
Assert.assertEquals(m4,m3);
}
/** test add */
@Test
public void testAdd() {
Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData);
Array2DRowRealMatrix mInv = new Array2DRowRealMatrix(testDataInv);
@ -132,7 +130,7 @@ public final class Array2DRowRealMatrixTest extends TestCase {
double[][] sumEntries = mPlusMInv.getData();
for (int row = 0; row < m.getRowDimension(); row++) {
for (int col = 0; col < m.getColumnDimension(); col++) {
assertEquals("sum entry entry",
Assert.assertEquals("sum entry entry",
testDataPlusInv[row][col],sumEntries[row][col],
entryTolerance);
}
@ -140,34 +138,38 @@ public final class Array2DRowRealMatrixTest extends TestCase {
}
/** test add failure */
@Test
public void testAddFail() {
Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData);
Array2DRowRealMatrix m2 = new Array2DRowRealMatrix(testData2);
try {
m.add(m2);
fail("MathIllegalArgumentException expected");
Assert.fail("MathIllegalArgumentException expected");
} catch (MathIllegalArgumentException ex) {
// ignored
}
}
/** test norm */
@Test
public void testNorm() {
Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData);
Array2DRowRealMatrix m2 = new Array2DRowRealMatrix(testData2);
assertEquals("testData norm",14d,m.getNorm(),entryTolerance);
assertEquals("testData2 norm",7d,m2.getNorm(),entryTolerance);
Assert.assertEquals("testData norm",14d,m.getNorm(),entryTolerance);
Assert.assertEquals("testData2 norm",7d,m2.getNorm(),entryTolerance);
}
/** test Frobenius norm */
@Test
public void testFrobeniusNorm() {
Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData);
Array2DRowRealMatrix m2 = new Array2DRowRealMatrix(testData2);
assertEquals("testData Frobenius norm", FastMath.sqrt(117.0), m.getFrobeniusNorm(), entryTolerance);
assertEquals("testData2 Frobenius norm", FastMath.sqrt(52.0), m2.getFrobeniusNorm(), entryTolerance);
Assert.assertEquals("testData Frobenius norm", FastMath.sqrt(117.0), m.getFrobeniusNorm(), entryTolerance);
Assert.assertEquals("testData2 Frobenius norm", FastMath.sqrt(52.0), m2.getFrobeniusNorm(), entryTolerance);
}
/** test m-n = m + -n */
@Test
public void testPlusMinus() {
Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData);
Array2DRowRealMatrix m2 = new Array2DRowRealMatrix(testDataInv);
@ -175,14 +177,15 @@ public final class Array2DRowRealMatrixTest extends TestCase {
m2.scalarMultiply(-1d).add(m),entryTolerance);
try {
m.subtract(new Array2DRowRealMatrix(testData2));
fail("Expecting illegalArgumentException");
Assert.fail("Expecting illegalArgumentException");
} catch (MathIllegalArgumentException ex) {
// ignored
}
}
/** test multiply */
public void testMultiply() {
@Test
public void testMultiply() {
Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData);
Array2DRowRealMatrix mInv = new Array2DRowRealMatrix(testDataInv);
Array2DRowRealMatrix identity = new Array2DRowRealMatrix(id);
@ -199,7 +202,7 @@ public final class Array2DRowRealMatrixTest extends TestCase {
m2,entryTolerance);
try {
m.multiply(new Array2DRowRealMatrix(bigSingular));
fail("Expecting illegalArgumentException");
Assert.fail("Expecting illegalArgumentException");
} catch (MathIllegalArgumentException ex) {
// ignored
}
@ -211,6 +214,7 @@ public final class Array2DRowRealMatrixTest extends TestCase {
private double[][] d4 = new double[][] {{1},{2},{3},{4}};
private double[][] d5 = new double[][] {{30},{70}};
@Test
public void testMultiply2() {
RealMatrix m3 = new Array2DRowRealMatrix(d3);
RealMatrix m4 = new Array2DRowRealMatrix(d4);
@ -219,19 +223,21 @@ public final class Array2DRowRealMatrixTest extends TestCase {
}
/** test trace */
@Test
public void testTrace() {
RealMatrix m = new Array2DRowRealMatrix(id);
assertEquals("identity trace",3d,m.getTrace(),entryTolerance);
Assert.assertEquals("identity trace",3d,m.getTrace(),entryTolerance);
m = new Array2DRowRealMatrix(testData2);
try {
m.getTrace();
fail("Expecting NonSquareMatrixException");
Assert.fail("Expecting NonSquareMatrixException");
} catch (NonSquareMatrixException ex) {
// ignored
}
}
/** test sclarAdd */
@Test
public void testScalarAdd() {
RealMatrix m = new Array2DRowRealMatrix(testData);
TestUtils.assertEquals("scalar add",new Array2DRowRealMatrix(testDataPlus2),
@ -239,6 +245,7 @@ public final class Array2DRowRealMatrixTest extends TestCase {
}
/** test operate */
@Test
public void testOperate() {
RealMatrix m = new Array2DRowRealMatrix(id);
TestUtils.assertEquals("identity operate", testVector,
@ -248,25 +255,27 @@ public final class Array2DRowRealMatrixTest extends TestCase {
m = new Array2DRowRealMatrix(bigSingular);
try {
m.operate(testVector);
fail("Expecting illegalArgumentException");
Assert.fail("Expecting illegalArgumentException");
} catch (MathIllegalArgumentException ex) {
// ignored
}
}
/** test issue MATH-209 */
@Test
public void testMath209() {
RealMatrix a = new Array2DRowRealMatrix(new double[][] {
{ 1, 2 }, { 3, 4 }, { 5, 6 }
}, false);
double[] b = a.operate(new double[] { 1, 1 });
assertEquals(a.getRowDimension(), b.length);
assertEquals( 3.0, b[0], 1.0e-12);
assertEquals( 7.0, b[1], 1.0e-12);
assertEquals(11.0, b[2], 1.0e-12);
Assert.assertEquals(a.getRowDimension(), b.length);
Assert.assertEquals( 3.0, b[0], 1.0e-12);
Assert.assertEquals( 7.0, b[1], 1.0e-12);
Assert.assertEquals(11.0, b[2], 1.0e-12);
}
/** test transpose */
@Test
public void testTranspose() {
RealMatrix m = new Array2DRowRealMatrix(testData);
RealMatrix mIT = new LUDecompositionImpl(m).getSolver().getInverse().transpose();
@ -278,6 +287,7 @@ public final class Array2DRowRealMatrixTest extends TestCase {
}
/** test preMultiply by vector */
@Test
public void testPremultiplyVector() {
RealMatrix m = new Array2DRowRealMatrix(testData);
TestUtils.assertEquals("premultiply", m.preMultiply(testVector),
@ -287,12 +297,13 @@ public final class Array2DRowRealMatrixTest extends TestCase {
m = new Array2DRowRealMatrix(bigSingular);
try {
m.preMultiply(testVector);
fail("expecting MathIllegalArgumentException");
Assert.fail("expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException ex) {
// ignored
}
}
@Test
public void testPremultiply() {
RealMatrix m3 = new Array2DRowRealMatrix(d3);
RealMatrix m4 = new Array2DRowRealMatrix(d4);
@ -312,42 +323,45 @@ public final class Array2DRowRealMatrixTest extends TestCase {
mInv,entryTolerance);
try {
m.preMultiply(new Array2DRowRealMatrix(bigSingular));
fail("Expecting illegalArgumentException");
Assert.fail("Expecting illegalArgumentException");
} catch (MathIllegalArgumentException ex) {
// ignored
}
}
@Test
public void testGetVectors() {
RealMatrix m = new Array2DRowRealMatrix(testData);
TestUtils.assertEquals("get row",m.getRow(0),testDataRow1,entryTolerance);
TestUtils.assertEquals("get col",m.getColumn(2),testDataCol3,entryTolerance);
try {
m.getRow(10);
fail("expecting OutOfRangeException");
Assert.fail("expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// ignored
}
try {
m.getColumn(-1);
fail("expecting OutOfRangeException");
Assert.fail("expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// ignored
}
}
@Test
public void testGetEntry() {
RealMatrix m = new Array2DRowRealMatrix(testData);
assertEquals("get entry",m.getEntry(0,1),2d,entryTolerance);
Assert.assertEquals("get entry",m.getEntry(0,1),2d,entryTolerance);
try {
m.getEntry(10, 4);
fail ("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
}
/** test examples in user guide */
@Test
public void testExamples() {
// Create a real matrix with two rows and three columns
double[][] matrixData = { {1d,2d,3d}, {2d,5d,3d}};
@ -357,25 +371,26 @@ public final class Array2DRowRealMatrixTest extends TestCase {
RealMatrix n = new Array2DRowRealMatrix(matrixData2);
// Now multiply m by n
RealMatrix p = m.multiply(n);
assertEquals(2, p.getRowDimension());
assertEquals(2, p.getColumnDimension());
Assert.assertEquals(2, p.getRowDimension());
Assert.assertEquals(2, p.getColumnDimension());
// Invert p
RealMatrix pInverse = new LUDecompositionImpl(p).getSolver().getInverse();
assertEquals(2, pInverse.getRowDimension());
assertEquals(2, pInverse.getColumnDimension());
Assert.assertEquals(2, pInverse.getRowDimension());
Assert.assertEquals(2, pInverse.getColumnDimension());
// Solve example
double[][] coefficientsData = {{2, 3, -2}, {-1, 7, 6}, {4, -3, -5}};
RealMatrix coefficients = new Array2DRowRealMatrix(coefficientsData);
double[] constants = {1, -2, 1};
double[] solution = new LUDecompositionImpl(coefficients).getSolver().solve(constants);
assertEquals(2 * solution[0] + 3 * solution[1] -2 * solution[2], constants[0], 1E-12);
assertEquals(-1 * solution[0] + 7 * solution[1] + 6 * solution[2], constants[1], 1E-12);
assertEquals(4 * solution[0] - 3 * solution[1] -5 * solution[2], constants[2], 1E-12);
Assert.assertEquals(2 * solution[0] + 3 * solution[1] -2 * solution[2], constants[0], 1E-12);
Assert.assertEquals(-1 * solution[0] + 7 * solution[1] + 6 * solution[2], constants[1], 1E-12);
Assert.assertEquals(4 * solution[0] - 3 * solution[1] -5 * solution[2], constants[2], 1E-12);
}
// test submatrix accessors
@Test
public void testGetSubMatrix() {
RealMatrix m = new Array2DRowRealMatrix(subTestData);
checkGetSubMatrix(m, subRows23Cols00, 2 , 3 , 0, 0, false);
@ -400,9 +415,9 @@ public final class Array2DRowRealMatrixTest extends TestCase {
boolean mustFail) {
try {
RealMatrix sub = m.getSubMatrix(startRow, endRow, startColumn, endColumn);
assertEquals(new Array2DRowRealMatrix(reference), sub);
Assert.assertEquals(new Array2DRowRealMatrix(reference), sub);
if (mustFail) {
fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException");
Assert.fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException");
}
} catch (OutOfRangeException e) {
if (!mustFail) {
@ -424,9 +439,9 @@ public final class Array2DRowRealMatrixTest extends TestCase {
boolean mustFail) {
try {
RealMatrix sub = m.getSubMatrix(selectedRows, selectedColumns);
assertEquals(new Array2DRowRealMatrix(reference), sub);
Assert.assertEquals(new Array2DRowRealMatrix(reference), sub);
if (mustFail) {
fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException");
Assert.fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException");
}
} catch (OutOfRangeException e) {
if (!mustFail) {
@ -443,6 +458,7 @@ public final class Array2DRowRealMatrixTest extends TestCase {
}
}
@Test
public void testCopySubMatrix() {
RealMatrix m = new Array2DRowRealMatrix(subTestData);
checkCopy(m, subRows23Cols00, 2 , 3 , 0, 0, false);
@ -471,9 +487,9 @@ public final class Array2DRowRealMatrixTest extends TestCase {
new double[1][1] :
new double[reference.length][reference[0].length];
m.copySubMatrix(startRow, endRow, startColumn, endColumn, sub);
assertEquals(new Array2DRowRealMatrix(reference), new Array2DRowRealMatrix(sub));
Assert.assertEquals(new Array2DRowRealMatrix(reference), new Array2DRowRealMatrix(sub));
if (mustFail) {
fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException");
Assert.fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException");
}
} catch (OutOfRangeException e) {
if (!mustFail) {
@ -498,9 +514,9 @@ public final class Array2DRowRealMatrixTest extends TestCase {
new double[1][1] :
new double[reference.length][reference[0].length];
m.copySubMatrix(selectedRows, selectedColumns, sub);
assertEquals(new Array2DRowRealMatrix(reference), new Array2DRowRealMatrix(sub));
Assert.assertEquals(new Array2DRowRealMatrix(reference), new Array2DRowRealMatrix(sub));
if (mustFail) {
fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException");
Assert.fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException");
}
} catch (OutOfRangeException e) {
if (!mustFail) {
@ -517,165 +533,173 @@ public final class Array2DRowRealMatrixTest extends TestCase {
}
}
@Test
public void testGetRowMatrix() {
RealMatrix m = new Array2DRowRealMatrix(subTestData);
RealMatrix mRow0 = new Array2DRowRealMatrix(subRow0);
RealMatrix mRow3 = new Array2DRowRealMatrix(subRow3);
assertEquals("Row0", mRow0,
Assert.assertEquals("Row0", mRow0,
m.getRowMatrix(0));
assertEquals("Row3", mRow3,
Assert.assertEquals("Row3", mRow3,
m.getRowMatrix(3));
try {
m.getRowMatrix(-1);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.getRowMatrix(4);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
}
@Test
public void testSetRowMatrix() {
RealMatrix m = new Array2DRowRealMatrix(subTestData);
RealMatrix mRow3 = new Array2DRowRealMatrix(subRow3);
assertNotSame(mRow3, m.getRowMatrix(0));
Assert.assertNotSame(mRow3, m.getRowMatrix(0));
m.setRowMatrix(0, mRow3);
assertEquals(mRow3, m.getRowMatrix(0));
Assert.assertEquals(mRow3, m.getRowMatrix(0));
try {
m.setRowMatrix(-1, mRow3);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.setRowMatrix(0, m);
fail("Expecting MatrixDimensionMismatchException");
Assert.fail("Expecting MatrixDimensionMismatchException");
} catch (MatrixDimensionMismatchException ex) {
// expected
}
}
@Test
public void testGetColumnMatrix() {
RealMatrix m = new Array2DRowRealMatrix(subTestData);
RealMatrix mColumn1 = new Array2DRowRealMatrix(subColumn1);
RealMatrix mColumn3 = new Array2DRowRealMatrix(subColumn3);
assertEquals("Column1", mColumn1,
Assert.assertEquals("Column1", mColumn1,
m.getColumnMatrix(1));
assertEquals("Column3", mColumn3,
Assert.assertEquals("Column3", mColumn3,
m.getColumnMatrix(3));
try {
m.getColumnMatrix(-1);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.getColumnMatrix(4);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
}
@Test
public void testSetColumnMatrix() {
RealMatrix m = new Array2DRowRealMatrix(subTestData);
RealMatrix mColumn3 = new Array2DRowRealMatrix(subColumn3);
assertNotSame(mColumn3, m.getColumnMatrix(1));
Assert.assertNotSame(mColumn3, m.getColumnMatrix(1));
m.setColumnMatrix(1, mColumn3);
assertEquals(mColumn3, m.getColumnMatrix(1));
Assert.assertEquals(mColumn3, m.getColumnMatrix(1));
try {
m.setColumnMatrix(-1, mColumn3);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.setColumnMatrix(0, m);
fail("Expecting MatrixDimensionMismatchException");
Assert.fail("Expecting MatrixDimensionMismatchException");
} catch (MatrixDimensionMismatchException ex) {
// expected
}
}
@Test
public void testGetRowVector() {
RealMatrix m = new Array2DRowRealMatrix(subTestData);
RealVector mRow0 = new ArrayRealVector(subRow0[0]);
RealVector mRow3 = new ArrayRealVector(subRow3[0]);
assertEquals("Row0", mRow0, m.getRowVector(0));
assertEquals("Row3", mRow3, m.getRowVector(3));
Assert.assertEquals("Row0", mRow0, m.getRowVector(0));
Assert.assertEquals("Row3", mRow3, m.getRowVector(3));
try {
m.getRowVector(-1);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.getRowVector(4);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
}
@Test
public void testSetRowVector() {
RealMatrix m = new Array2DRowRealMatrix(subTestData);
RealVector mRow3 = new ArrayRealVector(subRow3[0]);
assertNotSame(mRow3, m.getRowMatrix(0));
Assert.assertNotSame(mRow3, m.getRowMatrix(0));
m.setRowVector(0, mRow3);
assertEquals(mRow3, m.getRowVector(0));
Assert.assertEquals(mRow3, m.getRowVector(0));
try {
m.setRowVector(-1, mRow3);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.setRowVector(0, new ArrayRealVector(5));
fail("Expecting MatrixDimensionMismatchException");
Assert.fail("Expecting MatrixDimensionMismatchException");
} catch (MatrixDimensionMismatchException ex) {
// expected
}
}
@Test
public void testGetColumnVector() {
RealMatrix m = new Array2DRowRealMatrix(subTestData);
RealVector mColumn1 = columnToVector(subColumn1);
RealVector mColumn3 = columnToVector(subColumn3);
assertEquals("Column1", mColumn1, m.getColumnVector(1));
assertEquals("Column3", mColumn3, m.getColumnVector(3));
Assert.assertEquals("Column1", mColumn1, m.getColumnVector(1));
Assert.assertEquals("Column3", mColumn3, m.getColumnVector(3));
try {
m.getColumnVector(-1);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.getColumnVector(4);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
}
@Test
public void testSetColumnVector() {
RealMatrix m = new Array2DRowRealMatrix(subTestData);
RealVector mColumn3 = columnToVector(subColumn3);
assertNotSame(mColumn3, m.getColumnVector(1));
Assert.assertNotSame(mColumn3, m.getColumnVector(1));
m.setColumnVector(1, mColumn3);
assertEquals(mColumn3, m.getColumnVector(1));
Assert.assertEquals(mColumn3, m.getColumnVector(1));
try {
m.setColumnVector(-1, mColumn3);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.setColumnVector(0, new ArrayRealVector(5));
fail("Expecting MatrixDimensionMismatchException");
Assert.fail("Expecting MatrixDimensionMismatchException");
} catch (MatrixDimensionMismatchException ex) {
// expected
}
@ -689,43 +713,46 @@ public final class Array2DRowRealMatrixTest extends TestCase {
return new ArrayRealVector(data, false);
}
@Test
public void testGetRow() {
RealMatrix m = new Array2DRowRealMatrix(subTestData);
checkArrays(subRow0[0], m.getRow(0));
checkArrays(subRow3[0], m.getRow(3));
try {
m.getRow(-1);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.getRow(4);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
}
@Test
public void testSetRow() {
RealMatrix m = new Array2DRowRealMatrix(subTestData);
assertTrue(subRow3[0][0] != m.getRow(0)[0]);
Assert.assertTrue(subRow3[0][0] != m.getRow(0)[0]);
m.setRow(0, subRow3[0]);
checkArrays(subRow3[0], m.getRow(0));
try {
m.setRow(-1, subRow3[0]);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.setRow(0, new double[5]);
fail("Expecting MatrixDimensionMismatchException");
Assert.fail("Expecting MatrixDimensionMismatchException");
} catch (MatrixDimensionMismatchException ex) {
// expected
}
}
@Test
public void testGetColumn() {
RealMatrix m = new Array2DRowRealMatrix(subTestData);
double[] mColumn1 = columnToArray(subColumn1);
@ -734,33 +761,34 @@ public final class Array2DRowRealMatrixTest extends TestCase {
checkArrays(mColumn3, m.getColumn(3));
try {
m.getColumn(-1);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.getColumn(4);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
}
@Test
public void testSetColumn() {
RealMatrix m = new Array2DRowRealMatrix(subTestData);
double[] mColumn3 = columnToArray(subColumn3);
assertTrue(mColumn3[0] != m.getColumn(1)[0]);
Assert.assertTrue(mColumn3[0] != m.getColumn(1)[0]);
m.setColumn(1, mColumn3);
checkArrays(mColumn3, m.getColumn(1));
try {
m.setColumn(-1, mColumn3);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.setColumn(0, new double[5]);
fail("Expecting MatrixDimensionMismatchException");
Assert.fail("Expecting MatrixDimensionMismatchException");
} catch (MatrixDimensionMismatchException ex) {
// expected
}
@ -775,68 +803,71 @@ public final class Array2DRowRealMatrixTest extends TestCase {
}
private void checkArrays(double[] expected, double[] actual) {
assertEquals(expected.length, actual.length);
Assert.assertEquals(expected.length, actual.length);
for (int i = 0; i < expected.length; ++i) {
assertEquals(expected[i], actual[i]);
Assert.assertEquals(expected[i], actual[i], 0);
}
}
@Test
public void testEqualsAndHashCode() {
Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData);
Array2DRowRealMatrix m1 = (Array2DRowRealMatrix) m.copy();
Array2DRowRealMatrix mt = (Array2DRowRealMatrix) m.transpose();
assertTrue(m.hashCode() != mt.hashCode());
assertEquals(m.hashCode(), m1.hashCode());
assertEquals(m, m);
assertEquals(m, m1);
assertFalse(m.equals(null));
assertFalse(m.equals(mt));
assertFalse(m.equals(new Array2DRowRealMatrix(bigSingular)));
Assert.assertTrue(m.hashCode() != mt.hashCode());
Assert.assertEquals(m.hashCode(), m1.hashCode());
Assert.assertEquals(m, m);
Assert.assertEquals(m, m1);
Assert.assertFalse(m.equals(null));
Assert.assertFalse(m.equals(mt));
Assert.assertFalse(m.equals(new Array2DRowRealMatrix(bigSingular)));
}
@Test
public void testToString() {
Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData);
assertEquals("Array2DRowRealMatrix{{1.0,2.0,3.0},{2.0,5.0,3.0},{1.0,0.0,8.0}}",
Assert.assertEquals("Array2DRowRealMatrix{{1.0,2.0,3.0},{2.0,5.0,3.0},{1.0,0.0,8.0}}",
m.toString());
m = new Array2DRowRealMatrix();
assertEquals("Array2DRowRealMatrix{}",
Assert.assertEquals("Array2DRowRealMatrix{}",
m.toString());
}
@Test
public void testSetSubMatrix() throws Exception {
Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData);
m.setSubMatrix(detData2,1,1);
RealMatrix expected = MatrixUtils.createRealMatrix
(new double[][] {{1.0,2.0,3.0},{2.0,1.0,3.0},{1.0,2.0,4.0}});
assertEquals(expected, m);
Assert.assertEquals(expected, m);
m.setSubMatrix(detData2,0,0);
expected = MatrixUtils.createRealMatrix
(new double[][] {{1.0,3.0,3.0},{2.0,4.0,3.0},{1.0,2.0,4.0}});
assertEquals(expected, m);
Assert.assertEquals(expected, m);
m.setSubMatrix(testDataPlus2,0,0);
expected = MatrixUtils.createRealMatrix
(new double[][] {{3.0,4.0,5.0},{4.0,7.0,5.0},{3.0,2.0,10.0}});
assertEquals(expected, m);
Assert.assertEquals(expected, m);
// dimension overflow
try {
m.setSubMatrix(testData,1,1);
fail("expecting OutOfRangeException");
Assert.fail("expecting OutOfRangeException");
} catch (OutOfRangeException e) {
// expected
}
// dimension underflow
try {
m.setSubMatrix(testData,-1,1);
fail("expecting OutOfRangeException");
Assert.fail("expecting OutOfRangeException");
} catch (OutOfRangeException e) {
// expected
}
try {
m.setSubMatrix(testData,1,-1);
fail("expecting OutOfRangeException");
Assert.fail("expecting OutOfRangeException");
} catch (OutOfRangeException e) {
// expected
}
@ -844,20 +875,20 @@ public final class Array2DRowRealMatrixTest extends TestCase {
// null
try {
m.setSubMatrix(null,1,1);
fail("expecting NullPointerException");
Assert.fail("expecting NullPointerException");
} catch (NullPointerException e) {
// expected
}
Array2DRowRealMatrix m2 = new Array2DRowRealMatrix();
try {
m2.setSubMatrix(testData,0,1);
fail("expecting MathIllegalStateException");
Assert.fail("expecting MathIllegalStateException");
} catch (MathIllegalStateException e) {
// expected
}
try {
m2.setSubMatrix(testData,1,0);
fail("expecting MathIllegalStateException");
Assert.fail("expecting MathIllegalStateException");
} catch (MathIllegalStateException e) {
// expected
}
@ -865,7 +896,7 @@ public final class Array2DRowRealMatrixTest extends TestCase {
// ragged
try {
m.setSubMatrix(new double[][] {{1}, {2, 3}}, 0, 0);
fail("expecting MathIllegalArgumentException");
Assert.fail("expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException e) {
// expected
}
@ -873,13 +904,13 @@ public final class Array2DRowRealMatrixTest extends TestCase {
// empty
try {
m.setSubMatrix(new double[][] {{}}, 0, 0);
fail("expecting MathIllegalArgumentException");
Assert.fail("expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException e) {
// expected
}
}
@Test
public void testWalk() throws MathUserException {
int rows = 150;
int columns = 75;
@ -888,87 +919,87 @@ public final class Array2DRowRealMatrixTest extends TestCase {
m.walkInRowOrder(new SetVisitor());
GetVisitor getVisitor = new GetVisitor();
m.walkInOptimizedOrder(getVisitor);
assertEquals(rows * columns, getVisitor.getCount());
Assert.assertEquals(rows * columns, getVisitor.getCount());
m = new Array2DRowRealMatrix(rows, columns);
m.walkInRowOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
getVisitor = new GetVisitor();
m.walkInOptimizedOrder(getVisitor, 1, rows - 2, 1, columns - 2);
assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
for (int i = 0; i < rows; ++i) {
assertEquals(0.0, m.getEntry(i, 0), 0);
assertEquals(0.0, m.getEntry(i, columns - 1), 0);
Assert.assertEquals(0.0, m.getEntry(i, 0), 0);
Assert.assertEquals(0.0, m.getEntry(i, columns - 1), 0);
}
for (int j = 0; j < columns; ++j) {
assertEquals(0.0, m.getEntry(0, j), 0);
assertEquals(0.0, m.getEntry(rows - 1, j), 0);
Assert.assertEquals(0.0, m.getEntry(0, j), 0);
Assert.assertEquals(0.0, m.getEntry(rows - 1, j), 0);
}
m = new Array2DRowRealMatrix(rows, columns);
m.walkInColumnOrder(new SetVisitor());
getVisitor = new GetVisitor();
m.walkInOptimizedOrder(getVisitor);
assertEquals(rows * columns, getVisitor.getCount());
Assert.assertEquals(rows * columns, getVisitor.getCount());
m = new Array2DRowRealMatrix(rows, columns);
m.walkInColumnOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
getVisitor = new GetVisitor();
m.walkInOptimizedOrder(getVisitor, 1, rows - 2, 1, columns - 2);
assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
for (int i = 0; i < rows; ++i) {
assertEquals(0.0, m.getEntry(i, 0), 0);
assertEquals(0.0, m.getEntry(i, columns - 1), 0);
Assert.assertEquals(0.0, m.getEntry(i, 0), 0);
Assert.assertEquals(0.0, m.getEntry(i, columns - 1), 0);
}
for (int j = 0; j < columns; ++j) {
assertEquals(0.0, m.getEntry(0, j), 0);
assertEquals(0.0, m.getEntry(rows - 1, j), 0);
Assert.assertEquals(0.0, m.getEntry(0, j), 0);
Assert.assertEquals(0.0, m.getEntry(rows - 1, j), 0);
}
m = new Array2DRowRealMatrix(rows, columns);
m.walkInOptimizedOrder(new SetVisitor());
getVisitor = new GetVisitor();
m.walkInRowOrder(getVisitor);
assertEquals(rows * columns, getVisitor.getCount());
Assert.assertEquals(rows * columns, getVisitor.getCount());
m = new Array2DRowRealMatrix(rows, columns);
m.walkInOptimizedOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
getVisitor = new GetVisitor();
m.walkInRowOrder(getVisitor, 1, rows - 2, 1, columns - 2);
assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
for (int i = 0; i < rows; ++i) {
assertEquals(0.0, m.getEntry(i, 0), 0);
assertEquals(0.0, m.getEntry(i, columns - 1), 0);
Assert.assertEquals(0.0, m.getEntry(i, 0), 0);
Assert.assertEquals(0.0, m.getEntry(i, columns - 1), 0);
}
for (int j = 0; j < columns; ++j) {
assertEquals(0.0, m.getEntry(0, j), 0);
assertEquals(0.0, m.getEntry(rows - 1, j), 0);
Assert.assertEquals(0.0, m.getEntry(0, j), 0);
Assert.assertEquals(0.0, m.getEntry(rows - 1, j), 0);
}
m = new Array2DRowRealMatrix(rows, columns);
m.walkInOptimizedOrder(new SetVisitor());
getVisitor = new GetVisitor();
m.walkInColumnOrder(getVisitor);
assertEquals(rows * columns, getVisitor.getCount());
Assert.assertEquals(rows * columns, getVisitor.getCount());
m = new Array2DRowRealMatrix(rows, columns);
m.walkInOptimizedOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
getVisitor = new GetVisitor();
m.walkInColumnOrder(getVisitor, 1, rows - 2, 1, columns - 2);
assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
for (int i = 0; i < rows; ++i) {
assertEquals(0.0, m.getEntry(i, 0), 0);
assertEquals(0.0, m.getEntry(i, columns - 1), 0);
Assert.assertEquals(0.0, m.getEntry(i, 0), 0);
Assert.assertEquals(0.0, m.getEntry(i, columns - 1), 0);
}
for (int j = 0; j < columns; ++j) {
assertEquals(0.0, m.getEntry(0, j), 0);
assertEquals(0.0, m.getEntry(rows - 1, j), 0);
Assert.assertEquals(0.0, m.getEntry(0, j), 0);
Assert.assertEquals(0.0, m.getEntry(rows - 1, j), 0);
}
}
@Test
public void testSerial() {
Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData);
assertEquals(m,TestUtils.serializeAndRecover(m));
Assert.assertEquals(m,TestUtils.serializeAndRecover(m));
}
@ -984,7 +1015,7 @@ public final class Array2DRowRealMatrixTest extends TestCase {
@Override
public void visit(int i, int j, double value) {
++count;
assertEquals(i + j / 1024.0, value, 0.0);
Assert.assertEquals(i + j / 1024.0, value, 0.0);
}
public int getCount() {
return count;

View File

@ -17,13 +17,8 @@
package org.apache.commons.math.linear;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.apache.commons.math.exception.NonSquareMatrixException;
import org.apache.commons.math.exception.NonPositiveDefiniteMatrixException;
import org.apache.commons.math.exception.NonSymmetricMatrixException;
import org.junit.Test;
import org.junit.Assert;
public class CholeskyDecompositionImplTest {
@ -40,10 +35,10 @@ public class CholeskyDecompositionImplTest {
public void testDimensions() {
CholeskyDecomposition llt =
new CholeskyDecompositionImpl(MatrixUtils.createRealMatrix(testData));
assertEquals(testData.length, llt.getL().getRowDimension());
assertEquals(testData.length, llt.getL().getColumnDimension());
assertEquals(testData.length, llt.getLT().getRowDimension());
assertEquals(testData.length, llt.getLT().getColumnDimension());
Assert.assertEquals(testData.length, llt.getL().getRowDimension());
Assert.assertEquals(testData.length, llt.getL().getColumnDimension());
Assert.assertEquals(testData.length, llt.getLT().getRowDimension());
Assert.assertEquals(testData.length, llt.getLT().getColumnDimension());
}
/** test non-square matrix */
@ -91,7 +86,7 @@ public class CholeskyDecompositionImplTest {
RealMatrix l = llt.getL();
RealMatrix lt = llt.getLT();
double norm = l.multiply(lt).subtract(matrix).getNorm();
assertEquals(0, norm, 1.0e-15);
Assert.assertEquals(0, norm, 1.0e-15);
}
/** test that L is lower triangular */
@ -101,7 +96,7 @@ public class CholeskyDecompositionImplTest {
RealMatrix l = new CholeskyDecompositionImpl(matrix).getL();
for (int i = 0; i < l.getRowDimension(); i++) {
for (int j = i + 1; j < l.getColumnDimension(); j++) {
assertEquals(0.0, l.getEntry(i, j), 0.0);
Assert.assertEquals(0.0, l.getEntry(i, j), 0.0);
}
}
}
@ -114,7 +109,7 @@ public class CholeskyDecompositionImplTest {
RealMatrix l = llt.getL();
RealMatrix lt = llt.getLT();
double norm = l.subtract(lt.transpose()).getNorm();
assertEquals(0, norm, 1.0e-15);
Assert.assertEquals(0, norm, 1.0e-15);
}
/** test matrices values */
@ -132,14 +127,12 @@ public class CholeskyDecompositionImplTest {
// check values against known references
RealMatrix l = llt.getL();
assertEquals(0, l.subtract(lRef).getNorm(), 1.0e-13);
Assert.assertEquals(0, l.subtract(lRef).getNorm(), 1.0e-13);
RealMatrix lt = llt.getLT();
assertEquals(0, lt.subtract(lRef.transpose()).getNorm(), 1.0e-13);
Assert.assertEquals(0, lt.subtract(lRef.transpose()).getNorm(), 1.0e-13);
// check the same cached instance is returned the second time
assertTrue(l == llt.getL());
assertTrue(lt == llt.getLT());
Assert.assertTrue(l == llt.getL());
Assert.assertTrue(lt == llt.getLT());
}
}

View File

@ -20,73 +20,75 @@ package org.apache.commons.math.linear;
import java.util.Random;
import org.apache.commons.math.util.MathUtils;
import org.apache.commons.math.exception.SingularMatrixException;
import org.apache.commons.math.exception.MathIllegalArgumentException;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.Assert;
public class EigenSolverTest extends TestCase {
private double[] refValues;
private RealMatrix matrix;
public EigenSolverTest(String name) {
super(name);
}
public class EigenSolverTest {
/** test non invertible matrix */
@Test
public void testNonInvertible() {
Random r = new Random(9994100315209l);
RealMatrix m =
EigenDecompositionImplTest.createTestMatrix(r, new double[] { 1.0, 0.0, -1.0, -2.0, -3.0 });
DecompositionSolver es = new EigenDecompositionImpl(m, MathUtils.SAFE_MIN).getSolver();
assertFalse(es.isNonSingular());
Assert.assertFalse(es.isNonSingular());
try {
es.getInverse();
fail("an exception should have been thrown");
Assert.fail("an exception should have been thrown");
} catch (SingularMatrixException ime) {
// expected behavior
}
}
/** test invertible matrix */
@Test
public void testInvertible() {
Random r = new Random(9994100315209l);
RealMatrix m =
EigenDecompositionImplTest.createTestMatrix(r, new double[] { 1.0, 0.5, -1.0, -2.0, -3.0 });
DecompositionSolver es = new EigenDecompositionImpl(m, MathUtils.SAFE_MIN).getSolver();
assertTrue(es.isNonSingular());
Assert.assertTrue(es.isNonSingular());
RealMatrix inverse = es.getInverse();
RealMatrix error =
m.multiply(inverse).subtract(MatrixUtils.createRealIdentityMatrix(m.getRowDimension()));
assertEquals(0, error.getNorm(), 4.0e-15);
Assert.assertEquals(0, error.getNorm(), 4.0e-15);
}
/** test solve dimension errors */
@Test
public void testSolveDimensionErrors() {
final double[] refValues = new double[] {
2.003, 2.002, 2.001, 1.001, 1.000, 0.001
};
final RealMatrix matrix = EigenDecompositionImplTest.createTestMatrix(new Random(35992629946426l), refValues);
DecompositionSolver es = new EigenDecompositionImpl(matrix, MathUtils.SAFE_MIN).getSolver();
RealMatrix b = MatrixUtils.createRealMatrix(new double[2][2]);
try {
es.solve(b);
fail("an exception should have been thrown");
Assert.fail("an exception should have been thrown");
} catch (MathIllegalArgumentException iae) {
// expected behavior
}
try {
es.solve(b.getColumn(0));
fail("an exception should have been thrown");
Assert.fail("an exception should have been thrown");
} catch (MathIllegalArgumentException iae) {
// expected behavior
}
try {
es.solve(new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(0)));
fail("an exception should have been thrown");
Assert.fail("an exception should have been thrown");
} catch (MathIllegalArgumentException iae) {
// expected behavior
}
}
/** test solve */
@Test
public void testSolve() {
RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {
{ 91, 5, 29, 32, 40, 14 },
@ -116,18 +118,18 @@ public class EigenSolverTest extends TestCase {
// using RealMatrix
RealMatrix solution=es.solve(b);
assertEquals(0, solution.subtract(xRef).getNorm(), 2.5e-12);
Assert.assertEquals(0, solution.subtract(xRef).getNorm(), 2.5e-12);
// using double[]
for (int i = 0; i < b.getColumnDimension(); ++i) {
assertEquals(0,
Assert.assertEquals(0,
new ArrayRealVector(es.solve(b.getColumn(i))).subtract(xRef.getColumnVector(i)).getNorm(),
2.0e-11);
}
// using Array2DRowRealMatrix
for (int i = 0; i < b.getColumnDimension(); ++i) {
assertEquals(0,
Assert.assertEquals(0,
es.solve(b.getColumnVector(i)).subtract(xRef.getColumnVector(i)).getNorm(),
2.0e-11);
}
@ -136,25 +138,9 @@ public class EigenSolverTest extends TestCase {
for (int i = 0; i < b.getColumnDimension(); ++i) {
ArrayRealVectorTest.RealVectorTestImpl v =
new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(i));
assertEquals(0,
Assert.assertEquals(0,
es.solve(v).subtract(xRef.getColumnVector(i)).getNorm(),
2.0e-11);
}
}
@Override
public void setUp() {
refValues = new double[] {
2.003, 2.002, 2.001, 1.001, 1.000, 0.001
};
matrix = EigenDecompositionImplTest.createTestMatrix(new Random(35992629946426l), refValues);
}
@Override
public void tearDown() {
refValues = null;
matrix = null;
}
}

View File

@ -17,14 +17,14 @@
package org.apache.commons.math.linear;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.Assert;
import org.apache.commons.math.TestUtils;
import org.apache.commons.math.fraction.Fraction;
import org.apache.commons.math.fraction.FractionField;
import org.apache.commons.math.exception.NonSquareMatrixException;
public class FieldLUDecompositionImplTest extends TestCase {
public class FieldLUDecompositionImplTest {
private Fraction[][] testData = {
{ new Fraction(1), new Fraction(2), new Fraction(3)},
{ new Fraction(2), new Fraction(5), new Fraction(3)},
@ -53,24 +53,22 @@ public class FieldLUDecompositionImplTest extends TestCase {
{ new Fraction(3), new Fraction(7), new Fraction(6), new Fraction(8) }
}; // 4th row = 1st + 2nd
public FieldLUDecompositionImplTest(String name) {
super(name);
}
/** test dimensions */
@Test
public void testDimensions() {
FieldMatrix<Fraction> matrix = new Array2DRowFieldMatrix<Fraction>(testData);
FieldLUDecomposition<Fraction> LU = new FieldLUDecompositionImpl<Fraction>(matrix);
assertEquals(testData.length, LU.getL().getRowDimension());
assertEquals(testData.length, LU.getL().getColumnDimension());
assertEquals(testData.length, LU.getU().getRowDimension());
assertEquals(testData.length, LU.getU().getColumnDimension());
assertEquals(testData.length, LU.getP().getRowDimension());
assertEquals(testData.length, LU.getP().getColumnDimension());
Assert.assertEquals(testData.length, LU.getL().getRowDimension());
Assert.assertEquals(testData.length, LU.getL().getColumnDimension());
Assert.assertEquals(testData.length, LU.getU().getRowDimension());
Assert.assertEquals(testData.length, LU.getU().getColumnDimension());
Assert.assertEquals(testData.length, LU.getP().getRowDimension());
Assert.assertEquals(testData.length, LU.getP().getColumnDimension());
}
/** test non-square matrix */
@Test
public void testNonSquare() {
try {
new FieldLUDecompositionImpl<Fraction>(new Array2DRowFieldMatrix<Fraction>(new Fraction[][] {
@ -78,13 +76,14 @@ public class FieldLUDecompositionImplTest extends TestCase {
{ Fraction.ZERO, Fraction.ZERO },
{ Fraction.ZERO, Fraction.ZERO }
}));
fail("Expected NonSquareMatrixException");
Assert.fail("Expected NonSquareMatrixException");
} catch (NonSquareMatrixException ime) {
// expected behavior
}
}
/** test PA = LU */
@Test
public void testPAEqualLU() {
FieldMatrix<Fraction> matrix = new Array2DRowFieldMatrix<Fraction>(testData);
FieldLUDecomposition<Fraction> lu = new FieldLUDecompositionImpl<Fraction>(matrix);
@ -112,44 +111,47 @@ public class FieldLUDecompositionImplTest extends TestCase {
matrix = new Array2DRowFieldMatrix<Fraction>(singular);
lu = new FieldLUDecompositionImpl<Fraction>(matrix);
assertFalse(lu.getSolver().isNonSingular());
assertNull(lu.getL());
assertNull(lu.getU());
assertNull(lu.getP());
Assert.assertFalse(lu.getSolver().isNonSingular());
Assert.assertNull(lu.getL());
Assert.assertNull(lu.getU());
Assert.assertNull(lu.getP());
matrix = new Array2DRowFieldMatrix<Fraction>(bigSingular);
lu = new FieldLUDecompositionImpl<Fraction>(matrix);
assertFalse(lu.getSolver().isNonSingular());
assertNull(lu.getL());
assertNull(lu.getU());
assertNull(lu.getP());
Assert.assertFalse(lu.getSolver().isNonSingular());
Assert.assertNull(lu.getL());
Assert.assertNull(lu.getU());
Assert.assertNull(lu.getP());
}
/** test that L is lower triangular with unit diagonal */
@Test
public void testLLowerTriangular() {
FieldMatrix<Fraction> matrix = new Array2DRowFieldMatrix<Fraction>(testData);
FieldMatrix<Fraction> l = new FieldLUDecompositionImpl<Fraction>(matrix).getL();
for (int i = 0; i < l.getRowDimension(); i++) {
assertEquals(Fraction.ONE, l.getEntry(i, i));
Assert.assertEquals(Fraction.ONE, l.getEntry(i, i));
for (int j = i + 1; j < l.getColumnDimension(); j++) {
assertEquals(Fraction.ZERO, l.getEntry(i, j));
Assert.assertEquals(Fraction.ZERO, l.getEntry(i, j));
}
}
}
/** test that U is upper triangular */
@Test
public void testUUpperTriangular() {
FieldMatrix<Fraction> matrix = new Array2DRowFieldMatrix<Fraction>(testData);
FieldMatrix<Fraction> u = new FieldLUDecompositionImpl<Fraction>(matrix).getU();
for (int i = 0; i < u.getRowDimension(); i++) {
for (int j = 0; j < i; j++) {
assertEquals(Fraction.ZERO, u.getEntry(i, j));
Assert.assertEquals(Fraction.ZERO, u.getEntry(i, j));
}
}
}
/** test that P is a permutation matrix */
@Test
public void testPPermutation() {
FieldMatrix<Fraction> matrix = new Array2DRowFieldMatrix<Fraction>(testData);
FieldMatrix<Fraction> p = new FieldLUDecompositionImpl<Fraction>(matrix).getP();
@ -177,9 +179,9 @@ public class FieldLUDecompositionImplTest extends TestCase {
++otherCount;
}
}
assertEquals(p.getColumnDimension() - 1, zeroCount);
assertEquals(1, oneCount);
assertEquals(0, otherCount);
Assert.assertEquals(p.getColumnDimension() - 1, zeroCount);
Assert.assertEquals(1, oneCount);
Assert.assertEquals(0, otherCount);
}
for (int j = 0; j < p.getColumnDimension(); j++) {
@ -196,26 +198,28 @@ public class FieldLUDecompositionImplTest extends TestCase {
++otherCount;
}
}
assertEquals(p.getRowDimension() - 1, zeroCount);
assertEquals(1, oneCount);
assertEquals(0, otherCount);
Assert.assertEquals(p.getRowDimension() - 1, zeroCount);
Assert.assertEquals(1, oneCount);
Assert.assertEquals(0, otherCount);
}
}
/** test singular */
@Test
public void testSingular() {
FieldLUDecomposition<Fraction> lu =
new FieldLUDecompositionImpl<Fraction>(new Array2DRowFieldMatrix<Fraction>(testData));
assertTrue(lu.getSolver().isNonSingular());
Assert.assertTrue(lu.getSolver().isNonSingular());
lu = new FieldLUDecompositionImpl<Fraction>(new Array2DRowFieldMatrix<Fraction>(singular));
assertFalse(lu.getSolver().isNonSingular());
Assert.assertFalse(lu.getSolver().isNonSingular());
lu = new FieldLUDecompositionImpl<Fraction>(new Array2DRowFieldMatrix<Fraction>(bigSingular));
assertFalse(lu.getSolver().isNonSingular());
Assert.assertFalse(lu.getSolver().isNonSingular());
}
/** test matrices values */
@Test
public void testMatricesValues1() {
FieldLUDecomposition<Fraction> lu =
new FieldLUDecompositionImpl<Fraction>(new Array2DRowFieldMatrix<Fraction>(testData));
@ -245,17 +249,18 @@ public class FieldLUDecompositionImplTest extends TestCase {
TestUtils.assertEquals(pRef, p);
int[] pivot = lu.getPivot();
for (int i = 0; i < pivotRef.length; ++i) {
assertEquals(pivotRef[i], pivot[i]);
Assert.assertEquals(pivotRef[i], pivot[i]);
}
// check the same cached instance is returned the second time
assertTrue(l == lu.getL());
assertTrue(u == lu.getU());
assertTrue(p == lu.getP());
Assert.assertTrue(l == lu.getL());
Assert.assertTrue(u == lu.getU());
Assert.assertTrue(p == lu.getP());
}
/** test matrices values */
@Test
public void testMatricesValues2() {
FieldLUDecomposition<Fraction> lu =
new FieldLUDecompositionImpl<Fraction>(new Array2DRowFieldMatrix<Fraction>(luData));
@ -285,14 +290,12 @@ public class FieldLUDecompositionImplTest extends TestCase {
TestUtils.assertEquals(pRef, p);
int[] pivot = lu.getPivot();
for (int i = 0; i < pivotRef.length; ++i) {
assertEquals(pivotRef[i], pivot[i]);
Assert.assertEquals(pivotRef[i], pivot[i]);
}
// check the same cached instance is returned the second time
assertTrue(l == lu.getL());
assertTrue(u == lu.getU());
assertTrue(p == lu.getP());
Assert.assertTrue(l == lu.getL());
Assert.assertTrue(u == lu.getU());
Assert.assertTrue(p == lu.getP());
}
}

View File

@ -16,7 +16,8 @@
*/
package org.apache.commons.math.linear;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.Assert;
import org.apache.commons.math.TestUtils;
import org.apache.commons.math.fraction.Fraction;
@ -24,13 +25,11 @@ import org.apache.commons.math.fraction.FractionField;
import org.apache.commons.math.exception.MathUserException;
import org.apache.commons.math.exception.MathIllegalStateException;
import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.MatrixDimensionMismatchException;
import org.apache.commons.math.exception.NoDataException;
import org.apache.commons.math.exception.OutOfRangeException;
import org.apache.commons.math.exception.NumberIsTooSmallException;
import org.apache.commons.math.exception.NotStrictlyPositiveException;
import org.apache.commons.math.exception.NullArgumentException;
import org.apache.commons.math.exception.NonSquareMatrixException;
import org.apache.commons.math.exception.MathIllegalArgumentException;
/**
@ -39,7 +38,7 @@ import org.apache.commons.math.exception.MathIllegalArgumentException;
* @version $Revision$ $Date$
*/
public final class FieldMatrixImplTest extends TestCase {
public final class FieldMatrixImplTest {
// 3 x 3 identity matrix
protected Fraction[][] id = { {new Fraction(1),new Fraction(0),new Fraction(0)}, {new Fraction(0),new Fraction(1),new Fraction(0)}, {new Fraction(0),new Fraction(0),new Fraction(1)} };
@ -101,33 +100,32 @@ public final class FieldMatrixImplTest extends TestCase {
protected double entryTolerance = 10E-16;
protected double normTolerance = 10E-14;
public FieldMatrixImplTest(String name) {
super(name);
}
/** test dimensions */
@Test
public void testDimensions() {
Array2DRowFieldMatrix<Fraction> m = new Array2DRowFieldMatrix<Fraction>(testData);
Array2DRowFieldMatrix<Fraction> m2 = new Array2DRowFieldMatrix<Fraction>(testData2);
assertEquals("testData row dimension",3,m.getRowDimension());
assertEquals("testData column dimension",3,m.getColumnDimension());
assertTrue("testData is square",m.isSquare());
assertEquals("testData2 row dimension",m2.getRowDimension(),2);
assertEquals("testData2 column dimension",m2.getColumnDimension(),3);
assertTrue("testData2 is not square",!m2.isSquare());
Assert.assertEquals("testData row dimension",3,m.getRowDimension());
Assert.assertEquals("testData column dimension",3,m.getColumnDimension());
Assert.assertTrue("testData is square",m.isSquare());
Assert.assertEquals("testData2 row dimension",m2.getRowDimension(),2);
Assert.assertEquals("testData2 column dimension",m2.getColumnDimension(),3);
Assert.assertTrue("testData2 is not square",!m2.isSquare());
}
/** test copy functions */
@Test
public void testCopyFunctions() {
Array2DRowFieldMatrix<Fraction> m1 = new Array2DRowFieldMatrix<Fraction>(testData);
Array2DRowFieldMatrix<Fraction> m2 = new Array2DRowFieldMatrix<Fraction>(m1.getData());
assertEquals(m2,m1);
Assert.assertEquals(m2,m1);
Array2DRowFieldMatrix<Fraction> m3 = new Array2DRowFieldMatrix<Fraction>(testData);
Array2DRowFieldMatrix<Fraction> m4 = new Array2DRowFieldMatrix<Fraction>(m3.getData(), false);
assertEquals(m4,m3);
Assert.assertEquals(m4,m3);
}
/** test add */
@Test
public void testAdd() {
Array2DRowFieldMatrix<Fraction> m = new Array2DRowFieldMatrix<Fraction>(testData);
Array2DRowFieldMatrix<Fraction> mInv = new Array2DRowFieldMatrix<Fraction>(testDataInv);
@ -135,37 +133,40 @@ public final class FieldMatrixImplTest extends TestCase {
Fraction[][] sumEntries = mPlusMInv.getData();
for (int row = 0; row < m.getRowDimension(); row++) {
for (int col = 0; col < m.getColumnDimension(); col++) {
assertEquals(testDataPlusInv[row][col],sumEntries[row][col]);
Assert.assertEquals(testDataPlusInv[row][col],sumEntries[row][col]);
}
}
}
/** test add failure */
@Test
public void testAddFail() {
Array2DRowFieldMatrix<Fraction> m = new Array2DRowFieldMatrix<Fraction>(testData);
Array2DRowFieldMatrix<Fraction> m2 = new Array2DRowFieldMatrix<Fraction>(testData2);
try {
m.add(m2);
fail("MathIllegalArgumentException expected");
Assert.fail("MathIllegalArgumentException expected");
} catch (MathIllegalArgumentException ex) {
// ignored
}
}
/** test m-n = m + -n */
@Test
public void testPlusMinus() {
Array2DRowFieldMatrix<Fraction> m = new Array2DRowFieldMatrix<Fraction>(testData);
Array2DRowFieldMatrix<Fraction> m2 = new Array2DRowFieldMatrix<Fraction>(testDataInv);
TestUtils.assertEquals(m.subtract(m2),m2.scalarMultiply(new Fraction(-1)).add(m));
try {
m.subtract(new Array2DRowFieldMatrix<Fraction>(testData2));
fail("Expecting illegalArgumentException");
Assert.fail("Expecting illegalArgumentException");
} catch (MathIllegalArgumentException ex) {
// ignored
}
}
/** test multiply */
@Test
public void testMultiply() {
Array2DRowFieldMatrix<Fraction> m = new Array2DRowFieldMatrix<Fraction>(testData);
Array2DRowFieldMatrix<Fraction> mInv = new Array2DRowFieldMatrix<Fraction>(testDataInv);
@ -178,7 +179,7 @@ public final class FieldMatrixImplTest extends TestCase {
TestUtils.assertEquals(m2.multiply(identity), m2);
try {
m.multiply(new Array2DRowFieldMatrix<Fraction>(bigSingular));
fail("Expecting illegalArgumentException");
Assert.fail("Expecting illegalArgumentException");
} catch (MathIllegalArgumentException ex) {
// ignored
}
@ -190,6 +191,7 @@ public final class FieldMatrixImplTest extends TestCase {
private Fraction[][] d4 = new Fraction[][] {{new Fraction(1)},{new Fraction(2)},{new Fraction(3)},{new Fraction(4)}};
private Fraction[][] d5 = new Fraction[][] {{new Fraction(30)},{new Fraction(70)}};
@Test
public void testMultiply2() {
FieldMatrix<Fraction> m3 = new Array2DRowFieldMatrix<Fraction>(d3);
FieldMatrix<Fraction> m4 = new Array2DRowFieldMatrix<Fraction>(d4);
@ -198,25 +200,28 @@ public final class FieldMatrixImplTest extends TestCase {
}
/** test trace */
@Test
public void testTrace() {
FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<Fraction>(id);
assertEquals("identity trace",new Fraction(3),m.getTrace());
Assert.assertEquals("identity trace",new Fraction(3),m.getTrace());
m = new Array2DRowFieldMatrix<Fraction>(testData2);
try {
m.getTrace();
fail("Expecting NonSquareMatrixException");
Assert.fail("Expecting NonSquareMatrixException");
} catch (NonSquareMatrixException ex) {
// ignored
}
}
/** test sclarAdd */
@Test
public void testScalarAdd() {
FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<Fraction>(testData);
TestUtils.assertEquals(new Array2DRowFieldMatrix<Fraction>(testDataPlus2), m.scalarAdd(new Fraction(2)));
}
/** test operate */
@Test
public void testOperate() {
FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<Fraction>(id);
TestUtils.assertEquals(testVector, m.operate(testVector));
@ -224,25 +229,27 @@ public final class FieldMatrixImplTest extends TestCase {
m = new Array2DRowFieldMatrix<Fraction>(bigSingular);
try {
m.operate(testVector);
fail("Expecting illegalArgumentException");
Assert.fail("Expecting illegalArgumentException");
} catch (MathIllegalArgumentException ex) {
// ignored
}
}
/** test issue MATH-209 */
@Test
public void testMath209() {
FieldMatrix<Fraction> a = new Array2DRowFieldMatrix<Fraction>(new Fraction[][] {
{ new Fraction(1), new Fraction(2) }, { new Fraction(3), new Fraction(4) }, { new Fraction(5), new Fraction(6) }
}, false);
Fraction[] b = a.operate(new Fraction[] { new Fraction(1), new Fraction(1) });
assertEquals(a.getRowDimension(), b.length);
assertEquals( new Fraction(3), b[0]);
assertEquals( new Fraction(7), b[1]);
assertEquals(new Fraction(11), b[2]);
Assert.assertEquals(a.getRowDimension(), b.length);
Assert.assertEquals( new Fraction(3), b[0]);
Assert.assertEquals( new Fraction(7), b[1]);
Assert.assertEquals(new Fraction(11), b[2]);
}
/** test transpose */
@Test
public void testTranspose() {
FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<Fraction>(testData);
FieldMatrix<Fraction> mIT = new FieldLUDecompositionImpl<Fraction>(m).getSolver().getInverse().transpose();
@ -254,6 +261,7 @@ public final class FieldMatrixImplTest extends TestCase {
}
/** test preMultiply by vector */
@Test
public void testPremultiplyVector() {
FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<Fraction>(testData);
TestUtils.assertEquals(m.preMultiply(testVector), preMultTest);
@ -262,12 +270,13 @@ public final class FieldMatrixImplTest extends TestCase {
m = new Array2DRowFieldMatrix<Fraction>(bigSingular);
try {
m.preMultiply(testVector);
fail("expecting MathIllegalArgumentException");
Assert.fail("expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException ex) {
// ignored
}
}
@Test
public void testPremultiply() {
FieldMatrix<Fraction> m3 = new Array2DRowFieldMatrix<Fraction>(d3);
FieldMatrix<Fraction> m4 = new Array2DRowFieldMatrix<Fraction>(d4);
@ -283,42 +292,45 @@ public final class FieldMatrixImplTest extends TestCase {
TestUtils.assertEquals(identity.preMultiply(mInv), mInv);
try {
m.preMultiply(new Array2DRowFieldMatrix<Fraction>(bigSingular));
fail("Expecting illegalArgumentException");
Assert.fail("Expecting illegalArgumentException");
} catch (MathIllegalArgumentException ex) {
// ignored
}
}
@Test
public void testGetVectors() {
FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<Fraction>(testData);
TestUtils.assertEquals(m.getRow(0), testDataRow1);
TestUtils.assertEquals(m.getColumn(2), testDataCol3);
try {
m.getRow(10);
fail("expecting OutOfRangeException");
Assert.fail("expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// ignored
}
try {
m.getColumn(-1);
fail("expecting OutOfRangeException");
Assert.fail("expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// ignored
}
}
@Test
public void testGetEntry() {
FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<Fraction>(testData);
assertEquals("get entry", m.getEntry(0,1), new Fraction(2));
Assert.assertEquals("get entry", m.getEntry(0,1), new Fraction(2));
try {
m.getEntry(10, 4);
fail ("Expecting OutOfRangeException");
Assert.fail ("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
}
/** test examples in user guide */
@Test
public void testExamples() {
// Create a real matrix with two rows and three columns
Fraction[][] matrixData = {
@ -335,12 +347,12 @@ public final class FieldMatrixImplTest extends TestCase {
FieldMatrix<Fraction> n = new Array2DRowFieldMatrix<Fraction>(matrixData2);
// Now multiply m by n
FieldMatrix<Fraction> p = m.multiply(n);
assertEquals(2, p.getRowDimension());
assertEquals(2, p.getColumnDimension());
Assert.assertEquals(2, p.getRowDimension());
Assert.assertEquals(2, p.getColumnDimension());
// Invert p
FieldMatrix<Fraction> pInverse = new FieldLUDecompositionImpl<Fraction>(p).getSolver().getInverse();
assertEquals(2, pInverse.getRowDimension());
assertEquals(2, pInverse.getColumnDimension());
Assert.assertEquals(2, pInverse.getRowDimension());
Assert.assertEquals(2, pInverse.getColumnDimension());
// Solve example
Fraction[][] coefficientsData = {
@ -351,19 +363,20 @@ public final class FieldMatrixImplTest extends TestCase {
FieldMatrix<Fraction> coefficients = new Array2DRowFieldMatrix<Fraction>(coefficientsData);
Fraction[] constants = {new Fraction(1), new Fraction(-2), new Fraction(1)};
Fraction[] solution = new FieldLUDecompositionImpl<Fraction>(coefficients).getSolver().solve(constants);
assertEquals(new Fraction(2).multiply(solution[0]).
Assert.assertEquals(new Fraction(2).multiply(solution[0]).
add(new Fraction(3).multiply(solution[1])).
subtract(new Fraction(2).multiply(solution[2])), constants[0]);
assertEquals(new Fraction(-1).multiply(solution[0]).
Assert.assertEquals(new Fraction(-1).multiply(solution[0]).
add(new Fraction(7).multiply(solution[1])).
add(new Fraction(6).multiply(solution[2])), constants[1]);
assertEquals(new Fraction(4).multiply(solution[0]).
Assert.assertEquals(new Fraction(4).multiply(solution[0]).
subtract(new Fraction(3).multiply(solution[1])).
subtract(new Fraction(5).multiply(solution[2])), constants[2]);
}
// test submatrix accessors
@Test
public void testGetSubMatrix() {
FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<Fraction>(subTestData);
checkGetSubMatrix(m, subRows23Cols00, 2 , 3 , 0, 0);
@ -388,9 +401,9 @@ public final class FieldMatrixImplTest extends TestCase {
try {
FieldMatrix<Fraction> sub = m.getSubMatrix(startRow, endRow, startColumn, endColumn);
if (reference != null) {
assertEquals(new Array2DRowFieldMatrix<Fraction>(reference), sub);
Assert.assertEquals(new Array2DRowFieldMatrix<Fraction>(reference), sub);
} else {
fail("Expecting OutOfRangeException or NotStrictlyPositiveException"
Assert.fail("Expecting OutOfRangeException or NotStrictlyPositiveException"
+ " or NumberIsTooSmallException or NoDataException");
}
} catch (OutOfRangeException e) {
@ -417,9 +430,9 @@ public final class FieldMatrixImplTest extends TestCase {
try {
FieldMatrix<Fraction> sub = m.getSubMatrix(selectedRows, selectedColumns);
if (reference != null) {
assertEquals(new Array2DRowFieldMatrix<Fraction>(reference), sub);
Assert.assertEquals(new Array2DRowFieldMatrix<Fraction>(reference), sub);
} else {
fail("Expecting OutOfRangeException or NotStrictlyPositiveException"
Assert.fail("Expecting OutOfRangeException or NotStrictlyPositiveException"
+ " or NumberIsTooSmallException or NoDataException");
}
} catch (OutOfRangeException e) {
@ -441,6 +454,7 @@ public final class FieldMatrixImplTest extends TestCase {
}
}
@Test
public void testCopySubMatrix() {
FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<Fraction>(subTestData);
checkCopy(m, subRows23Cols00, 2 , 3 , 0, 0);
@ -469,9 +483,9 @@ public final class FieldMatrixImplTest extends TestCase {
new Fraction[reference.length][reference[0].length];
m.copySubMatrix(startRow, endRow, startColumn, endColumn, sub);
if (reference != null) {
assertEquals(new Array2DRowFieldMatrix<Fraction>(reference), new Array2DRowFieldMatrix<Fraction>(sub));
Assert.assertEquals(new Array2DRowFieldMatrix<Fraction>(reference), new Array2DRowFieldMatrix<Fraction>(sub));
} else {
fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException");
Assert.fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException");
}
} catch (OutOfRangeException e) {
if (reference != null) {
@ -496,9 +510,9 @@ public final class FieldMatrixImplTest extends TestCase {
new Fraction[reference.length][reference[0].length];
m.copySubMatrix(selectedRows, selectedColumns, sub);
if (reference != null) {
assertEquals(new Array2DRowFieldMatrix<Fraction>(reference), new Array2DRowFieldMatrix<Fraction>(sub));
Assert.assertEquals(new Array2DRowFieldMatrix<Fraction>(reference), new Array2DRowFieldMatrix<Fraction>(sub));
} else {
fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException");
Assert.fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException");
}
} catch (OutOfRangeException e) {
if (reference != null) {
@ -515,165 +529,173 @@ public final class FieldMatrixImplTest extends TestCase {
}
}
@Test
public void testGetRowMatrix() {
FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<Fraction>(subTestData);
FieldMatrix<Fraction> mRow0 = new Array2DRowFieldMatrix<Fraction>(subRow0);
FieldMatrix<Fraction> mRow3 = new Array2DRowFieldMatrix<Fraction>(subRow3);
assertEquals("Row0", mRow0,
Assert.assertEquals("Row0", mRow0,
m.getRowMatrix(0));
assertEquals("Row3", mRow3,
Assert.assertEquals("Row3", mRow3,
m.getRowMatrix(3));
try {
m.getRowMatrix(-1);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.getRowMatrix(4);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
}
@Test
public void testSetRowMatrix() {
FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<Fraction>(subTestData);
FieldMatrix<Fraction> mRow3 = new Array2DRowFieldMatrix<Fraction>(subRow3);
assertNotSame(mRow3, m.getRowMatrix(0));
Assert.assertNotSame(mRow3, m.getRowMatrix(0));
m.setRowMatrix(0, mRow3);
assertEquals(mRow3, m.getRowMatrix(0));
Assert.assertEquals(mRow3, m.getRowMatrix(0));
try {
m.setRowMatrix(-1, mRow3);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.setRowMatrix(0, m);
fail("Expecting MatrixDimensionMismatchException");
Assert.fail("Expecting MatrixDimensionMismatchException");
} catch (MatrixDimensionMismatchException ex) {
// expected
}
}
@Test
public void testGetColumnMatrix() {
FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<Fraction>(subTestData);
FieldMatrix<Fraction> mColumn1 = new Array2DRowFieldMatrix<Fraction>(subColumn1);
FieldMatrix<Fraction> mColumn3 = new Array2DRowFieldMatrix<Fraction>(subColumn3);
assertEquals("Column1", mColumn1,
Assert.assertEquals("Column1", mColumn1,
m.getColumnMatrix(1));
assertEquals("Column3", mColumn3,
Assert.assertEquals("Column3", mColumn3,
m.getColumnMatrix(3));
try {
m.getColumnMatrix(-1);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.getColumnMatrix(4);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
}
@Test
public void testSetColumnMatrix() {
FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<Fraction>(subTestData);
FieldMatrix<Fraction> mColumn3 = new Array2DRowFieldMatrix<Fraction>(subColumn3);
assertNotSame(mColumn3, m.getColumnMatrix(1));
Assert.assertNotSame(mColumn3, m.getColumnMatrix(1));
m.setColumnMatrix(1, mColumn3);
assertEquals(mColumn3, m.getColumnMatrix(1));
Assert.assertEquals(mColumn3, m.getColumnMatrix(1));
try {
m.setColumnMatrix(-1, mColumn3);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.setColumnMatrix(0, m);
fail("Expecting MatrixDimensionMismatchException");
Assert.fail("Expecting MatrixDimensionMismatchException");
} catch (MatrixDimensionMismatchException ex) {
// expected
}
}
@Test
public void testGetRowVector() {
FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<Fraction>(subTestData);
FieldVector<Fraction> mRow0 = new ArrayFieldVector<Fraction>(subRow0[0]);
FieldVector<Fraction> mRow3 = new ArrayFieldVector<Fraction>(subRow3[0]);
assertEquals("Row0", mRow0, m.getRowVector(0));
assertEquals("Row3", mRow3, m.getRowVector(3));
Assert.assertEquals("Row0", mRow0, m.getRowVector(0));
Assert.assertEquals("Row3", mRow3, m.getRowVector(3));
try {
m.getRowVector(-1);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.getRowVector(4);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
}
@Test
public void testSetRowVector() {
FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<Fraction>(subTestData);
FieldVector<Fraction> mRow3 = new ArrayFieldVector<Fraction>(subRow3[0]);
assertNotSame(mRow3, m.getRowMatrix(0));
Assert.assertNotSame(mRow3, m.getRowMatrix(0));
m.setRowVector(0, mRow3);
assertEquals(mRow3, m.getRowVector(0));
Assert.assertEquals(mRow3, m.getRowVector(0));
try {
m.setRowVector(-1, mRow3);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.setRowVector(0, new ArrayFieldVector<Fraction>(FractionField.getInstance(), 5));
fail("Expecting MatrixDimensionMismatchException");
Assert.fail("Expecting MatrixDimensionMismatchException");
} catch (MatrixDimensionMismatchException ex) {
// expected
}
}
@Test
public void testGetColumnVector() {
FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<Fraction>(subTestData);
FieldVector<Fraction> mColumn1 = columnToVector(subColumn1);
FieldVector<Fraction> mColumn3 = columnToVector(subColumn3);
assertEquals("Column1", mColumn1, m.getColumnVector(1));
assertEquals("Column3", mColumn3, m.getColumnVector(3));
Assert.assertEquals("Column1", mColumn1, m.getColumnVector(1));
Assert.assertEquals("Column3", mColumn3, m.getColumnVector(3));
try {
m.getColumnVector(-1);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.getColumnVector(4);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
}
@Test
public void testSetColumnVector() {
FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<Fraction>(subTestData);
FieldVector<Fraction> mColumn3 = columnToVector(subColumn3);
assertNotSame(mColumn3, m.getColumnVector(1));
Assert.assertNotSame(mColumn3, m.getColumnVector(1));
m.setColumnVector(1, mColumn3);
assertEquals(mColumn3, m.getColumnVector(1));
Assert.assertEquals(mColumn3, m.getColumnVector(1));
try {
m.setColumnVector(-1, mColumn3);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.setColumnVector(0, new ArrayFieldVector<Fraction>(FractionField.getInstance(), 5));
fail("Expecting MatrixDimensionMismatchException");
Assert.fail("Expecting MatrixDimensionMismatchException");
} catch (MatrixDimensionMismatchException ex) {
// expected
}
@ -687,43 +709,46 @@ public final class FieldMatrixImplTest extends TestCase {
return new ArrayFieldVector<Fraction>(data, false);
}
@Test
public void testGetRow() {
FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<Fraction>(subTestData);
checkArrays(subRow0[0], m.getRow(0));
checkArrays(subRow3[0], m.getRow(3));
try {
m.getRow(-1);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.getRow(4);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
}
@Test
public void testSetRow() {
FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<Fraction>(subTestData);
assertTrue(subRow3[0][0] != m.getRow(0)[0]);
Assert.assertTrue(subRow3[0][0] != m.getRow(0)[0]);
m.setRow(0, subRow3[0]);
checkArrays(subRow3[0], m.getRow(0));
try {
m.setRow(-1, subRow3[0]);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.setRow(0, new Fraction[5]);
fail("Expecting MatrixDimensionMismatchException");
Assert.fail("Expecting MatrixDimensionMismatchException");
} catch (MatrixDimensionMismatchException ex) {
// expected
}
}
@Test
public void testGetColumn() {
FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<Fraction>(subTestData);
Fraction[] mColumn1 = columnToArray(subColumn1);
@ -732,33 +757,34 @@ public final class FieldMatrixImplTest extends TestCase {
checkArrays(mColumn3, m.getColumn(3));
try {
m.getColumn(-1);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.getColumn(4);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
}
@Test
public void testSetColumn() {
FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<Fraction>(subTestData);
Fraction[] mColumn3 = columnToArray(subColumn3);
assertTrue(mColumn3[0] != m.getColumn(1)[0]);
Assert.assertTrue(mColumn3[0] != m.getColumn(1)[0]);
m.setColumn(1, mColumn3);
checkArrays(mColumn3, m.getColumn(1));
try {
m.setColumn(-1, mColumn3);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.setColumn(0, new Fraction[5]);
fail("Expecting MatrixDimensionMismatchException");
Assert.fail("Expecting MatrixDimensionMismatchException");
} catch (MatrixDimensionMismatchException ex) {
// expected
}
@ -773,32 +799,35 @@ public final class FieldMatrixImplTest extends TestCase {
}
private void checkArrays(Fraction[] expected, Fraction[] actual) {
assertEquals(expected.length, actual.length);
Assert.assertEquals(expected.length, actual.length);
for (int i = 0; i < expected.length; ++i) {
assertEquals(expected[i], actual[i]);
Assert.assertEquals(expected[i], actual[i]);
}
}
@Test
public void testEqualsAndHashCode() {
Array2DRowFieldMatrix<Fraction> m = new Array2DRowFieldMatrix<Fraction>(testData);
Array2DRowFieldMatrix<Fraction> m1 = (Array2DRowFieldMatrix<Fraction>) m.copy();
Array2DRowFieldMatrix<Fraction> mt = (Array2DRowFieldMatrix<Fraction>) m.transpose();
assertTrue(m.hashCode() != mt.hashCode());
assertEquals(m.hashCode(), m1.hashCode());
assertEquals(m, m);
assertEquals(m, m1);
assertFalse(m.equals(null));
assertFalse(m.equals(mt));
assertFalse(m.equals(new Array2DRowFieldMatrix<Fraction>(bigSingular)));
Assert.assertTrue(m.hashCode() != mt.hashCode());
Assert.assertEquals(m.hashCode(), m1.hashCode());
Assert.assertEquals(m, m);
Assert.assertEquals(m, m1);
Assert.assertFalse(m.equals(null));
Assert.assertFalse(m.equals(mt));
Assert.assertFalse(m.equals(new Array2DRowFieldMatrix<Fraction>(bigSingular)));
}
@Test
public void testToString() {
Array2DRowFieldMatrix<Fraction> m = new Array2DRowFieldMatrix<Fraction>(testData);
assertEquals("Array2DRowFieldMatrix{{1,2,3},{2,5,3},{1,0,8}}", m.toString());
Assert.assertEquals("Array2DRowFieldMatrix{{1,2,3},{2,5,3},{1,0,8}}", m.toString());
m = new Array2DRowFieldMatrix<Fraction>(FractionField.getInstance());
assertEquals("Array2DRowFieldMatrix{}", m.toString());
Assert.assertEquals("Array2DRowFieldMatrix{}", m.toString());
}
@Test
public void testSetSubMatrix() throws Exception {
Array2DRowFieldMatrix<Fraction> m = new Array2DRowFieldMatrix<Fraction>(testData);
m.setSubMatrix(detData2,1,1);
@ -808,7 +837,7 @@ public final class FieldMatrixImplTest extends TestCase {
{new Fraction(2),new Fraction(1),new Fraction(3)},
{new Fraction(1),new Fraction(2),new Fraction(4)}
});
assertEquals(expected, m);
Assert.assertEquals(expected, m);
m.setSubMatrix(detData2,0,0);
expected = new Array2DRowFieldMatrix<Fraction>
@ -817,7 +846,7 @@ public final class FieldMatrixImplTest extends TestCase {
{new Fraction(2),new Fraction(4),new Fraction(3)},
{new Fraction(1),new Fraction(2),new Fraction(4)}
});
assertEquals(expected, m);
Assert.assertEquals(expected, m);
m.setSubMatrix(testDataPlus2,0,0);
expected = new Array2DRowFieldMatrix<Fraction>
@ -826,25 +855,25 @@ public final class FieldMatrixImplTest extends TestCase {
{new Fraction(4),new Fraction(7),new Fraction(5)},
{new Fraction(3),new Fraction(2),new Fraction(10)}
});
assertEquals(expected, m);
Assert.assertEquals(expected, m);
// dimension overflow
try {
m.setSubMatrix(testData,1,1);
fail("expecting OutOfRangeException");
Assert.fail("expecting OutOfRangeException");
} catch (OutOfRangeException e) {
// expected
}
// dimension underflow
try {
m.setSubMatrix(testData,-1,1);
fail("expecting OutOfRangeException");
Assert.fail("expecting OutOfRangeException");
} catch (OutOfRangeException e) {
// expected
}
try {
m.setSubMatrix(testData,1,-1);
fail("expecting OutOfRangeException");
Assert.fail("expecting OutOfRangeException");
} catch (OutOfRangeException e) {
// expected
}
@ -852,20 +881,20 @@ public final class FieldMatrixImplTest extends TestCase {
// null
try {
m.setSubMatrix(null, 1, 1);
fail("expecting NullArgumentException");
Assert.fail("expecting NullArgumentException");
} catch (NullArgumentException e) {
// expected
}
Array2DRowFieldMatrix<Fraction> m2 = new Array2DRowFieldMatrix<Fraction>(FractionField.getInstance());
try {
m2.setSubMatrix(testData,0,1);
fail("expecting MathIllegalStateException");
Assert.fail("expecting MathIllegalStateException");
} catch (MathIllegalStateException e) {
// expected
}
try {
m2.setSubMatrix(testData,1,0);
fail("expecting MathIllegalStateException");
Assert.fail("expecting MathIllegalStateException");
} catch (MathIllegalStateException e) {
// expected
}
@ -873,7 +902,7 @@ public final class FieldMatrixImplTest extends TestCase {
// ragged
try {
m.setSubMatrix(new Fraction[][] {{new Fraction(1)}, {new Fraction(2), new Fraction(3)}}, 0, 0);
fail("expecting MathIllegalArgumentException");
Assert.fail("expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException e) {
// expected
}
@ -881,13 +910,14 @@ public final class FieldMatrixImplTest extends TestCase {
// empty
try {
m.setSubMatrix(new Fraction[][] {{}}, 0, 0);
fail("expecting MathIllegalArgumentException");
Assert.fail("expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException e) {
// expected
}
}
@Test
public void testWalk() throws MathUserException {
int rows = 150;
int columns = 75;
@ -897,87 +927,87 @@ public final class FieldMatrixImplTest extends TestCase {
m.walkInRowOrder(new SetVisitor());
GetVisitor getVisitor = new GetVisitor();
m.walkInOptimizedOrder(getVisitor);
assertEquals(rows * columns, getVisitor.getCount());
Assert.assertEquals(rows * columns, getVisitor.getCount());
m = new Array2DRowFieldMatrix<Fraction>(FractionField.getInstance(), rows, columns);
m.walkInRowOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
getVisitor = new GetVisitor();
m.walkInOptimizedOrder(getVisitor, 1, rows - 2, 1, columns - 2);
assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
for (int i = 0; i < rows; ++i) {
assertEquals(new Fraction(0), m.getEntry(i, 0));
assertEquals(new Fraction(0), m.getEntry(i, columns - 1));
Assert.assertEquals(new Fraction(0), m.getEntry(i, 0));
Assert.assertEquals(new Fraction(0), m.getEntry(i, columns - 1));
}
for (int j = 0; j < columns; ++j) {
assertEquals(new Fraction(0), m.getEntry(0, j));
assertEquals(new Fraction(0), m.getEntry(rows - 1, j));
Assert.assertEquals(new Fraction(0), m.getEntry(0, j));
Assert.assertEquals(new Fraction(0), m.getEntry(rows - 1, j));
}
m = new Array2DRowFieldMatrix<Fraction>(FractionField.getInstance(), rows, columns);
m.walkInColumnOrder(new SetVisitor());
getVisitor = new GetVisitor();
m.walkInOptimizedOrder(getVisitor);
assertEquals(rows * columns, getVisitor.getCount());
Assert.assertEquals(rows * columns, getVisitor.getCount());
m = new Array2DRowFieldMatrix<Fraction>(FractionField.getInstance(), rows, columns);
m.walkInColumnOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
getVisitor = new GetVisitor();
m.walkInOptimizedOrder(getVisitor, 1, rows - 2, 1, columns - 2);
assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
for (int i = 0; i < rows; ++i) {
assertEquals(new Fraction(0), m.getEntry(i, 0));
assertEquals(new Fraction(0), m.getEntry(i, columns - 1));
Assert.assertEquals(new Fraction(0), m.getEntry(i, 0));
Assert.assertEquals(new Fraction(0), m.getEntry(i, columns - 1));
}
for (int j = 0; j < columns; ++j) {
assertEquals(new Fraction(0), m.getEntry(0, j));
assertEquals(new Fraction(0), m.getEntry(rows - 1, j));
Assert.assertEquals(new Fraction(0), m.getEntry(0, j));
Assert.assertEquals(new Fraction(0), m.getEntry(rows - 1, j));
}
m = new Array2DRowFieldMatrix<Fraction>(FractionField.getInstance(), rows, columns);
m.walkInOptimizedOrder(new SetVisitor());
getVisitor = new GetVisitor();
m.walkInRowOrder(getVisitor);
assertEquals(rows * columns, getVisitor.getCount());
Assert.assertEquals(rows * columns, getVisitor.getCount());
m = new Array2DRowFieldMatrix<Fraction>(FractionField.getInstance(), rows, columns);
m.walkInOptimizedOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
getVisitor = new GetVisitor();
m.walkInRowOrder(getVisitor, 1, rows - 2, 1, columns - 2);
assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
for (int i = 0; i < rows; ++i) {
assertEquals(new Fraction(0), m.getEntry(i, 0));
assertEquals(new Fraction(0), m.getEntry(i, columns - 1));
Assert.assertEquals(new Fraction(0), m.getEntry(i, 0));
Assert.assertEquals(new Fraction(0), m.getEntry(i, columns - 1));
}
for (int j = 0; j < columns; ++j) {
assertEquals(new Fraction(0), m.getEntry(0, j));
assertEquals(new Fraction(0), m.getEntry(rows - 1, j));
Assert.assertEquals(new Fraction(0), m.getEntry(0, j));
Assert.assertEquals(new Fraction(0), m.getEntry(rows - 1, j));
}
m = new Array2DRowFieldMatrix<Fraction>(FractionField.getInstance(), rows, columns);
m.walkInOptimizedOrder(new SetVisitor());
getVisitor = new GetVisitor();
m.walkInColumnOrder(getVisitor);
assertEquals(rows * columns, getVisitor.getCount());
Assert.assertEquals(rows * columns, getVisitor.getCount());
m = new Array2DRowFieldMatrix<Fraction>(FractionField.getInstance(), rows, columns);
m.walkInOptimizedOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
getVisitor = new GetVisitor();
m.walkInColumnOrder(getVisitor, 1, rows - 2, 1, columns - 2);
assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
for (int i = 0; i < rows; ++i) {
assertEquals(new Fraction(0), m.getEntry(i, 0));
assertEquals(new Fraction(0), m.getEntry(i, columns - 1));
Assert.assertEquals(new Fraction(0), m.getEntry(i, 0));
Assert.assertEquals(new Fraction(0), m.getEntry(i, columns - 1));
}
for (int j = 0; j < columns; ++j) {
assertEquals(new Fraction(0), m.getEntry(0, j));
assertEquals(new Fraction(0), m.getEntry(rows - 1, j));
Assert.assertEquals(new Fraction(0), m.getEntry(0, j));
Assert.assertEquals(new Fraction(0), m.getEntry(rows - 1, j));
}
}
@Test
public void testSerial() {
Array2DRowFieldMatrix<Fraction> m = new Array2DRowFieldMatrix<Fraction>(testData);
assertEquals(m,TestUtils.serializeAndRecover(m));
Assert.assertEquals(m,TestUtils.serializeAndRecover(m));
}
private static class SetVisitor extends DefaultFieldMatrixChangingVisitor<Fraction> {
@ -999,7 +1029,7 @@ public final class FieldMatrixImplTest extends TestCase {
@Override
public void visit(int i, int j, Fraction value) {
++count;
assertEquals(new Fraction(i * 1024 + j, 1024), value);
Assert.assertEquals(new Fraction(i * 1024 + j, 1024), value);
}
public int getCount() {
return count;

View File

@ -17,10 +17,10 @@
package org.apache.commons.math.linear;
import org.apache.commons.math.exception.NonSquareMatrixException;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.Assert;
public class LUDecompositionImplTest extends TestCase {
public class LUDecompositionImplTest {
private double[][] testData = {
{ 1.0, 2.0, 3.0},
{ 2.0, 5.0, 3.0},
@ -53,34 +53,33 @@ public class LUDecompositionImplTest extends TestCase {
private static final double normTolerance = 10e-14;
public LUDecompositionImplTest(String name) {
super(name);
}
/** test dimensions */
@Test
public void testDimensions() {
RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
LUDecomposition LU = new LUDecompositionImpl(matrix);
assertEquals(testData.length, LU.getL().getRowDimension());
assertEquals(testData.length, LU.getL().getColumnDimension());
assertEquals(testData.length, LU.getU().getRowDimension());
assertEquals(testData.length, LU.getU().getColumnDimension());
assertEquals(testData.length, LU.getP().getRowDimension());
assertEquals(testData.length, LU.getP().getColumnDimension());
Assert.assertEquals(testData.length, LU.getL().getRowDimension());
Assert.assertEquals(testData.length, LU.getL().getColumnDimension());
Assert.assertEquals(testData.length, LU.getU().getRowDimension());
Assert.assertEquals(testData.length, LU.getU().getColumnDimension());
Assert.assertEquals(testData.length, LU.getP().getRowDimension());
Assert.assertEquals(testData.length, LU.getP().getColumnDimension());
}
/** test non-square matrix */
@Test
public void testNonSquare() {
try {
new LUDecompositionImpl(MatrixUtils.createRealMatrix(new double[3][2]));
fail("Expecting NonSquareMatrixException");
Assert.fail("Expecting NonSquareMatrixException");
} catch (NonSquareMatrixException ime) {
// expected behavior
}
}
/** test PA = LU */
@Test
public void testPAEqualLU() {
RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
LUDecomposition lu = new LUDecompositionImpl(matrix);
@ -88,7 +87,7 @@ public class LUDecompositionImplTest extends TestCase {
RealMatrix u = lu.getU();
RealMatrix p = lu.getP();
double norm = l.multiply(u).subtract(p.multiply(matrix)).getNorm();
assertEquals(0, norm, normTolerance);
Assert.assertEquals(0, norm, normTolerance);
matrix = MatrixUtils.createRealMatrix(testDataMinus);
lu = new LUDecompositionImpl(matrix);
@ -96,7 +95,7 @@ public class LUDecompositionImplTest extends TestCase {
u = lu.getU();
p = lu.getP();
norm = l.multiply(u).subtract(p.multiply(matrix)).getNorm();
assertEquals(0, norm, normTolerance);
Assert.assertEquals(0, norm, normTolerance);
matrix = MatrixUtils.createRealIdentityMatrix(17);
lu = new LUDecompositionImpl(matrix);
@ -104,55 +103,58 @@ public class LUDecompositionImplTest extends TestCase {
u = lu.getU();
p = lu.getP();
norm = l.multiply(u).subtract(p.multiply(matrix)).getNorm();
assertEquals(0, norm, normTolerance);
Assert.assertEquals(0, norm, normTolerance);
matrix = MatrixUtils.createRealMatrix(singular);
lu = new LUDecompositionImpl(matrix);
assertFalse(lu.getSolver().isNonSingular());
assertNull(lu.getL());
assertNull(lu.getU());
assertNull(lu.getP());
Assert.assertFalse(lu.getSolver().isNonSingular());
Assert.assertNull(lu.getL());
Assert.assertNull(lu.getU());
Assert.assertNull(lu.getP());
matrix = MatrixUtils.createRealMatrix(bigSingular);
lu = new LUDecompositionImpl(matrix);
assertFalse(lu.getSolver().isNonSingular());
assertNull(lu.getL());
assertNull(lu.getU());
assertNull(lu.getP());
Assert.assertFalse(lu.getSolver().isNonSingular());
Assert.assertNull(lu.getL());
Assert.assertNull(lu.getU());
Assert.assertNull(lu.getP());
}
/** test that L is lower triangular with unit diagonal */
@Test
public void testLLowerTriangular() {
RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
RealMatrix l = new LUDecompositionImpl(matrix).getL();
for (int i = 0; i < l.getRowDimension(); i++) {
assertEquals(l.getEntry(i, i), 1, entryTolerance);
Assert.assertEquals(l.getEntry(i, i), 1, entryTolerance);
for (int j = i + 1; j < l.getColumnDimension(); j++) {
assertEquals(l.getEntry(i, j), 0, entryTolerance);
Assert.assertEquals(l.getEntry(i, j), 0, entryTolerance);
}
}
}
/** test that U is upper triangular */
@Test
public void testUUpperTriangular() {
RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
RealMatrix u = new LUDecompositionImpl(matrix).getU();
for (int i = 0; i < u.getRowDimension(); i++) {
for (int j = 0; j < i; j++) {
assertEquals(u.getEntry(i, j), 0, entryTolerance);
Assert.assertEquals(u.getEntry(i, j), 0, entryTolerance);
}
}
}
/** test that P is a permutation matrix */
@Test
public void testPPermutation() {
RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
RealMatrix p = new LUDecompositionImpl(matrix).getP();
RealMatrix ppT = p.multiply(p.transpose());
RealMatrix id = MatrixUtils.createRealIdentityMatrix(p.getRowDimension());
assertEquals(0, ppT.subtract(id).getNorm(), normTolerance);
Assert.assertEquals(0, ppT.subtract(id).getNorm(), normTolerance);
for (int i = 0; i < p.getRowDimension(); i++) {
int zeroCount = 0;
@ -168,9 +170,9 @@ public class LUDecompositionImplTest extends TestCase {
++otherCount;
}
}
assertEquals(p.getColumnDimension() - 1, zeroCount);
assertEquals(1, oneCount);
assertEquals(0, otherCount);
Assert.assertEquals(p.getColumnDimension() - 1, zeroCount);
Assert.assertEquals(1, oneCount);
Assert.assertEquals(0, otherCount);
}
for (int j = 0; j < p.getColumnDimension(); j++) {
@ -187,26 +189,27 @@ public class LUDecompositionImplTest extends TestCase {
++otherCount;
}
}
assertEquals(p.getRowDimension() - 1, zeroCount);
assertEquals(1, oneCount);
assertEquals(0, otherCount);
Assert.assertEquals(p.getRowDimension() - 1, zeroCount);
Assert.assertEquals(1, oneCount);
Assert.assertEquals(0, otherCount);
}
}
/** test singular */
@Test
public void testSingular() {
LUDecomposition lu =
new LUDecompositionImpl(MatrixUtils.createRealMatrix(testData));
assertTrue(lu.getSolver().isNonSingular());
Assert.assertTrue(lu.getSolver().isNonSingular());
lu = new LUDecompositionImpl(MatrixUtils.createRealMatrix(singular));
assertFalse(lu.getSolver().isNonSingular());
Assert.assertFalse(lu.getSolver().isNonSingular());
lu = new LUDecompositionImpl(MatrixUtils.createRealMatrix(bigSingular));
assertFalse(lu.getSolver().isNonSingular());
Assert.assertFalse(lu.getSolver().isNonSingular());
}
/** test matrices values */
@Test
public void testMatricesValues1() {
LUDecomposition lu =
new LUDecompositionImpl(MatrixUtils.createRealMatrix(testData));
@ -229,24 +232,25 @@ public class LUDecompositionImplTest extends TestCase {
// check values against known references
RealMatrix l = lu.getL();
assertEquals(0, l.subtract(lRef).getNorm(), 1.0e-13);
Assert.assertEquals(0, l.subtract(lRef).getNorm(), 1.0e-13);
RealMatrix u = lu.getU();
assertEquals(0, u.subtract(uRef).getNorm(), 1.0e-13);
Assert.assertEquals(0, u.subtract(uRef).getNorm(), 1.0e-13);
RealMatrix p = lu.getP();
assertEquals(0, p.subtract(pRef).getNorm(), 1.0e-13);
Assert.assertEquals(0, p.subtract(pRef).getNorm(), 1.0e-13);
int[] pivot = lu.getPivot();
for (int i = 0; i < pivotRef.length; ++i) {
assertEquals(pivotRef[i], pivot[i]);
Assert.assertEquals(pivotRef[i], pivot[i]);
}
// check the same cached instance is returned the second time
assertTrue(l == lu.getL());
assertTrue(u == lu.getU());
assertTrue(p == lu.getP());
Assert.assertTrue(l == lu.getL());
Assert.assertTrue(u == lu.getU());
Assert.assertTrue(p == lu.getP());
}
/** test matrices values */
@Test
public void testMatricesValues2() {
LUDecomposition lu =
new LUDecompositionImpl(MatrixUtils.createRealMatrix(luData));
@ -269,21 +273,19 @@ public class LUDecompositionImplTest extends TestCase {
// check values against known references
RealMatrix l = lu.getL();
assertEquals(0, l.subtract(lRef).getNorm(), 1.0e-13);
Assert.assertEquals(0, l.subtract(lRef).getNorm(), 1.0e-13);
RealMatrix u = lu.getU();
assertEquals(0, u.subtract(uRef).getNorm(), 1.0e-13);
Assert.assertEquals(0, u.subtract(uRef).getNorm(), 1.0e-13);
RealMatrix p = lu.getP();
assertEquals(0, p.subtract(pRef).getNorm(), 1.0e-13);
Assert.assertEquals(0, p.subtract(pRef).getNorm(), 1.0e-13);
int[] pivot = lu.getPivot();
for (int i = 0; i < pivotRef.length; ++i) {
assertEquals(pivotRef[i], pivot[i]);
Assert.assertEquals(pivotRef[i], pivot[i]);
}
// check the same cached instance is returned the second time
assertTrue(l == lu.getL());
assertTrue(u == lu.getU());
assertTrue(p == lu.getP());
Assert.assertTrue(l == lu.getL());
Assert.assertTrue(u == lu.getU());
Assert.assertTrue(p == lu.getP());
}
}

View File

@ -17,11 +17,12 @@
package org.apache.commons.math.linear;
import org.apache.commons.math.exception.SingularMatrixException;
import org.apache.commons.math.exception.MathIllegalArgumentException;
import junit.framework.TestCase;
public class LUSolverTest extends TestCase {
import org.junit.Test;
import org.junit.Assert;
public class LUSolverTest {
private double[][] testData = {
{ 1.0, 2.0, 3.0},
{ 2.0, 5.0, 3.0},
@ -45,89 +46,90 @@ public class LUSolverTest extends TestCase {
{ 3.0, 7.0, 6.0, 8.0 }
}; // 4th row = 1st + 2nd
public LUSolverTest(String name) {
super(name);
}
/** test threshold impact */
@Test
public void testThreshold() {
final RealMatrix matrix = MatrixUtils.createRealMatrix(new double[][] {
{ 1.0, 2.0, 3.0},
{ 2.0, 5.0, 3.0},
{ 4.000001, 9.0, 9.0}
});
assertFalse(new LUDecompositionImpl(matrix, 1.0e-5).getSolver().isNonSingular());
assertTrue(new LUDecompositionImpl(matrix, 1.0e-10).getSolver().isNonSingular());
Assert.assertFalse(new LUDecompositionImpl(matrix, 1.0e-5).getSolver().isNonSingular());
Assert.assertTrue(new LUDecompositionImpl(matrix, 1.0e-10).getSolver().isNonSingular());
}
/** test singular */
@Test
public void testSingular() {
DecompositionSolver solver =
new LUDecompositionImpl(MatrixUtils.createRealMatrix(testData)).getSolver();
assertTrue(solver.isNonSingular());
Assert.assertTrue(solver.isNonSingular());
solver = new LUDecompositionImpl(MatrixUtils.createRealMatrix(singular)).getSolver();
assertFalse(solver.isNonSingular());
Assert.assertFalse(solver.isNonSingular());
solver = new LUDecompositionImpl(MatrixUtils.createRealMatrix(bigSingular)).getSolver();
assertFalse(solver.isNonSingular());
Assert.assertFalse(solver.isNonSingular());
}
/** test solve dimension errors */
@Test
public void testSolveDimensionErrors() {
DecompositionSolver solver =
new LUDecompositionImpl(MatrixUtils.createRealMatrix(testData)).getSolver();
RealMatrix b = MatrixUtils.createRealMatrix(new double[2][2]);
try {
solver.solve(b);
fail("an exception should have been thrown");
Assert.fail("an exception should have been thrown");
} catch (MathIllegalArgumentException iae) {
// expected behavior
}
try {
solver.solve(b.getColumn(0));
fail("an exception should have been thrown");
Assert.fail("an exception should have been thrown");
} catch (MathIllegalArgumentException iae) {
// expected behavior
}
try {
solver.solve(new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(0)));
fail("an exception should have been thrown");
Assert.fail("an exception should have been thrown");
} catch (MathIllegalArgumentException iae) {
// expected behavior
}
}
/** test solve singularity errors */
@Test
public void testSolveSingularityErrors() {
DecompositionSolver solver =
new LUDecompositionImpl(MatrixUtils.createRealMatrix(singular)).getSolver();
RealMatrix b = MatrixUtils.createRealMatrix(new double[2][2]);
try {
solver.solve(b);
fail("an exception should have been thrown");
Assert.fail("an exception should have been thrown");
} catch (SingularMatrixException ime) {
// expected behavior
}
try {
solver.solve(b.getColumn(0));
fail("an exception should have been thrown");
Assert.fail("an exception should have been thrown");
} catch (SingularMatrixException ime) {
// expected behavior
}
try {
solver.solve(b.getColumnVector(0));
fail("an exception should have been thrown");
Assert.fail("an exception should have been thrown");
} catch (SingularMatrixException ime) {
// expected behavior
}
try {
solver.solve(new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(0)));
fail("an exception should have been thrown");
Assert.fail("an exception should have been thrown");
} catch (SingularMatrixException ime) {
// expected behavior
}
}
/** test solve */
@Test
public void testSolve() {
DecompositionSolver solver =
new LUDecompositionImpl(MatrixUtils.createRealMatrix(testData)).getSolver();
@ -139,18 +141,18 @@ public class LUSolverTest extends TestCase {
});
// using RealMatrix
assertEquals(0, solver.solve(b).subtract(xRef).getNorm(), 1.0e-13);
Assert.assertEquals(0, solver.solve(b).subtract(xRef).getNorm(), 1.0e-13);
// using double[]
for (int i = 0; i < b.getColumnDimension(); ++i) {
assertEquals(0,
Assert.assertEquals(0,
new ArrayRealVector(solver.solve(b.getColumn(i))).subtract(xRef.getColumnVector(i)).getNorm(),
1.0e-13);
}
// using ArrayRealVector
for (int i = 0; i < b.getColumnDimension(); ++i) {
assertEquals(0,
Assert.assertEquals(0,
solver.solve(b.getColumnVector(i)).subtract(xRef.getColumnVector(i)).getNorm(),
1.0e-13);
}
@ -159,23 +161,22 @@ public class LUSolverTest extends TestCase {
for (int i = 0; i < b.getColumnDimension(); ++i) {
ArrayRealVectorTest.RealVectorTestImpl v =
new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(i));
assertEquals(0,
Assert.assertEquals(0,
solver.solve(v).subtract(xRef.getColumnVector(i)).getNorm(),
1.0e-13);
}
}
/** test determinant */
@Test
public void testDeterminant() {
assertEquals( -1, getDeterminant(MatrixUtils.createRealMatrix(testData)), 1.0e-15);
assertEquals(-10, getDeterminant(MatrixUtils.createRealMatrix(luData)), 1.0e-14);
assertEquals( 0, getDeterminant(MatrixUtils.createRealMatrix(singular)), 1.0e-17);
assertEquals( 0, getDeterminant(MatrixUtils.createRealMatrix(bigSingular)), 1.0e-10);
Assert.assertEquals( -1, getDeterminant(MatrixUtils.createRealMatrix(testData)), 1.0e-15);
Assert.assertEquals(-10, getDeterminant(MatrixUtils.createRealMatrix(luData)), 1.0e-14);
Assert.assertEquals( 0, getDeterminant(MatrixUtils.createRealMatrix(singular)), 1.0e-17);
Assert.assertEquals( 0, getDeterminant(MatrixUtils.createRealMatrix(bigSingular)), 1.0e-10);
}
private double getDeterminant(RealMatrix m) {
return new LUDecompositionImpl(m).getDeterminant();
}
}

View File

@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math.exception;
package org.apache.commons.math.linear;
import org.junit.Assert;
import org.junit.Test;

View File

@ -20,12 +20,12 @@ package org.apache.commons.math.linear;
import java.util.Random;
import org.apache.commons.math.exception.MathUserException;
import org.apache.commons.math.exception.SingularMatrixException;
import org.apache.commons.math.exception.MathIllegalArgumentException;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.Assert;
public class QRSolverTest extends TestCase {
public class QRSolverTest {
double[][] testData3x3NonSingular = {
{ 12, -51, 4 },
{ 6, 167, -68 },
@ -51,78 +51,78 @@ public class QRSolverTest extends TestCase {
{ -5, 34, 7 }
};
public QRSolverTest(String name) {
super(name);
}
/** test rank */
@Test
public void testRank() {
DecompositionSolver solver =
new QRDecompositionImpl(MatrixUtils.createRealMatrix(testData3x3NonSingular)).getSolver();
assertTrue(solver.isNonSingular());
Assert.assertTrue(solver.isNonSingular());
solver = new QRDecompositionImpl(MatrixUtils.createRealMatrix(testData3x3Singular)).getSolver();
assertFalse(solver.isNonSingular());
Assert.assertFalse(solver.isNonSingular());
solver = new QRDecompositionImpl(MatrixUtils.createRealMatrix(testData3x4)).getSolver();
assertTrue(solver.isNonSingular());
Assert.assertTrue(solver.isNonSingular());
solver = new QRDecompositionImpl(MatrixUtils.createRealMatrix(testData4x3)).getSolver();
assertTrue(solver.isNonSingular());
Assert.assertTrue(solver.isNonSingular());
}
/** test solve dimension errors */
@Test
public void testSolveDimensionErrors() {
DecompositionSolver solver =
new QRDecompositionImpl(MatrixUtils.createRealMatrix(testData3x3NonSingular)).getSolver();
RealMatrix b = MatrixUtils.createRealMatrix(new double[2][2]);
try {
solver.solve(b);
fail("an exception should have been thrown");
Assert.fail("an exception should have been thrown");
} catch (MathIllegalArgumentException iae) {
// expected behavior
}
try {
solver.solve(b.getColumn(0));
fail("an exception should have been thrown");
Assert.fail("an exception should have been thrown");
} catch (MathIllegalArgumentException iae) {
// expected behavior
}
try {
solver.solve(b.getColumnVector(0));
fail("an exception should have been thrown");
Assert.fail("an exception should have been thrown");
} catch (MathIllegalArgumentException iae) {
// expected behavior
}
}
/** test solve rank errors */
@Test
public void testSolveRankErrors() {
DecompositionSolver solver =
new QRDecompositionImpl(MatrixUtils.createRealMatrix(testData3x3Singular)).getSolver();
RealMatrix b = MatrixUtils.createRealMatrix(new double[3][2]);
try {
solver.solve(b);
fail("an exception should have been thrown");
Assert.fail("an exception should have been thrown");
} catch (SingularMatrixException iae) {
// expected behavior
}
try {
solver.solve(b.getColumn(0));
fail("an exception should have been thrown");
Assert.fail("an exception should have been thrown");
} catch (SingularMatrixException iae) {
// expected behavior
}
try {
solver.solve(b.getColumnVector(0));
fail("an exception should have been thrown");
Assert.fail("an exception should have been thrown");
} catch (SingularMatrixException iae) {
// expected behavior
}
}
/** test solve */
@Test
public void testSolve() {
QRDecomposition decomposition =
new QRDecompositionImpl(MatrixUtils.createRealMatrix(testData3x3NonSingular));
@ -135,20 +135,20 @@ public class QRSolverTest extends TestCase {
});
// using RealMatrix
assertEquals(0, solver.solve(b).subtract(xRef).getNorm(), 2.0e-16 * xRef.getNorm());
Assert.assertEquals(0, solver.solve(b).subtract(xRef).getNorm(), 2.0e-16 * xRef.getNorm());
// using double[]
for (int i = 0; i < b.getColumnDimension(); ++i) {
final double[] x = solver.solve(b.getColumn(i));
final double error = new ArrayRealVector(x).subtract(xRef.getColumnVector(i)).getNorm();
assertEquals(0, error, 3.0e-16 * xRef.getColumnVector(i).getNorm());
Assert.assertEquals(0, error, 3.0e-16 * xRef.getColumnVector(i).getNorm());
}
// using ArrayRealVector
for (int i = 0; i < b.getColumnDimension(); ++i) {
final RealVector x = solver.solve(b.getColumnVector(i));
final double error = x.subtract(xRef.getColumnVector(i)).getNorm();
assertEquals(0, error, 3.0e-16 * xRef.getColumnVector(i).getNorm());
Assert.assertEquals(0, error, 3.0e-16 * xRef.getColumnVector(i).getNorm());
}
// using RealVector with an alternate implementation
@ -157,11 +157,12 @@ public class QRSolverTest extends TestCase {
new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(i));
final RealVector x = solver.solve(v);
final double error = x.subtract(xRef.getColumnVector(i)).getNorm();
assertEquals(0, error, 3.0e-16 * xRef.getColumnVector(i).getNorm());
Assert.assertEquals(0, error, 3.0e-16 * xRef.getColumnVector(i).getNorm());
}
}
@Test
public void testOverdetermined() throws MathUserException {
final Random r = new Random(5559252868205245l);
int p = (7 * BlockRealMatrix.BLOCK_SIZE) / 4;
@ -181,10 +182,11 @@ public class QRSolverTest extends TestCase {
// despite perturbation, the least square solution should be pretty good
RealMatrix x = new QRDecompositionImpl(a).getSolver().solve(b);
assertEquals(0, x.subtract(xRef).getNorm(), 0.01 * noise * p * q);
Assert.assertEquals(0, x.subtract(xRef).getNorm(), 0.01 * noise * p * q);
}
@Test
public void testUnderdetermined() throws MathUserException {
final Random r = new Random(42185006424567123l);
int p = (5 * BlockRealMatrix.BLOCK_SIZE) / 4;
@ -195,21 +197,20 @@ public class QRSolverTest extends TestCase {
RealMatrix x = new QRDecompositionImpl(a).getSolver().solve(b);
// too many equations, the system cannot be solved at all
assertTrue(x.subtract(xRef).getNorm() / (p * q) > 0.01);
Assert.assertTrue(x.subtract(xRef).getNorm() / (p * q) > 0.01);
// the last unknown should have been set to 0
assertEquals(0.0, x.getSubMatrix(p, q - 1, 0, x.getColumnDimension() - 1).getNorm());
Assert.assertEquals(0.0, x.getSubMatrix(p, q - 1, 0, x.getColumnDimension() - 1).getNorm(), 0);
}
private RealMatrix createTestMatrix(final Random r, final int rows, final int columns) {
RealMatrix m = MatrixUtils.createRealMatrix(rows, columns);
m.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor(){
@Override
public double visit(int row, int column, double value) {
return 2.0 * r.nextDouble() - 1.0;
}
});
m.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
@Override
public double visit(int row, int column, double value) {
return 2.0 * r.nextDouble() - 1.0;
}
});
return m;
}
}

View File

@ -16,7 +16,8 @@
*/
package org.apache.commons.math.linear;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.Assert;
import org.apache.commons.math.Field;
import org.apache.commons.math.fraction.Fraction;
@ -26,7 +27,6 @@ import org.apache.commons.math.exception.NoDataException;
import org.apache.commons.math.exception.OutOfRangeException;
import org.apache.commons.math.exception.NumberIsTooSmallException;
import org.apache.commons.math.exception.NullArgumentException;
import org.apache.commons.math.exception.NonSquareMatrixException;
import org.apache.commons.math.exception.MathIllegalArgumentException;
/**
@ -34,7 +34,7 @@ import org.apache.commons.math.exception.MathIllegalArgumentException;
*
* @version $Revision$ $Date$
*/
public class SparseFieldMatrixTest extends TestCase {
public class SparseFieldMatrixTest {
// 3 x 3 identity matrix
protected Fraction[][] id = { {new Fraction(1), new Fraction(0), new Fraction(0) }, { new Fraction(0), new Fraction(1), new Fraction(0) }, { new Fraction(0), new Fraction(0), new Fraction(1) } };
// Test data for group operations
@ -107,12 +107,7 @@ public class SparseFieldMatrixTest extends TestCase {
protected double normTolerance = 10E-14;
protected Field<Fraction> field = FractionField.getInstance();
public SparseFieldMatrixTest(String name) {
super(name);
setupFractionArrays();
}
private void setupFractionArrays() {
public SparseFieldMatrixTest() {
try {
testDataLU = new Fraction[][]{ { new Fraction(2), new Fraction(5), new Fraction(3) }, { new Fraction(.5d), new Fraction(-2.5d), new Fraction(6.5d) },
{ new Fraction(0.5d), new Fraction(0.2d), new Fraction(.2d) } };
@ -127,35 +122,36 @@ public class SparseFieldMatrixTest extends TestCase {
} catch (FractionConversionException e) {
// ignore, can't happen
}
}
/** test dimensions */
@Test
public void testDimensions() {
SparseFieldMatrix<Fraction> m = createSparseMatrix(testData);
SparseFieldMatrix<Fraction> m2 = createSparseMatrix(testData2);
assertEquals("testData row dimension", 3, m.getRowDimension());
assertEquals("testData column dimension", 3, m.getColumnDimension());
assertTrue("testData is square", m.isSquare());
assertEquals("testData2 row dimension", m2.getRowDimension(), 2);
assertEquals("testData2 column dimension", m2.getColumnDimension(), 3);
assertTrue("testData2 is not square", !m2.isSquare());
Assert.assertEquals("testData row dimension", 3, m.getRowDimension());
Assert.assertEquals("testData column dimension", 3, m.getColumnDimension());
Assert.assertTrue("testData is square", m.isSquare());
Assert.assertEquals("testData2 row dimension", m2.getRowDimension(), 2);
Assert.assertEquals("testData2 column dimension", m2.getColumnDimension(), 3);
Assert.assertTrue("testData2 is not square", !m2.isSquare());
}
/** test copy functions */
@Test
public void testCopyFunctions() {
SparseFieldMatrix<Fraction> m1 = createSparseMatrix(testData);
FieldMatrix<Fraction> m2 = m1.copy();
assertEquals(m1.getClass(), m2.getClass());
assertEquals((m2), m1);
Assert.assertEquals(m1.getClass(), m2.getClass());
Assert.assertEquals((m2), m1);
SparseFieldMatrix<Fraction> m3 = createSparseMatrix(testData);
FieldMatrix<Fraction> m4 = m3.copy();
assertEquals(m3.getClass(), m4.getClass());
assertEquals((m4), m3);
Assert.assertEquals(m3.getClass(), m4.getClass());
Assert.assertEquals((m4), m3);
}
/** test add */
@Test
public void testAdd() {
SparseFieldMatrix<Fraction> m = createSparseMatrix(testData);
SparseFieldMatrix<Fraction> mInv = createSparseMatrix(testDataInv);
@ -163,7 +159,7 @@ public class SparseFieldMatrixTest extends TestCase {
FieldMatrix<Fraction> mPlusMInv = m.add(mInv);
for (int row = 0; row < m.getRowDimension(); row++) {
for (int col = 0; col < m.getColumnDimension(); col++) {
assertEquals("sum entry entry",
Assert.assertEquals("sum entry entry",
mDataPlusInv.getEntry(row, col).doubleValue(), mPlusMInv.getEntry(row, col).doubleValue(),
entryTolerance);
}
@ -171,12 +167,13 @@ public class SparseFieldMatrixTest extends TestCase {
}
/** test add failure */
@Test
public void testAddFail() {
SparseFieldMatrix<Fraction> m = createSparseMatrix(testData);
SparseFieldMatrix<Fraction> m2 = createSparseMatrix(testData2);
try {
m.add(m2);
fail("MathIllegalArgumentException expected");
Assert.fail("MathIllegalArgumentException expected");
} catch (MathIllegalArgumentException ex) {
// ignored
}
@ -184,6 +181,7 @@ public class SparseFieldMatrixTest extends TestCase {
/** test m-n = m + -n */
@Test
public void testPlusMinus() {
SparseFieldMatrix<Fraction> m = createSparseMatrix(testData);
SparseFieldMatrix<Fraction> n = createSparseMatrix(testDataInv);
@ -191,13 +189,14 @@ public class SparseFieldMatrixTest extends TestCase {
n.scalarMultiply(new Fraction(-1)).add(m), entryTolerance);
try {
m.subtract(createSparseMatrix(testData2));
fail("Expecting illegalArgumentException");
Assert.fail("Expecting illegalArgumentException");
} catch (MathIllegalArgumentException ex) {
// ignored
}
}
/** test multiply */
@Test
public void testMultiply() {
SparseFieldMatrix<Fraction> m = createSparseMatrix(testData);
SparseFieldMatrix<Fraction> mInv = createSparseMatrix(testDataInv);
@ -217,7 +216,7 @@ public class SparseFieldMatrixTest extends TestCase {
entryTolerance);
try {
m.multiply(createSparseMatrix(bigSingular));
fail("Expecting illegalArgumentException");
Assert.fail("Expecting illegalArgumentException");
} catch (MathIllegalArgumentException ex) {
// ignored
}
@ -229,6 +228,7 @@ public class SparseFieldMatrixTest extends TestCase {
private Fraction[][] d4 = new Fraction[][] { { new Fraction(1) }, { new Fraction(2) }, { new Fraction(3) }, { new Fraction(4) } };
private Fraction[][] d5 = new Fraction[][] { { new Fraction(30) }, { new Fraction(70) } };
@Test
public void testMultiply2() {
FieldMatrix<Fraction> m3 = createSparseMatrix(d3);
FieldMatrix<Fraction> m4 = createSparseMatrix(d4);
@ -237,19 +237,21 @@ public class SparseFieldMatrixTest extends TestCase {
}
/** test trace */
@Test
public void testTrace() {
FieldMatrix<Fraction> m = createSparseMatrix(id);
assertEquals("identity trace", 3d, m.getTrace().doubleValue(), entryTolerance);
Assert.assertEquals("identity trace", 3d, m.getTrace().doubleValue(), entryTolerance);
m = createSparseMatrix(testData2);
try {
m.getTrace();
fail("Expecting NonSquareMatrixException");
Assert.fail("Expecting NonSquareMatrixException");
} catch (NonSquareMatrixException ex) {
// ignored
}
}
/** test sclarAdd */
@Test
public void testScalarAdd() {
FieldMatrix<Fraction> m = createSparseMatrix(testData);
assertClose("scalar add", createSparseMatrix(testDataPlus2),
@ -257,6 +259,7 @@ public class SparseFieldMatrixTest extends TestCase {
}
/** test operate */
@Test
public void testOperate() {
FieldMatrix<Fraction> m = createSparseMatrix(id);
assertClose("identity operate", testVector, m.operate(testVector),
@ -266,26 +269,27 @@ public class SparseFieldMatrixTest extends TestCase {
m = createSparseMatrix(bigSingular);
try {
m.operate(testVector);
fail("Expecting illegalArgumentException");
Assert.fail("Expecting illegalArgumentException");
} catch (MathIllegalArgumentException ex) {
// ignored
}
}
/** test issue MATH-209 */
@Test
public void testMath209() {
FieldMatrix<Fraction> a = createSparseMatrix(new Fraction[][] {
{ new Fraction(1), new Fraction(2) }, { new Fraction(3), new Fraction(4) }, { new Fraction(5), new Fraction(6) } });
Fraction[] b = a.operate(new Fraction[] { new Fraction(1), new Fraction(1) });
assertEquals(a.getRowDimension(), b.length);
assertEquals(3.0, b[0].doubleValue(), 1.0e-12);
assertEquals(7.0, b[1].doubleValue(), 1.0e-12);
assertEquals(11.0, b[2].doubleValue(), 1.0e-12);
Assert.assertEquals(a.getRowDimension(), b.length);
Assert.assertEquals(3.0, b[0].doubleValue(), 1.0e-12);
Assert.assertEquals(7.0, b[1].doubleValue(), 1.0e-12);
Assert.assertEquals(11.0, b[2].doubleValue(), 1.0e-12);
}
/** test transpose */
@Test
public void testTranspose() {
FieldMatrix<Fraction> m = createSparseMatrix(testData);
FieldMatrix<Fraction> mIT = new FieldLUDecompositionImpl<Fraction>(m).getSolver().getInverse().transpose();
FieldMatrix<Fraction> mTI = new FieldLUDecompositionImpl<Fraction>(m.transpose()).getSolver().getInverse();
@ -296,6 +300,7 @@ public class SparseFieldMatrixTest extends TestCase {
}
/** test preMultiply by vector */
@Test
public void testPremultiplyVector() {
FieldMatrix<Fraction> m = createSparseMatrix(testData);
assertClose("premultiply", m.preMultiply(testVector), preMultTest,
@ -305,12 +310,13 @@ public class SparseFieldMatrixTest extends TestCase {
m = createSparseMatrix(bigSingular);
try {
m.preMultiply(testVector);
fail("expecting MathIllegalArgumentException");
Assert.fail("expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException ex) {
// ignored
}
}
@Test
public void testPremultiply() {
FieldMatrix<Fraction> m3 = createSparseMatrix(d3);
FieldMatrix<Fraction> m4 = createSparseMatrix(d4);
@ -330,42 +336,45 @@ public class SparseFieldMatrixTest extends TestCase {
entryTolerance);
try {
m.preMultiply(createSparseMatrix(bigSingular));
fail("Expecting illegalArgumentException");
Assert.fail("Expecting illegalArgumentException");
} catch (MathIllegalArgumentException ex) {
// ignored
}
}
@Test
public void testGetVectors() {
FieldMatrix<Fraction> m = createSparseMatrix(testData);
assertClose("get row", m.getRow(0), testDataRow1, entryTolerance);
assertClose("get col", m.getColumn(2), testDataCol3, entryTolerance);
try {
m.getRow(10);
fail("expecting OutOfRangeException");
Assert.fail("expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// ignored
}
try {
m.getColumn(-1);
fail("expecting OutOfRangeException");
Assert.fail("expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// ignored
}
}
@Test
public void testGetEntry() {
FieldMatrix<Fraction> m = createSparseMatrix(testData);
assertEquals("get entry", m.getEntry(0, 1).doubleValue(), 2d, entryTolerance);
Assert.assertEquals("get entry", m.getEntry(0, 1).doubleValue(), 2d, entryTolerance);
try {
m.getEntry(10, 4);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
}
/** test examples in user guide */
@Test
public void testExamples() {
// Create a real matrix with two rows and three columns
Fraction[][] matrixData = { { new Fraction(1), new Fraction(2), new Fraction(3) }, { new Fraction(2), new Fraction(5), new Fraction(3) } };
@ -375,12 +384,12 @@ public class SparseFieldMatrixTest extends TestCase {
FieldMatrix<Fraction> n = createSparseMatrix(matrixData2);
// Now multiply m by n
FieldMatrix<Fraction> p = m.multiply(n);
assertEquals(2, p.getRowDimension());
assertEquals(2, p.getColumnDimension());
Assert.assertEquals(2, p.getRowDimension());
Assert.assertEquals(2, p.getColumnDimension());
// Invert p
FieldMatrix<Fraction> pInverse = new FieldLUDecompositionImpl<Fraction>(p).getSolver().getInverse();
assertEquals(2, pInverse.getRowDimension());
assertEquals(2, pInverse.getColumnDimension());
Assert.assertEquals(2, pInverse.getRowDimension());
Assert.assertEquals(2, pInverse.getColumnDimension());
// Solve example
Fraction[][] coefficientsData = { { new Fraction(2), new Fraction(3), new Fraction(-2) }, { new Fraction(-1), new Fraction(7), new Fraction(6) },
@ -388,16 +397,17 @@ public class SparseFieldMatrixTest extends TestCase {
FieldMatrix<Fraction> coefficients = createSparseMatrix(coefficientsData);
Fraction[] constants = { new Fraction(1), new Fraction(-2), new Fraction(1) };
Fraction[] solution = new FieldLUDecompositionImpl<Fraction>(coefficients).getSolver().solve(constants);
assertEquals((new Fraction(2).multiply((solution[0])).add(new Fraction(3).multiply(solution[1])).subtract(new Fraction(2).multiply(solution[2]))).doubleValue(),
Assert.assertEquals((new Fraction(2).multiply((solution[0])).add(new Fraction(3).multiply(solution[1])).subtract(new Fraction(2).multiply(solution[2]))).doubleValue(),
constants[0].doubleValue(), 1E-12);
assertEquals(((new Fraction(-1).multiply(solution[0])).add(new Fraction(7).multiply(solution[1])).add(new Fraction(6).multiply(solution[2]))).doubleValue(),
Assert.assertEquals(((new Fraction(-1).multiply(solution[0])).add(new Fraction(7).multiply(solution[1])).add(new Fraction(6).multiply(solution[2]))).doubleValue(),
constants[1].doubleValue(), 1E-12);
assertEquals(((new Fraction(4).multiply(solution[0])).subtract(new Fraction(3).multiply( solution[1])).subtract(new Fraction(5).multiply(solution[2]))).doubleValue(),
Assert.assertEquals(((new Fraction(4).multiply(solution[0])).subtract(new Fraction(3).multiply( solution[1])).subtract(new Fraction(5).multiply(solution[2]))).doubleValue(),
constants[2].doubleValue(), 1E-12);
}
// test submatrix accessors
@Test
public void testSubMatrix() {
FieldMatrix<Fraction> m = createSparseMatrix(subTestData);
FieldMatrix<Fraction> mRows23Cols00 = createSparseMatrix(subRows23Cols00);
@ -408,135 +418,139 @@ public class SparseFieldMatrixTest extends TestCase {
FieldMatrix<Fraction> mRows03Cols123 = createSparseMatrix(subRows03Cols123);
FieldMatrix<Fraction> mRows20Cols123 = createSparseMatrix(subRows20Cols123);
FieldMatrix<Fraction> mRows31Cols31 = createSparseMatrix(subRows31Cols31);
assertEquals("Rows23Cols00", mRows23Cols00, m.getSubMatrix(2, 3, 0, 0));
assertEquals("Rows00Cols33", mRows00Cols33, m.getSubMatrix(0, 0, 3, 3));
assertEquals("Rows01Cols23", mRows01Cols23, m.getSubMatrix(0, 1, 2, 3));
assertEquals("Rows02Cols13", mRows02Cols13,
Assert.assertEquals("Rows23Cols00", mRows23Cols00, m.getSubMatrix(2, 3, 0, 0));
Assert.assertEquals("Rows00Cols33", mRows00Cols33, m.getSubMatrix(0, 0, 3, 3));
Assert.assertEquals("Rows01Cols23", mRows01Cols23, m.getSubMatrix(0, 1, 2, 3));
Assert.assertEquals("Rows02Cols13", mRows02Cols13,
m.getSubMatrix(new int[] { 0, 2 }, new int[] { 1, 3 }));
assertEquals("Rows03Cols12", mRows03Cols12,
Assert.assertEquals("Rows03Cols12", mRows03Cols12,
m.getSubMatrix(new int[] { 0, 3 }, new int[] { 1, 2 }));
assertEquals("Rows03Cols123", mRows03Cols123,
Assert.assertEquals("Rows03Cols123", mRows03Cols123,
m.getSubMatrix(new int[] { 0, 3 }, new int[] { 1, 2, 3 }));
assertEquals("Rows20Cols123", mRows20Cols123,
Assert.assertEquals("Rows20Cols123", mRows20Cols123,
m.getSubMatrix(new int[] { 2, 0 }, new int[] { 1, 2, 3 }));
assertEquals("Rows31Cols31", mRows31Cols31,
Assert.assertEquals("Rows31Cols31", mRows31Cols31,
m.getSubMatrix(new int[] { 3, 1 }, new int[] { 3, 1 }));
assertEquals("Rows31Cols31", mRows31Cols31,
Assert.assertEquals("Rows31Cols31", mRows31Cols31,
m.getSubMatrix(new int[] { 3, 1 }, new int[] { 3, 1 }));
try {
m.getSubMatrix(1, 0, 2, 4);
fail("Expecting NumberIsTooSmallException");
Assert.fail("Expecting NumberIsTooSmallException");
} catch (NumberIsTooSmallException ex) {
// expected
}
try {
m.getSubMatrix(-1, 1, 2, 2);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.getSubMatrix(1, 0, 2, 2);
fail("Expecting NumberIsTooSmallException");
Assert.fail("Expecting NumberIsTooSmallException");
} catch (NumberIsTooSmallException ex) {
// expected
}
try {
m.getSubMatrix(1, 0, 2, 4);
fail("Expecting NumberIsTooSmallException");
Assert.fail("Expecting NumberIsTooSmallException");
} catch (NumberIsTooSmallException ex) {
// expected
}
try {
m.getSubMatrix(new int[] {}, new int[] { 0 });
fail("Expecting NoDataException");
Assert.fail("Expecting NoDataException");
} catch (NoDataException ex) {
// expected
}
try {
m.getSubMatrix(new int[] { 0 }, new int[] { 4 });
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
}
@Test
public void testGetRowMatrix() {
FieldMatrix<Fraction> m = createSparseMatrix(subTestData);
FieldMatrix<Fraction> mRow0 = createSparseMatrix(subRow0);
FieldMatrix<Fraction> mRow3 = createSparseMatrix(subRow3);
assertEquals("Row0", mRow0, m.getRowMatrix(0));
assertEquals("Row3", mRow3, m.getRowMatrix(3));
Assert.assertEquals("Row0", mRow0, m.getRowMatrix(0));
Assert.assertEquals("Row3", mRow3, m.getRowMatrix(3));
try {
m.getRowMatrix(-1);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.getRowMatrix(4);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
}
@Test
public void testGetColumnMatrix() {
FieldMatrix<Fraction> m = createSparseMatrix(subTestData);
FieldMatrix<Fraction> mColumn1 = createSparseMatrix(subColumn1);
FieldMatrix<Fraction> mColumn3 = createSparseMatrix(subColumn3);
assertEquals("Column1", mColumn1, m.getColumnMatrix(1));
assertEquals("Column3", mColumn3, m.getColumnMatrix(3));
Assert.assertEquals("Column1", mColumn1, m.getColumnMatrix(1));
Assert.assertEquals("Column3", mColumn3, m.getColumnMatrix(3));
try {
m.getColumnMatrix(-1);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.getColumnMatrix(4);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
}
@Test
public void testGetRowVector() {
FieldMatrix<Fraction> m = createSparseMatrix(subTestData);
FieldVector<Fraction> mRow0 = new ArrayFieldVector<Fraction>(subRow0[0]);
FieldVector<Fraction> mRow3 = new ArrayFieldVector<Fraction>(subRow3[0]);
assertEquals("Row0", mRow0, m.getRowVector(0));
assertEquals("Row3", mRow3, m.getRowVector(3));
Assert.assertEquals("Row0", mRow0, m.getRowVector(0));
Assert.assertEquals("Row3", mRow3, m.getRowVector(3));
try {
m.getRowVector(-1);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.getRowVector(4);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
}
@Test
public void testGetColumnVector() {
FieldMatrix<Fraction> m = createSparseMatrix(subTestData);
FieldVector<Fraction> mColumn1 = columnToVector(subColumn1);
FieldVector<Fraction> mColumn3 = columnToVector(subColumn3);
assertEquals("Column1", mColumn1, m.getColumnVector(1));
assertEquals("Column3", mColumn3, m.getColumnVector(3));
Assert.assertEquals("Column1", mColumn1, m.getColumnVector(1));
Assert.assertEquals("Column3", mColumn3, m.getColumnVector(3));
try {
m.getColumnVector(-1);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.getColumnVector(4);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
@ -550,45 +564,47 @@ public class SparseFieldMatrixTest extends TestCase {
return new ArrayFieldVector<Fraction>(data, false);
}
@Test
public void testEqualsAndHashCode() {
SparseFieldMatrix<Fraction> m = createSparseMatrix(testData);
SparseFieldMatrix<Fraction> m1 = (SparseFieldMatrix<Fraction>) m.copy();
SparseFieldMatrix<Fraction> mt = (SparseFieldMatrix<Fraction>) m.transpose();
assertTrue(m.hashCode() != mt.hashCode());
assertEquals(m.hashCode(), m1.hashCode());
assertEquals(m, m);
assertEquals(m, m1);
assertFalse(m.equals(null));
assertFalse(m.equals(mt));
assertFalse(m.equals(createSparseMatrix(bigSingular)));
Assert.assertTrue(m.hashCode() != mt.hashCode());
Assert.assertEquals(m.hashCode(), m1.hashCode());
Assert.assertEquals(m, m);
Assert.assertEquals(m, m1);
Assert.assertFalse(m.equals(null));
Assert.assertFalse(m.equals(mt));
Assert.assertFalse(m.equals(createSparseMatrix(bigSingular)));
}
/* Disable for now
public void testToString() {
SparseFieldMatrix<Fraction> m = createSparseMatrix(testData);
assertEquals("SparseFieldMatrix<Fraction>{{1.0,2.0,3.0},{2.0,5.0,3.0},{1.0,0.0,8.0}}",
Assert.assertEquals("SparseFieldMatrix<Fraction>{{1.0,2.0,3.0},{2.0,5.0,3.0},{1.0,0.0,8.0}}",
m.toString());
m = new SparseFieldMatrix<Fraction>(field, 1, 1);
assertEquals("SparseFieldMatrix<Fraction>{{0.0}}", m.toString());
Assert.assertEquals("SparseFieldMatrix<Fraction>{{0.0}}", m.toString());
}
*/
@Test
public void testSetSubMatrix() throws Exception {
SparseFieldMatrix<Fraction> m = createSparseMatrix(testData);
m.setSubMatrix(detData2, 1, 1);
FieldMatrix<Fraction> expected = createSparseMatrix(new Fraction[][] {
{ new Fraction(1), new Fraction(2), new Fraction(3) }, { new Fraction(2), new Fraction(1), new Fraction(3) }, { new Fraction(1), new Fraction(2), new Fraction(4) } });
assertEquals(expected, m);
Assert.assertEquals(expected, m);
m.setSubMatrix(detData2, 0, 0);
expected = createSparseMatrix(new Fraction[][] {
{ new Fraction(1), new Fraction(3), new Fraction(3) }, { new Fraction(2), new Fraction(4), new Fraction(3) }, { new Fraction(1), new Fraction(2), new Fraction(4) } });
assertEquals(expected, m);
Assert.assertEquals(expected, m);
m.setSubMatrix(testDataPlus2, 0, 0);
expected = createSparseMatrix(new Fraction[][] {
{ new Fraction(3), new Fraction(4), new Fraction(5) }, { new Fraction(4), new Fraction(7), new Fraction(5) }, { new Fraction(3), new Fraction(2), new Fraction(10) } });
assertEquals(expected, m);
Assert.assertEquals(expected, m);
// javadoc example
SparseFieldMatrix<Fraction> matrix =
@ -597,25 +613,25 @@ public class SparseFieldMatrixTest extends TestCase {
matrix.setSubMatrix(new Fraction[][] { { new Fraction(3), new Fraction(4) }, { new Fraction(5), new Fraction(6) } }, 1, 1);
expected = createSparseMatrix(new Fraction[][] {
{ new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4) }, { new Fraction(5), new Fraction(3), new Fraction(4), new Fraction(8) }, { new Fraction(9), new Fraction(5), new Fraction(6), new Fraction(2) } });
assertEquals(expected, matrix);
Assert.assertEquals(expected, matrix);
// dimension overflow
try {
m.setSubMatrix(testData, 1, 1);
fail("expecting OutOfRangeException");
Assert.fail("expecting OutOfRangeException");
} catch (OutOfRangeException e) {
// expected
}
// dimension underflow
try {
m.setSubMatrix(testData, -1, 1);
fail("expecting OutOfRangeException");
Assert.fail("expecting OutOfRangeException");
} catch (OutOfRangeException e) {
// expected
}
try {
m.setSubMatrix(testData, 1, -1);
fail("expecting OutOfRangeException");
Assert.fail("expecting OutOfRangeException");
} catch (OutOfRangeException e) {
// expected
}
@ -623,13 +639,13 @@ public class SparseFieldMatrixTest extends TestCase {
// null
try {
m.setSubMatrix(null, 1, 1);
fail("expecting NullArgumentException");
Assert.fail("expecting NullArgumentException");
} catch (NullArgumentException e) {
// expected
}
try {
new SparseFieldMatrix<Fraction>(field, 0, 0);
fail("expecting MathIllegalArgumentException");
Assert.fail("expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException e) {
// expected
}
@ -637,7 +653,7 @@ public class SparseFieldMatrixTest extends TestCase {
// ragged
try {
m.setSubMatrix(new Fraction[][] { { new Fraction(1) }, { new Fraction(2), new Fraction(3) } }, 0, 0);
fail("expecting MathIllegalArgumentException");
Assert.fail("expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException e) {
// expected
}
@ -645,11 +661,10 @@ public class SparseFieldMatrixTest extends TestCase {
// empty
try {
m.setSubMatrix(new Fraction[][] { {} }, 0, 0);
fail("expecting MathIllegalArgumentException");
Assert.fail("expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException e) {
// expected
}
}
// --------------- -----------------Protected methods
@ -659,7 +674,7 @@ public class SparseFieldMatrixTest extends TestCase {
double tolerance) {
for(int i=0; i < m.getRowDimension(); i++){
for(int j=0; j < m.getColumnDimension(); j++){
assertEquals(msg, m.getEntry(i,j).doubleValue(), n.getEntry(i,j).doubleValue(), tolerance);
Assert.assertEquals(msg, m.getEntry(i,j).doubleValue(), n.getEntry(i,j).doubleValue(), tolerance);
}
}
@ -669,10 +684,10 @@ public class SparseFieldMatrixTest extends TestCase {
protected void assertClose(String msg, Fraction[] m, Fraction[] n,
double tolerance) {
if (m.length != n.length) {
fail("vectors not same length");
Assert.fail("vectors not same length");
}
for (int i = 0; i < m.length; i++) {
assertEquals(msg + " " + i + " elements differ", m[i].doubleValue(), n[i].doubleValue(),
Assert.assertEquals(msg + " " + i + " elements differ", m[i].doubleValue(), n[i].doubleValue(),
tolerance);
}
}
@ -686,5 +701,4 @@ public class SparseFieldMatrixTest extends TestCase {
}
return matrix;
}
}

View File

@ -16,13 +16,13 @@
*/
package org.apache.commons.math.linear;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.Assert;
import org.apache.commons.math.TestUtils;
import org.apache.commons.math.exception.OutOfRangeException;
import org.apache.commons.math.exception.NoDataException;
import org.apache.commons.math.exception.NumberIsTooSmallException;
import org.apache.commons.math.exception.NonSquareMatrixException;
import org.apache.commons.math.exception.MathIllegalArgumentException;
/**
@ -31,7 +31,7 @@ import org.apache.commons.math.exception.MathIllegalArgumentException;
* @version $Revision$ $Date: 2008-11-07 06:48:13 -0800 (Fri, 07 Nov
* 2008) $
*/
public final class SparseRealMatrixTest extends TestCase {
public final class SparseRealMatrixTest {
// 3 x 3 identity matrix
protected double[][] id = { { 1d, 0d, 0d }, { 0d, 1d, 0d }, { 0d, 0d, 1d } };
@ -107,35 +107,34 @@ public final class SparseRealMatrixTest extends TestCase {
protected double entryTolerance = 10E-16;
protected double normTolerance = 10E-14;
public SparseRealMatrixTest(String name) {
super(name);
}
/** test dimensions */
@Test
public void testDimensions() {
OpenMapRealMatrix m = createSparseMatrix(testData);
OpenMapRealMatrix m2 = createSparseMatrix(testData2);
assertEquals("testData row dimension", 3, m.getRowDimension());
assertEquals("testData column dimension", 3, m.getColumnDimension());
assertTrue("testData is square", m.isSquare());
assertEquals("testData2 row dimension", m2.getRowDimension(), 2);
assertEquals("testData2 column dimension", m2.getColumnDimension(), 3);
assertTrue("testData2 is not square", !m2.isSquare());
Assert.assertEquals("testData row dimension", 3, m.getRowDimension());
Assert.assertEquals("testData column dimension", 3, m.getColumnDimension());
Assert.assertTrue("testData is square", m.isSquare());
Assert.assertEquals("testData2 row dimension", m2.getRowDimension(), 2);
Assert.assertEquals("testData2 column dimension", m2.getColumnDimension(), 3);
Assert.assertTrue("testData2 is not square", !m2.isSquare());
}
/** test copy functions */
@Test
public void testCopyFunctions() {
OpenMapRealMatrix m1 = createSparseMatrix(testData);
RealMatrix m2 = m1.copy();
assertEquals(m1.getClass(), m2.getClass());
assertEquals((m2), m1);
Assert.assertEquals(m1.getClass(), m2.getClass());
Assert.assertEquals((m2), m1);
OpenMapRealMatrix m3 = createSparseMatrix(testData);
RealMatrix m4 = m3.copy();
assertEquals(m3.getClass(), m4.getClass());
assertEquals((m4), m3);
Assert.assertEquals(m3.getClass(), m4.getClass());
Assert.assertEquals((m4), m3);
}
/** test add */
@Test
public void testAdd() {
OpenMapRealMatrix m = createSparseMatrix(testData);
OpenMapRealMatrix mInv = createSparseMatrix(testDataInv);
@ -143,7 +142,7 @@ public final class SparseRealMatrixTest extends TestCase {
RealMatrix mPlusMInv = m.add(mInv);
for (int row = 0; row < m.getRowDimension(); row++) {
for (int col = 0; col < m.getColumnDimension(); col++) {
assertEquals("sum entry entry",
Assert.assertEquals("sum entry entry",
mDataPlusInv.getEntry(row, col), mPlusMInv.getEntry(row, col),
entryTolerance);
}
@ -151,26 +150,29 @@ public final class SparseRealMatrixTest extends TestCase {
}
/** test add failure */
@Test
public void testAddFail() {
OpenMapRealMatrix m = createSparseMatrix(testData);
OpenMapRealMatrix m2 = createSparseMatrix(testData2);
try {
m.add(m2);
fail("MathIllegalArgumentException expected");
Assert.fail("MathIllegalArgumentException expected");
} catch (MathIllegalArgumentException ex) {
// ignored
}
}
/** test norm */
@Test
public void testNorm() {
OpenMapRealMatrix m = createSparseMatrix(testData);
OpenMapRealMatrix m2 = createSparseMatrix(testData2);
assertEquals("testData norm", 14d, m.getNorm(), entryTolerance);
assertEquals("testData2 norm", 7d, m2.getNorm(), entryTolerance);
Assert.assertEquals("testData norm", 14d, m.getNorm(), entryTolerance);
Assert.assertEquals("testData2 norm", 7d, m2.getNorm(), entryTolerance);
}
/** test m-n = m + -n */
@Test
public void testPlusMinus() {
OpenMapRealMatrix m = createSparseMatrix(testData);
OpenMapRealMatrix n = createSparseMatrix(testDataInv);
@ -178,13 +180,14 @@ public final class SparseRealMatrixTest extends TestCase {
n.scalarMultiply(-1d).add(m), entryTolerance);
try {
m.subtract(createSparseMatrix(testData2));
fail("Expecting illegalArgumentException");
Assert.fail("Expecting illegalArgumentException");
} catch (MathIllegalArgumentException ex) {
// ignored
}
}
/** test multiply */
@Test
public void testMultiply() {
OpenMapRealMatrix m = createSparseMatrix(testData);
OpenMapRealMatrix mInv = createSparseMatrix(testDataInv);
@ -204,7 +207,7 @@ public final class SparseRealMatrixTest extends TestCase {
entryTolerance);
try {
m.multiply(createSparseMatrix(bigSingular));
fail("Expecting illegalArgumentException");
Assert.fail("Expecting illegalArgumentException");
} catch (MathIllegalArgumentException ex) {
// ignored
}
@ -216,6 +219,7 @@ public final class SparseRealMatrixTest extends TestCase {
private double[][] d4 = new double[][] { { 1 }, { 2 }, { 3 }, { 4 } };
private double[][] d5 = new double[][] { { 30 }, { 70 } };
@Test
public void testMultiply2() {
RealMatrix m3 = createSparseMatrix(d3);
RealMatrix m4 = createSparseMatrix(d4);
@ -224,19 +228,21 @@ public final class SparseRealMatrixTest extends TestCase {
}
/** test trace */
@Test
public void testTrace() {
RealMatrix m = createSparseMatrix(id);
assertEquals("identity trace", 3d, m.getTrace(), entryTolerance);
Assert.assertEquals("identity trace", 3d, m.getTrace(), entryTolerance);
m = createSparseMatrix(testData2);
try {
m.getTrace();
fail("Expecting NonSquareMatrixException");
Assert.fail("Expecting NonSquareMatrixException");
} catch (NonSquareMatrixException ex) {
// ignored
}
}
/** test sclarAdd */
@Test
public void testScalarAdd() {
RealMatrix m = createSparseMatrix(testData);
assertClose("scalar add", createSparseMatrix(testDataPlus2),
@ -244,6 +250,7 @@ public final class SparseRealMatrixTest extends TestCase {
}
/** test operate */
@Test
public void testOperate() {
RealMatrix m = createSparseMatrix(id);
assertClose("identity operate", testVector, m.operate(testVector),
@ -253,26 +260,27 @@ public final class SparseRealMatrixTest extends TestCase {
m = createSparseMatrix(bigSingular);
try {
m.operate(testVector);
fail("Expecting illegalArgumentException");
Assert.fail("Expecting illegalArgumentException");
} catch (MathIllegalArgumentException ex) {
// ignored
}
}
/** test issue MATH-209 */
@Test
public void testMath209() {
RealMatrix a = createSparseMatrix(new double[][] {
{ 1, 2 }, { 3, 4 }, { 5, 6 } });
double[] b = a.operate(new double[] { 1, 1 });
assertEquals(a.getRowDimension(), b.length);
assertEquals(3.0, b[0], 1.0e-12);
assertEquals(7.0, b[1], 1.0e-12);
assertEquals(11.0, b[2], 1.0e-12);
Assert.assertEquals(a.getRowDimension(), b.length);
Assert.assertEquals(3.0, b[0], 1.0e-12);
Assert.assertEquals(7.0, b[1], 1.0e-12);
Assert.assertEquals(11.0, b[2], 1.0e-12);
}
/** test transpose */
@Test
public void testTranspose() {
RealMatrix m = createSparseMatrix(testData);
RealMatrix mIT = new LUDecompositionImpl(m).getSolver().getInverse().transpose();
RealMatrix mTI = new LUDecompositionImpl(m.transpose()).getSolver().getInverse();
@ -283,6 +291,7 @@ public final class SparseRealMatrixTest extends TestCase {
}
/** test preMultiply by vector */
@Test
public void testPremultiplyVector() {
RealMatrix m = createSparseMatrix(testData);
assertClose("premultiply", m.preMultiply(testVector), preMultTest,
@ -292,12 +301,13 @@ public final class SparseRealMatrixTest extends TestCase {
m = createSparseMatrix(bigSingular);
try {
m.preMultiply(testVector);
fail("expecting MathIllegalArgumentException");
Assert.fail("expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException ex) {
// ignored
}
}
@Test
public void testPremultiply() {
RealMatrix m3 = createSparseMatrix(d3);
RealMatrix m4 = createSparseMatrix(d4);
@ -317,42 +327,45 @@ public final class SparseRealMatrixTest extends TestCase {
entryTolerance);
try {
m.preMultiply(createSparseMatrix(bigSingular));
fail("Expecting illegalArgumentException");
Assert.fail("Expecting illegalArgumentException");
} catch (MathIllegalArgumentException ex) {
// ignored
}
}
@Test
public void testGetVectors() {
RealMatrix m = createSparseMatrix(testData);
assertClose("get row", m.getRow(0), testDataRow1, entryTolerance);
assertClose("get col", m.getColumn(2), testDataCol3, entryTolerance);
try {
m.getRow(10);
fail("expecting OutOfRangeException");
Assert.fail("expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// ignored
}
try {
m.getColumn(-1);
fail("expecting OutOfRangeException");
Assert.fail("expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// ignored
}
}
@Test
public void testGetEntry() {
RealMatrix m = createSparseMatrix(testData);
assertEquals("get entry", m.getEntry(0, 1), 2d, entryTolerance);
Assert.assertEquals("get entry", m.getEntry(0, 1), 2d, entryTolerance);
try {
m.getEntry(10, 4);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
}
/** test examples in user guide */
@Test
public void testExamples() {
// Create a real matrix with two rows and three columns
double[][] matrixData = { { 1d, 2d, 3d }, { 2d, 5d, 3d } };
@ -362,12 +375,12 @@ public final class SparseRealMatrixTest extends TestCase {
RealMatrix n = createSparseMatrix(matrixData2);
// Now multiply m by n
RealMatrix p = m.multiply(n);
assertEquals(2, p.getRowDimension());
assertEquals(2, p.getColumnDimension());
Assert.assertEquals(2, p.getRowDimension());
Assert.assertEquals(2, p.getColumnDimension());
// Invert p
RealMatrix pInverse = new LUDecompositionImpl(p).getSolver().getInverse();
assertEquals(2, pInverse.getRowDimension());
assertEquals(2, pInverse.getColumnDimension());
Assert.assertEquals(2, pInverse.getRowDimension());
Assert.assertEquals(2, pInverse.getColumnDimension());
// Solve example
double[][] coefficientsData = { { 2, 3, -2 }, { -1, 7, 6 },
@ -375,16 +388,17 @@ public final class SparseRealMatrixTest extends TestCase {
RealMatrix coefficients = createSparseMatrix(coefficientsData);
double[] constants = { 1, -2, 1 };
double[] solution = new LUDecompositionImpl(coefficients).getSolver().solve(constants);
assertEquals(2 * solution[0] + 3 * solution[1] - 2 * solution[2],
Assert.assertEquals(2 * solution[0] + 3 * solution[1] - 2 * solution[2],
constants[0], 1E-12);
assertEquals(-1 * solution[0] + 7 * solution[1] + 6 * solution[2],
Assert.assertEquals(-1 * solution[0] + 7 * solution[1] + 6 * solution[2],
constants[1], 1E-12);
assertEquals(4 * solution[0] - 3 * solution[1] - 5 * solution[2],
Assert.assertEquals(4 * solution[0] - 3 * solution[1] - 5 * solution[2],
constants[2], 1E-12);
}
// test submatrix accessors
@Test
public void testSubMatrix() {
RealMatrix m = createSparseMatrix(subTestData);
RealMatrix mRows23Cols00 = createSparseMatrix(subRows23Cols00);
@ -395,135 +409,139 @@ public final class SparseRealMatrixTest extends TestCase {
RealMatrix mRows03Cols123 = createSparseMatrix(subRows03Cols123);
RealMatrix mRows20Cols123 = createSparseMatrix(subRows20Cols123);
RealMatrix mRows31Cols31 = createSparseMatrix(subRows31Cols31);
assertEquals("Rows23Cols00", mRows23Cols00, m.getSubMatrix(2, 3, 0, 0));
assertEquals("Rows00Cols33", mRows00Cols33, m.getSubMatrix(0, 0, 3, 3));
assertEquals("Rows01Cols23", mRows01Cols23, m.getSubMatrix(0, 1, 2, 3));
assertEquals("Rows02Cols13", mRows02Cols13,
Assert.assertEquals("Rows23Cols00", mRows23Cols00, m.getSubMatrix(2, 3, 0, 0));
Assert.assertEquals("Rows00Cols33", mRows00Cols33, m.getSubMatrix(0, 0, 3, 3));
Assert.assertEquals("Rows01Cols23", mRows01Cols23, m.getSubMatrix(0, 1, 2, 3));
Assert.assertEquals("Rows02Cols13", mRows02Cols13,
m.getSubMatrix(new int[] { 0, 2 }, new int[] { 1, 3 }));
assertEquals("Rows03Cols12", mRows03Cols12,
Assert.assertEquals("Rows03Cols12", mRows03Cols12,
m.getSubMatrix(new int[] { 0, 3 }, new int[] { 1, 2 }));
assertEquals("Rows03Cols123", mRows03Cols123,
Assert.assertEquals("Rows03Cols123", mRows03Cols123,
m.getSubMatrix(new int[] { 0, 3 }, new int[] { 1, 2, 3 }));
assertEquals("Rows20Cols123", mRows20Cols123,
Assert.assertEquals("Rows20Cols123", mRows20Cols123,
m.getSubMatrix(new int[] { 2, 0 }, new int[] { 1, 2, 3 }));
assertEquals("Rows31Cols31", mRows31Cols31,
Assert.assertEquals("Rows31Cols31", mRows31Cols31,
m.getSubMatrix(new int[] { 3, 1 }, new int[] { 3, 1 }));
assertEquals("Rows31Cols31", mRows31Cols31,
Assert.assertEquals("Rows31Cols31", mRows31Cols31,
m.getSubMatrix(new int[] { 3, 1 }, new int[] { 3, 1 }));
try {
m.getSubMatrix(1, 0, 2, 4);
fail("Expecting NumberIsTooSmallException");
Assert.fail("Expecting NumberIsTooSmallException");
} catch (NumberIsTooSmallException ex) {
// expected
}
try {
m.getSubMatrix(-1, 1, 2, 2);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.getSubMatrix(1, 0, 2, 2);
fail("Expecting NumberIsTooSmallException");
Assert.fail("Expecting NumberIsTooSmallException");
} catch (NumberIsTooSmallException ex) {
// expected
}
try {
m.getSubMatrix(1, 0, 2, 4);
fail("Expecting NumberIsTooSmallException");
Assert.fail("Expecting NumberIsTooSmallException");
} catch (NumberIsTooSmallException ex) {
// expected
}
try {
m.getSubMatrix(new int[] {}, new int[] { 0 });
fail("Expecting NoDataException");
Assert.fail("Expecting NoDataException");
} catch (NoDataException ex) {
// expected
}
try {
m.getSubMatrix(new int[] { 0 }, new int[] { 4 });
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
}
@Test
public void testGetRowMatrix() {
RealMatrix m = createSparseMatrix(subTestData);
RealMatrix mRow0 = createSparseMatrix(subRow0);
RealMatrix mRow3 = createSparseMatrix(subRow3);
assertEquals("Row0", mRow0, m.getRowMatrix(0));
assertEquals("Row3", mRow3, m.getRowMatrix(3));
Assert.assertEquals("Row0", mRow0, m.getRowMatrix(0));
Assert.assertEquals("Row3", mRow3, m.getRowMatrix(3));
try {
m.getRowMatrix(-1);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.getRowMatrix(4);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
}
@Test
public void testGetColumnMatrix() {
RealMatrix m = createSparseMatrix(subTestData);
RealMatrix mColumn1 = createSparseMatrix(subColumn1);
RealMatrix mColumn3 = createSparseMatrix(subColumn3);
assertEquals("Column1", mColumn1, m.getColumnMatrix(1));
assertEquals("Column3", mColumn3, m.getColumnMatrix(3));
Assert.assertEquals("Column1", mColumn1, m.getColumnMatrix(1));
Assert.assertEquals("Column3", mColumn3, m.getColumnMatrix(3));
try {
m.getColumnMatrix(-1);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.getColumnMatrix(4);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
}
@Test
public void testGetRowVector() {
RealMatrix m = createSparseMatrix(subTestData);
RealVector mRow0 = new ArrayRealVector(subRow0[0]);
RealVector mRow3 = new ArrayRealVector(subRow3[0]);
assertEquals("Row0", mRow0, m.getRowVector(0));
assertEquals("Row3", mRow3, m.getRowVector(3));
Assert.assertEquals("Row0", mRow0, m.getRowVector(0));
Assert.assertEquals("Row3", mRow3, m.getRowVector(3));
try {
m.getRowVector(-1);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.getRowVector(4);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
}
@Test
public void testGetColumnVector() {
RealMatrix m = createSparseMatrix(subTestData);
RealVector mColumn1 = columnToVector(subColumn1);
RealVector mColumn3 = columnToVector(subColumn3);
assertEquals("Column1", mColumn1, m.getColumnVector(1));
assertEquals("Column3", mColumn3, m.getColumnVector(3));
Assert.assertEquals("Column1", mColumn1, m.getColumnVector(1));
Assert.assertEquals("Column3", mColumn3, m.getColumnVector(3));
try {
m.getColumnVector(-1);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.getColumnVector(4);
fail("Expecting OutOfRangeException");
Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
@ -537,43 +555,46 @@ public final class SparseRealMatrixTest extends TestCase {
return new ArrayRealVector(data, false);
}
@Test
public void testEqualsAndHashCode() {
OpenMapRealMatrix m = createSparseMatrix(testData);
OpenMapRealMatrix m1 = m.copy();
OpenMapRealMatrix mt = (OpenMapRealMatrix) m.transpose();
assertTrue(m.hashCode() != mt.hashCode());
assertEquals(m.hashCode(), m1.hashCode());
assertEquals(m, m);
assertEquals(m, m1);
assertFalse(m.equals(null));
assertFalse(m.equals(mt));
assertFalse(m.equals(createSparseMatrix(bigSingular)));
Assert.assertTrue(m.hashCode() != mt.hashCode());
Assert.assertEquals(m.hashCode(), m1.hashCode());
Assert.assertEquals(m, m);
Assert.assertEquals(m, m1);
Assert.assertFalse(m.equals(null));
Assert.assertFalse(m.equals(mt));
Assert.assertFalse(m.equals(createSparseMatrix(bigSingular)));
}
@Test
public void testToString() {
OpenMapRealMatrix m = createSparseMatrix(testData);
assertEquals("OpenMapRealMatrix{{1.0,2.0,3.0},{2.0,5.0,3.0},{1.0,0.0,8.0}}",
Assert.assertEquals("OpenMapRealMatrix{{1.0,2.0,3.0},{2.0,5.0,3.0},{1.0,0.0,8.0}}",
m.toString());
m = new OpenMapRealMatrix(1, 1);
assertEquals("OpenMapRealMatrix{{0.0}}", m.toString());
Assert.assertEquals("OpenMapRealMatrix{{0.0}}", m.toString());
}
public void testSetSubMatrix() throws Exception {
@Test
public void testSetSubMatrix() {
OpenMapRealMatrix m = createSparseMatrix(testData);
m.setSubMatrix(detData2, 1, 1);
RealMatrix expected = createSparseMatrix(new double[][] {
{ 1.0, 2.0, 3.0 }, { 2.0, 1.0, 3.0 }, { 1.0, 2.0, 4.0 } });
assertEquals(expected, m);
Assert.assertEquals(expected, m);
m.setSubMatrix(detData2, 0, 0);
expected = createSparseMatrix(new double[][] {
{ 1.0, 3.0, 3.0 }, { 2.0, 4.0, 3.0 }, { 1.0, 2.0, 4.0 } });
assertEquals(expected, m);
Assert.assertEquals(expected, m);
m.setSubMatrix(testDataPlus2, 0, 0);
expected = createSparseMatrix(new double[][] {
{ 3.0, 4.0, 5.0 }, { 4.0, 7.0, 5.0 }, { 3.0, 2.0, 10.0 } });
assertEquals(expected, m);
Assert.assertEquals(expected, m);
// javadoc example
OpenMapRealMatrix matrix =
@ -582,25 +603,25 @@ public final class SparseRealMatrixTest extends TestCase {
matrix.setSubMatrix(new double[][] { { 3, 4 }, { 5, 6 } }, 1, 1);
expected = createSparseMatrix(new double[][] {
{ 1, 2, 3, 4 }, { 5, 3, 4, 8 }, { 9, 5, 6, 2 } });
assertEquals(expected, matrix);
Assert.assertEquals(expected, matrix);
// dimension overflow
try {
m.setSubMatrix(testData, 1, 1);
fail("expecting OutOfRangeException");
Assert.fail("expecting OutOfRangeException");
} catch (OutOfRangeException e) {
// expected
}
// dimension underflow
try {
m.setSubMatrix(testData, -1, 1);
fail("expecting OutOfRangeException");
Assert.fail("expecting OutOfRangeException");
} catch (OutOfRangeException e) {
// expected
}
try {
m.setSubMatrix(testData, 1, -1);
fail("expecting OutOfRangeException");
Assert.fail("expecting OutOfRangeException");
} catch (OutOfRangeException e) {
// expected
}
@ -608,13 +629,13 @@ public final class SparseRealMatrixTest extends TestCase {
// null
try {
m.setSubMatrix(null, 1, 1);
fail("expecting NullPointerException");
Assert.fail("expecting NullPointerException");
} catch (NullPointerException e) {
// expected
}
try {
new OpenMapRealMatrix(0, 0);
fail("expecting MathIllegalArgumentException");
Assert.fail("expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException e) {
// expected
}
@ -622,7 +643,7 @@ public final class SparseRealMatrixTest extends TestCase {
// ragged
try {
m.setSubMatrix(new double[][] { { 1 }, { 2, 3 } }, 0, 0);
fail("expecting MathIllegalArgumentException");
Assert.fail("expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException e) {
// expected
}
@ -630,16 +651,17 @@ public final class SparseRealMatrixTest extends TestCase {
// empty
try {
m.setSubMatrix(new double[][] { {} }, 0, 0);
fail("expecting MathIllegalArgumentException");
Assert.fail("expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException e) {
// expected
}
}
@Test
public void testSerial() {
OpenMapRealMatrix m = createSparseMatrix(testData);
assertEquals(m,TestUtils.serializeAndRecover(m));
Assert.assertEquals(m,TestUtils.serializeAndRecover(m));
}
// --------------- -----------------Protected methods
@ -647,17 +669,17 @@ public final class SparseRealMatrixTest extends TestCase {
/** verifies that two matrices are close (1-norm) */
protected void assertClose(String msg, RealMatrix m, RealMatrix n,
double tolerance) {
assertTrue(msg, m.subtract(n).getNorm() < tolerance);
Assert.assertTrue(msg, m.subtract(n).getNorm() < tolerance);
}
/** verifies that two vectors are close (sup norm) */
protected void assertClose(String msg, double[] m, double[] n,
double tolerance) {
if (m.length != n.length) {
fail("vectors not same length");
Assert.fail("vectors not same length");
}
for (int i = 0; i < m.length; i++) {
assertEquals(msg + " " + i + " elements differ", m[i], n[i],
Assert.assertEquals(msg + " " + i + " elements differ", m[i], n[i],
tolerance);
}
}
@ -671,6 +693,4 @@ public final class SparseRealMatrixTest extends TestCase {
}
return matrix;
}
}

View File

@ -20,11 +20,11 @@ package org.apache.commons.math.linear;
import java.util.Arrays;
import org.apache.commons.math.util.FastMath;
import org.apache.commons.math.exception.NonSquareMatrixException;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.Assert;
public class TriDiagonalTransformerTest extends TestCase {
public class TriDiagonalTransformerTest {
private double[][] testSquare5 = {
{ 1, 2, 3, 1, 1 },
@ -40,19 +40,17 @@ public class TriDiagonalTransformerTest extends TestCase {
{ 4, 2, 0 }
};
public TriDiagonalTransformerTest(String name) {
super(name);
}
@Test
public void testNonSquare() {
try {
new TriDiagonalTransformer(MatrixUtils.createRealMatrix(new double[3][2]));
fail("an exception should have been thrown");
Assert.fail("an exception should have been thrown");
} catch (NonSquareMatrixException ime) {
// expected behavior
}
}
@Test
public void testAEqualQTQt() {
checkAEqualQTQt(MatrixUtils.createRealMatrix(testSquare5));
checkAEqualQTQt(MatrixUtils.createRealMatrix(testSquare3));
@ -64,9 +62,10 @@ public class TriDiagonalTransformerTest extends TestCase {
RealMatrix qT = transformer.getQT();
RealMatrix t = transformer.getT();
double norm = q.multiply(t).multiply(qT).subtract(matrix).getNorm();
assertEquals(0, norm, 4.0e-15);
Assert.assertEquals(0, norm, 4.0e-15);
}
@Test
public void testNoAccessBelowDiagonal() {
checkNoAccessBelowDiagonal(testSquare5);
checkNoAccessBelowDiagonal(testSquare3);
@ -84,14 +83,16 @@ public class TriDiagonalTransformerTest extends TestCase {
RealMatrix qT = transformer.getQT();
RealMatrix t = transformer.getT();
double norm = q.multiply(t).multiply(qT).subtract(MatrixUtils.createRealMatrix(data)).getNorm();
assertEquals(0, norm, 4.0e-15);
Assert.assertEquals(0, norm, 4.0e-15);
}
@Test
public void testQOrthogonal() {
checkOrthogonal(new TriDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare5)).getQ());
checkOrthogonal(new TriDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare3)).getQ());
}
@Test
public void testQTOrthogonal() {
checkOrthogonal(new TriDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare5)).getQT());
checkOrthogonal(new TriDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare3)).getQT());
@ -100,9 +101,10 @@ public class TriDiagonalTransformerTest extends TestCase {
private void checkOrthogonal(RealMatrix m) {
RealMatrix mTm = m.transpose().multiply(m);
RealMatrix id = MatrixUtils.createRealIdentityMatrix(mTm.getRowDimension());
assertEquals(0, mTm.subtract(id).getNorm(), 1.0e-15);
Assert.assertEquals(0, mTm.subtract(id).getNorm(), 1.0e-15);
}
@Test
public void testTTriDiagonal() {
checkTriDiagonal(new TriDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare5)).getT());
checkTriDiagonal(new TriDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare3)).getT());
@ -114,12 +116,13 @@ public class TriDiagonalTransformerTest extends TestCase {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
if ((i < j - 1) || (i > j + 1)) {
assertEquals(0, m.getEntry(i, j), 1.0e-16);
Assert.assertEquals(0, m.getEntry(i, j), 1.0e-16);
}
}
}
}
@Test
public void testMatricesValues5() {
checkMatricesValues(testSquare5,
new double[][] {
@ -133,6 +136,7 @@ public class TriDiagonalTransformerTest extends TestCase {
new double[] { -FastMath.sqrt(15), -3.0832882879592476, 0.6082710842351517, 1.1786086405912128 });
}
@Test
public void testMatricesValues3() {
checkMatricesValues(testSquare3,
new double[][] {
@ -152,7 +156,7 @@ public class TriDiagonalTransformerTest extends TestCase {
// check values against known references
RealMatrix q = transformer.getQ();
assertEquals(0, q.subtract(MatrixUtils.createRealMatrix(qRef)).getNorm(), 1.0e-14);
Assert.assertEquals(0, q.subtract(MatrixUtils.createRealMatrix(qRef)).getNorm(), 1.0e-14);
RealMatrix t = transformer.getT();
double[][] tData = new double[mainDiagnonal.length][mainDiagnonal.length];
@ -165,12 +169,10 @@ public class TriDiagonalTransformerTest extends TestCase {
tData[i][i + 1] = secondaryDiagonal[i];
}
}
assertEquals(0, t.subtract(MatrixUtils.createRealMatrix(tData)).getNorm(), 1.0e-14);
Assert.assertEquals(0, t.subtract(MatrixUtils.createRealMatrix(tData)).getNorm(), 1.0e-14);
// check the same cached instance is returned the second time
assertTrue(q == transformer.getQ());
assertTrue(t == transformer.getT());
Assert.assertTrue(q == transformer.getQ());
Assert.assertTrue(t == transformer.getT());
}
}

View File

@ -25,7 +25,6 @@ import java.util.List;
import junit.framework.TestCase;
import org.apache.commons.math.exception.SingularMatrixException;
import org.apache.commons.math.exception.ConvergenceException;
import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.TooManyEvaluationsException;
@ -34,6 +33,7 @@ import org.apache.commons.math.analysis.DifferentiableMultivariateVectorialFunct
import org.apache.commons.math.analysis.MultivariateMatrixFunction;
import org.apache.commons.math.linear.BlockRealMatrix;
import org.apache.commons.math.linear.RealMatrix;
import org.apache.commons.math.linear.SingularMatrixException;
import org.apache.commons.math.optimization.SimpleVectorialValueChecker;
import org.apache.commons.math.optimization.VectorialPointValuePair;
import org.apache.commons.math.util.MathUtils;