From 0b3c388aebe23cde0c7d3afbcd1171c4a8a3f5c5 Mon Sep 17 00:00:00 2001 From: Gilles Sadowski Date: Wed, 24 Nov 2010 00:42:12 +0000 Subject: [PATCH] MATH-425 Removed redundant exception "throws" clauses. Javadoc cleanup. Throwing "NoDataException" instead of "ZeroException" when row or column data is missing. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1038403 13f79535-47bb-0310-9956-ffa450edef68 --- .../math/linear/AbstractFieldMatrix.java | 44 ++-- .../math/linear/AbstractRealMatrix.java | 18 +- .../math/linear/AbstractRealVector.java | 15 +- .../math/linear/Array2DRowFieldMatrix.java | 213 +++++++----------- .../math/linear/Array2DRowRealMatrix.java | 68 +++--- .../commons/math/linear/ArrayRealVector.java | 138 ++++++------ .../commons/math/linear/BlockFieldMatrix.java | 10 +- .../commons/math/linear/BlockRealMatrix.java | 39 ++-- .../DefaultFieldMatrixChangingVisitor.java | 3 - .../DefaultFieldMatrixPreservingVisitor.java | 3 - .../DefaultRealMatrixChangingVisitor.java | 4 - .../DefaultRealMatrixPreservingVisitor.java | 4 - .../commons/math/linear/FieldMatrix.java | 2 - .../linear/FieldMatrixChangingVisitor.java | 2 - .../linear/FieldMatrixPreservingVisitor.java | 2 - .../commons/math/linear/MatrixUtils.java | 109 +++++---- .../math/linear/OpenMapRealMatrix.java | 6 +- .../commons/math/linear/RealMatrix.java | 4 - .../linear/RealMatrixChangingVisitor.java | 3 - .../linear/RealMatrixPreservingVisitor.java | 3 - .../linear/SingularValueDecomposition.java | 2 +- .../SingularValueDecompositionImpl.java | 131 ++++------- .../math/linear/Array2DRowRealMatrixTest.java | 18 +- .../math/linear/BlockRealMatrixTest.java | 18 +- .../math/linear/SparseRealMatrixTest.java | 6 +- 25 files changed, 343 insertions(+), 522 deletions(-) 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 dbcb9811a..7903524ca 100644 --- a/src/main/java/org/apache/commons/math/linear/AbstractFieldMatrix.java +++ b/src/main/java/org/apache/commons/math/linear/AbstractFieldMatrix.java @@ -37,11 +37,13 @@ import org.apache.commons.math.exception.util.LocalizedFormats; *

All the methods implemented here use {@link #getEntry(int, int)} to access * matrix elements. Derived class can provide faster implementations.

* - * @param the type of the field elements + * @param Type of the field elements. + * * @version $Revision$ $Date$ * @since 2.0 */ -public abstract class AbstractFieldMatrix> implements FieldMatrix { +public abstract class AbstractFieldMatrix> + implements FieldMatrix { /** Field to which the elements belong. */ private final Field field; @@ -170,15 +172,13 @@ public abstract class AbstractFieldMatrix> implements } /** {@inheritDoc} */ - public abstract FieldMatrix createMatrix(final int rowDimension, final int columnDimension) - throws IllegalArgumentException; + public abstract FieldMatrix createMatrix(final int rowDimension, final int columnDimension); /** {@inheritDoc} */ public abstract FieldMatrix copy(); /** {@inheritDoc} */ - public FieldMatrix add(FieldMatrix m) throws IllegalArgumentException { - + public FieldMatrix add(FieldMatrix m) { // safety check checkAdditionCompatible(m); @@ -192,12 +192,10 @@ public abstract class AbstractFieldMatrix> implements } return out; - } /** {@inheritDoc} */ - public FieldMatrix subtract(final FieldMatrix m) throws IllegalArgumentException { - + public FieldMatrix subtract(final FieldMatrix m) { // safety check checkSubtractionCompatible(m); @@ -211,7 +209,6 @@ public abstract class AbstractFieldMatrix> implements } return out; - } /** {@inheritDoc} */ @@ -227,12 +224,10 @@ public abstract class AbstractFieldMatrix> implements } return out; - } /** {@inheritDoc} */ public FieldMatrix scalarMultiply(final T d) { - final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final FieldMatrix out = createMatrix(rowCount, columnCount); @@ -243,13 +238,10 @@ public abstract class AbstractFieldMatrix> implements } return out; - } /** {@inheritDoc} */ - public FieldMatrix multiply(final FieldMatrix m) - throws IllegalArgumentException { - + public FieldMatrix multiply(final FieldMatrix m) { // safety check checkMultiplicationCompatible(m); @@ -268,18 +260,15 @@ public abstract class AbstractFieldMatrix> implements } return out; - } /** {@inheritDoc} */ - public FieldMatrix preMultiply(final FieldMatrix m) - throws IllegalArgumentException { + public FieldMatrix preMultiply(final FieldMatrix m) { return m.multiply(this); } /** {@inheritDoc} */ public T[][] getData() { - final T[][] data = buildArray(field, getRowDimension(), getColumnDimension()); for (int i = 0; i < data.length; ++i) { @@ -290,13 +279,11 @@ public abstract class AbstractFieldMatrix> implements } return data; - } /** {@inheritDoc} */ public FieldMatrix getSubMatrix(final int startRow, final int endRow, final int startColumn, final int endColumn) { - checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); final FieldMatrix subMatrix = @@ -635,8 +622,7 @@ public abstract class AbstractFieldMatrix> implements } /** {@inheritDoc} */ - public T[] operate(final T[] v) - throws IllegalArgumentException { + public T[] operate(final T[] v) { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); @@ -657,8 +643,7 @@ public abstract class AbstractFieldMatrix> implements } /** {@inheritDoc} */ - public FieldVector operate(final FieldVector v) - throws IllegalArgumentException { + public FieldVector operate(final FieldVector v) { try { return new ArrayFieldVector(operate(((ArrayFieldVector) v).getDataRef()), false); } catch (ClassCastException cce) { @@ -682,8 +667,7 @@ public abstract class AbstractFieldMatrix> implements } /** {@inheritDoc} */ - public T[] preMultiply(final T[] v) - throws IllegalArgumentException { + public T[] preMultiply(final T[] v) { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); @@ -704,12 +688,10 @@ public abstract class AbstractFieldMatrix> implements } /** {@inheritDoc} */ - public FieldVector preMultiply(final FieldVector v) - throws IllegalArgumentException { + public FieldVector preMultiply(final FieldVector v) { try { return new ArrayFieldVector(preMultiply(((ArrayFieldVector) v).getDataRef()), false); } catch (ClassCastException cce) { - final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (v.getDimension() != nRows) { 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 df4d05818..1c45990a7 100644 --- a/src/main/java/org/apache/commons/math/linear/AbstractRealMatrix.java +++ b/src/main/java/org/apache/commons/math/linear/AbstractRealMatrix.java @@ -57,8 +57,7 @@ public abstract class AbstractRealMatrix implements RealMatrix { } /** {@inheritDoc} */ - public abstract RealMatrix createMatrix(final int rowDimension, final int columnDimension) - throws IllegalArgumentException; + public abstract RealMatrix createMatrix(final int rowDimension, final int columnDimension); /** {@inheritDoc} */ public abstract RealMatrix copy(); @@ -271,7 +270,6 @@ public abstract class AbstractRealMatrix implements RealMatrix { public void copySubMatrix(final int startRow, final int endRow, final int startColumn, final int endColumn, final double[][] destination) { - // safety checks MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); final int rowsCount = endRow + 1 - startRow; @@ -310,7 +308,6 @@ public abstract class AbstractRealMatrix implements RealMatrix { /** {@inheritDoc} */ public void copySubMatrix(int[] selectedRows, int[] selectedColumns, double[][] destination) { - // safety checks MatrixUtils.checkSubMatrixIndex(this, selectedRows, selectedColumns); if ((destination.length < selectedRows.length) || @@ -552,9 +549,7 @@ public abstract class AbstractRealMatrix implements RealMatrix { } /** {@inheritDoc} */ - public double[] operate(final double[] v) - throws IllegalArgumentException { - + public double[] operate(final double[] v) { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (v.length != nCols) { @@ -574,8 +569,7 @@ public abstract class AbstractRealMatrix implements RealMatrix { } /** {@inheritDoc} */ - public RealVector operate(final RealVector v) - throws IllegalArgumentException { + public RealVector operate(final RealVector v) { try { return new ArrayRealVector(operate(((ArrayRealVector) v).getDataRef()), false); } catch (ClassCastException cce) { @@ -599,8 +593,7 @@ public abstract class AbstractRealMatrix implements RealMatrix { } /** {@inheritDoc} */ - public double[] preMultiply(final double[] v) - throws IllegalArgumentException { + public double[] preMultiply(final double[] v) { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); @@ -621,8 +614,7 @@ public abstract class AbstractRealMatrix implements RealMatrix { } /** {@inheritDoc} */ - public RealVector preMultiply(final RealVector v) - throws IllegalArgumentException { + public RealVector preMultiply(final RealVector v) { try { return new ArrayRealVector(preMultiply(((ArrayRealVector) v).getDataRef()), false); } catch (ClassCastException cce) { diff --git a/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java b/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java index 0679c98ec..8bd853c6e 100644 --- a/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java +++ b/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java @@ -39,9 +39,10 @@ public abstract class AbstractRealVector implements RealVector { /** * Check if instance and specified vectors have the same dimension. - * @param v vector to compare instance with - * @exception IllegalArgumentException if the vectors do not - * have the same dimension + * + * @param v Vector to compare instance with. + * @throws DimensionMismatchException if the vectors do not + * have the same dimension. */ protected void checkVectorDimensions(RealVector v) { checkVectorDimensions(v.getDimension()); @@ -50,12 +51,11 @@ public abstract class AbstractRealVector implements RealVector { /** * Check if instance dimension is equal to some expected value. * - * @param n expected dimension. + * @param n Expected dimension. * @throws DimensionMismatchException if the dimension is - * inconsistent with vector size + * inconsistent with the vector size. */ - protected void checkVectorDimensions(int n) - throws DimensionMismatchException { + protected void checkVectorDimensions(int n) { int d = getDimension(); if (d != n) { throw new DimensionMismatchException(d, n); @@ -823,5 +823,4 @@ public abstract class AbstractRealVector implements RealVector { throw new MathUnsupportedOperationException(); } } - } 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 9f3786a7e..ac9a655eb 100644 --- a/src/main/java/org/apache/commons/math/linear/Array2DRowFieldMatrix.java +++ b/src/main/java/org/apache/commons/math/linear/Array2DRowFieldMatrix.java @@ -21,8 +21,9 @@ import java.io.Serializable; import org.apache.commons.math.Field; import org.apache.commons.math.FieldElement; -import org.apache.commons.math.MathRuntimeException; -import org.apache.commons.math.exception.MathUserException; +import org.apache.commons.math.exception.NoDataException; +import org.apache.commons.math.exception.DimensionMismatchException; +import org.apache.commons.math.exception.MathIllegalStateException; import org.apache.commons.math.exception.util.LocalizedFormats; /** @@ -36,11 +37,11 @@ import org.apache.commons.math.exception.util.LocalizedFormats; * @param the type of the field elements * @version $Revision$ $Date$ */ -public class Array2DRowFieldMatrix> extends AbstractFieldMatrix implements Serializable { - +public class Array2DRowFieldMatrix> + extends AbstractFieldMatrix + implements Serializable { /** Serializable version identifier */ private static final long serialVersionUID = 7260756672015356458L; - /** Entries of the matrix */ protected T[][] data; @@ -53,57 +54,57 @@ public class Array2DRowFieldMatrix> extends AbstractFi } /** - * Create a new FieldMatrix with the supplied row and column dimensions. + * Create a new {@code FieldMatrix} with the supplied row and column dimensions. * - * @param field field to which the elements belong - * @param rowDimension the number of rows in the new matrix - * @param columnDimension the number of columns in the new matrix - * @throws IllegalArgumentException if row or column dimension is not - * positive + * @param field Field to which the elements belong. + * @param rowDimension Number of rows in the new matrix. + * @param columnDimension Number of columns in the new matrix. + * @throws org.apache.commons.math.exception.NotStrictlyPositiveException + * if row or column dimension is not positive. */ public Array2DRowFieldMatrix(final Field field, - final int rowDimension, final int columnDimension) - throws IllegalArgumentException { + final int rowDimension, + final int columnDimension) { super(field, rowDimension, columnDimension); data = buildArray(field, rowDimension, columnDimension); } /** - * Create a new FieldMatrix using the input array as the underlying + * Create a new {@code FieldMatrix} using the input array as the underlying * data array. *

The input array is copied, not referenced. This constructor has * the same effect as calling {@link #Array2DRowFieldMatrix(FieldElement[][], boolean)} - * with the second argument set to true.

+ * with the second argument set to {@code true}.

* - * @param d data for new matrix - * @throws IllegalArgumentException if d is not rectangular - * (not all rows have the same length) or empty - * @throws NullPointerException if d is null + * @param d Data for the new matrix. + * @throws DimensionMismatchException if {@code d} is not rectangular. + * @throws org.apache.commons.math.exception.NullArgumentException if + * {@code d} is {@code null}. + * @throws NoDataException if there are not at least one row and one column. * @see #Array2DRowFieldMatrix(FieldElement[][], boolean) */ - public Array2DRowFieldMatrix(final T[][] d) - throws IllegalArgumentException, NullPointerException { + public Array2DRowFieldMatrix(final T[][] d) { super(extractField(d)); copyIn(d); } /** - * Create a new FieldMatrix using the input array as the underlying + * Create a new {@code FieldMatrix} using the input array as the underlying * data array. *

If an array is built specially in order to be embedded in a - * FieldMatrix and not used directly, the copyArray may be - * set to false} and not used directly, the {@code copyArray} may be + * set to {@code false}. This will prevent the copying and improve * performance as no new array will be built and no data will be copied.

- * @param d data for new matrix - * @param copyArray if true, the input array will be copied, otherwise - * it will be referenced - * @throws IllegalArgumentException if d is not rectangular - * (not all rows have the same length) or empty - * @throws NullPointerException if d is null + * + * @param d Data for the new matrix. + * @param copyArray Whether to copy or reference the input array. + * @throws DimensionMismatchException if {@code d} is not rectangular. + * @throws NoDataException if there are not at least one row and one column. + * @throws org.apache.commons.math.exception.NullArgumentException + * if {@code d} is {@code null}. * @see #Array2DRowFieldMatrix(FieldElement[][]) */ - public Array2DRowFieldMatrix(final T[][] d, final boolean copyArray) - throws IllegalArgumentException, NullPointerException { + public Array2DRowFieldMatrix(final T[][] d, final boolean copyArray) { super(extractField(d)); if (copyArray) { copyIn(d); @@ -113,18 +114,15 @@ public class Array2DRowFieldMatrix> extends AbstractFi } final int nRows = d.length; if (nRows == 0) { - throw MathRuntimeException.createIllegalArgumentException( - LocalizedFormats.AT_LEAST_ONE_ROW); + throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_ROW); } final int nCols = d[0].length; if (nCols == 0) { - throw MathRuntimeException.createIllegalArgumentException( - LocalizedFormats.AT_LEAST_ONE_COLUMN); + throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN); } for (int r = 1; r < nRows; r++) { if (d[r].length != nCols) { - throw MathRuntimeException.createIllegalArgumentException( - LocalizedFormats.DIFFERENT_ROWS_LENGTHS, nCols, d[r].length); + throw new DimensionMismatchException(nCols, d[r].length); } } data = d; @@ -132,12 +130,11 @@ public class Array2DRowFieldMatrix> extends AbstractFi } /** - * Create a new (column) FieldMatrix using v as the - * data for the unique column of the v.length x 1 matrix - * created. - *

The input array is copied, not referenced.

+ * Create a new (column) {@code FieldMatrix} using {@code v} as the + * data for the unique column of the created matrix. + * The input array is copied. * - * @param v column vector holding data for new matrix + * @param v Column vector holding data for new matrix. */ public Array2DRowFieldMatrix(final T[] v) { super(extractField(v)); @@ -150,8 +147,7 @@ public class Array2DRowFieldMatrix> extends AbstractFi /** {@inheritDoc} */ @Override - public FieldMatrix createMatrix(final int rowDimension, final int columnDimension) - throws IllegalArgumentException { + public FieldMatrix createMatrix(final int rowDimension, final int columnDimension) { return new Array2DRowFieldMatrix(getField(), rowDimension, columnDimension); } @@ -161,27 +157,15 @@ public class Array2DRowFieldMatrix> extends AbstractFi return new Array2DRowFieldMatrix(copyOut(), false); } - /** {@inheritDoc} */ - @Override - public FieldMatrix add(final FieldMatrix m) - throws IllegalArgumentException { - try { - return add((Array2DRowFieldMatrix) m); - } catch (ClassCastException cce) { - return super.add(m); - } - } - /** - * Compute the sum of this and m. + * Add {@code m} to this matrix. * - * @param m matrix to be added - * @return this + m - * @throws IllegalArgumentException if m is not the same size as this + * @param m Matrix to be added. + * @return {@code this} + m. + * @throws org.apache.commons.math.exception.MatrixDimensionMismatchException + * if {@code m} is not the same size as this matrix. */ - public Array2DRowFieldMatrix add(final Array2DRowFieldMatrix m) - throws IllegalArgumentException { - + public Array2DRowFieldMatrix add(final Array2DRowFieldMatrix m) { // safety check checkAdditionCompatible(m); @@ -198,30 +182,17 @@ public class Array2DRowFieldMatrix> extends AbstractFi } return new Array2DRowFieldMatrix(outData, false); - - } - - /** {@inheritDoc} */ - @Override - public FieldMatrix subtract(final FieldMatrix m) - throws IllegalArgumentException { - try { - return subtract((Array2DRowFieldMatrix) m); - } catch (ClassCastException cce) { - return super.subtract(m); - } } /** - * Compute this minus m. + * Subtract {@code m} from this matrix. * - * @param m matrix to be subtracted - * @return this + m - * @throws IllegalArgumentException if m is not the same size as this + * @param m Matrix to be subtracted. + * @return {@code this} + m. + * @throws org.apache.commons.math.exception.MatrixDimensionMismatchException + * if {@code m} is not the same size as this matrix. */ - public Array2DRowFieldMatrix subtract(final Array2DRowFieldMatrix m) - throws IllegalArgumentException { - + public Array2DRowFieldMatrix subtract(final Array2DRowFieldMatrix m) { // safety check checkSubtractionCompatible(m); @@ -241,27 +212,15 @@ public class Array2DRowFieldMatrix> extends AbstractFi } - /** {@inheritDoc} */ - @Override - public FieldMatrix multiply(final FieldMatrix m) - throws IllegalArgumentException { - try { - return multiply((Array2DRowFieldMatrix) m); - } catch (ClassCastException cce) { - return super.multiply(m); - } - } - /** - * Returns the result of postmultiplying this by m. - * @param m matrix to postmultiply by - * @return this*m - * @throws IllegalArgumentException - * if columnDimension(this) != rowDimension(m) + * Postmultiplying this matrix by {@code m}. + * + * @param m Matrix to postmultiply by. + * @return {@code this} * m. + * @throws DimensionMismatchException if the number of columns of this + * matrix is not equal to the number of rows of {@code m}. */ - public Array2DRowFieldMatrix multiply(final Array2DRowFieldMatrix m) - throws IllegalArgumentException { - + public Array2DRowFieldMatrix multiply(final Array2DRowFieldMatrix m) { // safety check checkMultiplicationCompatible(m); @@ -292,11 +251,10 @@ public class Array2DRowFieldMatrix> extends AbstractFi } /** - * Returns a reference to the underlying data array. - *

- * Does not make a fresh copy of the underlying data.

+ * Get a reference to the underlying data array. + * This methods returns internal data, not fresh copy of it. * - * @return 2-dimensional array of entries + * @return the 2-dimensional array of entries. */ public T[][] getDataRef() { return data; @@ -307,29 +265,24 @@ public class Array2DRowFieldMatrix> extends AbstractFi public void setSubMatrix(final T[][] subMatrix, final int row, final int column) { if (data == null) { if (row > 0) { - throw MathRuntimeException.createIllegalStateException( - LocalizedFormats.FIRST_ROWS_NOT_INITIALIZED_YET, row); + throw new MathIllegalStateException(LocalizedFormats.FIRST_ROWS_NOT_INITIALIZED_YET, row); } if (column > 0) { - throw MathRuntimeException.createIllegalStateException( - LocalizedFormats.FIRST_COLUMNS_NOT_INITIALIZED_YET, column); + throw new MathIllegalStateException(LocalizedFormats.FIRST_COLUMNS_NOT_INITIALIZED_YET, column); } final int nRows = subMatrix.length; if (nRows == 0) { - throw MathRuntimeException.createIllegalArgumentException( - LocalizedFormats.AT_LEAST_ONE_ROW); + throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_ROW); } final int nCols = subMatrix[0].length; if (nCols == 0) { - throw MathRuntimeException.createIllegalArgumentException( - LocalizedFormats.AT_LEAST_ONE_COLUMN); + throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN); } data = buildArray(getField(), subMatrix.length, nCols); for (int i = 0; i < data.length; ++i) { if (subMatrix[i].length != nCols) { - throw MathRuntimeException.createIllegalArgumentException( - LocalizedFormats.DIFFERENT_ROWS_LENGTHS, nCols, subMatrix[i].length); + throw new DimensionMismatchException(nCols, subMatrix[i].length); } System.arraycopy(subMatrix[i], 0, data[i + row], column, nCols); } @@ -389,13 +342,11 @@ public class Array2DRowFieldMatrix> extends AbstractFi /** {@inheritDoc} */ @Override - public T[] operate(final T[] v) - throws IllegalArgumentException { + public T[] operate(final T[] v) { final int nRows = this.getRowDimension(); final int nCols = this.getColumnDimension(); if (v.length != nCols) { - throw MathRuntimeException.createIllegalArgumentException( - LocalizedFormats.VECTOR_LENGTH_MISMATCH, v.length, nCols); + throw new DimensionMismatchException(v.length, nCols); } final T[] out = buildArray(getField(), nRows); for (int row = 0; row < nRows; row++) { @@ -411,14 +362,11 @@ public class Array2DRowFieldMatrix> extends AbstractFi /** {@inheritDoc} */ @Override - public T[] preMultiply(final T[] v) - throws IllegalArgumentException { - + public T[] preMultiply(final T[] v) { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (v.length != nRows) { - throw MathRuntimeException.createIllegalArgumentException( - LocalizedFormats.VECTOR_LENGTH_MISMATCH, v.length, nRows); + throw new DimensionMismatchException(v.length, nRows); } final T[] out = buildArray(getField(), nCols); @@ -431,7 +379,6 @@ public class Array2DRowFieldMatrix> extends AbstractFi } return out; - } /** {@inheritDoc} */ @@ -561,7 +508,7 @@ public class Array2DRowFieldMatrix> extends AbstractFi } /** - * Returns a fresh copy of the underlying data array. + * Get a fresh copy of the underlying data array. * * @return a copy of the underlying data array. */ @@ -576,17 +523,15 @@ public class Array2DRowFieldMatrix> extends AbstractFi } /** - * Replaces data with a fresh copy of the input array. - *

- * Verifies that the input array is rectangular and non-empty.

+ * Replace data with a fresh copy of the input array. * - * @param in data to copy in - * @throws IllegalArgumentException if input array is empty or not - * rectangular - * @throws NullPointerException if input array is null + * @param in Data to copy. + * @throws NoDataException if the input array is empty. + * @throws DimensionMismatchException if the input array is not rectangular. + * @throws org.apache.commons.math.exception.NullArgumentException if + * the input array is {@code null}. */ private void copyIn(final T[][] in) { setSubMatrix(in, 0, 0); } - } 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 1c868352e..db61eab24 100644 --- a/src/main/java/org/apache/commons/math/linear/Array2DRowRealMatrix.java +++ b/src/main/java/org/apache/commons/math/linear/Array2DRowRealMatrix.java @@ -21,7 +21,7 @@ import java.io.Serializable; import org.apache.commons.math.exception.DimensionMismatchException; import org.apache.commons.math.exception.NullArgumentException; -import org.apache.commons.math.exception.ZeroException; +import org.apache.commons.math.exception.NoDataException; import org.apache.commons.math.exception.MathIllegalStateException; import org.apache.commons.math.exception.util.LocalizedFormats; @@ -67,32 +67,30 @@ public class Array2DRowRealMatrix extends AbstractRealMatrix implements Serializ /** * Create a new RealMatrix with the supplied row and column dimensions. * - * @param rowDimension the number of rows in the new matrix - * @param columnDimension the number of columns in the new matrix - * @throws IllegalArgumentException if row or column dimension is not - * positive + * @param rowDimension Number of rows in the new matrix. + * @param columnDimension Number of columns in the new matrix. + * @throws org.apache.commons.math.exception.NotStrictlyPositiveException + * if the row or column dimension is not positive. */ - public Array2DRowRealMatrix(final int rowDimension, final int columnDimension) - throws IllegalArgumentException { + public Array2DRowRealMatrix(final int rowDimension, final int columnDimension) { super(rowDimension, columnDimension); data = new double[rowDimension][columnDimension]; } /** - * Create a new RealMatrix using the input array as the underlying + * Create a new {@code RealMatrix} using the input array as the underlying * data array. *

The input array is copied, not referenced. This constructor has * the same effect as calling {@link #Array2DRowRealMatrix(double[][], boolean)} - * with the second argument set to true.

+ * with the second argument set to {@code true}.

* - * @param d data for new matrix - * @throws IllegalArgumentException if d is not rectangular - * (not all rows have the same length) or empty - * @throws NullPointerException if d is null + * @param d Data for the new matrix. + * @throws DimensionMismatchException if {@code d} is not rectangular. + * @throws NoDataException if {@code d} row or colum dimension is zero. + * @throws NullPointerException if {@code d} is {@code null}. * @see #Array2DRowRealMatrix(double[][], boolean) */ - public Array2DRowRealMatrix(final double[][] d) - throws IllegalArgumentException, NullPointerException { + public Array2DRowRealMatrix(final double[][] d) { copyIn(d); } @@ -110,7 +108,7 @@ public class Array2DRowRealMatrix extends AbstractRealMatrix implements Serializ * @throws DimensionMismatchException if {@code d} is not rectangular * (not all rows have the same length) or empty. * @throws NullArgumentException if {@code d} is {@code null}. - * @throws ZeroException if there are not at least one row and one column. + * @throws NoDataException if there are not at least one row and one column. * @see #Array2DRowRealMatrix(double[][]) */ public Array2DRowRealMatrix(final double[][] d, final boolean copyArray) { @@ -122,11 +120,11 @@ public class Array2DRowRealMatrix extends AbstractRealMatrix implements Serializ } final int nRows = d.length; if (nRows == 0) { - throw new ZeroException(LocalizedFormats.AT_LEAST_ONE_ROW); + throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_ROW); } final int nCols = d[0].length; if (nCols == 0) { - throw new ZeroException(LocalizedFormats.AT_LEAST_ONE_COLUMN); + throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN); } for (int r = 1; r < nRows; r++) { if (d[r].length != nCols) { @@ -139,9 +137,8 @@ public class Array2DRowRealMatrix extends AbstractRealMatrix implements Serializ /** * Create a new (column) RealMatrix using {@code v} as the - * data for the unique column of the {@code v.length x 1} matrix - * created. - * The input array is copied, not referenced. + * data for the unique column of the created matrix. + * The input array is copied. * * @param v Column vector holding data for new matrix. */ @@ -221,13 +218,12 @@ public class Array2DRowRealMatrix extends AbstractRealMatrix implements Serializ } /** - * Returns the result of postmultiplying this matrix with {@code m}. + * Postmultiplying this matrix by {@code m}. * * @param m Matrix to postmultiply by. * @return {@code this} * m. - * @throws org.apache.commons.math.exception.MatrixDimensionMismatchException - * if the column dimension of this matrix is different from the row - * dimension of {@code m}. + * @throws DimensionMismatchException if the number of columns of this + * matrix is not equal to the number of rows of {@code m}. */ public Array2DRowRealMatrix multiply(final Array2DRowRealMatrix m) { // Safety check. @@ -260,7 +256,7 @@ public class Array2DRowRealMatrix extends AbstractRealMatrix implements Serializ } /** - * Return a reference to the underlying data array. + * Get a reference to the underlying data array. * * @return 2-dimensional array of entries. */ @@ -281,12 +277,12 @@ public class Array2DRowRealMatrix extends AbstractRealMatrix implements Serializ } final int nRows = subMatrix.length; if (nRows == 0) { - throw new ZeroException(LocalizedFormats.AT_LEAST_ONE_ROW); + throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_ROW); } final int nCols = subMatrix[0].length; if (nCols == 0) { - throw new ZeroException(LocalizedFormats.AT_LEAST_ONE_COLUMN); + throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN); } data = new double[subMatrix.length][nCols]; for (int i = 0; i < data.length; ++i) { @@ -510,7 +506,7 @@ public class Array2DRowRealMatrix extends AbstractRealMatrix implements Serializ } /** - * Returns a fresh copy of the underlying data array. + * Get a fresh copy of the underlying data array. * * @return a copy of the underlying data array. */ @@ -525,17 +521,15 @@ public class Array2DRowRealMatrix extends AbstractRealMatrix implements Serializ } /** - * Replaces data with a fresh copy of the input array. - *

- * Verifies that the input array is rectangular and non-empty.

+ * Replace data with a fresh copy of the input array. * - * @param in data to copy in - * @throws IllegalArgumentException if input array is empty or not - * rectangular - * @throws NullPointerException if input array is null + * @param in Data to copy. + * @throws NoDataException if the input array is empty. + * @throws DimensionMismatchException if the input array is not rectangular. + * @throws org.apache.commons.math.exception.NullArgumentException if + * the input array is {@code null}. */ private void copyIn(final double[][] in) { setSubMatrix(in, 0, 0); } - } diff --git a/src/main/java/org/apache/commons/math/linear/ArrayRealVector.java b/src/main/java/org/apache/commons/math/linear/ArrayRealVector.java index 18f8da01b..888cd957e 100644 --- a/src/main/java/org/apache/commons/math/linear/ArrayRealVector.java +++ b/src/main/java/org/apache/commons/math/linear/ArrayRealVector.java @@ -200,8 +200,8 @@ public class ArrayRealVector extends AbstractRealVector implements Serializable /** * Construct a vector by appending one vector to another vector. - * @param v1 first vector (will be put in front of the new vector) - * @param v2 second vector (will be put at back of the new vector) + * @param v1 First vector (will be put in front of the new vector). + * @param v2 Second vector (will be put at back of the new vector). */ public ArrayRealVector(ArrayRealVector v1, ArrayRealVector v2) { data = new double[v1.data.length + v2.data.length]; @@ -211,8 +211,8 @@ public class ArrayRealVector extends AbstractRealVector implements Serializable /** * Construct a vector by appending one vector to another vector. - * @param v1 first vector (will be put in front of the new vector) - * @param v2 second vector (will be put at back of the new vector) + * @param v1 First vector (will be put in front of the new vector). + * @param v2 Second vector (will be put at back of the new vector). */ public ArrayRealVector(ArrayRealVector v1, RealVector v2) { final int l1 = v1.data.length; @@ -226,8 +226,8 @@ public class ArrayRealVector extends AbstractRealVector implements Serializable /** * Construct a vector by appending one vector to another vector. - * @param v1 first vector (will be put in front of the new vector) - * @param v2 second vector (will be put at back of the new vector) + * @param v1 First vector (will be put in front of the new vector). + * @param v2 Second vector (will be put at back of the new vector). */ public ArrayRealVector(RealVector v1, ArrayRealVector v2) { final int l1 = v1.getDimension(); @@ -241,8 +241,8 @@ public class ArrayRealVector extends AbstractRealVector implements Serializable /** * Construct a vector by appending one vector to another vector. - * @param v1 first vector (will be put in front of the new vector) - * @param v2 second vector (will be put at back of the new vector) + * @param v1 First vector (will be put in front of the new vector). + * @param v2 Second vector (will be put at back of the new vector). */ public ArrayRealVector(ArrayRealVector v1, double[] v2) { final int l1 = v1.getDimension(); @@ -254,8 +254,8 @@ public class ArrayRealVector extends AbstractRealVector implements Serializable /** * Construct a vector by appending one vector to another vector. - * @param v1 first vector (will be put in front of the new vector) - * @param v2 second vector (will be put at back of the new vector) + * @param v1 First vector (will be put in front of the new vector). + * @param v2 Second vector (will be put at back of the new vector). */ public ArrayRealVector(double[] v1, ArrayRealVector v2) { final int l1 = v1.length; @@ -286,8 +286,7 @@ public class ArrayRealVector extends AbstractRealVector implements Serializable /** {@inheritDoc} */ @Override - public RealVector add(RealVector v) - throws IllegalArgumentException { + public RealVector add(RealVector v) { if (v instanceof ArrayRealVector) { return add((ArrayRealVector) v); } else { @@ -304,8 +303,7 @@ public class ArrayRealVector extends AbstractRealVector implements Serializable /** {@inheritDoc} */ @Override - public RealVector add(double[] v) - throws IllegalArgumentException { + public RealVector add(double[] v) { checkVectorDimensions(v.length); double[] out = data.clone(); for (int i = 0; i < data.length; i++) { @@ -315,20 +313,20 @@ public class ArrayRealVector extends AbstractRealVector implements Serializable } /** - * Compute the sum of this and v. - * @param v vector to be added - * @return this + v - * @throws IllegalArgumentException if v is not the same size as this + * Add {@code v} to this vector. + * + * @param v Vector to be added + * @return {@code this} + v. + * @throws DimensionMismatchException if {@code v} is not the same + * size as this vector. */ - public ArrayRealVector add(ArrayRealVector v) - throws IllegalArgumentException { + public ArrayRealVector add(ArrayRealVector v) { return (ArrayRealVector) add(v.data); } /** {@inheritDoc} */ @Override - public RealVector subtract(RealVector v) - throws IllegalArgumentException { + public RealVector subtract(RealVector v) { if (v instanceof ArrayRealVector) { return subtract((ArrayRealVector) v); } else { @@ -345,8 +343,7 @@ public class ArrayRealVector extends AbstractRealVector implements Serializable /** {@inheritDoc} */ @Override - public RealVector subtract(double[] v) - throws IllegalArgumentException { + public RealVector subtract(double[] v) { checkVectorDimensions(v.length); double[] out = data.clone(); for (int i = 0; i < data.length; i++) { @@ -356,13 +353,14 @@ public class ArrayRealVector extends AbstractRealVector implements Serializable } /** - * Compute this minus v. - * @param v vector to be subtracted - * @return this + v - * @throws IllegalArgumentException if v is not the same size as this + * Subtract {@code v} from this vector. + * + * @param v Vector to be subtracted. + * @return {@code this} - v. + * @throws DimensionMismatchException if {@code v} is not the + * same size as this vector. */ - public ArrayRealVector subtract(ArrayRealVector v) - throws IllegalArgumentException { + public ArrayRealVector subtract(ArrayRealVector v) { return (ArrayRealVector) subtract(v.data); } @@ -619,8 +617,7 @@ public class ArrayRealVector extends AbstractRealVector implements Serializable } /** {@inheritDoc} */ - public RealVector ebeMultiply(RealVector v) - throws IllegalArgumentException { + public RealVector ebeMultiply(RealVector v) { if (v instanceof ArrayRealVector) { return ebeMultiply((ArrayRealVector) v); } else { @@ -635,8 +632,7 @@ public class ArrayRealVector extends AbstractRealVector implements Serializable /** {@inheritDoc} */ @Override - public RealVector ebeMultiply(double[] v) - throws IllegalArgumentException { + public RealVector ebeMultiply(double[] v) { checkVectorDimensions(v.length); double[] out = data.clone(); for (int i = 0; i < data.length; i++) { @@ -647,18 +643,17 @@ public class ArrayRealVector extends AbstractRealVector implements Serializable /** * Element-by-element multiplication. - * @param v vector by which instance elements must be multiplied - * @return a vector containing this[i] * v[i] for all i - * @exception IllegalArgumentException if v is not the same size as this + * @param v Vector by which instance elements must be multiplied. + * @return a Vector containing {@code this[i] * v[i]} for all {@code i}. + * @exception DimensionMismatchException if {@code v} is not the same + * size as this vector. */ - public ArrayRealVector ebeMultiply(ArrayRealVector v) - throws IllegalArgumentException { + public ArrayRealVector ebeMultiply(ArrayRealVector v) { return (ArrayRealVector) ebeMultiply(v.data); } /** {@inheritDoc} */ - public RealVector ebeDivide(RealVector v) - throws IllegalArgumentException { + public RealVector ebeDivide(RealVector v) { if (v instanceof ArrayRealVector) { return ebeDivide((ArrayRealVector) v); } else { @@ -673,8 +668,7 @@ public class ArrayRealVector extends AbstractRealVector implements Serializable /** {@inheritDoc} */ @Override - public RealVector ebeDivide(double[] v) - throws IllegalArgumentException { + public RealVector ebeDivide(double[] v) { checkVectorDimensions(v.length); double[] out = data.clone(); for (int i = 0; i < data.length; i++) { @@ -685,12 +679,13 @@ public class ArrayRealVector extends AbstractRealVector implements Serializable /** * Element-by-element division. - * @param v vector by which instance elements must be divided - * @return a vector containing this[i] / v[i] for all i - * @throws IllegalArgumentException if v is not the same size as this + * + * @param v Vector by which instance elements must be divided. + * @return a vector containing {@code this[i] / v[i]} for all {@code i}. + * @exception DimensionMismatchException if {@code v} is not the same + * size as this vector. */ - public ArrayRealVector ebeDivide(ArrayRealVector v) - throws IllegalArgumentException { + public ArrayRealVector ebeDivide(ArrayRealVector v) { return (ArrayRealVector) ebeDivide(v.data); } @@ -701,9 +696,10 @@ public class ArrayRealVector extends AbstractRealVector implements Serializable } /** - * Returns a reference to the underlying data array. - *

Does not make a fresh copy of the underlying data.

- * @return array of entries + * Get a reference to the underlying data array. + * This method does not make a fresh copy of the underlying data. + * + * @return the array of entries. */ public double[] getDataRef() { return data; @@ -711,8 +707,7 @@ public class ArrayRealVector extends AbstractRealVector implements Serializable /** {@inheritDoc} */ @Override - public double dotProduct(RealVector v) - throws IllegalArgumentException { + public double dotProduct(RealVector v) { if (v instanceof ArrayRealVector) { return dotProduct((ArrayRealVector) v); } else { @@ -729,8 +724,7 @@ public class ArrayRealVector extends AbstractRealVector implements Serializable /** {@inheritDoc} */ @Override - public double dotProduct(double[] v) - throws IllegalArgumentException { + public double dotProduct(double[] v) { checkVectorDimensions(v.length); double dot = 0; for (int i = 0; i < data.length; i++) { @@ -783,8 +777,7 @@ public class ArrayRealVector extends AbstractRealVector implements Serializable /** {@inheritDoc} */ @Override - public double getDistance(RealVector v) - throws IllegalArgumentException { + public double getDistance(RealVector v) { if (v instanceof ArrayRealVector) { return getDistance((ArrayRealVector) v); } else { @@ -800,8 +793,7 @@ public class ArrayRealVector extends AbstractRealVector implements Serializable /** {@inheritDoc} */ @Override - public double getDistance(double[] v) - throws IllegalArgumentException { + public double getDistance(double[] v) { checkVectorDimensions(v.length); double sum = 0; for (int i = 0; i < data.length; ++i) { @@ -817,7 +809,7 @@ public class ArrayRealVector extends AbstractRealVector implements Serializable * L2 norm, i.e. the square root of the sum of * elements differences, or euclidian distance. * - * @param v vector to which distance is requested + * @param v Vector to which distance is requested. * @return the distance between two vectors. * @throws DimensionMismatchException if {@code v} is not the same size as * this vector. @@ -848,8 +840,7 @@ public class ArrayRealVector extends AbstractRealVector implements Serializable /** {@inheritDoc} */ @Override - public double getL1Distance(double[] v) - throws IllegalArgumentException { + public double getL1Distance(double[] v) { checkVectorDimensions(v.length); double sum = 0; for (int i = 0; i < data.length; ++i) { @@ -896,8 +887,7 @@ public class ArrayRealVector extends AbstractRealVector implements Serializable /** {@inheritDoc} */ @Override - public double getLInfDistance(double[] v) - throws IllegalArgumentException { + public double getLInfDistance(double[] v) { checkVectorDimensions(v.length); double max = 0; for (int i = 0; i < data.length; ++i) { @@ -970,8 +960,7 @@ public class ArrayRealVector extends AbstractRealVector implements Serializable /** {@inheritDoc} */ @Override - public RealMatrix outerProduct(RealVector v) - throws IllegalArgumentException { + public RealMatrix outerProduct(RealVector v) { if (v instanceof ArrayRealVector) { return outerProduct((ArrayRealVector) v); } else { @@ -989,19 +978,18 @@ public class ArrayRealVector extends AbstractRealVector implements Serializable /** * Compute the outer product. - * @param v vector with which outer product should be computed - * @return the square matrix outer product between instance and v - * @exception IllegalArgumentException if v is not the same size as this + * @param v Vector with which outer product should be computed. + * @return the square matrix outer product between this instance and {@code v}. + * @throws DimensionMismatchException if {@code v} is not the same + * size as this vector. */ - public RealMatrix outerProduct(ArrayRealVector v) - throws IllegalArgumentException { + public RealMatrix outerProduct(ArrayRealVector v) { return outerProduct(v.data); } /** {@inheritDoc} */ @Override - public RealMatrix outerProduct(double[] v) - throws IllegalArgumentException { + public RealMatrix outerProduct(double[] v) { checkVectorDimensions(v.length); final int m = data.length; final RealMatrix out = MatrixUtils.createRealMatrix(m, m); @@ -1084,7 +1072,7 @@ public class ArrayRealVector extends AbstractRealVector implements Serializable set(index, (ArrayRealVector) v); } catch (ClassCastException cce) { for (int i = index; i < index + v.getDimension(); ++i) { - data[i] = v.getEntry(i-index); + data[i] = v.getEntry(i - index); } } } catch (IndexOutOfBoundsException e) { @@ -1110,7 +1098,7 @@ public class ArrayRealVector extends AbstractRealVector implements Serializable * @param index Index of first element to be set. * @param v Vector containing the values to set. * @throws org.apache.commons.math.exception.OutOfRangeException - * if the index is inconsistent with vector size. + * if the index is inconsistent with the vector size. */ public void set(int index, ArrayRealVector v) { setSubVector(index, v.data); 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 75e3c6949..d59ab69fb 100644 --- a/src/main/java/org/apache/commons/math/linear/BlockFieldMatrix.java +++ b/src/main/java/org/apache/commons/math/linear/BlockFieldMatrix.java @@ -370,8 +370,7 @@ public class BlockFieldMatrix> extends AbstractFieldMa /** {@inheritDoc} */ @Override - public FieldMatrix subtract(final FieldMatrix m) - throws IllegalArgumentException { + public FieldMatrix subtract(final FieldMatrix m) { try { return subtract((BlockFieldMatrix) m); } catch (ClassCastException cce) { @@ -1229,9 +1228,7 @@ public class BlockFieldMatrix> extends AbstractFieldMa /** {@inheritDoc} */ @Override - public T[] operate(final T[] v) - throws IllegalArgumentException { - + public T[] operate(final T[] v) { if (v.length != columns) { throw new DimensionMismatchException(v.length, columns); } @@ -1272,8 +1269,7 @@ public class BlockFieldMatrix> extends AbstractFieldMa /** {@inheritDoc} */ @Override - public T[] preMultiply(final T[] v) - throws IllegalArgumentException { + public T[] preMultiply(final T[] v) { if (v.length != rows) { throw new DimensionMismatchException(v.length, rows); 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 031b58e42..ca6a0f109 100644 --- a/src/main/java/org/apache/commons/math/linear/BlockRealMatrix.java +++ b/src/main/java/org/apache/commons/math/linear/BlockRealMatrix.java @@ -86,8 +86,8 @@ public class BlockRealMatrix extends AbstractRealMatrix implements Serializable * * @param rows the number of rows in the new matrix * @param columns the number of columns in the new matrix - * @throws IllegalArgumentException if row or column dimension is not - * positive. + * @throws org.apache.commons.math.exception.NotStrictlyPositiveException + * if row or column dimension is not positive. */ public BlockRealMatrix(final int rows, final int columns) { super(rows, columns); @@ -109,28 +109,26 @@ public class BlockRealMatrix extends AbstractRealMatrix implements Serializable *
matrix = new BlockRealMatrix(rawData.length, rawData[0].length,
      *                                   toBlocksLayout(rawData), false);
*

- * @param rawData data for new matrix, in raw layout * - * @exception IllegalArgumentException if blockData shape is - * inconsistent with block layout + * @param rawData data for new matrix, in raw layout + * @throws DimensionMismatchException if the shape of {@code blockData} is + * inconsistent with block layout. * @see #BlockRealMatrix(int, int, double[][], boolean) */ - public BlockRealMatrix(final double[][] rawData) - throws IllegalArgumentException { + public BlockRealMatrix(final double[][] rawData) { this(rawData.length, rawData[0].length, toBlocksLayout(rawData), false); } /** * Create a new dense matrix copying entries from block layout data. *

The input array must already be in blocks layout.

- * @param rows the number of rows in the new matrix - * @param columns the number of columns in the new matrix - * @param blockData data for new matrix - * @param copyArray if true, the input array will be copied, otherwise - * it will be referenced * - * @exception IllegalArgumentException if blockData shape is - * inconsistent with block layout + * @param rows Number of rows in the new matrix. + * @param columns Number of columns in the new matrix. + * @param blockData data for new matrix + * @param copyArray Whether the input array will be copied or referenced. + * @throws DimensionMismatchException if the shape of {@code blockData} is + * inconsistent with block layout. * @see #createBlocksLayout(int, int) * @see #toBlocksLayout(double[][]) * @see #BlockRealMatrix(double[][]) @@ -183,10 +181,9 @@ public class BlockRealMatrix extends AbstractRealMatrix implements Serializable * It can be used to provide the array argument of the {@link * #BlockRealMatrix(int, int, double[][], boolean)} constructor. *

- * @param rawData data array in raw layout - * @return a new data array containing the same entries but in blocks layout - * @exception IllegalArgumentException if rawData is not rectangular - * (not all rows have the same length) + * @param rawData Data array in raw layout. + * @return a new data array containing the same entries but in blocks layout. + * @throws DimensionMismatchException if {@code rawData} is not rectangular. * @see #createBlocksLayout(int, int) * @see #BlockRealMatrix(int, int, double[][], boolean) */ @@ -239,9 +236,9 @@ public class BlockRealMatrix extends AbstractRealMatrix implements Serializable * This method can be used to create the array argument of the {@link * #BlockRealMatrix(int, int, double[][], boolean)} constructor. *

- * @param rows the number of rows in the new matrix - * @param columns the number of columns in the new matrix - * @return a new data array in blocks layout + * @param rows Number of rows in the new matrix. + * @param columns Number of columns in the new matrix. + * @return a new data array in blocks layout. * @see #toBlocksLayout(double[][]) * @see #BlockRealMatrix(int, int, double[][], boolean) */ diff --git a/src/main/java/org/apache/commons/math/linear/DefaultFieldMatrixChangingVisitor.java b/src/main/java/org/apache/commons/math/linear/DefaultFieldMatrixChangingVisitor.java index 769f7c460..c59d09d15 100644 --- a/src/main/java/org/apache/commons/math/linear/DefaultFieldMatrixChangingVisitor.java +++ b/src/main/java/org/apache/commons/math/linear/DefaultFieldMatrixChangingVisitor.java @@ -18,7 +18,6 @@ package org.apache.commons.math.linear; import org.apache.commons.math.FieldElement; -import org.apache.commons.math.exception.MathUserException; /** * Default implementation of the {@link FieldMatrixChangingVisitor} interface. @@ -33,7 +32,6 @@ import org.apache.commons.math.exception.MathUserException; */ public class DefaultFieldMatrixChangingVisitor> implements FieldMatrixChangingVisitor { - /** Zero element of the field. */ private final T zero; @@ -58,5 +56,4 @@ public class DefaultFieldMatrixChangingVisitor> public T end() { return zero; } - } diff --git a/src/main/java/org/apache/commons/math/linear/DefaultFieldMatrixPreservingVisitor.java b/src/main/java/org/apache/commons/math/linear/DefaultFieldMatrixPreservingVisitor.java index 0505b5632..623221696 100644 --- a/src/main/java/org/apache/commons/math/linear/DefaultFieldMatrixPreservingVisitor.java +++ b/src/main/java/org/apache/commons/math/linear/DefaultFieldMatrixPreservingVisitor.java @@ -18,7 +18,6 @@ package org.apache.commons.math.linear; import org.apache.commons.math.FieldElement; -import org.apache.commons.math.exception.MathUserException; /** * Default implementation of the {@link FieldMatrixPreservingVisitor} interface. @@ -33,7 +32,6 @@ import org.apache.commons.math.exception.MathUserException; */ public class DefaultFieldMatrixPreservingVisitor> implements FieldMatrixPreservingVisitor { - /** Zero element of the field. */ private final T zero; @@ -56,5 +54,4 @@ public class DefaultFieldMatrixPreservingVisitor> public T end() { return zero; } - } diff --git a/src/main/java/org/apache/commons/math/linear/DefaultRealMatrixChangingVisitor.java b/src/main/java/org/apache/commons/math/linear/DefaultRealMatrixChangingVisitor.java index 1010d9429..625ea931c 100644 --- a/src/main/java/org/apache/commons/math/linear/DefaultRealMatrixChangingVisitor.java +++ b/src/main/java/org/apache/commons/math/linear/DefaultRealMatrixChangingVisitor.java @@ -17,8 +17,6 @@ package org.apache.commons.math.linear; -import org.apache.commons.math.exception.MathUserException; - /** * Default implementation of the {@link RealMatrixChangingVisitor} interface. *

@@ -30,7 +28,6 @@ import org.apache.commons.math.exception.MathUserException; * @since 2.0 */ public class DefaultRealMatrixChangingVisitor implements RealMatrixChangingVisitor { - /** {@inheritDoc} */ public void start(int rows, int columns, int startRow, int endRow, int startColumn, int endColumn) { @@ -45,5 +42,4 @@ public class DefaultRealMatrixChangingVisitor implements RealMatrixChangingVisit public double end() { return 0; } - } diff --git a/src/main/java/org/apache/commons/math/linear/DefaultRealMatrixPreservingVisitor.java b/src/main/java/org/apache/commons/math/linear/DefaultRealMatrixPreservingVisitor.java index 403cfeb69..981babdd9 100644 --- a/src/main/java/org/apache/commons/math/linear/DefaultRealMatrixPreservingVisitor.java +++ b/src/main/java/org/apache/commons/math/linear/DefaultRealMatrixPreservingVisitor.java @@ -17,8 +17,6 @@ package org.apache.commons.math.linear; -import org.apache.commons.math.exception.MathUserException; - /** * Default implementation of the {@link RealMatrixPreservingVisitor} interface. *

@@ -30,7 +28,6 @@ import org.apache.commons.math.exception.MathUserException; * @since 2.0 */ public class DefaultRealMatrixPreservingVisitor implements RealMatrixPreservingVisitor { - /** {@inheritDoc} */ public void start(int rows, int columns, int startRow, int endRow, int startColumn, int endColumn) { @@ -43,5 +40,4 @@ public class DefaultRealMatrixPreservingVisitor implements RealMatrixPreservingV public double end() { return 0; } - } 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 3b7597ae8..b143961fe 100644 --- a/src/main/java/org/apache/commons/math/linear/FieldMatrix.java +++ b/src/main/java/org/apache/commons/math/linear/FieldMatrix.java @@ -20,7 +20,6 @@ package org.apache.commons.math.linear; import org.apache.commons.math.Field; import org.apache.commons.math.FieldElement; -import org.apache.commons.math.exception.MathUserException; /** * Interface defining field-valued matrix with basic algebraic operations. @@ -32,7 +31,6 @@ import org.apache.commons.math.exception.MathUserException; * @version $Revision$ $Date$ */ public interface FieldMatrix> extends AnyMatrix { - /** * Get the type of field elements of the matrix. * diff --git a/src/main/java/org/apache/commons/math/linear/FieldMatrixChangingVisitor.java b/src/main/java/org/apache/commons/math/linear/FieldMatrixChangingVisitor.java index 9555645d6..34b963910 100644 --- a/src/main/java/org/apache/commons/math/linear/FieldMatrixChangingVisitor.java +++ b/src/main/java/org/apache/commons/math/linear/FieldMatrixChangingVisitor.java @@ -18,7 +18,6 @@ package org.apache.commons.math.linear; import org.apache.commons.math.FieldElement; -import org.apache.commons.math.exception.MathUserException; /** * Interface defining a visitor for matrix entries. @@ -28,7 +27,6 @@ import org.apache.commons.math.exception.MathUserException; * @since 2.0 */ public interface FieldMatrixChangingVisitor> { - /** * Start visiting a matrix. *

This method is called once before any entry of the matrix is visited.

diff --git a/src/main/java/org/apache/commons/math/linear/FieldMatrixPreservingVisitor.java b/src/main/java/org/apache/commons/math/linear/FieldMatrixPreservingVisitor.java index 6f8920455..46f2b68cb 100644 --- a/src/main/java/org/apache/commons/math/linear/FieldMatrixPreservingVisitor.java +++ b/src/main/java/org/apache/commons/math/linear/FieldMatrixPreservingVisitor.java @@ -18,7 +18,6 @@ package org.apache.commons.math.linear; import org.apache.commons.math.FieldElement; -import org.apache.commons.math.exception.MathUserException; /** * Interface defining a visitor for matrix entries. @@ -28,7 +27,6 @@ import org.apache.commons.math.exception.MathUserException; * @since 2.0 */ public interface FieldMatrixPreservingVisitor> { - /** * Start visiting a matrix. *

This method is called once before any entry of the matrix is visited.

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 200de15a5..03666610d 100644 --- a/src/main/java/org/apache/commons/math/linear/MatrixUtils.java +++ b/src/main/java/org/apache/commons/math/linear/MatrixUtils.java @@ -25,11 +25,12 @@ import java.util.Arrays; import org.apache.commons.math.Field; import org.apache.commons.math.FieldElement; -import org.apache.commons.math.MathRuntimeException; import org.apache.commons.math.exception.OutOfRangeException; -import org.apache.commons.math.exception.ZeroException; +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; import org.apache.commons.math.fraction.Fraction; @@ -102,7 +103,7 @@ public class MatrixUtils { * @return RealMatrix containing the values of the array * @throws org.apache.commons.math.exception.DimensionMismatchException * if {@code data} is not rectangular (not all rows have the same length). - * @throws ZeroException if a row or column is empty. + * @throws NoDataException if a row or column is empty. * @throws NullArgumentException if either {@code data} or {@code data[0]} * is {@code null}. * @see #createRealMatrix(int, int) @@ -129,7 +130,7 @@ public class MatrixUtils { * @return a matrix containing the values of the array. * @throws org.apache.commons.math.exception.DimensionMismatchException * if {@code data} is not rectangular (not all rows have the same length). - * @throws ZeroException if a row or column is empty. + * @throws NoDataException if a row or column is empty. * @throws NullArgumentException if either {@code data} or {@code data[0]} * is {@code null}. * @see #createFieldMatrix(Field, int, int) @@ -224,7 +225,7 @@ public class MatrixUtils { * * @param data the input data * @return a data.length RealVector - * @throws ZeroException if {@code data} is empty. + * @throws NoDataException if {@code data} is empty. * @throws NullArgumentException if {@code data} is {@code null}. */ public static RealVector createRealVector(double[] data) { @@ -240,7 +241,7 @@ public class MatrixUtils { * @param the type of the field elements * @param data the input data * @return a data.length FieldVector - * @throws ZeroException if {@code data} is empty. + * @throws NoDataException if {@code data} is empty. * @throws NullArgumentException if {@code data} is {@code null}. */ public static > FieldVector createFieldVector(final T[] data) { @@ -256,7 +257,7 @@ public class MatrixUtils { * * @param rowData the input row data * @return a 1 x rowData.length RealMatrix - * @throws ZeroException if {@code rowData} is empty. + * @throws NoDataException if {@code rowData} is empty. * @throws NullArgumentException if {@code rowData} is {@code null}. */ public static RealMatrix createRowRealMatrix(double[] rowData) { @@ -278,7 +279,7 @@ public class MatrixUtils { * @param the type of the field elements * @param rowData the input row data * @return a 1 x rowData.length FieldMatrix - * @throws ZeroException if {@code rowData} is empty. + * @throws NoDataException if {@code rowData} is empty. * @throws NullArgumentException if {@code rowData} is {@code null}. */ public static > FieldMatrix @@ -288,7 +289,7 @@ public class MatrixUtils { } final int nCols = rowData.length; if (nCols == 0) { - throw new ZeroException(LocalizedFormats.AT_LEAST_ONE_COLUMN); + throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN); } final FieldMatrix m = createFieldMatrix(rowData[0].getField(), 1, nCols); for (int i = 0; i < nCols; ++i) { @@ -303,7 +304,7 @@ public class MatrixUtils { * * @param columnData the input column data * @return a columnData x 1 RealMatrix - * @throws ZeroException if {@code columnData} is empty. + * @throws NoDataException if {@code columnData} is empty. * @throws NullArgumentException if {@code columnData} is {@code null}. */ public static RealMatrix createColumnRealMatrix(double[] columnData) { @@ -325,7 +326,7 @@ public class MatrixUtils { * @param the type of the field elements * @param columnData the input column data * @return a columnData x 1 FieldMatrix - * @throws ZeroException if {@code data} is empty. + * @throws NoDataException if {@code data} is empty. * @throws NullArgumentException if {@code columnData} is {@code null}. */ public static > FieldMatrix @@ -335,7 +336,7 @@ public class MatrixUtils { } final int nRows = columnData.length; if (nRows == 0) { - throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.AT_LEAST_ONE_ROW); + throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_ROW); } final FieldMatrix m = createFieldMatrix(columnData[0].getField(), nRows, 1); for (int i = 0; i < nRows; ++i) { @@ -430,7 +431,7 @@ public class MatrixUtils { * @param selectedColumns Array of column indices. * @throws NullArgumentException if {@code selectedRows} or * {@code selectedColumns} are {@code null}. - * @throws ZeroException if the row or column selections are empty (zero + * @throws NoDataException if the row or column selections are empty (zero * length). * @throws OutOfRangeException if row or column selections are not valid. */ @@ -444,10 +445,10 @@ public class MatrixUtils { throw new NullArgumentException(); } if (selectedRows.length == 0) { - throw new ZeroException(LocalizedFormats.EMPTY_SELECTED_ROW_INDEX_ARRAY); + throw new NoDataException(LocalizedFormats.EMPTY_SELECTED_ROW_INDEX_ARRAY); } if (selectedColumns.length == 0) { - throw new ZeroException(LocalizedFormats.EMPTY_SELECTED_COLUMN_INDEX_ARRAY); + throw new NoDataException(LocalizedFormats.EMPTY_SELECTED_COLUMN_INDEX_ARRAY); } for (final int row : selectedRows) { @@ -459,59 +460,53 @@ public class MatrixUtils { } /** - * Check if matrices are addition compatible - * @param left left hand side matrix - * @param right right hand side matrix - * @exception IllegalArgumentException if matrices are not addition compatible + * Check if matrices are addition compatible. + * + * @param left Left hand side matrix. + * @param right Right hand side matrix. + * @throws MatrixDimensionMismatchException if the matrices are not addition compatible. */ - public static void checkAdditionCompatible(final AnyMatrix left, final AnyMatrix right) - throws IllegalArgumentException { + public static void checkAdditionCompatible(final AnyMatrix left, final AnyMatrix right) { if ((left.getRowDimension() != right.getRowDimension()) || (left.getColumnDimension() != right.getColumnDimension())) { - throw MathRuntimeException.createIllegalArgumentException( - LocalizedFormats.NOT_ADDITION_COMPATIBLE_MATRICES, - left.getRowDimension(), left.getColumnDimension(), - right.getRowDimension(), right.getColumnDimension()); + throw new MatrixDimensionMismatchException(left.getRowDimension(), left.getColumnDimension(), + right.getRowDimension(), right.getColumnDimension()); } } /** * Check if matrices are subtraction compatible - * @param left left hand side matrix - * @param right right hand side matrix - * @exception IllegalArgumentException if matrices are not subtraction compatible + * + * @param left Left hand side matrix. + * @param right Right hand side matrix. + * @throws MatrixDimensionMismatchException if the matrices are not addition compatible. */ - public static void checkSubtractionCompatible(final AnyMatrix left, final AnyMatrix right) - throws IllegalArgumentException { + public static void checkSubtractionCompatible(final AnyMatrix left, final AnyMatrix right) { if ((left.getRowDimension() != right.getRowDimension()) || (left.getColumnDimension() != right.getColumnDimension())) { - throw MathRuntimeException.createIllegalArgumentException( - LocalizedFormats.NOT_SUBTRACTION_COMPATIBLE_MATRICES, - left.getRowDimension(), left.getColumnDimension(), - right.getRowDimension(), right.getColumnDimension()); + throw new MatrixDimensionMismatchException(left.getRowDimension(), left.getColumnDimension(), + right.getRowDimension(), right.getColumnDimension()); } } /** * Check if matrices are multiplication compatible - * @param left left hand side matrix - * @param right right hand side matrix - * @exception IllegalArgumentException if matrices are not multiplication compatible + * + * @param left Left hand side matrix. + * @param right Right hand side matrix. + * @throws DimensionMismatchException if matrices are not multiplication compatible. */ - public static void checkMultiplicationCompatible(final AnyMatrix left, final AnyMatrix right) - throws IllegalArgumentException { + public static void checkMultiplicationCompatible(final AnyMatrix left, final AnyMatrix right) { if (left.getColumnDimension() != right.getRowDimension()) { - throw MathRuntimeException.createIllegalArgumentException( - LocalizedFormats.NOT_MULTIPLICATION_COMPATIBLE_MATRICES, - left.getRowDimension(), left.getColumnDimension(), - right.getRowDimension(), right.getColumnDimension()); + throw new DimensionMismatchException(left.getColumnDimension(), + right.getRowDimension()); } } /** * Convert a {@link FieldMatrix}/{@link Fraction} matrix to a {@link RealMatrix}. - * @param m matrix to convert - * @return converted matrix + * @param m Matrix to convert. + * @return the converted matrix. */ public static Array2DRowRealMatrix fractionMatrixToRealMatrix(final FieldMatrix m) { final FractionMatrixConverter converter = new FractionMatrixConverter(); @@ -521,10 +516,8 @@ public class MatrixUtils { /** Converter for {@link FieldMatrix}/{@link Fraction}. */ private static class FractionMatrixConverter extends DefaultFieldMatrixPreservingVisitor { - /** Converted array. */ private double[][] data; - /** Simple constructor. */ public FractionMatrixConverter() { super(Fraction.ZERO); @@ -543,8 +536,10 @@ public class MatrixUtils { data[row][column] = value.doubleValue(); } - /** Get the converted matrix. - * @return converted matrix + /** + * Get the converted matrix. + * + * @return the converted matrix. */ Array2DRowRealMatrix getConvertedMatrix() { return new Array2DRowRealMatrix(data, false); @@ -554,8 +549,9 @@ public class MatrixUtils { /** * Convert a {@link FieldMatrix}/{@link BigFraction} matrix to a {@link RealMatrix}. - * @param m matrix to convert - * @return converted matrix + * + * @param m Matrix to convert. + * @return the converted matrix. */ public static Array2DRowRealMatrix bigFractionMatrixToRealMatrix(final FieldMatrix m) { final BigFractionMatrixConverter converter = new BigFractionMatrixConverter(); @@ -565,10 +561,8 @@ public class MatrixUtils { /** Converter for {@link FieldMatrix}/{@link BigFraction}. */ private static class BigFractionMatrixConverter extends DefaultFieldMatrixPreservingVisitor { - /** Converted array. */ private double[][] data; - /** Simple constructor. */ public BigFractionMatrixConverter() { super(BigFraction.ZERO); @@ -587,13 +581,14 @@ public class MatrixUtils { data[row][column] = value.doubleValue(); } - /** Get the converted matrix. - * @return converted matrix + /** + * Get the converted matrix. + * + * @return the converted matrix. */ Array2DRowRealMatrix getConvertedMatrix() { return new Array2DRowRealMatrix(data, false); } - } /** Serialize a {@link RealVector}. @@ -803,7 +798,5 @@ public class MatrixUtils { ioe.initCause(iae); throw ioe; } - } - } 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 2f362ee37..dcd4d144b 100644 --- a/src/main/java/org/apache/commons/math/linear/OpenMapRealMatrix.java +++ b/src/main/java/org/apache/commons/math/linear/OpenMapRealMatrix.java @@ -107,8 +107,7 @@ public class OpenMapRealMatrix extends AbstractRealMatrix /** {@inheritDoc} */ @Override - public OpenMapRealMatrix subtract(final RealMatrix m) - throws IllegalArgumentException { + public OpenMapRealMatrix subtract(final RealMatrix m) { try { return subtract((OpenMapRealMatrix) m); } catch (ClassCastException cce) { @@ -141,8 +140,7 @@ public class OpenMapRealMatrix extends AbstractRealMatrix /** {@inheritDoc} */ @Override - public RealMatrix multiply(final RealMatrix m) - throws IllegalArgumentException { + public RealMatrix multiply(final RealMatrix m) { try { return multiply((OpenMapRealMatrix) m); } catch (ClassCastException cce) { 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 b29541391..5b6dc7b6e 100644 --- a/src/main/java/org/apache/commons/math/linear/RealMatrix.java +++ b/src/main/java/org/apache/commons/math/linear/RealMatrix.java @@ -17,9 +17,6 @@ package org.apache.commons.math.linear; -import org.apache.commons.math.exception.MathUserException; - - /** * Interface defining a real-valued matrix with basic algebraic operations. *

@@ -29,7 +26,6 @@ import org.apache.commons.math.exception.MathUserException; * @version $Revision$ $Date$ */ public interface RealMatrix extends AnyMatrix { - /** * Create a new RealMatrix of the same type as the instance with the supplied * row and column dimensions. diff --git a/src/main/java/org/apache/commons/math/linear/RealMatrixChangingVisitor.java b/src/main/java/org/apache/commons/math/linear/RealMatrixChangingVisitor.java index 395c6b841..0465b073a 100644 --- a/src/main/java/org/apache/commons/math/linear/RealMatrixChangingVisitor.java +++ b/src/main/java/org/apache/commons/math/linear/RealMatrixChangingVisitor.java @@ -17,8 +17,6 @@ package org.apache.commons.math.linear; -import org.apache.commons.math.exception.MathUserException; - /** * Interface defining a visitor for matrix entries. * @@ -27,7 +25,6 @@ import org.apache.commons.math.exception.MathUserException; * @since 2.0 */ public interface RealMatrixChangingVisitor { - /** * Start visiting a matrix. *

This method is called once before any entry of the matrix is visited.

diff --git a/src/main/java/org/apache/commons/math/linear/RealMatrixPreservingVisitor.java b/src/main/java/org/apache/commons/math/linear/RealMatrixPreservingVisitor.java index 5dee5255e..5c999b3e3 100644 --- a/src/main/java/org/apache/commons/math/linear/RealMatrixPreservingVisitor.java +++ b/src/main/java/org/apache/commons/math/linear/RealMatrixPreservingVisitor.java @@ -17,8 +17,6 @@ package org.apache.commons.math.linear; -import org.apache.commons.math.exception.MathUserException; - /** * Interface defining a visitor for matrix entries. * @@ -27,7 +25,6 @@ import org.apache.commons.math.exception.MathUserException; * @since 2.0 */ public interface RealMatrixPreservingVisitor { - /** * Start visiting a matrix. *

This method is called once before any entry of the matrix is visited.

diff --git a/src/main/java/org/apache/commons/math/linear/SingularValueDecomposition.java b/src/main/java/org/apache/commons/math/linear/SingularValueDecomposition.java index c65d04e82..ae7e62a7f 100644 --- a/src/main/java/org/apache/commons/math/linear/SingularValueDecomposition.java +++ b/src/main/java/org/apache/commons/math/linear/SingularValueDecomposition.java @@ -111,7 +111,7 @@ public interface SingularValueDecomposition { * @exception IllegalArgumentException if minSingularValue is larger than * the largest singular value, meaning all singular values are ignored */ - RealMatrix getCovariance(double minSingularValue) throws IllegalArgumentException; + RealMatrix getCovariance(double minSingularValue); /** * Returns the L2 norm of the matrix. diff --git a/src/main/java/org/apache/commons/math/linear/SingularValueDecompositionImpl.java b/src/main/java/org/apache/commons/math/linear/SingularValueDecompositionImpl.java index e8ecd12ab..1c5082857 100644 --- a/src/main/java/org/apache/commons/math/linear/SingularValueDecompositionImpl.java +++ b/src/main/java/org/apache/commons/math/linear/SingularValueDecompositionImpl.java @@ -34,38 +34,29 @@ import org.apache.commons.math.util.FastMath; * @version $Revision$ $Date$ * @since 2.0 */ -public class SingularValueDecompositionImpl implements - SingularValueDecomposition { - +public class SingularValueDecompositionImpl implements SingularValueDecomposition { /** Number of rows of the initial matrix. */ private int m; - /** Number of columns of the initial matrix. */ private int n; - /** Eigen decomposition of the tridiagonal matrix. */ private EigenDecomposition eigenDecomposition; - /** Singular values. */ private double[] singularValues; - /** Cached value of U. */ private RealMatrix cachedU; - /** Cached value of UT. */ private RealMatrix cachedUt; - /** Cached value of S. */ private RealMatrix cachedS; - /** Cached value of V. */ private RealMatrix cachedV; - /** Cached value of VT. */ private RealMatrix cachedVt; /** * Calculates the compact Singular Value Decomposition of the given matrix. + * * @param matrix Matrix to decompose. */ public SingularValueDecompositionImpl(final RealMatrix matrix) { @@ -88,7 +79,7 @@ public class SingularValueDecompositionImpl implements for (int k = 0; k < m; k++) { matATA[i][j] += localcopy[k][i] * localcopy[k][j]; } - matATA[j][i]=matATA[i][j]; + matATA[j][i] = matATA[i][j]; } } @@ -102,33 +93,33 @@ public class SingularValueDecompositionImpl implements for (int k = 0; k < n; k++) { matAAT[i][j] += localcopy[i][k] * localcopy[j][k]; } - matAAT[j][i]=matAAT[i][j]; + matAAT[j][i] = matAAT[i][j]; } } int p; - if (m>=n) { - p=n; + if (m >= n) { + p = n; // compute eigen decomposition of A^T*A - eigenDecomposition = new EigenDecompositionImpl( - new Array2DRowRealMatrix(matATA),1.0); + eigenDecomposition + = new EigenDecompositionImpl(new Array2DRowRealMatrix(matATA), 1); singularValues = eigenDecomposition.getRealEigenvalues(); cachedV = eigenDecomposition.getV(); // compute eigen decomposition of A*A^T - eigenDecomposition = new EigenDecompositionImpl( - new Array2DRowRealMatrix(matAAT),1.0); + eigenDecomposition + = new EigenDecompositionImpl(new Array2DRowRealMatrix(matAAT), 1); cachedU = eigenDecomposition.getV().getSubMatrix(0, m - 1, 0, p - 1); } else { - p=m; + p = m; // compute eigen decomposition of A*A^T - eigenDecomposition = new EigenDecompositionImpl( - new Array2DRowRealMatrix(matAAT),1.0); + eigenDecomposition + = new EigenDecompositionImpl(new Array2DRowRealMatrix(matAAT), 1); singularValues = eigenDecomposition.getRealEigenvalues(); cachedU = eigenDecomposition.getV(); // compute eigen decomposition of A^T*A - eigenDecomposition = new EigenDecompositionImpl( - new Array2DRowRealMatrix(matATA),1.0); - cachedV = eigenDecomposition.getV().getSubMatrix(0,n-1,0,p-1); + eigenDecomposition + = new EigenDecompositionImpl(new Array2DRowRealMatrix(matATA), 1); + cachedV = eigenDecomposition.getV().getSubMatrix(0, n - 1 , 0, p - 1); } for (int i = 0; i < p; i++) { singularValues[i] = FastMath.sqrt(FastMath.abs(singularValues[i])); @@ -138,11 +129,11 @@ public class SingularValueDecompositionImpl implements // The sign is set such that A.V_i=sigma_i.U_i (i<=p) // The right sign corresponds to a positive dot product of A.V_i and U_i for (int i = 0; i < p; i++) { - RealVector tmp = cachedU.getColumnVector(i); - double product=matrix.operate(cachedV.getColumnVector(i)).dotProduct(tmp); - if (product<0) { - cachedU.setColumnVector(i, tmp.mapMultiply(-1.0)); - } + RealVector tmp = cachedU.getColumnVector(i); + double product=matrix.operate(cachedV.getColumnVector(i)).dotProduct(tmp); + if (product < 0) { + cachedU.setColumnVector(i, tmp.mapMultiply(-1)); + } } } @@ -155,24 +146,18 @@ public class SingularValueDecompositionImpl implements /** {@inheritDoc} */ public RealMatrix getUT() { - if (cachedUt == null) { cachedUt = getU().transpose(); } - // return the cached matrix return cachedUt; - } /** {@inheritDoc} */ public RealMatrix getS() { - if (cachedS == null) { - // cache the matrix for subsequent calls cachedS = MatrixUtils.createRealDiagonalMatrix(singularValues); - } return cachedS; } @@ -186,24 +171,19 @@ public class SingularValueDecompositionImpl implements public RealMatrix getV() { // return the cached matrix return cachedV; - } /** {@inheritDoc} */ public RealMatrix getVT() { - if (cachedVt == null) { cachedVt = getV().transpose(); } - // return the cached matrix return cachedVt; - } /** {@inheritDoc} */ public RealMatrix getCovariance(final double minSingularValue) { - // get the number of singular values to consider final int p = singularValues.length; int dimension = 0; @@ -229,7 +209,6 @@ public class SingularValueDecompositionImpl implements RealMatrix jv = new Array2DRowRealMatrix(data, false); return jv.transpose().multiply(jv); - } /** {@inheritDoc} */ @@ -243,8 +222,7 @@ public class SingularValueDecompositionImpl implements } /** {@inheritDoc} */ - public int getRank() throws IllegalStateException { - + public int getRank() { final double threshold = FastMath.max(m, n) * FastMath.ulp(singularValues[0]); for (int i = singularValues.length - 1; i >= 0; --i) { @@ -253,44 +231,37 @@ public class SingularValueDecompositionImpl implements } } return 0; - } /** {@inheritDoc} */ public DecompositionSolver getSolver() { - return new Solver(singularValues, getUT(), getV(), getRank() == Math - .max(m, n)); + return new Solver(singularValues, getUT(), getV(), getRank() == Math.max(m, n)); } /** Specialized solver. */ private static class Solver implements DecompositionSolver { - /** Pseudo-inverse of the initial matrix. */ private final RealMatrix pseudoInverse; - /** Singularity indicator. */ private boolean nonSingular; /** * Build a solver from decomposed matrix. - * @param singularValues - * singularValues - * @param uT - * UT matrix of the decomposition - * @param v - * V matrix of the decomposition - * @param nonSingular - * singularity indicator + * + * @param singularValues Singular values. + * @param uT UT matrix of the decomposition. + * @param v V matrix of the decomposition. + * @param nonSingular Singularity indicator. */ private Solver(final double[] singularValues, final RealMatrix uT, - final RealMatrix v, final boolean nonSingular) { + final RealMatrix v, final boolean nonSingular) { double[][] suT = uT.getData(); for (int i = 0; i < singularValues.length; ++i) { final double a; - if (singularValues[i]>0) { - a=1.0 / singularValues[i]; + if (singularValues[i] > 0) { + a = 1 / singularValues[i]; } else { - a=0.0; + a = 0; } final double[] suTi = suT[i]; for (int j = 0; j < suTi.length; ++j) { @@ -307,13 +278,12 @@ public class SingularValueDecompositionImpl implements * The m×n matrix A may not be square, the solution X is such that * ||A × X - B|| is minimal. *

- * @param b - * right-hand side of the equation A × X = B + * @param b Right-hand side of the equation A × X = B * @return a vector X that minimizes the two norm of A × X - B - * @exception IllegalArgumentException - * if matrices dimensions don't match + * @throws org.apache.commons.math.exception.DimensionMismatchException + * if the matrices dimensions do not match. */ - public double[] solve(final double[] b) throws IllegalArgumentException { + public double[] solve(final double[] b) { return pseudoInverse.operate(b); } @@ -323,14 +293,12 @@ public class SingularValueDecompositionImpl implements * The m×n matrix A may not be square, the solution X is such that * ||A × X - B|| is minimal. *

- * @param b - * right-hand side of the equation A × X = B + * @param b Right-hand side of the equation A × X = B * @return a vector X that minimizes the two norm of A × X - B - * @exception IllegalArgumentException - * if matrices dimensions don't match + * @throws org.apache.commons.math.exception.DimensionMismatchException + * if the matrices dimensions do not match. */ - public RealVector solve(final RealVector b) - throws IllegalArgumentException { + public RealVector solve(final RealVector b) { return pseudoInverse.operate(b); } @@ -340,20 +308,20 @@ public class SingularValueDecompositionImpl implements * The m×n matrix A may not be square, the solution X is such that * ||A × X - B|| is minimal. *

- * @param b - * right-hand side of the equation A × X = B + * + * @param b Right-hand side of the equation A × X = B * @return a matrix X that minimizes the two norm of A × X - B - * @exception IllegalArgumentException - * if matrices dimensions don't match + * @throws org.apache.commons.math.exception.DimensionMismatchException + * if the matrices dimensions do not match. */ - public RealMatrix solve(final RealMatrix b) - throws IllegalArgumentException { + public RealMatrix solve(final RealMatrix b) { return pseudoInverse.multiply(b); } /** * Check if the decomposed matrix is non-singular. - * @return true if the decomposed matrix is non-singular + * + * @return {@code true} if the decomposed matrix is non-singular. */ public boolean isNonSingular() { return nonSingular; @@ -361,12 +329,11 @@ public class SingularValueDecompositionImpl implements /** * Get the pseudo-inverse of the decomposed matrix. - * @return inverse matrix + * + * @return the inverse matrix. */ public RealMatrix getInverse() { return pseudoInverse; } - } - } 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 22b54d123..ec763c899 100644 --- a/src/test/java/org/apache/commons/math/linear/Array2DRowRealMatrixTest.java +++ b/src/test/java/org/apache/commons/math/linear/Array2DRowRealMatrixTest.java @@ -23,7 +23,7 @@ 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.ZeroException; +import org.apache.commons.math.exception.NoDataException; import org.apache.commons.math.exception.NumberIsTooSmallException; import org.apache.commons.math.exception.NonSquareMatrixException; @@ -399,7 +399,7 @@ public final class Array2DRowRealMatrixTest extends TestCase { RealMatrix sub = m.getSubMatrix(startRow, endRow, startColumn, endColumn); assertEquals(new Array2DRowRealMatrix(reference), sub); if (mustFail) { - fail("Expecting OutOfRangeException or NumberIsTooSmallException or ZeroException"); + fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException"); } } catch (OutOfRangeException e) { if (!mustFail) { @@ -409,7 +409,7 @@ public final class Array2DRowRealMatrixTest extends TestCase { if (!mustFail) { throw e; } - } catch (ZeroException e) { + } catch (NoDataException e) { if (!mustFail) { throw e; } @@ -423,7 +423,7 @@ public final class Array2DRowRealMatrixTest extends TestCase { RealMatrix sub = m.getSubMatrix(selectedRows, selectedColumns); assertEquals(new Array2DRowRealMatrix(reference), sub); if (mustFail) { - fail("Expecting OutOfRangeException or NumberIsTooSmallException or ZeroException"); + fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException"); } } catch (OutOfRangeException e) { if (!mustFail) { @@ -433,7 +433,7 @@ public final class Array2DRowRealMatrixTest extends TestCase { if (!mustFail) { throw e; } - } catch (ZeroException e) { + } catch (NoDataException e) { if (!mustFail) { throw e; } @@ -470,7 +470,7 @@ public final class Array2DRowRealMatrixTest extends TestCase { m.copySubMatrix(startRow, endRow, startColumn, endColumn, sub); assertEquals(new Array2DRowRealMatrix(reference), new Array2DRowRealMatrix(sub)); if (mustFail) { - fail("Expecting OutOfRangeException or NumberIsTooSmallException or ZeroException"); + fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException"); } } catch (OutOfRangeException e) { if (!mustFail) { @@ -480,7 +480,7 @@ public final class Array2DRowRealMatrixTest extends TestCase { if (!mustFail) { throw e; } - } catch (ZeroException e) { + } catch (NoDataException e) { if (!mustFail) { throw e; } @@ -497,7 +497,7 @@ public final class Array2DRowRealMatrixTest extends TestCase { m.copySubMatrix(selectedRows, selectedColumns, sub); assertEquals(new Array2DRowRealMatrix(reference), new Array2DRowRealMatrix(sub)); if (mustFail) { - fail("Expecting OutOfRangeException or NumberIsTooSmallException or ZeroException"); + fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException"); } } catch (OutOfRangeException e) { if (!mustFail) { @@ -507,7 +507,7 @@ public final class Array2DRowRealMatrixTest extends TestCase { if (!mustFail) { throw e; } - } catch (ZeroException e) { + } catch (NoDataException e) { if (!mustFail) { throw e; } 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 8cd65e898..060798d39 100644 --- a/src/test/java/org/apache/commons/math/linear/BlockRealMatrixTest.java +++ b/src/test/java/org/apache/commons/math/linear/BlockRealMatrixTest.java @@ -26,7 +26,7 @@ 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.ZeroException; +import org.apache.commons.math.exception.NoDataException; import org.apache.commons.math.exception.NumberIsTooSmallException; import org.apache.commons.math.exception.NonSquareMatrixException; @@ -499,7 +499,7 @@ public final class BlockRealMatrixTest extends TestCase { if (reference != null) { assertEquals(new BlockRealMatrix(reference), sub); } else { - fail("Expecting OutOfRangeException or NumberIsTooSmallException or ZeroException"); + fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException"); } } catch (OutOfRangeException e) { if (reference != null) { @@ -509,7 +509,7 @@ public final class BlockRealMatrixTest extends TestCase { if (reference != null) { throw e; } - } catch (ZeroException e) { + } catch (NoDataException e) { if (reference != null) { throw e; } @@ -523,7 +523,7 @@ public final class BlockRealMatrixTest extends TestCase { if (reference != null) { assertEquals(new BlockRealMatrix(reference), sub); } else { - fail("Expecting OutOfRangeException or NumberIsTooSmallExceptiono r ZeroException"); + fail("Expecting OutOfRangeException or NumberIsTooSmallExceptiono r NoDataException"); } } catch (OutOfRangeException e) { if (reference != null) { @@ -533,7 +533,7 @@ public final class BlockRealMatrixTest extends TestCase { if (reference != null) { throw e; } - } catch (ZeroException e) { + } catch (NoDataException e) { if (reference != null) { throw e; } @@ -589,7 +589,7 @@ public final class BlockRealMatrixTest extends TestCase { if (reference != null) { assertEquals(new BlockRealMatrix(reference), new BlockRealMatrix(sub)); } else { - fail("Expecting OutOfRangeException or NumberIsTooSmallException or ZeroException"); + fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException"); } } catch (OutOfRangeException e) { if (reference != null) { @@ -599,7 +599,7 @@ public final class BlockRealMatrixTest extends TestCase { if (reference != null) { throw e; } - } catch (ZeroException e) { + } catch (NoDataException e) { if (reference != null) { throw e; } @@ -616,7 +616,7 @@ public final class BlockRealMatrixTest extends TestCase { if (reference != null) { assertEquals(new BlockRealMatrix(reference), new BlockRealMatrix(sub)); } else { - fail("Expecting OutOfRangeException or NumberIsTooSmallException or ZeroException"); + fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException"); } } catch (OutOfRangeException e) { if (reference != null) { @@ -626,7 +626,7 @@ public final class BlockRealMatrixTest extends TestCase { if (reference != null) { throw e; } - } catch (ZeroException e) { + } catch (NoDataException e) { if (reference != null) { throw e; } 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 c54a57efc..fb29845f6 100644 --- a/src/test/java/org/apache/commons/math/linear/SparseRealMatrixTest.java +++ b/src/test/java/org/apache/commons/math/linear/SparseRealMatrixTest.java @@ -20,7 +20,7 @@ import junit.framework.TestCase; import org.apache.commons.math.TestUtils; import org.apache.commons.math.exception.OutOfRangeException; -import org.apache.commons.math.exception.ZeroException; +import org.apache.commons.math.exception.NoDataException; import org.apache.commons.math.exception.NumberIsTooSmallException; import org.apache.commons.math.exception.NonSquareMatrixException; @@ -436,8 +436,8 @@ public final class SparseRealMatrixTest extends TestCase { } try { m.getSubMatrix(new int[] {}, new int[] { 0 }); - fail("Expecting ZeroException"); - } catch (ZeroException ex) { + fail("Expecting NoDataException"); + } catch (NoDataException ex) { // expected } try {