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  package org.apache.commons.geometry.euclidean.internal;
    18
    19  /** This class consists exclusively of static matrix utility methods.
    20   */
    21  public final class Matrices {
    22
    23      /** Private constructor. */
    24      private Matrices() {}
    25
    26      /** Compute the determinant of the 2x2 matrix represented by the given values.
    27       * The values are listed in row-major order.
    28       * @param a00 matrix entry <code>a<sub>0,0</sub></code>
    29       * @param a01 matrix entry <code>a<sub>0,1</sub></code>
    30       * @param a10 matrix entry <code>a<sub>1,0</sub></code>
    31       * @param a11 matrix entry <code>a<sub>1,1</sub></code>
    32       * @return computed 2x2 matrix determinant
    33       */
    34      public static double determinant(
    35              final double a00, final double a01,
    36              final double a10, final double a11) {
    37
    38          return (a00 * a11) - (a01 * a10);
    39      }
    40
    41      /** Compute the determinant of the 3x3 matrix represented by the given values.
    42       * The values are listed in row-major order.
    43       * @param a00 matrix entry <code>a<sub>0,0</sub></code>
    44       * @param a01 matrix entry <code>a<sub>0,1</sub></code>
    45       * @param a02 matrix entry <code>a<sub>0,2</sub></code>
    46       * @param a10 matrix entry <code>a<sub>1,0</sub></code>
    47       * @param a11 matrix entry <code>a<sub>1,1</sub></code>
    48       * @param a12 matrix entry <code>a<sub>1,2</sub></code>
    49       * @param a20 matrix entry <code>a<sub>2,0</sub></code>
    50       * @param a21 matrix entry <code>a<sub>2,1</sub></code>
    51       * @param a22 matrix entry <code>a<sub>2,2</sub></code>
    52       * @return computed 3x3 matrix determinant
    53       */
    54      public static double determinant(
    55              final double a00, final double a01, final double a02,
    56              final double a10, final double a11, final double a12,
    57              final double a20, final double a21, final double a22) {
    58
    59          return ((a00 * a11 * a22) + (a01 * a12 * a20) + (a02 * a10 * a21)) -
    60                  ((a00 * a12 * a21) + (a01 * a10 * a22) + (a02 * a11 * a20));
    61      }
    62
    63      /** Check that the given determinant is valid for use in calculating a matrix
    64       * inverse. An {@link IllegalStateException} is thrown if the determinant is
    65       * NaN, infinite, or zero.
    66       * @param det the determinant to check
    67       * @return the checked determinant
    68       * @throws IllegalStateException if the matrix determinant value is NaN, infinite,
    69       *      or zero
    70       */
    71      public static double checkDeterminantForInverse(final double det) {
    72          if (!Vectors.isRealNonZero(det)) {
    73              throw nonInvertibleTransform("matrix determinant is " + det);
    74          }
    75          return det;
    76      }
    77
    78      /** Check that the given matrix element is valid for use in calculation of
    79       * a matrix inverse, throwing an {@link IllegalStateException} if not.
    80       * @param element matrix entry to check
    81       * @return the checked element
    82       * @throws IllegalStateException if the element is not valid for use
    83       *      in calculating a matrix inverse, ie if it is NaN or infinite.
    84       */
    85      public static double checkElementForInverse(final double element) {
    86          if (!Double.isFinite(element)) {
    87              throw nonInvertibleTransform("invalid matrix element: " + element);
    88          }
    89
    90          return element;
    91      }
    92
    93      /** Create an exception indicating that a matrix is not able to be inverted.
    94       * @param msg message containing the specific reason that the matrix cannot
    95       *      be inverted
    96       * @return IllegalStateException containing the given error message
    97       */
    98      private static IllegalStateException nonInvertibleTransform(final String msg) {
    99          return new IllegalStateException("Matrix is not invertible; " + msg);
    100     }
    101 }