Add "default" methods.

This commit is contained in:
Gilles Sadowski 2019-10-27 14:32:58 +01:00
parent 78e4c98096
commit 7acf4d9ccc
2 changed files with 54 additions and 21 deletions

View File

@ -17,6 +17,7 @@
package org.apache.commons.math4.linear; package org.apache.commons.math4.linear;
import org.apache.commons.math4.exception.DimensionMismatchException;
/** /**
* Interface defining very basic matrix operations. * Interface defining very basic matrix operations.
@ -46,4 +47,51 @@ public interface AnyMatrix {
* @return the number of columns. * @return the number of columns.
*/ */
int getColumnDimension(); int getColumnDimension();
/**
* Checks that this matrix and the {@code other} matrix can be added.
*
* @param other Matrix to be added.
* @return {@code false} if the dimensions do not match.
*/
default boolean canAdd(AnyMatrix other) {
return getRowDimension() == other.getRowDimension() &&
getColumnDimension() == other.getColumnDimension();
}
/**
* Checks that this matrix and the {@code other} matrix can be added.
*
* @param other Matrix to check.
* @throws IllegalArgumentException if the dimensions do not match.
*/
default void checkAdd(AnyMatrix other) {
if (!canAdd(other)) {
throw new MatrixDimensionMismatchException(getRowDimension(), getColumnDimension(),
other.getRowDimension(), other.getColumnDimension());
}
}
/**
* Checks that this matrix can be multiplied by the {@code other} matrix.
*
* @param other Matrix to be added.
* @return {@code false} if the dimensions do not match.
*/
default boolean canMultiply(AnyMatrix other) {
return getColumnDimension() == other.getRowDimension();
}
/**
* Checks that this matrix can be multiplied by the {@code other} matrix.
*
* @param other Matrix to check.
* @throws IllegalArgumentException if the dimensions do not match.
*/
default void checkMultiply(AnyMatrix other) {
if (!canMultiply(other)) {
throw new DimensionMismatchException(getColumnDimension(),
other.getRowDimension());
}
}
} }

View File

@ -582,13 +582,8 @@ public class MatrixUtils {
* @throws MatrixDimensionMismatchException if the matrices are not addition * @throws MatrixDimensionMismatchException if the matrices are not addition
* compatible. * compatible.
*/ */
public static void checkAdditionCompatible(final AnyMatrix left, final AnyMatrix right) public static void checkAdditionCompatible(final AnyMatrix left, final AnyMatrix right) {
throws MatrixDimensionMismatchException { left.checkAdd(right);
if ((left.getRowDimension() != right.getRowDimension()) ||
(left.getColumnDimension() != right.getColumnDimension())) {
throw new MatrixDimensionMismatchException(left.getRowDimension(), left.getColumnDimension(),
right.getRowDimension(), right.getColumnDimension());
}
} }
/** /**
@ -599,13 +594,8 @@ public class MatrixUtils {
* @throws MatrixDimensionMismatchException if the matrices are not addition * @throws MatrixDimensionMismatchException if the matrices are not addition
* compatible. * compatible.
*/ */
public static void checkSubtractionCompatible(final AnyMatrix left, final AnyMatrix right) public static void checkSubtractionCompatible(final AnyMatrix left, final AnyMatrix right) {
throws MatrixDimensionMismatchException { left.checkAdd(right);
if ((left.getRowDimension() != right.getRowDimension()) ||
(left.getColumnDimension() != right.getColumnDimension())) {
throw new MatrixDimensionMismatchException(left.getRowDimension(), left.getColumnDimension(),
right.getRowDimension(), right.getColumnDimension());
}
} }
/** /**
@ -616,13 +606,8 @@ public class MatrixUtils {
* @throws DimensionMismatchException if matrices are not multiplication * @throws DimensionMismatchException if matrices are not multiplication
* compatible. * compatible.
*/ */
public static void checkMultiplicationCompatible(final AnyMatrix left, final AnyMatrix right) public static void checkMultiplicationCompatible(final AnyMatrix left, final AnyMatrix right) {
throws DimensionMismatchException { left.checkMultiply(right);
if (left.getColumnDimension() != right.getRowDimension()) {
throw new DimensionMismatchException(left.getColumnDimension(),
right.getRowDimension());
}
} }
/** /**