001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.math4.linear;
019
020import org.apache.commons.math4.exception.DimensionMismatchException;
021
022/**
023 * Interface defining very basic matrix operations.
024 *
025 * @since 2.0
026 */
027public interface AnyMatrix {
028    /**
029     * Indicates whether this is a square matrix.
030     *
031     * @return {@code true} if the number of rows is the same as the number of columns.
032     */
033    default boolean isSquare() {
034        return getRowDimension() == getColumnDimension();
035    }
036
037    /**
038     * Gets the number of rows.
039     *
040     * @return the number of rows.
041     */
042    int getRowDimension();
043
044    /**
045     * Gets the number of columns.
046     *
047     * @return the number of columns.
048     */
049    int getColumnDimension();
050
051    /**
052     * Checks that this matrix and the {@code other} matrix can be added.
053     *
054     * @param other Matrix to be added.
055     * @return {@code false} if the dimensions do not match.
056     */
057    default boolean canAdd(AnyMatrix other) {
058        return getRowDimension() == other.getRowDimension() &&
059            getColumnDimension() == other.getColumnDimension();
060    }
061
062    /**
063     * Checks that this matrix and the {@code other} matrix can be added.
064     *
065     * @param other Matrix to check.
066     * @throws IllegalArgumentException if the dimensions do not match.
067     */
068    default void checkAdd(AnyMatrix other) {
069        if (!canAdd(other)) {
070            throw new MatrixDimensionMismatchException(getRowDimension(), getColumnDimension(),
071                                                       other.getRowDimension(), other.getColumnDimension());
072        }
073    }
074
075    /**
076     * Checks that this matrix can be multiplied by the {@code other} matrix.
077     *
078     * @param other Matrix to be added.
079     * @return {@code false} if the dimensions do not match.
080     */
081    default boolean canMultiply(AnyMatrix other) {
082        return getColumnDimension() == other.getRowDimension();
083    }
084
085    /**
086     * Checks that this matrix can be multiplied by the {@code other} matrix.
087     *
088     * @param other Matrix to check.
089     * @throws IllegalArgumentException if the dimensions do not match.
090     */
091    default void checkMultiply(AnyMatrix other) {
092        if (!canMultiply(other)) {
093            throw new DimensionMismatchException(getColumnDimension(),
094                                                 other.getRowDimension());
095        }
096    }
097}