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  import org.apache.commons.geometry.core.GeometryTestUtils;
    20  import org.junit.Assert;
    21  import org.junit.Test;
    22
    23  public class MatricesTest {
    24
    25      private static final double EPS = 1e-12;
    26
    27      @Test
    28      public void testDeterminant_2x2() {
    29          // act/assert
    30          Assert.assertEquals(1, Matrices.determinant(
    31                  1, 0,
    32                  0, 1), EPS);
    33
    34          Assert.assertEquals(-1, Matrices.determinant(
    35                  -1, 0,
    36                  0, 1), EPS);
    37
    38          Assert.assertEquals(0, Matrices.determinant(
    39                  1, 1,
    40                  1, 1), EPS);
    41
    42          Assert.assertEquals(-2, Matrices.determinant(
    43                  1, 2,
    44                  3, 4), EPS);
    45
    46          Assert.assertEquals(7, Matrices.determinant(
    47                  -5, -4,
    48                  -2, -3), EPS);
    49
    50          Assert.assertEquals(9, Matrices.determinant(
    51                  -1, -2,
    52                  6, 3), EPS);
    53      }
    54
    55      @Test
    56      public void testDeterminant_3x3() {
    57          // act/assert
    58          Assert.assertEquals(1, Matrices.determinant(
    59                  1, 0, 0,
    60                  0, 1, 0,
    61                  0, 0, 1), EPS);
    62
    63          Assert.assertEquals(-1, Matrices.determinant(
    64                  -1, 0, 0,
    65                  0, -1, 0,
    66                  0, 0, -1), EPS);
    67
    68          Assert.assertEquals(0, Matrices.determinant(
    69                  1, 2, 3,
    70                  4, 5, 6,
    71                  7, 8, 9), EPS);
    72
    73          Assert.assertEquals(49, Matrices.determinant(
    74                  2, -3, 1,
    75                  2, 0, -1,
    76                  1, 4, 5), EPS);
    77
    78          Assert.assertEquals(-40, Matrices.determinant(
    79                  -5, 0, -1,
    80                  1, 2, -1,
    81                  -3, 4, 1
    82                  ), EPS);
    83      }
    84
    85      @Test
    86      public void testCheckDeterminantForInverse() {
    87          // act/assert
    88          Assert.assertEquals(1.0, Matrices.checkDeterminantForInverse(1.0), EPS);
    89          Assert.assertEquals(-1.0, Matrices.checkDeterminantForInverse(-1.0), EPS);
    90      }
    91
    92      @Test
    93      public void testCheckDeterminantForInverse_invalid() {
    94          // act/assert
    95          GeometryTestUtils.assertThrows(() -> {
    96              Matrices.checkDeterminantForInverse(0);
    97          }, IllegalStateException.class, "Matrix is not invertible; matrix determinant is 0.0");
    98
    99          GeometryTestUtils.assertThrows(() -> {
    100             Matrices.checkDeterminantForInverse(Double.NaN);
    101         }, IllegalStateException.class, "Matrix is not invertible; matrix determinant is NaN");
    102
    103         GeometryTestUtils.assertThrows(() -> {
    104             Matrices.checkDeterminantForInverse(Double.POSITIVE_INFINITY);
    105         }, IllegalStateException.class, "Matrix is not invertible; matrix determinant is Infinity");
    106
    107         GeometryTestUtils.assertThrows(() -> {
    108             Matrices.checkDeterminantForInverse(Double.NEGATIVE_INFINITY);
    109         }, IllegalStateException.class, "Matrix is not invertible; matrix determinant is -Infinity");
    110     }
    111
    112     @Test
    113     public void testCheckElementForInverse() {
    114         // act/assert
    115         Assert.assertEquals(0.0, Matrices.checkElementForInverse(0.0), EPS);
    116
    117         Assert.assertEquals(1.0, Matrices.checkElementForInverse(1.0), EPS);
    118         Assert.assertEquals(-1.0, Matrices.checkElementForInverse(-1.0), EPS);
    119     }
    120
    121     @Test
    122     public void testCheckElementForInverse_invalid() {
    123         // act/assert
    124         GeometryTestUtils.assertThrows(() -> {
    125             Matrices.checkElementForInverse(Double.NaN);
    126         }, IllegalStateException.class, "Matrix is not invertible; invalid matrix element: NaN");
    127
    128         GeometryTestUtils.assertThrows(() -> {
    129             Matrices.checkElementForInverse(Double.POSITIVE_INFINITY);
    130         }, IllegalStateException.class, "Matrix is not invertible; invalid matrix element: Infinity");
    131
    132         GeometryTestUtils.assertThrows(() -> {
    133             Matrices.checkElementForInverse(Double.NEGATIVE_INFINITY);
    134         }, IllegalStateException.class, "Matrix is not invertible; invalid matrix element: -Infinity");
    135     }
    136 }