AnyMatrix.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.math4.legacy.linear;

  18. import org.apache.commons.math4.legacy.exception.DimensionMismatchException;

  19. /**
  20.  * Interface defining very basic matrix operations.
  21.  *
  22.  * @since 2.0
  23.  */
  24. public interface AnyMatrix {
  25.     /**
  26.      * Indicates whether this is a square matrix.
  27.      *
  28.      * @return {@code true} if the number of rows is the same as the number of columns.
  29.      */
  30.     default boolean isSquare() {
  31.         return getRowDimension() == getColumnDimension();
  32.     }

  33.     /**
  34.      * Gets the number of rows.
  35.      *
  36.      * @return the number of rows.
  37.      */
  38.     int getRowDimension();

  39.     /**
  40.      * Gets the number of columns.
  41.      *
  42.      * @return the number of columns.
  43.      */
  44.     int getColumnDimension();

  45.     /**
  46.      * Checks that this matrix and the {@code other} matrix can be added.
  47.      *
  48.      * @param other Matrix to be added.
  49.      * @return {@code false} if the dimensions do not match.
  50.      */
  51.     default boolean canAdd(AnyMatrix other) {
  52.         return getRowDimension() == other.getRowDimension() &&
  53.             getColumnDimension() == other.getColumnDimension();
  54.     }

  55.     /**
  56.      * Checks that this matrix and the {@code other} matrix can be added.
  57.      *
  58.      * @param other Matrix to check.
  59.      * @throws IllegalArgumentException if the dimensions do not match.
  60.      */
  61.     default void checkAdd(AnyMatrix other) {
  62.         if (!canAdd(other)) {
  63.             throw new MatrixDimensionMismatchException(getRowDimension(), getColumnDimension(),
  64.                                                        other.getRowDimension(), other.getColumnDimension());
  65.         }
  66.     }

  67.     /**
  68.      * Checks that this matrix can be multiplied by the {@code other} matrix.
  69.      *
  70.      * @param other Matrix to be added.
  71.      * @return {@code false} if the dimensions do not match.
  72.      */
  73.     default boolean canMultiply(AnyMatrix other) {
  74.         return getColumnDimension() == other.getRowDimension();
  75.     }

  76.     /**
  77.      * Checks that this matrix can be multiplied by the {@code other} matrix.
  78.      *
  79.      * @param other Matrix to check.
  80.      * @throws IllegalArgumentException if the dimensions do not match.
  81.      */
  82.     default void checkMultiply(AnyMatrix other) {
  83.         if (!canMultiply(other)) {
  84.             throw new DimensionMismatchException(getColumnDimension(),
  85.                                                  other.getRowDimension());
  86.         }
  87.     }
  88. }