diff --git a/src/main/java/org/apache/commons/math/linear/AbstractFieldMatrix.java b/src/main/java/org/apache/commons/math/linear/AbstractFieldMatrix.java index 7903524ca..7aea5a962 100644 --- a/src/main/java/org/apache/commons/math/linear/AbstractFieldMatrix.java +++ b/src/main/java/org/apache/commons/math/linear/AbstractFieldMatrix.java @@ -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; diff --git a/src/main/java/org/apache/commons/math/linear/AbstractRealMatrix.java b/src/main/java/org/apache/commons/math/linear/AbstractRealMatrix.java index 1c45990a7..bdc296173 100644 --- a/src/main/java/org/apache/commons/math/linear/AbstractRealMatrix.java +++ b/src/main/java/org/apache/commons/math/linear/AbstractRealMatrix.java @@ -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; diff --git a/src/main/java/org/apache/commons/math/linear/Array2DRowFieldMatrix.java b/src/main/java/org/apache/commons/math/linear/Array2DRowFieldMatrix.java index ac9a655eb..d93fd6181 100644 --- a/src/main/java/org/apache/commons/math/linear/Array2DRowFieldMatrix.java +++ b/src/main/java/org/apache/commons/math/linear/Array2DRowFieldMatrix.java @@ -162,7 +162,7 @@ public class Array2DRowFieldMatrix> * * @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 add(final Array2DRowFieldMatrix m) { @@ -189,7 +189,7 @@ public class Array2DRowFieldMatrix> * * @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 subtract(final Array2DRowFieldMatrix m) { diff --git a/src/main/java/org/apache/commons/math/linear/Array2DRowRealMatrix.java b/src/main/java/org/apache/commons/math/linear/Array2DRowRealMatrix.java index db61eab24..d0a1cf6ba 100644 --- a/src/main/java/org/apache/commons/math/linear/Array2DRowRealMatrix.java +++ b/src/main/java/org/apache/commons/math/linear/Array2DRowRealMatrix.java @@ -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) { diff --git a/src/main/java/org/apache/commons/math/linear/BlockFieldMatrix.java b/src/main/java/org/apache/commons/math/linear/BlockFieldMatrix.java index d59ab69fb..b90b6d00a 100644 --- a/src/main/java/org/apache/commons/math/linear/BlockFieldMatrix.java +++ b/src/main/java/org/apache/commons/math/linear/BlockFieldMatrix.java @@ -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; diff --git a/src/main/java/org/apache/commons/math/linear/BlockRealMatrix.java b/src/main/java/org/apache/commons/math/linear/BlockRealMatrix.java index ca6a0f109..9a53cb66a 100644 --- a/src/main/java/org/apache/commons/math/linear/BlockRealMatrix.java +++ b/src/main/java/org/apache/commons/math/linear/BlockRealMatrix.java @@ -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; diff --git a/src/main/java/org/apache/commons/math/linear/CholeskyDecomposition.java b/src/main/java/org/apache/commons/math/linear/CholeskyDecomposition.java index ff62370ac..c45c5e378 100644 --- a/src/main/java/org/apache/commons/math/linear/CholeskyDecomposition.java +++ b/src/main/java/org/apache/commons/math/linear/CholeskyDecomposition.java @@ -27,8 +27,7 @@ package org.apache.commons.math.linear; *
    *
  • a {@link #getLT() getLT} method has been added,
  • *
  • the isspd 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,
  • *
  • a {@link #getDeterminant() getDeterminant} method has been added,
  • *
  • the solve 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. *

    L is an lower-triangular matrix

    @@ -68,5 +66,4 @@ public interface CholeskyDecomposition { * @return a solver */ DecompositionSolver getSolver(); - } diff --git a/src/main/java/org/apache/commons/math/linear/CholeskyDecompositionImpl.java b/src/main/java/org/apache/commons/math/linear/CholeskyDecompositionImpl.java index d5234b130..57dd18a2f 100644 --- a/src/main/java/org/apache/commons/math/linear/CholeskyDecompositionImpl.java +++ b/src/main/java/org/apache/commons/math/linear/CholeskyDecompositionImpl.java @@ -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 × X = B * @return a vector X such that A × 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); diff --git a/src/main/java/org/apache/commons/math/linear/DecompositionSolver.java b/src/main/java/org/apache/commons/math/linear/DecompositionSolver.java index 845feb7cc..aab8fb91b 100644 --- a/src/main/java/org/apache/commons/math/linear/DecompositionSolver.java +++ b/src/main/java/org/apache/commons/math/linear/DecompositionSolver.java @@ -42,7 +42,7 @@ public interface DecompositionSolver { * @return a vector X that minimizes the two norm of A × 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 × 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 × 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(); diff --git a/src/main/java/org/apache/commons/math/linear/EigenDecompositionImpl.java b/src/main/java/org/apache/commons/math/linear/EigenDecompositionImpl.java index a764d0c7b..3f89d68a9 100644 --- a/src/main/java/org/apache/commons/math/linear/EigenDecompositionImpl.java +++ b/src/main/java/org/apache/commons/math/linear/EigenDecompositionImpl.java @@ -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; diff --git a/src/main/java/org/apache/commons/math/linear/FieldDecompositionSolver.java b/src/main/java/org/apache/commons/math/linear/FieldDecompositionSolver.java index bebe3f192..ff90bd630 100644 --- a/src/main/java/org/apache/commons/math/linear/FieldDecompositionSolver.java +++ b/src/main/java/org/apache/commons/math/linear/FieldDecompositionSolver.java @@ -44,7 +44,7 @@ public interface FieldDecompositionSolver> { * @return a vector X that minimizes the two norm of A × 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> { * @return a vector X that minimizes the two norm of A × 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 solve(final FieldVector b); @@ -68,7 +68,7 @@ public interface FieldDecompositionSolver> { * @return a matrix X that minimizes the two norm of A × 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 solve(final FieldMatrix b); @@ -81,7 +81,7 @@ public interface FieldDecompositionSolver> { /** 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 getInverse(); diff --git a/src/main/java/org/apache/commons/math/linear/FieldLUDecompositionImpl.java b/src/main/java/org/apache/commons/math/linear/FieldLUDecompositionImpl.java index d5cde014a..740e92e60 100644 --- a/src/main/java/org/apache/commons/math/linear/FieldLUDecompositionImpl.java +++ b/src/main/java/org/apache/commons/math/linear/FieldLUDecompositionImpl.java @@ -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. diff --git a/src/main/java/org/apache/commons/math/linear/FieldMatrix.java b/src/main/java/org/apache/commons/math/linear/FieldMatrix.java index b143961fe..3c6869972 100644 --- a/src/main/java/org/apache/commons/math/linear/FieldMatrix.java +++ b/src/main/java/org/apache/commons/math/linear/FieldMatrix.java @@ -63,7 +63,7 @@ public interface FieldMatrix> 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 add(FieldMatrix m); @@ -73,7 +73,7 @@ public interface FieldMatrix> 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 subtract(FieldMatrix m); @@ -201,7 +201,7 @@ public interface FieldMatrix> 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> 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 matrix); @@ -260,7 +260,7 @@ public interface FieldMatrix> 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 matrix); @@ -285,7 +285,7 @@ public interface FieldMatrix> 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 vector); @@ -310,7 +310,7 @@ public interface FieldMatrix> 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 vector); @@ -334,7 +334,7 @@ public interface FieldMatrix> 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> 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> extends AnyMatrix { * trace 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(); diff --git a/src/main/java/org/apache/commons/math/linear/LUDecompositionImpl.java b/src/main/java/org/apache/commons/math/linear/LUDecompositionImpl.java index c57c33745..01fa921df 100644 --- a/src/main/java/org/apache/commons/math/linear/LUDecompositionImpl.java +++ b/src/main/java/org/apache/commons/math/linear/LUDecompositionImpl.java @@ -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; /** diff --git a/src/main/java/org/apache/commons/math/exception/MatrixDimensionMismatchException.java b/src/main/java/org/apache/commons/math/linear/MatrixDimensionMismatchException.java similarity index 95% rename from src/main/java/org/apache/commons/math/exception/MatrixDimensionMismatchException.java rename to src/main/java/org/apache/commons/math/linear/MatrixDimensionMismatchException.java index c317ef445..3ec8f1a1a 100644 --- a/src/main/java/org/apache/commons/math/exception/MatrixDimensionMismatchException.java +++ b/src/main/java/org/apache/commons/math/linear/MatrixDimensionMismatchException.java @@ -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; /** diff --git a/src/main/java/org/apache/commons/math/linear/MatrixUtils.java b/src/main/java/org/apache/commons/math/linear/MatrixUtils.java index 03666610d..2715a685b 100644 --- a/src/main/java/org/apache/commons/math/linear/MatrixUtils.java +++ b/src/main/java/org/apache/commons/math/linear/MatrixUtils.java @@ -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; diff --git a/src/main/java/org/apache/commons/math/exception/NonPositiveDefiniteMatrixException.java b/src/main/java/org/apache/commons/math/linear/NonPositiveDefiniteMatrixException.java similarity index 94% rename from src/main/java/org/apache/commons/math/exception/NonPositiveDefiniteMatrixException.java rename to src/main/java/org/apache/commons/math/linear/NonPositiveDefiniteMatrixException.java index 88017df64..0585bd8a0 100644 --- a/src/main/java/org/apache/commons/math/exception/NonPositiveDefiniteMatrixException.java +++ b/src/main/java/org/apache/commons/math/linear/NonPositiveDefiniteMatrixException.java @@ -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; /** diff --git a/src/main/java/org/apache/commons/math/exception/NonSquareMatrixException.java b/src/main/java/org/apache/commons/math/linear/NonSquareMatrixException.java similarity index 93% rename from src/main/java/org/apache/commons/math/exception/NonSquareMatrixException.java rename to src/main/java/org/apache/commons/math/linear/NonSquareMatrixException.java index efaa70a92..ad0392623 100644 --- a/src/main/java/org/apache/commons/math/exception/NonSquareMatrixException.java +++ b/src/main/java/org/apache/commons/math/linear/NonSquareMatrixException.java @@ -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; /** diff --git a/src/main/java/org/apache/commons/math/exception/NonSymmetricMatrixException.java b/src/main/java/org/apache/commons/math/linear/NonSymmetricMatrixException.java similarity index 95% rename from src/main/java/org/apache/commons/math/exception/NonSymmetricMatrixException.java rename to src/main/java/org/apache/commons/math/linear/NonSymmetricMatrixException.java index 659825490..c8c603c35 100644 --- a/src/main/java/org/apache/commons/math/exception/NonSymmetricMatrixException.java +++ b/src/main/java/org/apache/commons/math/linear/NonSymmetricMatrixException.java @@ -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; /** diff --git a/src/main/java/org/apache/commons/math/linear/OpenMapRealMatrix.java b/src/main/java/org/apache/commons/math/linear/OpenMapRealMatrix.java index dcd4d144b..bdd171209 100644 --- a/src/main/java/org/apache/commons/math/linear/OpenMapRealMatrix.java +++ b/src/main/java/org/apache/commons/math/linear/OpenMapRealMatrix.java @@ -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. */ diff --git a/src/main/java/org/apache/commons/math/linear/QRDecompositionImpl.java b/src/main/java/org/apache/commons/math/linear/QRDecompositionImpl.java index 9ba2e1b21..ba56b3cf7 100644 --- a/src/main/java/org/apache/commons/math/linear/QRDecompositionImpl.java +++ b/src/main/java/org/apache/commons/math/linear/QRDecompositionImpl.java @@ -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; diff --git a/src/main/java/org/apache/commons/math/linear/RealMatrix.java b/src/main/java/org/apache/commons/math/linear/RealMatrix.java index 5b6dc7b6e..c6bd06fc1 100644 --- a/src/main/java/org/apache/commons/math/linear/RealMatrix.java +++ b/src/main/java/org/apache/commons/math/linear/RealMatrix.java @@ -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 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(); diff --git a/src/main/java/org/apache/commons/math/exception/SingularMatrixException.java b/src/main/java/org/apache/commons/math/linear/SingularMatrixException.java similarity index 92% rename from src/main/java/org/apache/commons/math/exception/SingularMatrixException.java rename to src/main/java/org/apache/commons/math/linear/SingularMatrixException.java index cebcb96cf..9bacbb036 100644 --- a/src/main/java/org/apache/commons/math/exception/SingularMatrixException.java +++ b/src/main/java/org/apache/commons/math/linear/SingularMatrixException.java @@ -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; /** diff --git a/src/main/java/org/apache/commons/math/linear/TriDiagonalTransformer.java b/src/main/java/org/apache/commons/math/linear/TriDiagonalTransformer.java index 199232ad5..867e1dffe 100644 --- a/src/main/java/org/apache/commons/math/linear/TriDiagonalTransformer.java +++ b/src/main/java/org/apache/commons/math/linear/TriDiagonalTransformer.java @@ -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.

    * * @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(); diff --git a/src/main/java/org/apache/commons/math/optimization/general/AbstractLeastSquaresOptimizer.java b/src/main/java/org/apache/commons/math/optimization/general/AbstractLeastSquaresOptimizer.java index 1d11b4d7b..a89b3aed0 100644 --- a/src/main/java/org/apache/commons/math/optimization/general/AbstractLeastSquaresOptimizer.java +++ b/src/main/java/org/apache/commons/math/optimization/general/AbstractLeastSquaresOptimizer.java @@ -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. diff --git a/src/main/java/org/apache/commons/math/optimization/general/GaussNewtonOptimizer.java b/src/main/java/org/apache/commons/math/optimization/general/GaussNewtonOptimizer.java index 8d3a1e1ce..3c9c729d8 100644 --- a/src/main/java/org/apache/commons/math/optimization/general/GaussNewtonOptimizer.java +++ b/src/main/java/org/apache/commons/math/optimization/general/GaussNewtonOptimizer.java @@ -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; diff --git a/src/main/java/org/apache/commons/math/random/CorrelatedRandomVectorGenerator.java b/src/main/java/org/apache/commons/math/random/CorrelatedRandomVectorGenerator.java index 7de1a0b19..264bfd296 100644 --- a/src/main/java/org/apache/commons/math/random/CorrelatedRandomVectorGenerator.java +++ b/src/main/java/org/apache/commons/math/random/CorrelatedRandomVectorGenerator.java @@ -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. - *

    Build 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 + /** + * 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. - *

    Build 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 - * @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(); diff --git a/src/test/java/org/apache/commons/math/linear/Array2DRowRealMatrixTest.java b/src/test/java/org/apache/commons/math/linear/Array2DRowRealMatrixTest.java index de1df589c..4e2394acf 100644 --- a/src/test/java/org/apache/commons/math/linear/Array2DRowRealMatrixTest.java +++ b/src/test/java/org/apache/commons/math/linear/Array2DRowRealMatrixTest.java @@ -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; diff --git a/src/test/java/org/apache/commons/math/linear/BlockFieldMatrixTest.java b/src/test/java/org/apache/commons/math/linear/BlockFieldMatrixTest.java index 98b1f7b33..a91c04ee1 100644 --- a/src/test/java/org/apache/commons/math/linear/BlockFieldMatrixTest.java +++ b/src/test/java/org/apache/commons/math/linear/BlockFieldMatrixTest.java @@ -19,18 +19,17 @@ package org.apache.commons.math.linear; import java.util.Arrays; import java.util.Random; -import junit.framework.TestCase; +import org.junit.Test; +import org.junit.Assert; import org.apache.commons.math.TestUtils; import org.apache.commons.math.exception.MathUserException; import org.apache.commons.math.fraction.Fraction; import org.apache.commons.math.fraction.FractionField; -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.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 BlockFieldMatrixTest extends TestCase { +public final class BlockFieldMatrixTest { // 3 x 3 identity matrix protected Fraction[][] id = { @@ -158,34 +157,33 @@ public final class BlockFieldMatrixTest extends TestCase { protected double entryTolerance = 10E-16; protected double normTolerance = 10E-14; - public BlockFieldMatrixTest(String name) { - super(name); - } - /** test dimensions */ + @Test public void testDimensions() { BlockFieldMatrix m = new BlockFieldMatrix(testData); BlockFieldMatrix m2 = new BlockFieldMatrix(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() { Random r = new Random(66636328996002l); BlockFieldMatrix m1 = createRandomMatrix(r, 47, 83); BlockFieldMatrix m2 = new BlockFieldMatrix(m1.getData()); - assertEquals(m1, m2); + Assert.assertEquals(m1, m2); BlockFieldMatrix m3 = new BlockFieldMatrix(testData); BlockFieldMatrix m4 = new BlockFieldMatrix(m3.getData()); - assertEquals(m3, m4); + Assert.assertEquals(m3, m4); } /** test add */ + @Test public void testAdd() { BlockFieldMatrix m = new BlockFieldMatrix(testData); BlockFieldMatrix mInv = new BlockFieldMatrix(testDataInv); @@ -193,38 +191,41 @@ public final class BlockFieldMatrixTest 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() { BlockFieldMatrix m = new BlockFieldMatrix(testData); BlockFieldMatrix m2 = new BlockFieldMatrix(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() { BlockFieldMatrix m = new BlockFieldMatrix(testData); BlockFieldMatrix m2 = new BlockFieldMatrix(testDataInv); TestUtils.assertEquals(m.subtract(m2), m2.scalarMultiply(new Fraction(-1)).add(m)); try { m.subtract(new BlockFieldMatrix(testData2)); - fail("Expecting illegalArgumentException"); + Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } /** test multiply */ - public void testMultiply() { + @Test + public void testMultiply() { BlockFieldMatrix m = new BlockFieldMatrix(testData); BlockFieldMatrix mInv = new BlockFieldMatrix(testDataInv); BlockFieldMatrix identity = new BlockFieldMatrix(id); @@ -236,14 +237,14 @@ public final class BlockFieldMatrixTest extends TestCase { TestUtils.assertEquals(m2.multiply(identity), m2); try { m.multiply(new BlockFieldMatrix(bigSingular)); - fail("Expecting illegalArgumentException"); + Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } } + @Test public void testSeveralBlocks() { - FieldMatrix m = new BlockFieldMatrix(FractionField.getInstance(), 37, 41); for (int i = 0; i < m.getRowDimension(); ++i) { @@ -253,25 +254,25 @@ public final class BlockFieldMatrixTest extends TestCase { } FieldMatrix mT = m.transpose(); - assertEquals(m.getRowDimension(), mT.getColumnDimension()); - assertEquals(m.getColumnDimension(), mT.getRowDimension()); + Assert.assertEquals(m.getRowDimension(), mT.getColumnDimension()); + Assert.assertEquals(m.getColumnDimension(), mT.getRowDimension()); for (int i = 0; i < mT.getRowDimension(); ++i) { for (int j = 0; j < mT.getColumnDimension(); ++j) { - assertEquals(m.getEntry(j, i), mT.getEntry(i, j)); + Assert.assertEquals(m.getEntry(j, i), mT.getEntry(i, j)); } } FieldMatrix mPm = m.add(m); for (int i = 0; i < mPm.getRowDimension(); ++i) { for (int j = 0; j < mPm.getColumnDimension(); ++j) { - assertEquals(m.getEntry(i, j).multiply(new Fraction(2)), mPm.getEntry(i, j)); + Assert.assertEquals(m.getEntry(i, j).multiply(new Fraction(2)), mPm.getEntry(i, j)); } } FieldMatrix mPmMm = mPm.subtract(m); for (int i = 0; i < mPmMm.getRowDimension(); ++i) { for (int j = 0; j < mPmMm.getColumnDimension(); ++j) { - assertEquals(m.getEntry(i, j), mPmMm.getEntry(i, j)); + Assert.assertEquals(m.getEntry(i, j), mPmMm.getEntry(i, j)); } } @@ -282,7 +283,7 @@ public final class BlockFieldMatrixTest extends TestCase { for (int k = 0; k < mT.getColumnDimension(); ++k) { sum = sum.add(new Fraction(k * 11 + i, 11).multiply(new Fraction(k * 11 + j, 11))); } - assertEquals(sum, mTm.getEntry(i, j)); + Assert.assertEquals(sum, mTm.getEntry(i, j)); } } @@ -293,35 +294,35 @@ public final class BlockFieldMatrixTest extends TestCase { for (int k = 0; k < m.getColumnDimension(); ++k) { sum = sum.add(new Fraction(i * 11 + k, 11).multiply(new Fraction(j * 11 + k, 11))); } - assertEquals(sum, mmT.getEntry(i, j)); + Assert.assertEquals(sum, mmT.getEntry(i, j)); } } FieldMatrix sub1 = m.getSubMatrix(2, 9, 5, 20); for (int i = 0; i < sub1.getRowDimension(); ++i) { for (int j = 0; j < sub1.getColumnDimension(); ++j) { - assertEquals(new Fraction((i + 2) * 11 + (j + 5), 11), sub1.getEntry(i, j)); + Assert.assertEquals(new Fraction((i + 2) * 11 + (j + 5), 11), sub1.getEntry(i, j)); } } FieldMatrix sub2 = m.getSubMatrix(10, 12, 3, 40); for (int i = 0; i < sub2.getRowDimension(); ++i) { for (int j = 0; j < sub2.getColumnDimension(); ++j) { - assertEquals(new Fraction((i + 10) * 11 + (j + 3), 11), sub2.getEntry(i, j)); + Assert.assertEquals(new Fraction((i + 10) * 11 + (j + 3), 11), sub2.getEntry(i, j)); } } FieldMatrix sub3 = m.getSubMatrix(30, 34, 0, 5); for (int i = 0; i < sub3.getRowDimension(); ++i) { for (int j = 0; j < sub3.getColumnDimension(); ++j) { - assertEquals(new Fraction((i + 30) * 11 + (j + 0), 11), sub3.getEntry(i, j)); + Assert.assertEquals(new Fraction((i + 30) * 11 + (j + 0), 11), sub3.getEntry(i, j)); } } FieldMatrix sub4 = m.getSubMatrix(30, 32, 32, 35); for (int i = 0; i < sub4.getRowDimension(); ++i) { for (int j = 0; j < sub4.getColumnDimension(); ++j) { - assertEquals(new Fraction((i + 30) * 11 + (j + 32), 11), sub4.getEntry(i, j)); + Assert.assertEquals(new Fraction((i + 30) * 11 + (j + 32), 11), sub4.getEntry(i, j)); } } @@ -341,6 +342,7 @@ public final class BlockFieldMatrixTest extends TestCase { }; private Fraction[][] d5 = new Fraction[][] {{new Fraction(30)},{new Fraction(70)}}; + @Test public void testMultiply2() { FieldMatrix m3 = new BlockFieldMatrix(d3); FieldMatrix m4 = new BlockFieldMatrix(d4); @@ -349,19 +351,21 @@ public final class BlockFieldMatrixTest extends TestCase { } /** test trace */ + @Test public void testTrace() { FieldMatrix m = new BlockFieldMatrix(id); - assertEquals(new Fraction(3),m.getTrace()); + Assert.assertEquals(new Fraction(3),m.getTrace()); m = new BlockFieldMatrix(testData2); try { m.getTrace(); - fail("Expecting NonSquareMatrixException"); + Assert.fail("Expecting NonSquareMatrixException"); } catch (NonSquareMatrixException ex) { // ignored } } /** test scalarAdd */ + @Test public void testScalarAdd() { FieldMatrix m = new BlockFieldMatrix(testData); TestUtils.assertEquals(new BlockFieldMatrix(testDataPlus2), @@ -369,6 +373,7 @@ public final class BlockFieldMatrixTest extends TestCase { } /** test operate */ + @Test public void testOperate() { FieldMatrix m = new BlockFieldMatrix(id); TestUtils.assertEquals(testVector, m.operate(testVector)); @@ -376,12 +381,13 @@ public final class BlockFieldMatrixTest extends TestCase { m = new BlockFieldMatrix(bigSingular); try { m.operate(testVector); - fail("Expecting illegalArgumentException"); + Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } + @Test public void testOperateLarge() { int p = (11 * BlockFieldMatrix.BLOCK_SIZE) / 10; int q = (11 * BlockFieldMatrix.BLOCK_SIZE) / 10; @@ -395,6 +401,7 @@ public final class BlockFieldMatrixTest extends TestCase { } } + @Test public void testOperatePremultiplyLarge() { int p = (11 * BlockFieldMatrix.BLOCK_SIZE) / 10; int q = (11 * BlockFieldMatrix.BLOCK_SIZE) / 10; @@ -409,6 +416,7 @@ public final class BlockFieldMatrixTest extends TestCase { } /** test issue MATH-209 */ + @Test public void testMath209() { FieldMatrix a = new BlockFieldMatrix(new Fraction[][] { { new Fraction(1), new Fraction(2) }, @@ -416,13 +424,14 @@ public final class BlockFieldMatrixTest extends TestCase { { new Fraction(5), new Fraction(6) } }); 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 m = new BlockFieldMatrix(testData); FieldMatrix mIT = new FieldLUDecompositionImpl(m).getSolver().getInverse().transpose(); @@ -434,6 +443,7 @@ public final class BlockFieldMatrixTest extends TestCase { } /** test preMultiply by vector */ + @Test public void testPremultiplyVector() { FieldMatrix m = new BlockFieldMatrix(testData); TestUtils.assertEquals(m.preMultiply(testVector), preMultTest); @@ -442,12 +452,13 @@ public final class BlockFieldMatrixTest extends TestCase { m = new BlockFieldMatrix(bigSingular); try { m.preMultiply(testVector); - fail("expecting MathIllegalArgumentException"); + Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } + @Test public void testPremultiply() { FieldMatrix m3 = new BlockFieldMatrix(d3); FieldMatrix m4 = new BlockFieldMatrix(d4); @@ -463,42 +474,45 @@ public final class BlockFieldMatrixTest extends TestCase { TestUtils.assertEquals(identity.preMultiply(mInv), mInv); try { m.preMultiply(new BlockFieldMatrix(bigSingular)); - fail("Expecting illegalArgumentException"); + Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } + @Test public void testGetVectors() { FieldMatrix m = new BlockFieldMatrix(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 m = new BlockFieldMatrix(testData); - assertEquals(m.getEntry(0,1),new Fraction(2)); + Assert.assertEquals(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 = { @@ -515,12 +529,12 @@ public final class BlockFieldMatrixTest extends TestCase { FieldMatrix n = new BlockFieldMatrix(matrixData2); // Now multiply m by n FieldMatrix 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 pInverse = new FieldLUDecompositionImpl(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 = { @@ -531,15 +545,15 @@ public final class BlockFieldMatrixTest extends TestCase { FieldMatrix coefficients = new BlockFieldMatrix(coefficientsData); Fraction[] constants = {new Fraction(1), new Fraction(-2), new Fraction(1)}; Fraction[] solution = new FieldLUDecompositionImpl(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]); @@ -547,6 +561,7 @@ public final class BlockFieldMatrixTest extends TestCase { } // test submatrix accessors + @Test public void testGetSubMatrix() { FieldMatrix m = new BlockFieldMatrix(subTestData); checkGetSubMatrix(m, subRows23Cols00, 2 , 3 , 0, 0); @@ -571,9 +586,9 @@ public final class BlockFieldMatrixTest extends TestCase { try { FieldMatrix sub = m.getSubMatrix(startRow, endRow, startColumn, endColumn); if (reference != null) { - assertEquals(new BlockFieldMatrix(reference), sub); + Assert.assertEquals(new BlockFieldMatrix(reference), sub); } else { - fail("Expecting OutOfRangeException or NotStrictlyPositiveException" + Assert.fail("Expecting OutOfRangeException or NotStrictlyPositiveException" + " or NumberIsTooSmallException or NoDataException"); } } catch (OutOfRangeException e) { @@ -600,9 +615,9 @@ public final class BlockFieldMatrixTest extends TestCase { try { FieldMatrix sub = m.getSubMatrix(selectedRows, selectedColumns); if (reference != null) { - assertEquals(new BlockFieldMatrix(reference), sub); + Assert.assertEquals(new BlockFieldMatrix(reference), sub); } else { - fail("Expecting OutOfRangeException"); + Assert.fail("Expecting OutOfRangeException"); } } catch (OutOfRangeException e) { if (reference != null) { @@ -623,6 +638,7 @@ public final class BlockFieldMatrixTest extends TestCase { } } + @Test public void testGetSetMatrixLarge() { int n = 3 * BlockFieldMatrix.BLOCK_SIZE; FieldMatrix m = @@ -634,16 +650,16 @@ public final class BlockFieldMatrixTest extends TestCase { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if ((i < 2) || (i > n - 3) || (j < 2) || (j > n - 3)) { - assertEquals(new Fraction(0), m.getEntry(i, j)); + Assert.assertEquals(new Fraction(0), m.getEntry(i, j)); } else { - assertEquals(new Fraction(1), m.getEntry(i, j)); + Assert.assertEquals(new Fraction(1), m.getEntry(i, j)); } } } - assertEquals(sub, m.getSubMatrix(2, n - 3, 2, n - 3)); - + Assert.assertEquals(sub, m.getSubMatrix(2, n - 3, 2, n - 3)); } + @Test public void testCopySubMatrix() { FieldMatrix m = new BlockFieldMatrix(subTestData); checkCopy(m, subRows23Cols00, 2 , 3 , 0, 0); @@ -672,9 +688,9 @@ public final class BlockFieldMatrixTest extends TestCase { new Fraction[reference.length][reference[0].length]; m.copySubMatrix(startRow, endRow, startColumn, endColumn, sub); if (reference != null) { - assertEquals(new BlockFieldMatrix(reference), new BlockFieldMatrix(sub)); + Assert.assertEquals(new BlockFieldMatrix(reference), new BlockFieldMatrix(sub)); } else { - fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException"); + Assert.fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException"); } } catch (OutOfRangeException e) { if (reference != null) { @@ -699,9 +715,9 @@ public final class BlockFieldMatrixTest extends TestCase { new Fraction[reference.length][reference[0].length]; m.copySubMatrix(selectedRows, selectedColumns, sub); if (reference != null) { - assertEquals(new BlockFieldMatrix(reference), new BlockFieldMatrix(sub)); + Assert.assertEquals(new BlockFieldMatrix(reference), new BlockFieldMatrix(sub)); } else { - fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException"); + Assert.fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException"); } } catch (OutOfRangeException e) { if (reference != null) { @@ -718,46 +734,49 @@ public final class BlockFieldMatrixTest extends TestCase { } } + @Test public void testGetRowMatrix() { FieldMatrix m = new BlockFieldMatrix(subTestData); FieldMatrix mRow0 = new BlockFieldMatrix(subRow0); FieldMatrix mRow3 = new BlockFieldMatrix(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 testSetRowMatrix() { FieldMatrix m = new BlockFieldMatrix(subTestData); FieldMatrix mRow3 = new BlockFieldMatrix(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 testGetSetRowMatrixLarge() { int n = 3 * BlockFieldMatrix.BLOCK_SIZE; FieldMatrix m = @@ -769,56 +788,59 @@ public final class BlockFieldMatrixTest extends TestCase { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i != 2) { - assertEquals(new Fraction(0), m.getEntry(i, j)); + Assert.assertEquals(new Fraction(0), m.getEntry(i, j)); } else { - assertEquals(new Fraction(1), m.getEntry(i, j)); + Assert.assertEquals(new Fraction(1), m.getEntry(i, j)); } } } - assertEquals(sub, m.getRowMatrix(2)); + Assert.assertEquals(sub, m.getRowMatrix(2)); } + @Test public void testGetColumnMatrix() { FieldMatrix m = new BlockFieldMatrix(subTestData); FieldMatrix mColumn1 = new BlockFieldMatrix(subColumn1); FieldMatrix mColumn3 = new BlockFieldMatrix(subColumn3); - assertEquals(mColumn1, m.getColumnMatrix(1)); - assertEquals(mColumn3, m.getColumnMatrix(3)); + Assert.assertEquals(mColumn1, m.getColumnMatrix(1)); + Assert.assertEquals(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 m = new BlockFieldMatrix(subTestData); FieldMatrix mColumn3 = new BlockFieldMatrix(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 testGetSetColumnMatrixLarge() { int n = 3 * BlockFieldMatrix.BLOCK_SIZE; FieldMatrix m = @@ -830,56 +852,59 @@ public final class BlockFieldMatrixTest extends TestCase { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (j != 2) { - assertEquals(new Fraction(0), m.getEntry(i, j)); + Assert.assertEquals(new Fraction(0), m.getEntry(i, j)); } else { - assertEquals(new Fraction(1), m.getEntry(i, j)); + Assert.assertEquals(new Fraction(1), m.getEntry(i, j)); } } } - assertEquals(sub, m.getColumnMatrix(2)); + Assert.assertEquals(sub, m.getColumnMatrix(2)); } + @Test public void testGetRowVector() { FieldMatrix m = new BlockFieldMatrix(subTestData); FieldVector mRow0 = new ArrayFieldVector(subRow0[0]); FieldVector mRow3 = new ArrayFieldVector(subRow3[0]); - assertEquals(mRow0, m.getRowVector(0)); - assertEquals(mRow3, m.getRowVector(3)); + Assert.assertEquals(mRow0, m.getRowVector(0)); + Assert.assertEquals(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 m = new BlockFieldMatrix(subTestData); FieldVector mRow3 = new ArrayFieldVector(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(FractionField.getInstance(), 5)); - fail("Expecting MatrixDimensionMismatchException"); + Assert.fail("Expecting MatrixDimensionMismatchException"); } catch (MatrixDimensionMismatchException ex) { // expected } } + @Test public void testGetSetRowVectorLarge() { int n = 3 * BlockFieldMatrix.BLOCK_SIZE; FieldMatrix m = new BlockFieldMatrix(FractionField.getInstance(), n, n); @@ -889,56 +914,59 @@ public final class BlockFieldMatrixTest extends TestCase { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i != 2) { - assertEquals(new Fraction(0), m.getEntry(i, j)); + Assert.assertEquals(new Fraction(0), m.getEntry(i, j)); } else { - assertEquals(new Fraction(1), m.getEntry(i, j)); + Assert.assertEquals(new Fraction(1), m.getEntry(i, j)); } } } - assertEquals(sub, m.getRowVector(2)); + Assert.assertEquals(sub, m.getRowVector(2)); } + @Test public void testGetColumnVector() { FieldMatrix m = new BlockFieldMatrix(subTestData); FieldVector mColumn1 = columnToVector(subColumn1); FieldVector mColumn3 = columnToVector(subColumn3); - assertEquals(mColumn1, m.getColumnVector(1)); - assertEquals(mColumn3, m.getColumnVector(3)); + Assert.assertEquals(mColumn1, m.getColumnVector(1)); + Assert.assertEquals(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 m = new BlockFieldMatrix(subTestData); FieldVector 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(FractionField.getInstance(), 5)); - fail("Expecting MatrixDimensionMismatchException"); + Assert.fail("Expecting MatrixDimensionMismatchException"); } catch (MatrixDimensionMismatchException ex) { // expected } } + @Test public void testGetSetColumnVectorLarge() { int n = 3 * BlockFieldMatrix.BLOCK_SIZE; FieldMatrix m = new BlockFieldMatrix(FractionField.getInstance(), n, n); @@ -948,13 +976,13 @@ public final class BlockFieldMatrixTest extends TestCase { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (j != 2) { - assertEquals(new Fraction(0), m.getEntry(i, j)); + Assert.assertEquals(new Fraction(0), m.getEntry(i, j)); } else { - assertEquals(new Fraction(1), m.getEntry(i, j)); + Assert.assertEquals(new Fraction(1), m.getEntry(i, j)); } } } - assertEquals(sub, m.getColumnVector(2)); + Assert.assertEquals(sub, m.getColumnVector(2)); } @@ -966,43 +994,46 @@ public final class BlockFieldMatrixTest extends TestCase { return new ArrayFieldVector(data, false); } + @Test public void testGetRow() { FieldMatrix m = new BlockFieldMatrix(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 m = new BlockFieldMatrix(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 testGetSetRowLarge() { int n = 3 * BlockFieldMatrix.BLOCK_SIZE; FieldMatrix m = new BlockFieldMatrix(FractionField.getInstance(), n, n); @@ -1013,9 +1044,9 @@ public final class BlockFieldMatrixTest extends TestCase { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i != 2) { - assertEquals(new Fraction(0), m.getEntry(i, j)); + Assert.assertEquals(new Fraction(0), m.getEntry(i, j)); } else { - assertEquals(new Fraction(1), m.getEntry(i, j)); + Assert.assertEquals(new Fraction(1), m.getEntry(i, j)); } } } @@ -1023,6 +1054,7 @@ public final class BlockFieldMatrixTest extends TestCase { } + @Test public void testGetColumn() { FieldMatrix m = new BlockFieldMatrix(subTestData); Fraction[] mColumn1 = columnToArray(subColumn1); @@ -1031,38 +1063,40 @@ public final class BlockFieldMatrixTest 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 m = new BlockFieldMatrix(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 } } + @Test public void testGetSetColumnLarge() { int n = 3 * BlockFieldMatrix.BLOCK_SIZE; FieldMatrix m = new BlockFieldMatrix(FractionField.getInstance(), n, n); @@ -1073,9 +1107,9 @@ public final class BlockFieldMatrixTest extends TestCase { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (j != 2) { - assertEquals(new Fraction(0), m.getEntry(i, j)); + Assert.assertEquals(new Fraction(0), m.getEntry(i, j)); } else { - assertEquals(new Fraction(1), m.getEntry(i, j)); + Assert.assertEquals(new Fraction(1), m.getEntry(i, j)); } } } @@ -1092,46 +1126,49 @@ public final class BlockFieldMatrixTest 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() { BlockFieldMatrix m = new BlockFieldMatrix(testData); BlockFieldMatrix m1 = (BlockFieldMatrix) m.copy(); BlockFieldMatrix mt = (BlockFieldMatrix) 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 BlockFieldMatrix(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 BlockFieldMatrix(bigSingular))); } + @Test public void testToString() { BlockFieldMatrix m = new BlockFieldMatrix(testData); - assertEquals("BlockFieldMatrix{{1,2,3},{2,5,3},{1,0,8}}", m.toString()); + Assert.assertEquals("BlockFieldMatrix{{1,2,3},{2,5,3},{1,0,8}}", m.toString()); } + @Test public void testSetSubMatrix() throws Exception { BlockFieldMatrix m = new BlockFieldMatrix(testData); m.setSubMatrix(detData2,1,1); FieldMatrix expected = new BlockFieldMatrix (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 = new BlockFieldMatrix (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 = new BlockFieldMatrix (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 BlockFieldMatrix matrix = @@ -1150,25 +1187,25 @@ public final class BlockFieldMatrixTest extends TestCase { {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 } @@ -1176,7 +1213,7 @@ public final class BlockFieldMatrixTest extends TestCase { // null try { m.setSubMatrix(null,1,1); - fail("expecting NullPointerException"); + Assert.fail("expecting NullPointerException"); } catch (NullPointerException e) { // expected } @@ -1184,7 +1221,7 @@ public final class BlockFieldMatrixTest 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 } @@ -1192,13 +1229,13 @@ public final class BlockFieldMatrixTest 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; @@ -1207,87 +1244,88 @@ public final class BlockFieldMatrixTest 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 BlockFieldMatrix(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 BlockFieldMatrix(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 BlockFieldMatrix(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 BlockFieldMatrix(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 BlockFieldMatrix(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 BlockFieldMatrix(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 BlockFieldMatrix(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() { BlockFieldMatrix m = new BlockFieldMatrix(testData); - assertEquals(m,TestUtils.serializeAndRecover(m)); + Assert.assertEquals(m,TestUtils.serializeAndRecover(m)); } private static class SetVisitor extends DefaultFieldMatrixChangingVisitor { @@ -1309,7 +1347,7 @@ public final class BlockFieldMatrixTest extends TestCase { @Override public void visit(int i, int j, Fraction value) { ++count; - assertEquals(new Fraction(i * 11 + j, 11), value); + Assert.assertEquals(new Fraction(i * 11 + j, 11), value); } public int getCount() { return count; @@ -1331,6 +1369,5 @@ public final class BlockFieldMatrixTest extends TestCase { } return m; } - } diff --git a/src/test/java/org/apache/commons/math/linear/BlockRealMatrixTest.java b/src/test/java/org/apache/commons/math/linear/BlockRealMatrixTest.java index 1c9ac1b2f..c0af0479a 100644 --- a/src/test/java/org/apache/commons/math/linear/BlockRealMatrixTest.java +++ b/src/test/java/org/apache/commons/math/linear/BlockRealMatrixTest.java @@ -19,16 +19,15 @@ package org.apache.commons.math.linear; import java.util.Arrays; import java.util.Random; -import junit.framework.TestCase; +import org.junit.Test; +import org.junit.Assert; import org.apache.commons.math.TestUtils; import org.apache.commons.math.exception.MathUserException; import org.apache.commons.math.util.FastMath; -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; /** @@ -37,7 +36,7 @@ import org.apache.commons.math.exception.MathIllegalArgumentException; * @version $Revision$ $Date$ */ -public final class BlockRealMatrixTest extends TestCase { +public final class BlockRealMatrixTest { // 3 x 3 identity matrix protected double[][] id = { {1d,0d,0d}, {0d,1d,0d}, {0d,0d,1d} }; @@ -99,34 +98,33 @@ public final class BlockRealMatrixTest extends TestCase { protected double entryTolerance = 10E-16; protected double normTolerance = 10E-14; - public BlockRealMatrixTest(String name) { - super(name); - } - /** test dimensions */ + @Test public void testDimensions() { BlockRealMatrix m = new BlockRealMatrix(testData); BlockRealMatrix m2 = new BlockRealMatrix(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() { Random r = new Random(66636328996002l); BlockRealMatrix m1 = createRandomMatrix(r, 47, 83); BlockRealMatrix m2 = new BlockRealMatrix(m1.getData()); - assertEquals(m1, m2); + Assert.assertEquals(m1, m2); BlockRealMatrix m3 = new BlockRealMatrix(testData); BlockRealMatrix m4 = new BlockRealMatrix(m3.getData()); - assertEquals(m3, m4); + Assert.assertEquals(m3, m4); } /** test add */ + @Test public void testAdd() { BlockRealMatrix m = new BlockRealMatrix(testData); BlockRealMatrix mInv = new BlockRealMatrix(testDataInv); @@ -134,7 +132,7 @@ public final class BlockRealMatrixTest 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); } @@ -142,48 +140,53 @@ public final class BlockRealMatrixTest extends TestCase { } /** test add failure */ + @Test public void testAddFail() { BlockRealMatrix m = new BlockRealMatrix(testData); BlockRealMatrix m2 = new BlockRealMatrix(testData2); try { m.add(m2); - fail("MathIllegalArgumentException expected"); + Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } } /** test norm */ + @Test public void testNorm() { BlockRealMatrix m = new BlockRealMatrix(testData); BlockRealMatrix m2 = new BlockRealMatrix(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() { BlockRealMatrix m = new BlockRealMatrix(testData); BlockRealMatrix m2 = new BlockRealMatrix(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 m-n = m + -n */ + @Test public void testPlusMinus() { BlockRealMatrix m = new BlockRealMatrix(testData); BlockRealMatrix m2 = new BlockRealMatrix(testDataInv); assertClose(m.subtract(m2), m2.scalarMultiply(-1d).add(m), entryTolerance); try { m.subtract(new BlockRealMatrix(testData2)); - fail("Expecting illegalArgumentException"); + Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } /** test multiply */ - public void testMultiply() { + @Test + public void testMultiply() { BlockRealMatrix m = new BlockRealMatrix(testData); BlockRealMatrix mInv = new BlockRealMatrix(testDataInv); BlockRealMatrix identity = new BlockRealMatrix(id); @@ -195,14 +198,14 @@ public final class BlockRealMatrixTest extends TestCase { assertClose(m2.multiply(identity), m2, entryTolerance); try { m.multiply(new BlockRealMatrix(bigSingular)); - fail("Expecting illegalArgumentException"); + Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } } + @Test public void testSeveralBlocks() { - RealMatrix m = new BlockRealMatrix(35, 71); for (int i = 0; i < m.getRowDimension(); ++i) { for (int j = 0; j < m.getColumnDimension(); ++j) { @@ -211,25 +214,25 @@ public final class BlockRealMatrixTest extends TestCase { } RealMatrix mT = m.transpose(); - assertEquals(m.getRowDimension(), mT.getColumnDimension()); - assertEquals(m.getColumnDimension(), mT.getRowDimension()); + Assert.assertEquals(m.getRowDimension(), mT.getColumnDimension()); + Assert.assertEquals(m.getColumnDimension(), mT.getRowDimension()); for (int i = 0; i < mT.getRowDimension(); ++i) { for (int j = 0; j < mT.getColumnDimension(); ++j) { - assertEquals(m.getEntry(j, i), mT.getEntry(i, j), 0); + Assert.assertEquals(m.getEntry(j, i), mT.getEntry(i, j), 0); } } RealMatrix mPm = m.add(m); for (int i = 0; i < mPm.getRowDimension(); ++i) { for (int j = 0; j < mPm.getColumnDimension(); ++j) { - assertEquals(2 * m.getEntry(i, j), mPm.getEntry(i, j), 0); + Assert.assertEquals(2 * m.getEntry(i, j), mPm.getEntry(i, j), 0); } } RealMatrix mPmMm = mPm.subtract(m); for (int i = 0; i < mPmMm.getRowDimension(); ++i) { for (int j = 0; j < mPmMm.getColumnDimension(); ++j) { - assertEquals(m.getEntry(i, j), mPmMm.getEntry(i, j), 0); + Assert.assertEquals(m.getEntry(i, j), mPmMm.getEntry(i, j), 0); } } @@ -240,7 +243,7 @@ public final class BlockRealMatrixTest extends TestCase { for (int k = 0; k < mT.getColumnDimension(); ++k) { sum += (k + i / 1024.0) * (k + j / 1024.0); } - assertEquals(sum, mTm.getEntry(i, j), 0); + Assert.assertEquals(sum, mTm.getEntry(i, j), 0); } } @@ -251,35 +254,35 @@ public final class BlockRealMatrixTest extends TestCase { for (int k = 0; k < m.getColumnDimension(); ++k) { sum += (i + k / 1024.0) * (j + k / 1024.0); } - assertEquals(sum, mmT.getEntry(i, j), 0); + Assert.assertEquals(sum, mmT.getEntry(i, j), 0); } } RealMatrix sub1 = m.getSubMatrix(2, 9, 5, 20); for (int i = 0; i < sub1.getRowDimension(); ++i) { for (int j = 0; j < sub1.getColumnDimension(); ++j) { - assertEquals((i + 2) + (j + 5) / 1024.0, sub1.getEntry(i, j), 0); + Assert.assertEquals((i + 2) + (j + 5) / 1024.0, sub1.getEntry(i, j), 0); } } RealMatrix sub2 = m.getSubMatrix(10, 12, 3, 70); for (int i = 0; i < sub2.getRowDimension(); ++i) { for (int j = 0; j < sub2.getColumnDimension(); ++j) { - assertEquals((i + 10) + (j + 3) / 1024.0, sub2.getEntry(i, j), 0); + Assert.assertEquals((i + 10) + (j + 3) / 1024.0, sub2.getEntry(i, j), 0); } } RealMatrix sub3 = m.getSubMatrix(30, 34, 0, 5); for (int i = 0; i < sub3.getRowDimension(); ++i) { for (int j = 0; j < sub3.getColumnDimension(); ++j) { - assertEquals((i + 30) + (j + 0) / 1024.0, sub3.getEntry(i, j), 0); + Assert.assertEquals((i + 30) + (j + 0) / 1024.0, sub3.getEntry(i, j), 0); } } RealMatrix sub4 = m.getSubMatrix(30, 32, 62, 65); for (int i = 0; i < sub4.getRowDimension(); ++i) { for (int j = 0; j < sub4.getColumnDimension(); ++j) { - assertEquals((i + 30) + (j + 62) / 1024.0, sub4.getEntry(i, j), 0); + Assert.assertEquals((i + 30) + (j + 62) / 1024.0, sub4.getEntry(i, j), 0); } } @@ -291,33 +294,37 @@ public final class BlockRealMatrixTest 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 BlockRealMatrix(d3); - RealMatrix m4 = new BlockRealMatrix(d4); - RealMatrix m5 = new BlockRealMatrix(d5); - assertClose(m3.multiply(m4), m5, entryTolerance); - } + RealMatrix m3 = new BlockRealMatrix(d3); + RealMatrix m4 = new BlockRealMatrix(d4); + RealMatrix m5 = new BlockRealMatrix(d5); + assertClose(m3.multiply(m4), m5, entryTolerance); + } /** test trace */ + @Test public void testTrace() { RealMatrix m = new BlockRealMatrix(id); - assertEquals("identity trace",3d,m.getTrace(),entryTolerance); + Assert.assertEquals("identity trace",3d,m.getTrace(),entryTolerance); m = new BlockRealMatrix(testData2); try { m.getTrace(); - fail("Expecting NonSquareMatrixException"); + Assert.fail("Expecting NonSquareMatrixException"); } catch (NonSquareMatrixException ex) { // ignored } } /** test scalarAdd */ + @Test public void testScalarAdd() { RealMatrix m = new BlockRealMatrix(testData); assertClose(new BlockRealMatrix(testDataPlus2), m.scalarAdd(2d), entryTolerance); } /** test operate */ + @Test public void testOperate() { RealMatrix m = new BlockRealMatrix(id); assertClose(testVector, m.operate(testVector), entryTolerance); @@ -325,12 +332,13 @@ public final class BlockRealMatrixTest extends TestCase { m = new BlockRealMatrix(bigSingular); try { m.operate(testVector); - fail("Expecting illegalArgumentException"); + Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } + @Test public void testOperateLarge() { int p = (7 * BlockRealMatrix.BLOCK_SIZE) / 2; int q = (5 * BlockRealMatrix.BLOCK_SIZE) / 2; @@ -344,6 +352,7 @@ public final class BlockRealMatrixTest extends TestCase { } } + @Test public void testOperatePremultiplyLarge() { int p = (7 * BlockRealMatrix.BLOCK_SIZE) / 2; int q = (5 * BlockRealMatrix.BLOCK_SIZE) / 2; @@ -358,18 +367,20 @@ public final class BlockRealMatrixTest extends TestCase { } /** test issue MATH-209 */ + @Test public void testMath209() { RealMatrix a = new BlockRealMatrix(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 = new BlockRealMatrix(testData); RealMatrix mIT = new LUDecompositionImpl(m).getSolver().getInverse().transpose(); @@ -381,6 +392,7 @@ public final class BlockRealMatrixTest extends TestCase { } /** test preMultiply by vector */ + @Test public void testPremultiplyVector() { RealMatrix m = new BlockRealMatrix(testData); assertClose(m.preMultiply(testVector), preMultTest, normTolerance); @@ -389,12 +401,13 @@ public final class BlockRealMatrixTest extends TestCase { m = new BlockRealMatrix(bigSingular); try { m.preMultiply(testVector); - fail("expecting MathIllegalArgumentException"); + Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } + @Test public void testPremultiply() { RealMatrix m3 = new BlockRealMatrix(d3); RealMatrix m4 = new BlockRealMatrix(d4); @@ -410,42 +423,45 @@ public final class BlockRealMatrixTest extends TestCase { assertClose(identity.preMultiply(mInv), mInv, entryTolerance); try { m.preMultiply(new BlockRealMatrix(bigSingular)); - fail("Expecting illegalArgumentException"); + Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } + @Test public void testGetVectors() { RealMatrix m = new BlockRealMatrix(testData); assertClose(m.getRow(0), testDataRow1, entryTolerance); assertClose(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 BlockRealMatrix(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}}; @@ -455,25 +471,26 @@ public final class BlockRealMatrixTest extends TestCase { RealMatrix n = new BlockRealMatrix(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 BlockRealMatrix(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 BlockRealMatrix(subTestData); checkGetSubMatrix(m, subRows23Cols00, 2 , 3 , 0, 0); @@ -498,9 +515,9 @@ public final class BlockRealMatrixTest extends TestCase { try { RealMatrix sub = m.getSubMatrix(startRow, endRow, startColumn, endColumn); if (reference != null) { - assertEquals(new BlockRealMatrix(reference), sub); + Assert.assertEquals(new BlockRealMatrix(reference), sub); } else { - fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException"); + Assert.fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException"); } } catch (OutOfRangeException e) { if (reference != null) { @@ -522,9 +539,9 @@ public final class BlockRealMatrixTest extends TestCase { try { RealMatrix sub = m.getSubMatrix(selectedRows, selectedColumns); if (reference != null) { - assertEquals(new BlockRealMatrix(reference), sub); + Assert.assertEquals(new BlockRealMatrix(reference), sub); } else { - fail("Expecting OutOfRangeException or NumberIsTooSmallExceptiono r NoDataException"); + Assert.fail("Expecting OutOfRangeException or NumberIsTooSmallExceptiono r NoDataException"); } } catch (OutOfRangeException e) { if (reference != null) { @@ -541,6 +558,7 @@ public final class BlockRealMatrixTest extends TestCase { } } + @Test public void testGetSetMatrixLarge() { int n = 3 * BlockRealMatrix.BLOCK_SIZE; RealMatrix m = new BlockRealMatrix(n, n); @@ -550,16 +568,17 @@ public final class BlockRealMatrixTest extends TestCase { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if ((i < 2) || (i > n - 3) || (j < 2) || (j > n - 3)) { - assertEquals(0.0, m.getEntry(i, j), 0.0); + Assert.assertEquals(0.0, m.getEntry(i, j), 0.0); } else { - assertEquals(1.0, m.getEntry(i, j), 0.0); + Assert.assertEquals(1.0, m.getEntry(i, j), 0.0); } } } - assertEquals(sub, m.getSubMatrix(2, n - 3, 2, n - 3)); + Assert.assertEquals(sub, m.getSubMatrix(2, n - 3, 2, n - 3)); } + @Test public void testCopySubMatrix() { RealMatrix m = new BlockRealMatrix(subTestData); checkCopy(m, subRows23Cols00, 2 , 3 , 0, 0); @@ -588,9 +607,9 @@ public final class BlockRealMatrixTest extends TestCase { new double[reference.length][reference[0].length]; m.copySubMatrix(startRow, endRow, startColumn, endColumn, sub); if (reference != null) { - assertEquals(new BlockRealMatrix(reference), new BlockRealMatrix(sub)); + Assert.assertEquals(new BlockRealMatrix(reference), new BlockRealMatrix(sub)); } else { - fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException"); + Assert.fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException"); } } catch (OutOfRangeException e) { if (reference != null) { @@ -615,9 +634,9 @@ public final class BlockRealMatrixTest extends TestCase { new double[reference.length][reference[0].length]; m.copySubMatrix(selectedRows, selectedColumns, sub); if (reference != null) { - assertEquals(new BlockRealMatrix(reference), new BlockRealMatrix(sub)); + Assert.assertEquals(new BlockRealMatrix(reference), new BlockRealMatrix(sub)); } else { - fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException"); + Assert.fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException"); } } catch (OutOfRangeException e) { if (reference != null) { @@ -634,46 +653,49 @@ public final class BlockRealMatrixTest extends TestCase { } } + @Test public void testGetRowMatrix() { RealMatrix m = new BlockRealMatrix(subTestData); RealMatrix mRow0 = new BlockRealMatrix(subRow0); RealMatrix mRow3 = new BlockRealMatrix(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 testSetRowMatrix() { RealMatrix m = new BlockRealMatrix(subTestData); RealMatrix mRow3 = new BlockRealMatrix(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 testGetSetRowMatrixLarge() { int n = 3 * BlockRealMatrix.BLOCK_SIZE; RealMatrix m = new BlockRealMatrix(n, n); @@ -683,56 +705,58 @@ public final class BlockRealMatrixTest extends TestCase { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i != 2) { - assertEquals(0.0, m.getEntry(i, j), 0.0); + Assert.assertEquals(0.0, m.getEntry(i, j), 0.0); } else { - assertEquals(1.0, m.getEntry(i, j), 0.0); + Assert.assertEquals(1.0, m.getEntry(i, j), 0.0); } } } - assertEquals(sub, m.getRowMatrix(2)); - + Assert.assertEquals(sub, m.getRowMatrix(2)); } + @Test public void testGetColumnMatrix() { RealMatrix m = new BlockRealMatrix(subTestData); RealMatrix mColumn1 = new BlockRealMatrix(subColumn1); RealMatrix mColumn3 = new BlockRealMatrix(subColumn3); - assertEquals(mColumn1, m.getColumnMatrix(1)); - assertEquals(mColumn3, m.getColumnMatrix(3)); + Assert.assertEquals(mColumn1, m.getColumnMatrix(1)); + Assert.assertEquals(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 BlockRealMatrix(subTestData); RealMatrix mColumn3 = new BlockRealMatrix(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 testGetSetColumnMatrixLarge() { int n = 3 * BlockRealMatrix.BLOCK_SIZE; RealMatrix m = new BlockRealMatrix(n, n); @@ -742,56 +766,59 @@ public final class BlockRealMatrixTest extends TestCase { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (j != 2) { - assertEquals(0.0, m.getEntry(i, j), 0.0); + Assert.assertEquals(0.0, m.getEntry(i, j), 0.0); } else { - assertEquals(1.0, m.getEntry(i, j), 0.0); + Assert.assertEquals(1.0, m.getEntry(i, j), 0.0); } } } - assertEquals(sub, m.getColumnMatrix(2)); + Assert.assertEquals(sub, m.getColumnMatrix(2)); } + @Test public void testGetRowVector() { RealMatrix m = new BlockRealMatrix(subTestData); RealVector mRow0 = new ArrayRealVector(subRow0[0]); RealVector mRow3 = new ArrayRealVector(subRow3[0]); - assertEquals(mRow0, m.getRowVector(0)); - assertEquals(mRow3, m.getRowVector(3)); + Assert.assertEquals(mRow0, m.getRowVector(0)); + Assert.assertEquals(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 BlockRealMatrix(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 testGetSetRowVectorLarge() { int n = 3 * BlockRealMatrix.BLOCK_SIZE; RealMatrix m = new BlockRealMatrix(n, n); @@ -801,56 +828,58 @@ public final class BlockRealMatrixTest extends TestCase { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i != 2) { - assertEquals(0.0, m.getEntry(i, j), 0.0); + Assert.assertEquals(0.0, m.getEntry(i, j), 0.0); } else { - assertEquals(1.0, m.getEntry(i, j), 0.0); + Assert.assertEquals(1.0, m.getEntry(i, j), 0.0); } } } - assertEquals(sub, m.getRowVector(2)); - + Assert.assertEquals(sub, m.getRowVector(2)); } + @Test public void testGetColumnVector() { RealMatrix m = new BlockRealMatrix(subTestData); RealVector mColumn1 = columnToVector(subColumn1); RealVector mColumn3 = columnToVector(subColumn3); - assertEquals(mColumn1, m.getColumnVector(1)); - assertEquals(mColumn3, m.getColumnVector(3)); + Assert.assertEquals(mColumn1, m.getColumnVector(1)); + Assert.assertEquals(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 BlockRealMatrix(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 } } + @Test public void testGetSetColumnVectorLarge() { int n = 3 * BlockRealMatrix.BLOCK_SIZE; RealMatrix m = new BlockRealMatrix(n, n); @@ -860,14 +889,13 @@ public final class BlockRealMatrixTest extends TestCase { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (j != 2) { - assertEquals(0.0, m.getEntry(i, j), 0.0); + Assert.assertEquals(0.0, m.getEntry(i, j), 0.0); } else { - assertEquals(1.0, m.getEntry(i, j), 0.0); + Assert.assertEquals(1.0, m.getEntry(i, j), 0.0); } } } - assertEquals(sub, m.getColumnVector(2)); - + Assert.assertEquals(sub, m.getColumnVector(2)); } private RealVector columnToVector(double[][] column) { @@ -878,43 +906,46 @@ public final class BlockRealMatrixTest extends TestCase { return new ArrayRealVector(data, false); } + @Test public void testGetRow() { RealMatrix m = new BlockRealMatrix(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 BlockRealMatrix(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 testGetSetRowLarge() { int n = 3 * BlockRealMatrix.BLOCK_SIZE; RealMatrix m = new BlockRealMatrix(n, n); @@ -925,16 +956,16 @@ public final class BlockRealMatrixTest extends TestCase { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i != 2) { - assertEquals(0.0, m.getEntry(i, j), 0.0); + Assert.assertEquals(0.0, m.getEntry(i, j), 0.0); } else { - assertEquals(1.0, m.getEntry(i, j), 0.0); + Assert.assertEquals(1.0, m.getEntry(i, j), 0.0); } } } checkArrays(sub, m.getRow(2)); - } + @Test public void testGetColumn() { RealMatrix m = new BlockRealMatrix(subTestData); double[] mColumn1 = columnToArray(subColumn1); @@ -943,38 +974,40 @@ public final class BlockRealMatrixTest 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 BlockRealMatrix(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 } } + @Test public void testGetSetColumnLarge() { int n = 3 * BlockRealMatrix.BLOCK_SIZE; RealMatrix m = new BlockRealMatrix(n, n); @@ -985,14 +1018,13 @@ public final class BlockRealMatrixTest extends TestCase { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (j != 2) { - assertEquals(0.0, m.getEntry(i, j), 0.0); + Assert.assertEquals(0.0, m.getEntry(i, j), 0.0); } else { - assertEquals(1.0, m.getEntry(i, j), 0.0); + Assert.assertEquals(1.0, m.getEntry(i, j), 0.0); } } } checkArrays(sub, m.getColumn(2)); - } private double[] columnToArray(double[][] column) { @@ -1004,47 +1036,50 @@ public final class BlockRealMatrixTest 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() { BlockRealMatrix m = new BlockRealMatrix(testData); BlockRealMatrix m1 = m.copy(); BlockRealMatrix mt = 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 BlockRealMatrix(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 BlockRealMatrix(bigSingular))); } + @Test public void testToString() { BlockRealMatrix m = new BlockRealMatrix(testData); - assertEquals("BlockRealMatrix{{1.0,2.0,3.0},{2.0,5.0,3.0},{1.0,0.0,8.0}}", + Assert.assertEquals("BlockRealMatrix{{1.0,2.0,3.0},{2.0,5.0,3.0},{1.0,0.0,8.0}}", m.toString()); } + @Test public void testSetSubMatrix() throws Exception { BlockRealMatrix m = new BlockRealMatrix(testData); m.setSubMatrix(detData2,1,1); RealMatrix expected = new BlockRealMatrix (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 = new BlockRealMatrix (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 = new BlockRealMatrix (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 BlockRealMatrix matrix = new BlockRealMatrix @@ -1052,25 +1087,25 @@ public final class BlockRealMatrixTest extends TestCase { matrix.setSubMatrix(new double[][] {{3, 4}, {5, 6}}, 1, 1); expected = new BlockRealMatrix (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 } @@ -1078,7 +1113,7 @@ public final class BlockRealMatrixTest extends TestCase { // null try { m.setSubMatrix(null,1,1); - fail("expecting NullPointerException"); + Assert.fail("expecting NullPointerException"); } catch (NullPointerException e) { // expected } @@ -1086,7 +1121,7 @@ public final class BlockRealMatrixTest extends TestCase { // ragged try { m.setSubMatrix(new double[][] {{1}, {2, 3}}, 0, 0); - fail("expecting MathIllegalArgumentException"); + Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException e) { // expected } @@ -1094,13 +1129,13 @@ public final class BlockRealMatrixTest 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; @@ -1109,87 +1144,88 @@ public final class BlockRealMatrixTest 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 BlockRealMatrix(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 BlockRealMatrix(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 BlockRealMatrix(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 BlockRealMatrix(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 BlockRealMatrix(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 BlockRealMatrix(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 BlockRealMatrix(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() { BlockRealMatrix m = new BlockRealMatrix(testData); - assertEquals(m,TestUtils.serializeAndRecover(m)); + Assert.assertEquals(m,TestUtils.serializeAndRecover(m)); } private static class SetVisitor extends DefaultRealMatrixChangingVisitor { @@ -1204,7 +1240,7 @@ public final class BlockRealMatrixTest 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; @@ -1215,16 +1251,16 @@ public final class BlockRealMatrixTest extends TestCase { /** verifies that two matrices are close (1-norm) */ protected void assertClose(RealMatrix m, RealMatrix n, double tolerance) { - assertTrue(m.subtract(n).getNorm() < tolerance); + Assert.assertTrue(m.subtract(n).getNorm() < tolerance); } /** verifies that two vectors are close (sup norm) */ protected void assertClose(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(m[i], n[i], tolerance); + Assert.assertEquals(m[i], n[i], tolerance); } } @@ -1237,6 +1273,5 @@ public final class BlockRealMatrixTest extends TestCase { } return m; } - } diff --git a/src/test/java/org/apache/commons/math/linear/CholeskyDecompositionImplTest.java b/src/test/java/org/apache/commons/math/linear/CholeskyDecompositionImplTest.java index c3448e19b..bd19da0f9 100644 --- a/src/test/java/org/apache/commons/math/linear/CholeskyDecompositionImplTest.java +++ b/src/test/java/org/apache/commons/math/linear/CholeskyDecompositionImplTest.java @@ -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()); } - } diff --git a/src/test/java/org/apache/commons/math/linear/EigenSolverTest.java b/src/test/java/org/apache/commons/math/linear/EigenSolverTest.java index ec456fc0f..70a279603 100644 --- a/src/test/java/org/apache/commons/math/linear/EigenSolverTest.java +++ b/src/test/java/org/apache/commons/math/linear/EigenSolverTest.java @@ -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; - } - } diff --git a/src/test/java/org/apache/commons/math/linear/FieldLUDecompositionImplTest.java b/src/test/java/org/apache/commons/math/linear/FieldLUDecompositionImplTest.java index c1a2a2536..6cf90a819 100644 --- a/src/test/java/org/apache/commons/math/linear/FieldLUDecompositionImplTest.java +++ b/src/test/java/org/apache/commons/math/linear/FieldLUDecompositionImplTest.java @@ -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 matrix = new Array2DRowFieldMatrix(testData); FieldLUDecomposition LU = new FieldLUDecompositionImpl(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(new Array2DRowFieldMatrix(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 matrix = new Array2DRowFieldMatrix(testData); FieldLUDecomposition lu = new FieldLUDecompositionImpl(matrix); @@ -112,44 +111,47 @@ public class FieldLUDecompositionImplTest extends TestCase { matrix = new Array2DRowFieldMatrix(singular); lu = new FieldLUDecompositionImpl(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(bigSingular); lu = new FieldLUDecompositionImpl(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 matrix = new Array2DRowFieldMatrix(testData); FieldMatrix l = new FieldLUDecompositionImpl(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 matrix = new Array2DRowFieldMatrix(testData); FieldMatrix u = new FieldLUDecompositionImpl(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 matrix = new Array2DRowFieldMatrix(testData); FieldMatrix p = new FieldLUDecompositionImpl(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 lu = new FieldLUDecompositionImpl(new Array2DRowFieldMatrix(testData)); - assertTrue(lu.getSolver().isNonSingular()); + Assert.assertTrue(lu.getSolver().isNonSingular()); lu = new FieldLUDecompositionImpl(new Array2DRowFieldMatrix(singular)); - assertFalse(lu.getSolver().isNonSingular()); + Assert.assertFalse(lu.getSolver().isNonSingular()); lu = new FieldLUDecompositionImpl(new Array2DRowFieldMatrix(bigSingular)); - assertFalse(lu.getSolver().isNonSingular()); + Assert.assertFalse(lu.getSolver().isNonSingular()); } /** test matrices values */ + @Test public void testMatricesValues1() { FieldLUDecomposition lu = new FieldLUDecompositionImpl(new Array2DRowFieldMatrix(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 lu = new FieldLUDecompositionImpl(new Array2DRowFieldMatrix(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()); } - } diff --git a/src/test/java/org/apache/commons/math/linear/FieldMatrixImplTest.java b/src/test/java/org/apache/commons/math/linear/FieldMatrixImplTest.java index e4487428e..d81a15995 100644 --- a/src/test/java/org/apache/commons/math/linear/FieldMatrixImplTest.java +++ b/src/test/java/org/apache/commons/math/linear/FieldMatrixImplTest.java @@ -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 m = new Array2DRowFieldMatrix(testData); Array2DRowFieldMatrix m2 = new Array2DRowFieldMatrix(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 m1 = new Array2DRowFieldMatrix(testData); Array2DRowFieldMatrix m2 = new Array2DRowFieldMatrix(m1.getData()); - assertEquals(m2,m1); + Assert.assertEquals(m2,m1); Array2DRowFieldMatrix m3 = new Array2DRowFieldMatrix(testData); Array2DRowFieldMatrix m4 = new Array2DRowFieldMatrix(m3.getData(), false); - assertEquals(m4,m3); + Assert.assertEquals(m4,m3); } /** test add */ + @Test public void testAdd() { Array2DRowFieldMatrix m = new Array2DRowFieldMatrix(testData); Array2DRowFieldMatrix mInv = new Array2DRowFieldMatrix(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 m = new Array2DRowFieldMatrix(testData); Array2DRowFieldMatrix m2 = new Array2DRowFieldMatrix(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 m = new Array2DRowFieldMatrix(testData); Array2DRowFieldMatrix m2 = new Array2DRowFieldMatrix(testDataInv); TestUtils.assertEquals(m.subtract(m2),m2.scalarMultiply(new Fraction(-1)).add(m)); try { m.subtract(new Array2DRowFieldMatrix(testData2)); - fail("Expecting illegalArgumentException"); + Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } /** test multiply */ + @Test public void testMultiply() { Array2DRowFieldMatrix m = new Array2DRowFieldMatrix(testData); Array2DRowFieldMatrix mInv = new Array2DRowFieldMatrix(testDataInv); @@ -178,7 +179,7 @@ public final class FieldMatrixImplTest extends TestCase { TestUtils.assertEquals(m2.multiply(identity), m2); try { m.multiply(new Array2DRowFieldMatrix(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 m3 = new Array2DRowFieldMatrix(d3); FieldMatrix m4 = new Array2DRowFieldMatrix(d4); @@ -198,25 +200,28 @@ public final class FieldMatrixImplTest extends TestCase { } /** test trace */ + @Test public void testTrace() { FieldMatrix m = new Array2DRowFieldMatrix(id); - assertEquals("identity trace",new Fraction(3),m.getTrace()); + Assert.assertEquals("identity trace",new Fraction(3),m.getTrace()); m = new Array2DRowFieldMatrix(testData2); try { m.getTrace(); - fail("Expecting NonSquareMatrixException"); + Assert.fail("Expecting NonSquareMatrixException"); } catch (NonSquareMatrixException ex) { // ignored } } /** test sclarAdd */ + @Test public void testScalarAdd() { FieldMatrix m = new Array2DRowFieldMatrix(testData); TestUtils.assertEquals(new Array2DRowFieldMatrix(testDataPlus2), m.scalarAdd(new Fraction(2))); } /** test operate */ + @Test public void testOperate() { FieldMatrix m = new Array2DRowFieldMatrix(id); TestUtils.assertEquals(testVector, m.operate(testVector)); @@ -224,25 +229,27 @@ public final class FieldMatrixImplTest extends TestCase { m = new Array2DRowFieldMatrix(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 a = new Array2DRowFieldMatrix(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 m = new Array2DRowFieldMatrix(testData); FieldMatrix mIT = new FieldLUDecompositionImpl(m).getSolver().getInverse().transpose(); @@ -254,6 +261,7 @@ public final class FieldMatrixImplTest extends TestCase { } /** test preMultiply by vector */ + @Test public void testPremultiplyVector() { FieldMatrix m = new Array2DRowFieldMatrix(testData); TestUtils.assertEquals(m.preMultiply(testVector), preMultTest); @@ -262,12 +270,13 @@ public final class FieldMatrixImplTest extends TestCase { m = new Array2DRowFieldMatrix(bigSingular); try { m.preMultiply(testVector); - fail("expecting MathIllegalArgumentException"); + Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } + @Test public void testPremultiply() { FieldMatrix m3 = new Array2DRowFieldMatrix(d3); FieldMatrix m4 = new Array2DRowFieldMatrix(d4); @@ -283,42 +292,45 @@ public final class FieldMatrixImplTest extends TestCase { TestUtils.assertEquals(identity.preMultiply(mInv), mInv); try { m.preMultiply(new Array2DRowFieldMatrix(bigSingular)); - fail("Expecting illegalArgumentException"); + Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } + @Test public void testGetVectors() { FieldMatrix m = new Array2DRowFieldMatrix(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 m = new Array2DRowFieldMatrix(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 n = new Array2DRowFieldMatrix(matrixData2); // Now multiply m by n FieldMatrix 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 pInverse = new FieldLUDecompositionImpl(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 coefficients = new Array2DRowFieldMatrix(coefficientsData); Fraction[] constants = {new Fraction(1), new Fraction(-2), new Fraction(1)}; Fraction[] solution = new FieldLUDecompositionImpl(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 m = new Array2DRowFieldMatrix(subTestData); checkGetSubMatrix(m, subRows23Cols00, 2 , 3 , 0, 0); @@ -388,9 +401,9 @@ public final class FieldMatrixImplTest extends TestCase { try { FieldMatrix sub = m.getSubMatrix(startRow, endRow, startColumn, endColumn); if (reference != null) { - assertEquals(new Array2DRowFieldMatrix(reference), sub); + Assert.assertEquals(new Array2DRowFieldMatrix(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 sub = m.getSubMatrix(selectedRows, selectedColumns); if (reference != null) { - assertEquals(new Array2DRowFieldMatrix(reference), sub); + Assert.assertEquals(new Array2DRowFieldMatrix(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 m = new Array2DRowFieldMatrix(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(reference), new Array2DRowFieldMatrix(sub)); + Assert.assertEquals(new Array2DRowFieldMatrix(reference), new Array2DRowFieldMatrix(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(reference), new Array2DRowFieldMatrix(sub)); + Assert.assertEquals(new Array2DRowFieldMatrix(reference), new Array2DRowFieldMatrix(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 m = new Array2DRowFieldMatrix(subTestData); FieldMatrix mRow0 = new Array2DRowFieldMatrix(subRow0); FieldMatrix mRow3 = new Array2DRowFieldMatrix(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 m = new Array2DRowFieldMatrix(subTestData); FieldMatrix mRow3 = new Array2DRowFieldMatrix(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 m = new Array2DRowFieldMatrix(subTestData); FieldMatrix mColumn1 = new Array2DRowFieldMatrix(subColumn1); FieldMatrix mColumn3 = new Array2DRowFieldMatrix(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 m = new Array2DRowFieldMatrix(subTestData); FieldMatrix mColumn3 = new Array2DRowFieldMatrix(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 m = new Array2DRowFieldMatrix(subTestData); FieldVector mRow0 = new ArrayFieldVector(subRow0[0]); FieldVector mRow3 = new ArrayFieldVector(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 m = new Array2DRowFieldMatrix(subTestData); FieldVector mRow3 = new ArrayFieldVector(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(FractionField.getInstance(), 5)); - fail("Expecting MatrixDimensionMismatchException"); + Assert.fail("Expecting MatrixDimensionMismatchException"); } catch (MatrixDimensionMismatchException ex) { // expected } } + @Test public void testGetColumnVector() { FieldMatrix m = new Array2DRowFieldMatrix(subTestData); FieldVector mColumn1 = columnToVector(subColumn1); FieldVector 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 m = new Array2DRowFieldMatrix(subTestData); FieldVector 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(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(data, false); } + @Test public void testGetRow() { FieldMatrix m = new Array2DRowFieldMatrix(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 m = new Array2DRowFieldMatrix(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 m = new Array2DRowFieldMatrix(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 m = new Array2DRowFieldMatrix(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 m = new Array2DRowFieldMatrix(testData); Array2DRowFieldMatrix m1 = (Array2DRowFieldMatrix) m.copy(); Array2DRowFieldMatrix mt = (Array2DRowFieldMatrix) 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(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(bigSingular))); } + @Test public void testToString() { Array2DRowFieldMatrix m = new Array2DRowFieldMatrix(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(FractionField.getInstance()); - assertEquals("Array2DRowFieldMatrix{}", m.toString()); + Assert.assertEquals("Array2DRowFieldMatrix{}", m.toString()); } + @Test public void testSetSubMatrix() throws Exception { Array2DRowFieldMatrix m = new Array2DRowFieldMatrix(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 @@ -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 @@ -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 m2 = new Array2DRowFieldMatrix(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(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(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(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(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(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(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(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 m = new Array2DRowFieldMatrix(testData); - assertEquals(m,TestUtils.serializeAndRecover(m)); + Assert.assertEquals(m,TestUtils.serializeAndRecover(m)); } private static class SetVisitor extends DefaultFieldMatrixChangingVisitor { @@ -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; diff --git a/src/test/java/org/apache/commons/math/linear/LUDecompositionImplTest.java b/src/test/java/org/apache/commons/math/linear/LUDecompositionImplTest.java index a42f90f58..a7b08247d 100644 --- a/src/test/java/org/apache/commons/math/linear/LUDecompositionImplTest.java +++ b/src/test/java/org/apache/commons/math/linear/LUDecompositionImplTest.java @@ -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()); } - } diff --git a/src/test/java/org/apache/commons/math/linear/LUSolverTest.java b/src/test/java/org/apache/commons/math/linear/LUSolverTest.java index f73727dbf..61beccadd 100644 --- a/src/test/java/org/apache/commons/math/linear/LUSolverTest.java +++ b/src/test/java/org/apache/commons/math/linear/LUSolverTest.java @@ -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(); } - } diff --git a/src/test/java/org/apache/commons/math/exception/MatrixDimensionMismatchExceptionTest.java b/src/test/java/org/apache/commons/math/linear/MatrixDimensionMismatchExceptionTest.java similarity index 97% rename from src/test/java/org/apache/commons/math/exception/MatrixDimensionMismatchExceptionTest.java rename to src/test/java/org/apache/commons/math/linear/MatrixDimensionMismatchExceptionTest.java index 1f90f6c2e..22271a88c 100644 --- a/src/test/java/org/apache/commons/math/exception/MatrixDimensionMismatchExceptionTest.java +++ b/src/test/java/org/apache/commons/math/linear/MatrixDimensionMismatchExceptionTest.java @@ -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; diff --git a/src/test/java/org/apache/commons/math/linear/QRSolverTest.java b/src/test/java/org/apache/commons/math/linear/QRSolverTest.java index bcf3499f3..0d2d4a42d 100644 --- a/src/test/java/org/apache/commons/math/linear/QRSolverTest.java +++ b/src/test/java/org/apache/commons/math/linear/QRSolverTest.java @@ -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; } } diff --git a/src/test/java/org/apache/commons/math/linear/SparseFieldMatrixTest.java b/src/test/java/org/apache/commons/math/linear/SparseFieldMatrixTest.java index 79f831374..139ca73e2 100644 --- a/src/test/java/org/apache/commons/math/linear/SparseFieldMatrixTest.java +++ b/src/test/java/org/apache/commons/math/linear/SparseFieldMatrixTest.java @@ -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 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 m = createSparseMatrix(testData); SparseFieldMatrix 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 m1 = createSparseMatrix(testData); FieldMatrix m2 = m1.copy(); - assertEquals(m1.getClass(), m2.getClass()); - assertEquals((m2), m1); + Assert.assertEquals(m1.getClass(), m2.getClass()); + Assert.assertEquals((m2), m1); SparseFieldMatrix m3 = createSparseMatrix(testData); FieldMatrix 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 m = createSparseMatrix(testData); SparseFieldMatrix mInv = createSparseMatrix(testDataInv); @@ -163,7 +159,7 @@ public class SparseFieldMatrixTest extends TestCase { FieldMatrix 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 m = createSparseMatrix(testData); SparseFieldMatrix 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 m = createSparseMatrix(testData); SparseFieldMatrix 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 m = createSparseMatrix(testData); SparseFieldMatrix 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 m3 = createSparseMatrix(d3); FieldMatrix m4 = createSparseMatrix(d4); @@ -237,19 +237,21 @@ public class SparseFieldMatrixTest extends TestCase { } /** test trace */ + @Test public void testTrace() { FieldMatrix 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 m = createSparseMatrix(testData); assertClose("scalar add", createSparseMatrix(testDataPlus2), @@ -257,6 +259,7 @@ public class SparseFieldMatrixTest extends TestCase { } /** test operate */ + @Test public void testOperate() { FieldMatrix 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 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 m = createSparseMatrix(testData); FieldMatrix mIT = new FieldLUDecompositionImpl(m).getSolver().getInverse().transpose(); FieldMatrix mTI = new FieldLUDecompositionImpl(m.transpose()).getSolver().getInverse(); @@ -296,6 +300,7 @@ public class SparseFieldMatrixTest extends TestCase { } /** test preMultiply by vector */ + @Test public void testPremultiplyVector() { FieldMatrix 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 m3 = createSparseMatrix(d3); FieldMatrix 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 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 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 n = createSparseMatrix(matrixData2); // Now multiply m by n FieldMatrix 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 pInverse = new FieldLUDecompositionImpl(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 coefficients = createSparseMatrix(coefficientsData); Fraction[] constants = { new Fraction(1), new Fraction(-2), new Fraction(1) }; Fraction[] solution = new FieldLUDecompositionImpl(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 m = createSparseMatrix(subTestData); FieldMatrix mRows23Cols00 = createSparseMatrix(subRows23Cols00); @@ -408,135 +418,139 @@ public class SparseFieldMatrixTest extends TestCase { FieldMatrix mRows03Cols123 = createSparseMatrix(subRows03Cols123); FieldMatrix mRows20Cols123 = createSparseMatrix(subRows20Cols123); FieldMatrix 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 m = createSparseMatrix(subTestData); FieldMatrix mRow0 = createSparseMatrix(subRow0); FieldMatrix 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 m = createSparseMatrix(subTestData); FieldMatrix mColumn1 = createSparseMatrix(subColumn1); FieldMatrix 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 m = createSparseMatrix(subTestData); FieldVector mRow0 = new ArrayFieldVector(subRow0[0]); FieldVector mRow3 = new ArrayFieldVector(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 m = createSparseMatrix(subTestData); FieldVector mColumn1 = columnToVector(subColumn1); FieldVector 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(data, false); } + @Test public void testEqualsAndHashCode() { SparseFieldMatrix m = createSparseMatrix(testData); SparseFieldMatrix m1 = (SparseFieldMatrix) m.copy(); SparseFieldMatrix mt = (SparseFieldMatrix) 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 m = createSparseMatrix(testData); - assertEquals("SparseFieldMatrix{{1.0,2.0,3.0},{2.0,5.0,3.0},{1.0,0.0,8.0}}", + Assert.assertEquals("SparseFieldMatrix{{1.0,2.0,3.0},{2.0,5.0,3.0},{1.0,0.0,8.0}}", m.toString()); m = new SparseFieldMatrix(field, 1, 1); - assertEquals("SparseFieldMatrix{{0.0}}", m.toString()); + Assert.assertEquals("SparseFieldMatrix{{0.0}}", m.toString()); } */ + @Test public void testSetSubMatrix() throws Exception { SparseFieldMatrix m = createSparseMatrix(testData); m.setSubMatrix(detData2, 1, 1); FieldMatrix 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 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(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; } - } diff --git a/src/test/java/org/apache/commons/math/linear/SparseRealMatrixTest.java b/src/test/java/org/apache/commons/math/linear/SparseRealMatrixTest.java index 95c94bf76..24cb793d1 100644 --- a/src/test/java/org/apache/commons/math/linear/SparseRealMatrixTest.java +++ b/src/test/java/org/apache/commons/math/linear/SparseRealMatrixTest.java @@ -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; } - - } diff --git a/src/test/java/org/apache/commons/math/linear/TriDiagonalTransformerTest.java b/src/test/java/org/apache/commons/math/linear/TriDiagonalTransformerTest.java index f0a674d32..773087f4e 100644 --- a/src/test/java/org/apache/commons/math/linear/TriDiagonalTransformerTest.java +++ b/src/test/java/org/apache/commons/math/linear/TriDiagonalTransformerTest.java @@ -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()); } - } diff --git a/src/test/java/org/apache/commons/math/optimization/general/LevenbergMarquardtOptimizerTest.java b/src/test/java/org/apache/commons/math/optimization/general/LevenbergMarquardtOptimizerTest.java index 99031880c..13a3d22eb 100644 --- a/src/test/java/org/apache/commons/math/optimization/general/LevenbergMarquardtOptimizerTest.java +++ b/src/test/java/org/apache/commons/math/optimization/general/LevenbergMarquardtOptimizerTest.java @@ -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;