diff --git a/src/main/java/org/apache/commons/math3/linear/AbstractFieldMatrix.java b/src/main/java/org/apache/commons/math3/linear/AbstractFieldMatrix.java index 3e533c1be..95a2718e9 100644 --- a/src/main/java/org/apache/commons/math3/linear/AbstractFieldMatrix.java +++ b/src/main/java/org/apache/commons/math3/linear/AbstractFieldMatrix.java @@ -25,6 +25,7 @@ import org.apache.commons.math3.Field; import org.apache.commons.math3.FieldElement; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NoDataException; +import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; @@ -72,7 +73,8 @@ public abstract class AbstractFieldMatrix> */ protected AbstractFieldMatrix(final Field field, final int rowDimension, - final int columnDimension) { + final int columnDimension) + throws NotStrictlyPositiveException { if (rowDimension <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.DIMENSION, rowDimension); @@ -93,7 +95,8 @@ public abstract class AbstractFieldMatrix> * @throws NullArgumentException if the array is {@code null}. * @throws NoDataException if the array is empty. */ - protected static > Field extractField(final T[][] d) { + protected static > Field extractField(final T[][] d) + throws NoDataException, NullArgumentException { if (d == null) { throw new NullArgumentException(); } @@ -114,7 +117,8 @@ public abstract class AbstractFieldMatrix> * @return the field to which the array elements belong. * @throws NoDataException if array is empty. */ - protected static > Field extractField(final T[] d) { + protected static > Field extractField(final T[] d) + throws NoDataException { if (d.length == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_ROW); } @@ -177,7 +181,8 @@ public abstract class AbstractFieldMatrix> public abstract FieldMatrix copy(); /** {@inheritDoc} */ - public FieldMatrix add(FieldMatrix m) { + public FieldMatrix add(FieldMatrix m) + throws MatrixDimensionMismatchException { // safety check checkAdditionCompatible(m); @@ -194,7 +199,8 @@ public abstract class AbstractFieldMatrix> } /** {@inheritDoc} */ - public FieldMatrix subtract(final FieldMatrix m) { + public FieldMatrix subtract(final FieldMatrix m) + throws MatrixDimensionMismatchException { // safety check checkSubtractionCompatible(m); @@ -240,7 +246,8 @@ public abstract class AbstractFieldMatrix> } /** {@inheritDoc} */ - public FieldMatrix multiply(final FieldMatrix m) { + public FieldMatrix multiply(final FieldMatrix m) + throws DimensionMismatchException { // safety check checkMultiplicationCompatible(m); @@ -262,14 +269,16 @@ public abstract class AbstractFieldMatrix> } /** {@inheritDoc} */ - public FieldMatrix preMultiply(final FieldMatrix m) { + public FieldMatrix preMultiply(final FieldMatrix m) + throws DimensionMismatchException { return m.multiply(this); } /** {@inheritDoc} */ - public FieldMatrix power(final int p) { + public FieldMatrix power(final int p) throws NonSquareMatrixException, + NotPositiveException { if (p < 0) { - throw new IllegalArgumentException("p must be >= 0"); + throw new NotPositiveException(p); } if (!isSquare()) { @@ -340,7 +349,8 @@ public abstract class AbstractFieldMatrix> /** {@inheritDoc} */ public FieldMatrix getSubMatrix(final int startRow, final int endRow, - final int startColumn, final int endColumn) { + final int startColumn, final int endColumn) + throws NumberIsTooSmallException, OutOfRangeException { checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); final FieldMatrix subMatrix = @@ -357,7 +367,8 @@ public abstract class AbstractFieldMatrix> /** {@inheritDoc} */ public FieldMatrix getSubMatrix(final int[] selectedRows, - final int[] selectedColumns) { + final int[] selectedColumns) + throws NoDataException, NullArgumentException, OutOfRangeException { // safety checks checkSubMatrixIndex(selectedRows, selectedColumns); @@ -382,7 +393,9 @@ public abstract class AbstractFieldMatrix> /** {@inheritDoc} */ public void copySubMatrix(final int startRow, final int endRow, final int startColumn, final int endColumn, - final T[][] destination) { + final T[][] destination) + throws MatrixDimensionMismatchException, NumberIsTooSmallException, + OutOfRangeException{ // safety checks checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); final int rowsCount = endRow + 1 - startRow; @@ -423,7 +436,9 @@ public abstract class AbstractFieldMatrix> } /** {@inheritDoc} */ - public void copySubMatrix(int[] selectedRows, int[] selectedColumns, T[][] destination) { + public void copySubMatrix(int[] selectedRows, int[] selectedColumns, T[][] destination) + throws MatrixDimensionMismatchException, NoDataException, + NullArgumentException, OutOfRangeException { // safety checks checkSubMatrixIndex(selectedRows, selectedColumns); if ((destination.length < selectedRows.length) || @@ -444,8 +459,13 @@ public abstract class AbstractFieldMatrix> } - /** {@inheritDoc} */ - public void setSubMatrix(final T[][] subMatrix, final int row, final int column) { + /** + * {@inheritDoc} + * + */ + public void setSubMatrix(final T[][] subMatrix, final int row, final int column) + throws DimensionMismatchException, NoDataException, NullArgumentException, + OutOfRangeException { if (subMatrix == null) { throw new NullArgumentException(); } @@ -478,7 +498,7 @@ public abstract class AbstractFieldMatrix> } /** {@inheritDoc} */ - public FieldMatrix getRowMatrix(final int row) { + public FieldMatrix getRowMatrix(final int row) throws OutOfRangeException { checkRowIndex(row); final int nCols = getColumnDimension(); final FieldMatrix out = createMatrix(1, nCols); @@ -491,7 +511,8 @@ public abstract class AbstractFieldMatrix> } /** {@inheritDoc} */ - public void setRowMatrix(final int row, final FieldMatrix matrix) { + public void setRowMatrix(final int row, final FieldMatrix matrix) + throws OutOfRangeException { checkRowIndex(row); final int nCols = getColumnDimension(); if ((matrix.getRowDimension() != 1) || @@ -507,7 +528,8 @@ public abstract class AbstractFieldMatrix> } /** {@inheritDoc} */ - public FieldMatrix getColumnMatrix(final int column) { + public FieldMatrix getColumnMatrix(final int column) + throws OutOfRangeException { checkColumnIndex(column); final int nRows = getRowDimension(); @@ -521,7 +543,8 @@ public abstract class AbstractFieldMatrix> } /** {@inheritDoc} */ - public void setColumnMatrix(final int column, final FieldMatrix matrix) { + public void setColumnMatrix(final int column, final FieldMatrix matrix) + throws OutOfRangeException { checkColumnIndex(column); final int nRows = getRowDimension(); if ((matrix.getRowDimension() != nRows) || @@ -542,7 +565,8 @@ public abstract class AbstractFieldMatrix> } /** {@inheritDoc} */ - public void setRowVector(final int row, final FieldVector vector) { + public void setRowVector(final int row, final FieldVector vector) + throws OutOfRangeException { checkRowIndex(row); final int nCols = getColumnDimension(); if (vector.getDimension() != nCols) { @@ -561,7 +585,9 @@ public abstract class AbstractFieldMatrix> } /** {@inheritDoc} */ - public void setColumnVector(final int column, final FieldVector vector) { + public void setColumnVector(final int column, final FieldVector vector) + throws OutOfRangeException { + checkColumnIndex(column); final int nRows = getRowDimension(); if (vector.getDimension() != nRows) { @@ -575,7 +601,7 @@ public abstract class AbstractFieldMatrix> } /** {@inheritDoc} */ - public T[] getRow(final int row) { + public T[] getRow(final int row) throws OutOfRangeException { checkRowIndex(row); final int nCols = getColumnDimension(); final T[] out = buildArray(field, nCols); @@ -588,7 +614,7 @@ public abstract class AbstractFieldMatrix> } /** {@inheritDoc} */ - public void setRow(final int row, final T[] array) { + public void setRow(final int row, final T[] array) throws OutOfRangeException { checkRowIndex(row); final int nCols = getColumnDimension(); if (array.length != nCols) { @@ -601,7 +627,7 @@ public abstract class AbstractFieldMatrix> } /** {@inheritDoc} */ - public T[] getColumn(final int column) { + public T[] getColumn(final int column) throws OutOfRangeException { checkColumnIndex(column); final int nRows = getRowDimension(); final T[] out = buildArray(field, nRows); @@ -614,7 +640,8 @@ public abstract class AbstractFieldMatrix> } /** {@inheritDoc} */ - public void setColumn(final int column, final T[] array) { + public void setColumn(final int column, final T[] array) + throws OutOfRangeException { checkColumnIndex(column); final int nRows = getRowDimension(); if (array.length != nRows) { @@ -665,7 +692,7 @@ public abstract class AbstractFieldMatrix> public abstract int getColumnDimension(); /** {@inheritDoc} */ - public T getTrace() { + public T getTrace() throws NonSquareMatrixException { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (nRows != nCols) { @@ -679,7 +706,7 @@ public abstract class AbstractFieldMatrix> } /** {@inheritDoc} */ - public T[] operate(final T[] v) { + public T[] operate(final T[] v) throws DimensionMismatchException { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); @@ -700,7 +727,8 @@ public abstract class AbstractFieldMatrix> } /** {@inheritDoc} */ - public FieldVector operate(final FieldVector v) { + public FieldVector operate(final FieldVector v) + throws DimensionMismatchException { try { return new ArrayFieldVector(field, operate(((ArrayFieldVector) v).getDataRef()), false); } catch (ClassCastException cce) { @@ -724,7 +752,7 @@ public abstract class AbstractFieldMatrix> } /** {@inheritDoc} */ - public T[] preMultiply(final T[] v) { + public T[] preMultiply(final T[] v) throws DimensionMismatchException { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); @@ -745,7 +773,8 @@ public abstract class AbstractFieldMatrix> } /** {@inheritDoc} */ - public FieldVector preMultiply(final FieldVector v) { + public FieldVector preMultiply(final FieldVector v) + throws DimensionMismatchException { try { return new ArrayFieldVector(field, preMultiply(((ArrayFieldVector) v).getDataRef()), false); } catch (ClassCastException cce) { @@ -859,7 +888,8 @@ public abstract class AbstractFieldMatrix> /** {@inheritDoc} */ public T walkInColumnOrder(final FieldMatrixChangingVisitor visitor, final int startRow, final int endRow, - final int startColumn, final int endColumn) { + final int startColumn, final int endColumn) + throws NumberIsTooSmallException, OutOfRangeException { checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); visitor.start(getRowDimension(), getColumnDimension(), startRow, endRow, startColumn, endColumn); @@ -876,7 +906,8 @@ public abstract class AbstractFieldMatrix> /** {@inheritDoc} */ public T walkInColumnOrder(final FieldMatrixPreservingVisitor visitor, final int startRow, final int endRow, - final int startColumn, final int endColumn) { + final int startColumn, final int endColumn) + throws NumberIsTooSmallException, OutOfRangeException{ checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); visitor.start(getRowDimension(), getColumnDimension(), startRow, endRow, startColumn, endColumn); @@ -1001,7 +1032,7 @@ public abstract class AbstractFieldMatrix> * @param row Row index to check. * @throws OutOfRangeException if {@code index} is not valid. */ - protected void checkRowIndex(final int row) { + protected void checkRowIndex(final int row) throws OutOfRangeException { if (row < 0 || row >= getRowDimension()) { throw new OutOfRangeException(LocalizedFormats.ROW_INDEX, row, 0, getRowDimension() - 1); @@ -1014,7 +1045,8 @@ public abstract class AbstractFieldMatrix> * @param column Column index to check. * @throws OutOfRangeException if {@code index} is not valid. */ - protected void checkColumnIndex(final int column) { + protected void checkColumnIndex(final int column) + throws OutOfRangeException { if (column < 0 || column >= getColumnDimension()) { throw new OutOfRangeException(LocalizedFormats.COLUMN_INDEX, column, 0, getColumnDimension() - 1); @@ -1034,7 +1066,8 @@ public abstract class AbstractFieldMatrix> * {@code endColumn < startColumn}. */ protected void checkSubMatrixIndex(final int startRow, final int endRow, - final int startColumn, final int endColumn) { + final int startColumn, final int endColumn) + throws NumberIsTooSmallException, OutOfRangeException { checkRowIndex(startRow); checkRowIndex(endRow); if (endRow < startRow) { @@ -1060,7 +1093,8 @@ public abstract class AbstractFieldMatrix> * @throws NoDataException if the arrays have zero length. * @throws OutOfRangeException if row or column selections are not valid. */ - protected void checkSubMatrixIndex(final int[] selectedRows, final int[] selectedColumns) { + protected void checkSubMatrixIndex(final int[] selectedRows, final int[] selectedColumns) + throws NoDataException, NullArgumentException, OutOfRangeException { if (selectedRows == null || selectedColumns == null) { throw new NullArgumentException(); @@ -1085,7 +1119,8 @@ public abstract class AbstractFieldMatrix> * @throws MatrixDimensionMismatchException if the matrix is not * addition-compatible with instance. */ - protected void checkAdditionCompatible(final FieldMatrix m) { + protected void checkAdditionCompatible(final FieldMatrix m) + throws MatrixDimensionMismatchException { if ((getRowDimension() != m.getRowDimension()) || (getColumnDimension() != m.getColumnDimension())) { throw new MatrixDimensionMismatchException(m.getRowDimension(), m.getColumnDimension(), @@ -1100,7 +1135,8 @@ public abstract class AbstractFieldMatrix> * @throws MatrixDimensionMismatchException if the matrix is not * subtraction-compatible with instance. */ - protected void checkSubtractionCompatible(final FieldMatrix m) { + protected void checkSubtractionCompatible(final FieldMatrix m) + throws MatrixDimensionMismatchException { if ((getRowDimension() != m.getRowDimension()) || (getColumnDimension() != m.getColumnDimension())) { throw new MatrixDimensionMismatchException(m.getRowDimension(), m.getColumnDimension(), @@ -1115,7 +1151,8 @@ public abstract class AbstractFieldMatrix> * @throws DimensionMismatchException if the matrix is not * multiplication-compatible with instance. */ - protected void checkMultiplicationCompatible(final FieldMatrix m) { + protected void checkMultiplicationCompatible(final FieldMatrix m) + throws DimensionMismatchException { if (getColumnDimension() != m.getRowDimension()) { throw new DimensionMismatchException(m.getRowDimension(), getColumnDimension()); } diff --git a/src/main/java/org/apache/commons/math3/linear/ConjugateGradient.java b/src/main/java/org/apache/commons/math3/linear/ConjugateGradient.java index 5681e3699..a7b80a43f 100644 --- a/src/main/java/org/apache/commons/math3/linear/ConjugateGradient.java +++ b/src/main/java/org/apache/commons/math3/linear/ConjugateGradient.java @@ -118,9 +118,11 @@ public class ConjugateGradient * @param delta the δ parameter for the default stopping criterion * @param check {@code true} if positive definiteness of both matrix and * preconditioner should be checked + * @throws NullArgumentException if {@code manager} is {@code null} */ public ConjugateGradient(final IterationManager manager, - final double delta, final boolean check) { + final double delta, final boolean check) + throws NullArgumentException { super(manager); this.delta = delta; this.check = check; @@ -136,12 +138,20 @@ public class ConjugateGradient return check; } - /** {@inheritDoc} */ + /** + * {@inheritDoc} + * + * @throws NonPositiveDefiniteOperatorException if {@code a} or {@code m} is + * not positive definite + */ @Override public RealVector solveInPlace(final RealLinearOperator a, - final RealLinearOperator m, final RealVector b, final RealVector x0) - throws NullArgumentException, NonSquareOperatorException, - DimensionMismatchException, MaxCountExceededException { + final RealLinearOperator m, + final RealVector b, + final RealVector x0) + throws NullArgumentException, NonPositiveDefiniteOperatorException, + NonSquareOperatorException, DimensionMismatchException, + MaxCountExceededException, NonPositiveDefiniteOperatorException { checkParameters(a, m, b, x0); final IterationManager manager = getIterationManager(); // Initialization of default stopping criterion diff --git a/src/main/java/org/apache/commons/math3/linear/EigenDecomposition.java b/src/main/java/org/apache/commons/math3/linear/EigenDecomposition.java index ceb278e63..cdf815083 100644 --- a/src/main/java/org/apache/commons/math3/linear/EigenDecomposition.java +++ b/src/main/java/org/apache/commons/math3/linear/EigenDecomposition.java @@ -111,7 +111,8 @@ public class EigenDecomposition { * @throws MathArithmeticException if the decomposition of a general matrix * results in a matrix with zero norm */ - public EigenDecomposition(final RealMatrix matrix) { + public EigenDecomposition(final RealMatrix matrix) + throws MathArithmeticException { if (isSymmetric(matrix, false)) { transformToTridiagonal(matrix); findEigenVectors(transformer.getQ().getData()); @@ -127,12 +128,15 @@ public class EigenDecomposition { * @param matrix Matrix to decompose. * @param splitTolerance Dummy parameter (present for backward * compatibility only). + * @throws MathArithmeticException if the decomposition of a general matrix + * results in a matrix with zero norm * @throws MaxCountExceededException if the algorithm fails to converge. * @deprecated in 3.1 (to be removed in 4.0) due to unused parameter */ @Deprecated public EigenDecomposition(final RealMatrix matrix, - final double splitTolerance) { + final double splitTolerance) + throws MathArithmeticException { this(matrix); } @@ -761,7 +765,8 @@ public class EigenDecomposition { * @param schur the schur transformation of the matrix * @throws MathArithmeticException if the Schur form has a norm of zero */ - private void findEigenVectorsFromSchur(final SchurTransformer schur) { + private void findEigenVectorsFromSchur(final SchurTransformer schur) + throws MathArithmeticException { final double[][] matrixT = schur.getT().getData(); final double[][] matrixP = schur.getP().getData(); diff --git a/src/main/java/org/apache/commons/math3/linear/FieldMatrix.java b/src/main/java/org/apache/commons/math3/linear/FieldMatrix.java index fc0110b7a..995c98970 100644 --- a/src/main/java/org/apache/commons/math3/linear/FieldMatrix.java +++ b/src/main/java/org/apache/commons/math3/linear/FieldMatrix.java @@ -20,6 +20,14 @@ package org.apache.commons.math3.linear; import org.apache.commons.math3.Field; import org.apache.commons.math3.FieldElement; +import org.apache.commons.math3.exception.DimensionMismatchException; +import org.apache.commons.math3.exception.NoDataException; +import org.apache.commons.math3.exception.NotPositiveException; +import org.apache.commons.math3.exception.NotStrictlyPositiveException; +import org.apache.commons.math3.exception.NullArgumentException; +import org.apache.commons.math3.exception.NumberIsTooSmallException; +import org.apache.commons.math3.exception.OutOfRangeException; +import org.apache.commons.math3.exception.ZeroException; /** * Interface defining field-valued matrix with basic algebraic operations. @@ -45,11 +53,12 @@ public interface FieldMatrix> extends AnyMatrix { * @param rowDimension the number of rows in the new matrix * @param columnDimension the number of columns in the new matrix * @return a new matrix of the same type as the instance - * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException - * if row or column dimension is not positive. + * @throws NotStrictlyPositiveException if row or column dimension is not + * positive. * @since 2.0 */ - FieldMatrix createMatrix(final int rowDimension, final int columnDimension); + FieldMatrix createMatrix(final int rowDimension, final int columnDimension) + throws NotStrictlyPositiveException; /** * Make a (deep) copy of this. @@ -63,20 +72,20 @@ public interface FieldMatrix> extends AnyMatrix { * * @param m Matrix to be added. * @return {@code this} + {@code m}. - * @throws MatrixDimensionMismatchException - * if {@code m} is not the same size as this matrix. + * @throws MatrixDimensionMismatchException if {@code m} is not the same + * size as {@code this} matrix. */ - FieldMatrix add(FieldMatrix m); + FieldMatrix add(FieldMatrix m) throws MatrixDimensionMismatchException; /** * Subtract {@code m} from this matrix. * * @param m Matrix to be subtracted. * @return {@code this} - {@code m}. - * @throws MatrixDimensionMismatchException - * if {@code m} is not the same size as this matrix. + * @throws MatrixDimensionMismatchException if {@code m} is not the same + * size as {@code this} matrix. */ - FieldMatrix subtract(FieldMatrix m); + FieldMatrix subtract(FieldMatrix m) throws MatrixDimensionMismatchException; /** * Increment each entry of this matrix. @@ -99,32 +108,34 @@ public interface FieldMatrix> extends AnyMatrix { * * @param m Matrix to postmultiply by. * @return {@code this} * {@code m}. - * @throws IllegalArgumentException - * if columnDimension(this) != rowDimension(m) + * @throws DimensionMismatchException if the number of columns of + * {@code this} matrix is not equal to the number of rows of matrix + * {@code m}. */ - FieldMatrix multiply(FieldMatrix m); + FieldMatrix multiply(FieldMatrix m) throws DimensionMismatchException; /** * Premultiply this matrix by {@code m}. * * @param m Matrix to premultiply by. * @return {@code m} * {@code this}. - * @throws org.apache.commons.math3.exception.DimensionMismatchException - * if the number of columns of {@code m} differ from the number of rows - * of this matrix. + * @throws DimensionMismatchException if the number of columns of {@code m} + * differs from the number of rows of {@code this} matrix. */ - FieldMatrix preMultiply(FieldMatrix m); + FieldMatrix preMultiply(FieldMatrix m) throws DimensionMismatchException; /** * Returns the result multiplying this with itself p times. - * Depending on the type of the field elements, T, - * instability for high powers might occur. - * @param p raise this to power p - * @return this^p - * @throws IllegalArgumentException if p < 0 - * NonSquareMatrixException if the matrix is not square + * Depending on the type of the field elements, T, instability for high + * powers might occur. + * + * @param p raise this to power p + * @return this^p + * @throws NotPositiveException if {@code p < 0} + * @throws NonSquareMatrixException if {@code this matrix} is not square */ - FieldMatrix power(final int p); + FieldMatrix power(final int p) throws NonSquareMatrixException, + NotPositiveException; /** * Returns matrix entries as a two-dimensional array. @@ -141,12 +152,13 @@ public interface FieldMatrix> extends AnyMatrix { * @param endRow Final row index (inclusive) * @param startColumn Initial column index * @param endColumn Final column index (inclusive) - * @return the matrix containing the data of the - * specified rows and columns. - * @throws org.apache.commons.math3.exception.OutOfRangeException - * if the indices are not valid. + * @return the matrix containing the data of the specified rows and columns. + * @throws NumberIsTooSmallException is {@code endRow < startRow} of + * {@code endColumn < startColumn}. + * @throws OutOfRangeException if the indices are not valid. */ - FieldMatrix getSubMatrix(int startRow, int endRow, int startColumn, int endColumn); + FieldMatrix getSubMatrix(int startRow, int endRow, int startColumn, int endColumn) + throws NumberIsTooSmallException, OutOfRangeException; /** * Get a submatrix. Rows and columns are indicated @@ -156,10 +168,14 @@ public interface FieldMatrix> extends AnyMatrix { * @param selectedColumns Array of column indices. * @return the matrix containing the data in the * specified rows and columns. - * @throws org.apache.commons.math3.exception.OutOfRangeException - * if row or column selections are not valid. + * @throws NoDataException if {@code selectedRows} or + * {@code selectedColumns} is empty + * @throws NullArgumentException if {@code selectedRows} or + * {@code selectedColumns} is {@code null}. + * @throws OutOfRangeException if row or column selections are not valid. */ - FieldMatrix getSubMatrix(int[] selectedRows, int[] selectedColumns); + FieldMatrix getSubMatrix(int[] selectedRows, int[] selectedColumns) + throws NoDataException, NullArgumentException, OutOfRangeException; /** * Copy a submatrix. Rows and columns are indicated @@ -171,12 +187,17 @@ public interface FieldMatrix> extends AnyMatrix { * @param endColumn Final column index (inclusive). * @param destination The arrays where the submatrix data should be copied * (if larger than rows/columns counts, only the upper-left part will be used). - * @throws org.apache.commons.math3.exception.OutOfRangeException - * if the indices are not valid. + * @throws MatrixDimensionMismatchException if the dimensions of + * {@code destination} do not match those of {@code this}. + * @throws NumberIsTooSmallException is {@code endRow < startRow} of + * {@code endColumn < startColumn}. + * @throws OutOfRangeException if the indices are not valid. * @exception IllegalArgumentException if the destination array is too small. */ - void copySubMatrix(int startRow, int endRow, int startColumn, int endColumn, - T[][] destination); + void copySubMatrix(int startRow, int endRow, int startColumn, int endColumn, + T[][] destination) + throws MatrixDimensionMismatchException, NumberIsTooSmallException, + OutOfRangeException; /** * Copy a submatrix. Rows and columns are indicated @@ -186,11 +207,17 @@ public interface FieldMatrix> extends AnyMatrix { * @param selectedColumns Array of column indices. * @param destination Arrays where the submatrix data should be copied * (if larger than rows/columns counts, only the upper-left part will be used) - * @throws org.apache.commons.math3.exception.OutOfRangeException - * if the indices are not valid. - * @exception IllegalArgumentException if the destination array is too small + * @throws MatrixDimensionMismatchException if the dimensions of + * {@code destination} do not match those of {@code this}. + * @throws NoDataException if {@code selectedRows} or + * {@code selectedColumns} is empty + * @throws NullArgumentException if {@code selectedRows} or + * {@code selectedColumns} is {@code null}. + * @throws OutOfRangeException if the indices are not valid. */ - void copySubMatrix(int[] selectedRows, int[] selectedColumns, T[][] destination); + void copySubMatrix(int[] selectedRows, int[] selectedColumns, T[][] destination) + throws MatrixDimensionMismatchException, NoDataException, NullArgumentException, + OutOfRangeException; /** * Replace the submatrix starting at {@code (row, column)} using data in @@ -224,7 +251,9 @@ public interface FieldMatrix> extends AnyMatrix { * if {@code subMatrix} is {@code null}. * @since 2.0 */ - void setSubMatrix(T[][] subMatrix, int row, int column); + void setSubMatrix(T[][] subMatrix, int row, int column) + throws DimensionMismatchException, MatrixDimensionMismatchException, + NullArgumentException, ZeroException; /** * Get the entries in row number {@code row} @@ -232,10 +261,9 @@ public interface FieldMatrix> extends AnyMatrix { * * @param row Row to be fetched. * @return a row matrix. - * @throws org.apache.commons.math3.exception.OutOfRangeException - * if the specified row index is invalid. + * @throws OutOfRangeException if the specified row index is invalid. */ - FieldMatrix getRowMatrix(int row); + FieldMatrix getRowMatrix(int row) throws OutOfRangeException; /** * Set the entries in row number {@code row} @@ -244,12 +272,12 @@ public interface FieldMatrix> extends AnyMatrix { * @param row Row to be set. * @param matrix Row matrix (must have one row and the same number * of columns as the instance). - * @throws org.apache.commons.math3.exception.OutOfRangeException - * if the specified row index is invalid. + * @throws OutOfRangeException if the specified row index is invalid. * @throws MatrixDimensionMismatchException * if the matrix dimensions do not match one instance row. */ - void setRowMatrix(int row, FieldMatrix matrix); + void setRowMatrix(int row, FieldMatrix matrix) + throws MatrixDimensionMismatchException, OutOfRangeException; /** * Get the entries in column number {@code column} @@ -257,10 +285,9 @@ public interface FieldMatrix> extends AnyMatrix { * * @param column Column to be fetched. * @return a column matrix. - * @throws org.apache.commons.math3.exception.OutOfRangeException - * if the specified column index is invalid. + * @throws OutOfRangeException if the specified column index is invalid. */ - FieldMatrix getColumnMatrix(int column); + FieldMatrix getColumnMatrix(int column) throws OutOfRangeException; /** * Set the entries in column number {@code column} @@ -269,12 +296,12 @@ public interface FieldMatrix> extends AnyMatrix { * @param column Column to be set. * @param matrix column matrix (must have one column and the same * number of rows as the instance). - * @throws org.apache.commons.math3.exception.OutOfRangeException - * if the specified column index is invalid. - * @throws MatrixDimensionMismatchException - * if the matrix dimensions do not match one instance column. + * @throws OutOfRangeException if the specified column index is invalid. + * @throws MatrixDimensionMismatchException if the matrix dimensions do + * not match one instance column. */ - void setColumnMatrix(int column, FieldMatrix matrix); + void setColumnMatrix(int column, FieldMatrix matrix) + throws MatrixDimensionMismatchException, OutOfRangeException; /** * Get the entries in row number {@code row} @@ -282,10 +309,9 @@ public interface FieldMatrix> extends AnyMatrix { * * @param row Row to be fetched * @return a row vector. - * @throws org.apache.commons.math3.exception.OutOfRangeException - * if the specified row index is invalid. + * @throws OutOfRangeException if the specified row index is invalid. */ - FieldVector getRowVector(int row); + FieldVector getRowVector(int row) throws OutOfRangeException; /** * Set the entries in row number {@code row} @@ -294,12 +320,12 @@ public interface FieldMatrix> extends AnyMatrix { * @param row Row to be set. * @param vector row vector (must have the same number of columns * as the instance). - * @throws org.apache.commons.math3.exception.OutOfRangeException - * if the specified row index is invalid. - * @throws MatrixDimensionMismatchException - * if the vector dimension does not match one instance row. + * @throws OutOfRangeException if the specified row index is invalid. + * @throws MatrixDimensionMismatchException if the vector dimension does not + * match one instance row. */ - void setRowVector(int row, FieldVector vector); + void setRowVector(int row, FieldVector vector) + throws MatrixDimensionMismatchException, OutOfRangeException; /** * Returns the entries in column number {@code column} @@ -307,10 +333,9 @@ public interface FieldMatrix> extends AnyMatrix { * * @param column Column to be fetched. * @return a column vector. - * @throws org.apache.commons.math3.exception.OutOfRangeException - * if the specified column index is invalid. + * @throws OutOfRangeException if the specified column index is invalid. */ - FieldVector getColumnVector(int column); + FieldVector getColumnVector(int column) throws OutOfRangeException; /** * Set the entries in column number {@code column} @@ -319,22 +344,21 @@ public interface FieldMatrix> extends AnyMatrix { * @param column Column to be set. * @param vector Column vector (must have the same number of rows * as the instance). - * @throws org.apache.commons.math3.exception.OutOfRangeException - * if the specified column index is invalid. - * @throws MatrixDimensionMismatchException - * if the vector dimension does not match one instance column. + * @throws OutOfRangeException if the specified column index is invalid. + * @throws MatrixDimensionMismatchException if the vector dimension does not + * match one instance column. */ - void setColumnVector(int column, FieldVector vector); + void setColumnVector(int column, FieldVector vector) + throws MatrixDimensionMismatchException, OutOfRangeException; /** * Get the entries in row number {@code row} as an array. * * @param row Row to be fetched. * @return array of entries in the row. - * @throws org.apache.commons.math3.exception.OutOfRangeException - * if the specified row index is not valid. + * @throws OutOfRangeException if the specified row index is not valid. */ - T[] getRow(int row); + T[] getRow(int row) throws OutOfRangeException; /** * Set the entries in row number {@code row} @@ -343,22 +367,21 @@ public interface FieldMatrix> extends AnyMatrix { * @param row Row to be set. * @param array Row matrix (must have the same number of columns as * the instance). - * @throws org.apache.commons.math3.exception.OutOfRangeException - * if the specified row index is invalid. - * @throws MatrixDimensionMismatchException - * if the array size does not match one instance row. + * @throws OutOfRangeException if the specified row index is invalid. + * @throws MatrixDimensionMismatchException if the array size does not match + * one instance row. */ - void setRow(int row, T[] array); + void setRow(int row, T[] array) throws MatrixDimensionMismatchException, + OutOfRangeException; /** * Get the entries in column number {@code col} as an array. * * @param column the column to be fetched * @return array of entries in the column - * @throws org.apache.commons.math3.exception.OutOfRangeException - * if the specified column index is not valid. + * @throws OutOfRangeException if the specified column index is not valid. */ - T[] getColumn(int column); + T[] getColumn(int column) throws OutOfRangeException; /** * Set the entries in column number {@code column} @@ -366,12 +389,12 @@ public interface FieldMatrix> extends AnyMatrix { * * @param column the column to be set * @param array column array (must have the same number of rows as the instance) - * @throws org.apache.commons.math3.exception.OutOfRangeException - * if the specified column index is invalid. - * @throws MatrixDimensionMismatchException - * if the array size does not match one instance column. + * @throws OutOfRangeException if the specified column index is invalid. + * @throws MatrixDimensionMismatchException if the array size does not match + * one instance column. */ - void setColumn(int column, T[] array); + void setColumn(int column, T[] array) throws MatrixDimensionMismatchException, + OutOfRangeException; /** * Returns the entry in the specified row and column. @@ -379,10 +402,9 @@ public interface FieldMatrix> extends AnyMatrix { * @param row row location of entry to be fetched * @param column column location of entry to be fetched * @return matrix entry in row,column - * @throws org.apache.commons.math3.exception.OutOfRangeException - * if the row or column index is not valid. + * @throws OutOfRangeException if the row or column index is not valid. */ - T getEntry(int row, int column); + T getEntry(int row, int column) throws OutOfRangeException; /** * Set the entry in the specified row and column. @@ -390,11 +412,10 @@ public interface FieldMatrix> extends AnyMatrix { * @param row row location of entry to be set * @param column column location of entry to be set * @param value matrix entry to be set in row,column - * @throws org.apache.commons.math3.exception.OutOfRangeException - * if the row or column index is not valid. + * @throws OutOfRangeException if the row or column index is not valid. * @since 2.0 */ - void setEntry(int row, int column, T value); + void setEntry(int row, int column, T value) throws OutOfRangeException; /** * Change an entry in the specified row and column. @@ -403,11 +424,10 @@ public interface FieldMatrix> extends AnyMatrix { * @param column Column location of entry to be set. * @param increment Value to add to the current matrix entry in * {@code (row, column)}. - * @throws org.apache.commons.math3.exception.OutOfRangeException - * if the row or column index is not valid. + * @throws OutOfRangeException if the row or column index is not valid. * @since 2.0 */ - void addToEntry(int row, int column, T increment); + void addToEntry(int row, int column, T increment) throws OutOfRangeException; /** * Change an entry in the specified row and column. @@ -416,11 +436,10 @@ public interface FieldMatrix> extends AnyMatrix { * @param column Column location of entry to be set. * @param factor Multiplication factor for the current matrix entry * in {@code (row,column)} - * @throws org.apache.commons.math3.exception.OutOfRangeException - * if the row or column index is not valid. + * @throws OutOfRangeException if the row or column index is not valid. * @since 2.0 */ - void multiplyEntry(int row, int column, T factor); + void multiplyEntry(int row, int column, T factor) throws OutOfRangeException; /** * Returns the transpose of this matrix. @@ -434,46 +453,51 @@ public interface FieldMatrix> extends AnyMatrix { * trace of the matrix (the sum of the elements on the main diagonal). * * @return trace - * @throws NonSquareMatrixException - * if the matrix is not square. + * @throws NonSquareMatrixException if the matrix is not square. */ - T getTrace(); + T getTrace() throws NonSquareMatrixException; /** - * Returns the result of multiplying this by the vector v. + * Returns the result of multiplying this by the vector {@code v}. * * @param v the vector to operate on - * @return this*v - * @throws IllegalArgumentException if columnDimension != v.size() + * @return {@code this * v} + * @throws DimensionMismatchException if the number of columns of + * {@code this} matrix is not equal to the size of the vector {@code v}. */ - T[] operate(T[] v); + T[] operate(T[] v) throws DimensionMismatchException; /** - * Returns the result of multiplying this by the vector v. + * Returns the result of multiplying this by the vector {@code v}. * * @param v the vector to operate on - * @return this*v - * @throws IllegalArgumentException if columnDimension != v.size() + * @return {@code this * v} + * @throws DimensionMismatchException if the number of columns of + * {@code this} matrix is not equal to the size of the vector {@code v}. */ - FieldVector operate(FieldVector v); + FieldVector operate(FieldVector v) throws DimensionMismatchException; /** - * Returns the (row) vector result of premultiplying this by the vector v. + * Returns the (row) vector result of premultiplying this by the vector + * {@code v}. * * @param v the row vector to premultiply by - * @return v*this - * @throws IllegalArgumentException if rowDimension != v.size() + * @return {@code v * this} + * @throws DimensionMismatchException if the number of rows of {@code this} + * matrix is not equal to the size of the vector {@code v} */ - T[] preMultiply(T[] v); + T[] preMultiply(T[] v) throws DimensionMismatchException; /** - * Returns the (row) vector result of premultiplying this by the vector v. + * Returns the (row) vector result of premultiplying this by the vector + * {@code v}. * * @param v the row vector to premultiply by - * @return v*this - * @throws IllegalArgumentException if rowDimension != v.size() + * @return {@code v * this} + * @throws DimensionMismatchException if the number of rows of {@code this} + * matrix is not equal to the size of the vector {@code v} */ - FieldVector preMultiply(FieldVector v); + FieldVector preMultiply(FieldVector v) throws DimensionMismatchException; /** * Visit (and possibly change) all matrix entries in row order. @@ -529,8 +553,7 @@ public interface FieldMatrix> extends AnyMatrix { * @param endRow Final row index (inclusive) * @param startColumn Initial column index * @param endColumn Final column index - * @throws org.apache.commons.math3.exception.OutOfRangeException - * if the indices are not valid. + * @throws OutOfRangeException if the indices are not valid. * @see #walkInRowOrder(FieldMatrixChangingVisitor) * @see #walkInRowOrder(FieldMatrixPreservingVisitor) * @see #walkInRowOrder(FieldMatrixPreservingVisitor, int, int, int, int) @@ -546,7 +569,8 @@ public interface FieldMatrix> extends AnyMatrix { * of the walk */ T walkInRowOrder(FieldMatrixChangingVisitor visitor, - int startRow, int endRow, int startColumn, int endColumn); + int startRow, int endRow, int startColumn, int endColumn) + throws OutOfRangeException; /** * Visit (but don't change) some matrix entries in row order. @@ -558,8 +582,7 @@ public interface FieldMatrix> extends AnyMatrix { * @param endRow Final row index (inclusive) * @param startColumn Initial column index * @param endColumn Final column index - * @throws org.apache.commons.math3.exception.OutOfRangeException - * if the indices are not valid. + * @throws OutOfRangeException if the indices are not valid. * @see #walkInRowOrder(FieldMatrixChangingVisitor) * @see #walkInRowOrder(FieldMatrixPreservingVisitor) * @see #walkInRowOrder(FieldMatrixChangingVisitor, int, int, int, int) @@ -575,7 +598,8 @@ public interface FieldMatrix> extends AnyMatrix { * of the walk */ T walkInRowOrder(FieldMatrixPreservingVisitor visitor, - int startRow, int endRow, int startColumn, int endColumn); + int startRow, int endRow, int startColumn, int endColumn) + throws OutOfRangeException; /** * Visit (and possibly change) all matrix entries in column order. @@ -631,8 +655,9 @@ public interface FieldMatrix> extends AnyMatrix { * @param endRow Final row index (inclusive) * @param startColumn Initial column index * @param endColumn Final column index - * @throws org.apache.commons.math3.exception.OutOfRangeException - * if the indices are not valid. + * @throws NumberIsTooSmallException if {@code endRow < startRow} or + * {@code endColumn < startColumn}. + * @throws OutOfRangeException if the indices are not valid. * @see #walkInRowOrder(FieldMatrixChangingVisitor) * @see #walkInRowOrder(FieldMatrixPreservingVisitor) * @see #walkInRowOrder(FieldMatrixChangingVisitor, int, int, int, int) @@ -648,7 +673,8 @@ public interface FieldMatrix> extends AnyMatrix { * of the walk */ T walkInColumnOrder(FieldMatrixChangingVisitor visitor, - int startRow, int endRow, int startColumn, int endColumn); + int startRow, int endRow, int startColumn, int endColumn) + throws NumberIsTooSmallException, OutOfRangeException; /** * Visit (but don't change) some matrix entries in column order. @@ -660,8 +686,9 @@ public interface FieldMatrix> extends AnyMatrix { * @param endRow Final row index (inclusive) * @param startColumn Initial column index * @param endColumn Final column index - * @throws org.apache.commons.math3.exception.OutOfRangeException - * if the indices are not valid. + * @throws NumberIsTooSmallException if {@code endRow < startRow} or + * {@code endColumn < startColumn}. + * @throws OutOfRangeException if the indices are not valid. * @see #walkInRowOrder(FieldMatrixChangingVisitor) * @see #walkInRowOrder(FieldMatrixPreservingVisitor) * @see #walkInRowOrder(FieldMatrixChangingVisitor, int, int, int, int) @@ -677,7 +704,8 @@ public interface FieldMatrix> extends AnyMatrix { * of the walk */ T walkInColumnOrder(FieldMatrixPreservingVisitor visitor, - int startRow, int endRow, int startColumn, int endColumn); + int startRow, int endRow, int startColumn, int endColumn) + throws NumberIsTooSmallException, OutOfRangeException; /** * Visit (and possibly change) all matrix entries using the fastest possible order. @@ -730,8 +758,9 @@ public interface FieldMatrix> extends AnyMatrix { * @param endRow Final row index (inclusive) * @param startColumn Initial column index * @param endColumn Final column index (inclusive) - * @throws org.apache.commons.math3.exception.OutOfRangeException - * if the indices are not valid. + * @throws NumberIsTooSmallException if {@code endRow < startRow} or + * {@code endColumn < startColumn}. + * @throws OutOfRangeException if the indices are not valid. * @see #walkInRowOrder(FieldMatrixChangingVisitor) * @see #walkInRowOrder(FieldMatrixPreservingVisitor) * @see #walkInRowOrder(FieldMatrixChangingVisitor, int, int, int, int) @@ -747,7 +776,8 @@ public interface FieldMatrix> extends AnyMatrix { * of the walk */ T walkInOptimizedOrder(FieldMatrixChangingVisitor visitor, - int startRow, int endRow, int startColumn, int endColumn); + int startRow, int endRow, int startColumn, int endColumn) + throws NumberIsTooSmallException, OutOfRangeException; /** * Visit (but don't change) some matrix entries using the fastest possible order. @@ -758,8 +788,9 @@ public interface FieldMatrix> extends AnyMatrix { * @param endRow Final row index (inclusive) * @param startColumn Initial column index * @param endColumn Final column index (inclusive) - * @throws org.apache.commons.math3.exception.OutOfRangeException - * if the indices are not valid. + * @throws NumberIsTooSmallException if {@code endRow < startRow} or + * {@code endColumn < startColumn}. + * @throws OutOfRangeException if the indices are not valid. * @see #walkInRowOrder(FieldMatrixChangingVisitor) * @see #walkInRowOrder(FieldMatrixPreservingVisitor) * @see #walkInRowOrder(FieldMatrixChangingVisitor, int, int, int, int) @@ -775,5 +806,6 @@ public interface FieldMatrix> extends AnyMatrix { * of the walk */ T walkInOptimizedOrder(FieldMatrixPreservingVisitor visitor, - int startRow, int endRow, int startColumn, int endColumn); + int startRow, int endRow, int startColumn, int endColumn) + throws NumberIsTooSmallException, OutOfRangeException; } diff --git a/src/main/java/org/apache/commons/math3/linear/MatrixUtils.java b/src/main/java/org/apache/commons/math3/linear/MatrixUtils.java index 1850845fe..ed8b80b29 100644 --- a/src/main/java/org/apache/commons/math3/linear/MatrixUtils.java +++ b/src/main/java/org/apache/commons/math3/linear/MatrixUtils.java @@ -26,7 +26,6 @@ import java.util.Arrays; import org.apache.commons.math3.Field; import org.apache.commons.math3.FieldElement; import org.apache.commons.math3.exception.MathArithmeticException; -import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NumberIsTooSmallException; @@ -122,9 +121,12 @@ public class MatrixUtils { * @throws NoDataException if a row or column is empty. * @throws NullArgumentException if either {@code data} or {@code data[0]} * is {@code null}. + * @throws DimensionMismatchException if {@code data} is not rectangular. * @see #createRealMatrix(int, int) */ - public static RealMatrix createRealMatrix(double[][] data) { + public static RealMatrix createRealMatrix(double[][] data) + throws NullArgumentException, DimensionMismatchException, + NoDataException { if (data == null || data[0] == null) { throw new NullArgumentException(); @@ -152,7 +154,8 @@ public class MatrixUtils { * @see #createFieldMatrix(Field, int, int) * @since 2.0 */ - public static > FieldMatrix createFieldMatrix(T[][] data) { + public static > FieldMatrix createFieldMatrix(T[][] data) + throws DimensionMismatchException, NoDataException, NullArgumentException { if (data == null || data[0] == null) { throw new NullArgumentException(); @@ -244,7 +247,8 @@ public class MatrixUtils { * @throws NoDataException if {@code data} is empty. * @throws NullArgumentException if {@code data} is {@code null}. */ - public static RealVector createRealVector(double[] data) { + public static RealVector createRealVector(double[] data) + throws NoDataException, NullArgumentException { if (data == null) { throw new NullArgumentException(); } @@ -261,7 +265,8 @@ public class MatrixUtils { * @throws NullArgumentException if {@code data} is {@code null}. * @throws ZeroException if {@code data} has 0 elements */ - public static > FieldVector createFieldVector(final T[] data) { + public static > FieldVector createFieldVector(final T[] data) + throws NoDataException, NullArgumentException, ZeroException { if (data == null) { throw new NullArgumentException(); } @@ -280,7 +285,8 @@ public class MatrixUtils { * @throws NoDataException if {@code rowData} is empty. * @throws NullArgumentException if {@code rowData} is {@code null}. */ - public static RealMatrix createRowRealMatrix(double[] rowData) { + public static RealMatrix createRowRealMatrix(double[] rowData) + throws NoDataException, NullArgumentException { if (rowData == null) { throw new NullArgumentException(); } @@ -303,7 +309,8 @@ public class MatrixUtils { * @throws NullArgumentException if {@code rowData} is {@code null}. */ public static > FieldMatrix - createRowFieldMatrix(final T[] rowData) { + createRowFieldMatrix(final T[] rowData) + throws NoDataException, NullArgumentException { if (rowData == null) { throw new NullArgumentException(); } @@ -327,7 +334,8 @@ public class MatrixUtils { * @throws NoDataException if {@code columnData} is empty. * @throws NullArgumentException if {@code columnData} is {@code null}. */ - public static RealMatrix createColumnRealMatrix(double[] columnData) { + public static RealMatrix createColumnRealMatrix(double[] columnData) + throws NoDataException, NullArgumentException { if (columnData == null) { throw new NullArgumentException(); } @@ -350,7 +358,8 @@ public class MatrixUtils { * @throws NullArgumentException if {@code columnData} is {@code null}. */ public static > FieldMatrix - createColumnFieldMatrix(final T[] columnData) { + createColumnFieldMatrix(final T[] columnData) + throws NoDataException, NullArgumentException { if (columnData == null) { throw new NullArgumentException(); } @@ -375,7 +384,8 @@ public class MatrixUtils { * a valid index. */ public static void checkMatrixIndex(final AnyMatrix m, - final int row, final int column) { + final int row, final int column) + throws OutOfRangeException { checkRowIndex(m, row); checkColumnIndex(m, column); } @@ -387,7 +397,8 @@ public class MatrixUtils { * @param row Row index to check. * @throws OutOfRangeException if {@code row} is not a valid index. */ - public static void checkRowIndex(final AnyMatrix m, final int row) { + public static void checkRowIndex(final AnyMatrix m, final int row) + throws OutOfRangeException { if (row < 0 || row >= m.getRowDimension()) { throw new OutOfRangeException(LocalizedFormats.ROW_INDEX, @@ -402,7 +413,8 @@ public class MatrixUtils { * @param column Column index to check. * @throws OutOfRangeException if {@code column} is not a valid index. */ - public static void checkColumnIndex(final AnyMatrix m, final int column) { + public static void checkColumnIndex(final AnyMatrix m, final int column) + throws OutOfRangeException { if (column < 0 || column >= m.getColumnDimension()) { throw new OutOfRangeException(LocalizedFormats.COLUMN_INDEX, column, 0, m.getColumnDimension() - 1); @@ -424,7 +436,8 @@ public class MatrixUtils { */ public static void checkSubMatrixIndex(final AnyMatrix m, final int startRow, final int endRow, - final int startColumn, final int endColumn) { + final int startColumn, final int endColumn) + throws NumberIsTooSmallException, OutOfRangeException { checkRowIndex(m, startRow); checkRowIndex(m, endRow); if (endRow < startRow) { @@ -457,7 +470,8 @@ public class MatrixUtils { */ public static void checkSubMatrixIndex(final AnyMatrix m, final int[] selectedRows, - final int[] selectedColumns) { + final int[] selectedColumns) + throws NoDataException, NullArgumentException, OutOfRangeException { if (selectedRows == null) { throw new NullArgumentException(); } @@ -484,9 +498,11 @@ public class MatrixUtils { * * @param left Left hand side matrix. * @param right Right hand side matrix. - * @throws MatrixDimensionMismatchException if the matrices are not addition compatible. + * @throws MatrixDimensionMismatchException if the matrices are not addition + * compatible. */ - public static void checkAdditionCompatible(final AnyMatrix left, final AnyMatrix right) { + public static void checkAdditionCompatible(final AnyMatrix left, final AnyMatrix right) + throws MatrixDimensionMismatchException { if ((left.getRowDimension() != right.getRowDimension()) || (left.getColumnDimension() != right.getColumnDimension())) { throw new MatrixDimensionMismatchException(left.getRowDimension(), left.getColumnDimension(), @@ -499,9 +515,11 @@ public class MatrixUtils { * * @param left Left hand side matrix. * @param right Right hand side matrix. - * @throws MatrixDimensionMismatchException if the matrices are not addition compatible. + * @throws MatrixDimensionMismatchException if the matrices are not addition + * compatible. */ - public static void checkSubtractionCompatible(final AnyMatrix left, final AnyMatrix right) { + public static void checkSubtractionCompatible(final AnyMatrix left, final AnyMatrix right) + throws MatrixDimensionMismatchException { if ((left.getRowDimension() != right.getRowDimension()) || (left.getColumnDimension() != right.getColumnDimension())) { throw new MatrixDimensionMismatchException(left.getRowDimension(), left.getColumnDimension(), @@ -514,9 +532,12 @@ public class MatrixUtils { * * @param left Left hand side matrix. * @param right Right hand side matrix. - * @throws DimensionMismatchException if matrices are not multiplication compatible. + * @throws DimensionMismatchException if matrices are not multiplication + * compatible. */ - public static void checkMultiplicationCompatible(final AnyMatrix left, final AnyMatrix right) { + public static void checkMultiplicationCompatible(final AnyMatrix left, final AnyMatrix right) + throws DimensionMismatchException { + if (left.getColumnDimension() != right.getRowDimension()) { throw new DimensionMismatchException(left.getColumnDimension(), right.getRowDimension()); @@ -832,19 +853,23 @@ public class MatrixUtils { *

* @param rm RealMatrix which is lower triangular * @param b RealVector this is overwritten - * @exception IllegalArgumentException if the matrix and vector are not conformable - * @exception ArithmeticException there is a zero or near zero on the diagonal of rm + * @throws DimensionMismatchException if the matrix and vector are not + * conformable + * @throws NonSquareMatrixException if the matrix {@code rm} is not square + * @throws MathArithmeticException if the absolute value of one of the diagonal + * coefficient of {@code rm} is lower than {@link Precision#SAFE_MIN} */ - public static void solveLowerTriangularSystem( RealMatrix rm, RealVector b){ + public static void solveLowerTriangularSystem(RealMatrix rm, RealVector b) + throws DimensionMismatchException, MathArithmeticException, + NonSquareMatrixException { if ((rm == null) || (b == null) || ( rm.getRowDimension() != b.getDimension())) { - throw new MathIllegalArgumentException(LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE, + throw new DimensionMismatchException( (rm == null) ? 0 : rm.getRowDimension(), (b == null) ? 0 : b.getDimension()); } if( rm.getColumnDimension() != rm.getRowDimension() ){ - throw new MathIllegalArgumentException(LocalizedFormats.DIMENSIONS_MISMATCH_2x2, - rm.getRowDimension(),rm.getRowDimension(), - rm.getRowDimension(),rm.getColumnDimension()); + throw new NonSquareMatrixException(rm.getRowDimension(), + rm.getColumnDimension()); } int rows = rm.getRowDimension(); for( int i = 0 ; i < rows ; i++ ){ @@ -872,19 +897,24 @@ public class MatrixUtils { *

* @param rm RealMatrix which is upper triangular * @param b RealVector this is overwritten - * @exception IllegalArgumentException if the matrix and vector are not conformable - * @exception ArithmeticException there is a zero or near zero on the diagonal of rm + * @throws DimensionMismatchException if the matrix and vector are not + * conformable + * @throws NonSquareMatrixException if the matrix {@code rm} is not + * square + * @throws MathArithmeticException if the absolute value of one of the diagonal + * coefficient of {@code rm} is lower than {@link Precision#SAFE_MIN} */ - public static void solveUpperTriangularSystem( RealMatrix rm, RealVector b){ + public static void solveUpperTriangularSystem(RealMatrix rm, RealVector b) + throws DimensionMismatchException, MathArithmeticException, + NonSquareMatrixException { if ((rm == null) || (b == null) || ( rm.getRowDimension() != b.getDimension())) { - throw new MathIllegalArgumentException(LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE, + throw new DimensionMismatchException( (rm == null) ? 0 : rm.getRowDimension(), (b == null) ? 0 : b.getDimension()); } if( rm.getColumnDimension() != rm.getRowDimension() ){ - throw new MathIllegalArgumentException(LocalizedFormats.DIMENSIONS_MISMATCH_2x2, - rm.getRowDimension(),rm.getRowDimension(), - rm.getRowDimension(),rm.getColumnDimension()); + throw new NonSquareMatrixException(rm.getRowDimension(), + rm.getColumnDimension()); } int rows = rm.getRowDimension(); for( int i = rows-1 ; i >-1 ; i-- ){ diff --git a/src/main/java/org/apache/commons/math3/linear/OpenMapRealVector.java b/src/main/java/org/apache/commons/math3/linear/OpenMapRealVector.java index a881aabf5..1de5805b4 100644 --- a/src/main/java/org/apache/commons/math3/linear/OpenMapRealVector.java +++ b/src/main/java/org/apache/commons/math3/linear/OpenMapRealVector.java @@ -640,7 +640,7 @@ public class OpenMapRealVector extends SparseRealVector /** {@inheritDoc} */ @Override - public OpenMapRealVector unitVector() { + public OpenMapRealVector unitVector() throws MathArithmeticException { OpenMapRealVector res = copy(); res.unitize(); return res; @@ -648,7 +648,7 @@ public class OpenMapRealVector extends SparseRealVector /** {@inheritDoc} */ @Override - public void unitize() { + public void unitize() throws MathArithmeticException { double norm = getNorm(); if (isDefaultValue(norm)) { throw new MathArithmeticException(LocalizedFormats.ZERO_NORM); diff --git a/src/main/java/org/apache/commons/math3/linear/RealVector.java b/src/main/java/org/apache/commons/math3/linear/RealVector.java index 184b16259..232e5d247 100644 --- a/src/main/java/org/apache/commons/math3/linear/RealVector.java +++ b/src/main/java/org/apache/commons/math3/linear/RealVector.java @@ -321,7 +321,7 @@ public abstract class RealVector { * @throws DimensionMismatchException if the dimensions of {@code this} and * {@code v} do not match */ - public double cosine(RealVector v) { + public double cosine(RealVector v) throws MathArithmeticException { final double norm = getNorm(); final double vNorm = v.getNorm(); @@ -664,11 +664,11 @@ public abstract class RealVector { * @param v vector onto which instance must be projected. * @return projection of the instance onto {@code v}. * @throws MathArithmeticException if {@code this} or {@code v} is the null - * vector - * @throws org.apache.commons.math3.exception.DimensionMismatchException - * if {@code v} is not the same size as this vector. + * vector + * @throws DimensionMismatchException if {@code v} is not the same size as + * this vector. */ - public RealVector projection(final RealVector v) { + public RealVector projection(final RealVector v) throws MathArithmeticException { final double norm2 = v.dotProduct(v); if (norm2 == 0.0) { throw new MathArithmeticException(LocalizedFormats.ZERO_NORM); @@ -710,9 +710,9 @@ public abstract class RealVector { * The instance is not changed by this method. * * @return a unit vector pointing in direction of this vector. - * @throws ArithmeticException if the norm is {@code null}. + * @throws MathArithmeticException if the norm is zero. */ - public RealVector unitVector() { + public RealVector unitVector() throws MathArithmeticException { final double norm = getNorm(); if (norm == 0) { throw new MathArithmeticException(LocalizedFormats.ZERO_NORM); @@ -724,10 +724,9 @@ public abstract class RealVector { * Converts this vector into a unit vector. * The instance itself is changed by this method. * - * @throws org.apache.commons.math3.exception.MathArithmeticException - * if the norm is zero. + * @throws MathArithmeticException if the norm is zero. */ - public void unitize() { + public void unitize() throws MathArithmeticException { final double norm = getNorm(); if (norm == 0) { throw new MathArithmeticException(LocalizedFormats.ZERO_NORM); @@ -1351,7 +1350,7 @@ public abstract class RealVector { /** {@inheritDoc} */ @Override - public double cosine(RealVector w) { + public double cosine(RealVector w) throws MathArithmeticException { return v.cosine(w); } @@ -1393,7 +1392,7 @@ public abstract class RealVector { /** {@inheritDoc} */ @Override - public RealVector unitVector() { + public RealVector unitVector() throws MathArithmeticException { return v.unitVector(); }