You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2019/10/28 01:46:01 UTC

[commons-math] 02/08: Add "default" methods.

This is an automated email from the ASF dual-hosted git repository.

erans pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-math.git

commit 7acf4d9ccc05fe9ed544b03c3d2308343e9d5514
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Sun Oct 27 14:32:58 2019 +0100

    Add "default" methods.
---
 .../org/apache/commons/math4/linear/AnyMatrix.java | 48 ++++++++++++++++++++++
 .../apache/commons/math4/linear/MatrixUtils.java   | 27 +++---------
 2 files changed, 54 insertions(+), 21 deletions(-)

diff --git a/src/main/java/org/apache/commons/math4/linear/AnyMatrix.java b/src/main/java/org/apache/commons/math4/linear/AnyMatrix.java
index 0e1bb56..062ed91 100644
--- a/src/main/java/org/apache/commons/math4/linear/AnyMatrix.java
+++ b/src/main/java/org/apache/commons/math4/linear/AnyMatrix.java
@@ -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());
+        }
+    }
 }
diff --git a/src/main/java/org/apache/commons/math4/linear/MatrixUtils.java b/src/main/java/org/apache/commons/math4/linear/MatrixUtils.java
index 04daf94..47d3017 100644
--- a/src/main/java/org/apache/commons/math4/linear/MatrixUtils.java
+++ b/src/main/java/org/apache/commons/math4/linear/MatrixUtils.java
@@ -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);
     }
 
     /**