View Javadoc
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  
18  package org.apache.commons.math4.legacy.linear;
19  
20  import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
21  
22  /**
23   * Interface defining very basic matrix operations.
24   *
25   * @since 2.0
26   */
27  public interface AnyMatrix {
28      /**
29       * Indicates whether this is a square matrix.
30       *
31       * @return {@code true} if the number of rows is the same as the number of columns.
32       */
33      default boolean isSquare() {
34          return getRowDimension() == getColumnDimension();
35      }
36  
37      /**
38       * Gets the number of rows.
39       *
40       * @return the number of rows.
41       */
42      int getRowDimension();
43  
44      /**
45       * Gets the number of columns.
46       *
47       * @return the number of columns.
48       */
49      int getColumnDimension();
50  
51      /**
52       * Checks that this matrix and the {@code other} matrix can be added.
53       *
54       * @param other Matrix to be added.
55       * @return {@code false} if the dimensions do not match.
56       */
57      default boolean canAdd(AnyMatrix other) {
58          return getRowDimension() == other.getRowDimension() &&
59              getColumnDimension() == other.getColumnDimension();
60      }
61  
62      /**
63       * Checks that this matrix and the {@code other} matrix can be added.
64       *
65       * @param other Matrix to check.
66       * @throws IllegalArgumentException if the dimensions do not match.
67       */
68      default void checkAdd(AnyMatrix other) {
69          if (!canAdd(other)) {
70              throw new MatrixDimensionMismatchException(getRowDimension(), getColumnDimension(),
71                                                         other.getRowDimension(), other.getColumnDimension());
72          }
73      }
74  
75      /**
76       * Checks that this matrix can be multiplied by the {@code other} matrix.
77       *
78       * @param other Matrix to be added.
79       * @return {@code false} if the dimensions do not match.
80       */
81      default boolean canMultiply(AnyMatrix other) {
82          return getColumnDimension() == other.getRowDimension();
83      }
84  
85      /**
86       * Checks that this matrix can be multiplied by the {@code other} matrix.
87       *
88       * @param other Matrix to check.
89       * @throws IllegalArgumentException if the dimensions do not match.
90       */
91      default void checkMultiply(AnyMatrix other) {
92          if (!canMultiply(other)) {
93              throw new DimensionMismatchException(getColumnDimension(),
94                                                   other.getRowDimension());
95          }
96      }
97  }