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;
import org.apache.commons.math4.exception.DimensionMismatchException;
/**
* Interface defining very basic matrix operations.
@ -46,4 +47,51 @@ public interface AnyMatrix {
* @return the number of columns.
*/
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
* compatible.
*/
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(),
right.getRowDimension(), right.getColumnDimension());
}
public static void checkAdditionCompatible(final AnyMatrix left, final AnyMatrix right) {
left.checkAdd(right);
}
/**
@ -599,13 +594,8 @@ public class MatrixUtils {
* @throws MatrixDimensionMismatchException if the matrices are not addition
* compatible.
*/
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(),
right.getRowDimension(), right.getColumnDimension());
}
public static void checkSubtractionCompatible(final AnyMatrix left, final AnyMatrix right) {
left.checkAdd(right);
}
/**
@ -616,13 +606,8 @@ public class MatrixUtils {
* @throws DimensionMismatchException if matrices are not multiplication
* compatible.
*/
public static void checkMultiplicationCompatible(final AnyMatrix left, final AnyMatrix right)
throws DimensionMismatchException {
if (left.getColumnDimension() != right.getRowDimension()) {
throw new DimensionMismatchException(left.getColumnDimension(),
right.getRowDimension());
}
public static void checkMultiplicationCompatible(final AnyMatrix left, final AnyMatrix right) {
left.checkMultiply(right);
}
/**