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