|
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
4 | 4 | import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | + |
5 | 6 | import org.junit.jupiter.api.Test; |
6 | 7 |
|
7 | 8 | class LUDecompositionTest { |
8 | 9 |
|
9 | | - private static final double EPSILON = 1e-6; |
| 10 | + private static final double EPSILON = 1e-10; |
10 | 11 |
|
11 | 12 | @Test |
12 | 13 | void testBasicLUDecomposition() { |
13 | | - double[][] matrix = { |
14 | | - {2, -1, -2}, |
15 | | - {-4, 6, 3}, |
16 | | - {-4, -2, 8} |
17 | | - }; |
| 14 | + double[][] matrix = {{2, -1, -2}, {-4, 6, 3}, {-4, -2, 8}}; |
18 | 15 |
|
19 | 16 | LUDecomposition.Result result = LUDecomposition.decompose(matrix); |
20 | 17 |
|
21 | | - double[][] expectedL = { |
22 | | - {1.0, 0.0, 0.0}, |
23 | | - {-2.0, 1.0, 0.0}, |
24 | | - {-2.0, -1.0, 1.0} |
25 | | - }; |
| 18 | + double[][] expectedL = {{1.0, 0.0, 0.0}, {-2.0, 1.0, 0.0}, {-2.0, -1.0, 1.0}}; |
26 | 19 |
|
27 | | - double[][] expectedU = { |
28 | | - {2.0, -1.0, -2.0}, |
29 | | - {0.0, 4.0, -1.0}, |
30 | | - {0.0, 0.0, 3.0} |
31 | | - }; |
| 20 | + double[][] expectedU = {{2.0, -1.0, -2.0}, {0.0, 4.0, -1.0}, {0.0, 0.0, 3.0}}; |
32 | 21 |
|
33 | 22 | assertMatrixEquals(expectedL, result.getL()); |
34 | 23 | assertMatrixEquals(expectedU, result.getU()); |
35 | 24 | } |
36 | 25 |
|
37 | 26 | @Test |
38 | 27 | void testIdentityMatrix() { |
39 | | - double[][] identity = { |
40 | | - {1, 0, 0}, |
41 | | - {0, 1, 0}, |
42 | | - {0, 0, 1} |
43 | | - }; |
| 28 | + double[][] matrix = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}; |
44 | 29 |
|
45 | | - LUDecomposition.Result result = LUDecomposition.decompose(identity); |
| 30 | + LUDecomposition.Result result = LUDecomposition.decompose(matrix); |
46 | 31 |
|
47 | | - assertMatrixEquals(identity, result.getL()); |
48 | | - assertMatrixEquals(identity, result.getU()); |
| 32 | + assertMatrixEquals(matrix, result.getL()); |
| 33 | + assertMatrixEquals(matrix, result.getU()); |
49 | 34 | } |
50 | 35 |
|
51 | 36 | @Test |
52 | | - void testTwoByTwoMatrix() { |
53 | | - double[][] matrix = { |
54 | | - {4, 3}, |
55 | | - {6, 3} |
56 | | - }; |
57 | | - |
58 | | - LUDecomposition.Result result = LUDecomposition.decompose(matrix); |
| 37 | + void testSingularMatrix() { |
| 38 | + double[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; |
59 | 39 |
|
60 | | - double[][] expectedL = { |
61 | | - {1.0, 0.0}, |
62 | | - {1.5, 1.0} |
63 | | - }; |
| 40 | + assertThrows(IllegalArgumentException.class, () -> LUDecomposition.decompose(matrix)); |
| 41 | + } |
64 | 42 |
|
65 | | - double[][] expectedU = { |
66 | | - {4.0, 3.0}, |
67 | | - {0.0, -1.5} |
68 | | - }; |
| 43 | + @Test |
| 44 | + void testEmptyMatrix() { |
| 45 | + double[][] matrix = {}; |
69 | 46 |
|
70 | | - assertMatrixEquals(expectedL, result.getL()); |
71 | | - assertMatrixEquals(expectedU, result.getU()); |
| 47 | + assertThrows(IllegalArgumentException.class, () -> LUDecomposition.decompose(matrix)); |
72 | 48 | } |
73 | 49 |
|
74 | 50 | @Test |
75 | 51 | void testNonSquareMatrix() { |
76 | | - double[][] nonSquare = { |
77 | | - {1, 2, 3}, |
78 | | - {4, 5, 6} |
79 | | - }; |
| 52 | + double[][] matrix = {{1, 2, 3}, {4, 5, 6}}; |
80 | 53 |
|
81 | | - assertThrows(IllegalArgumentException.class, () -> LUDecomposition.decompose(nonSquare)); |
| 54 | + assertThrows(IllegalArgumentException.class, () -> LUDecomposition.decompose(matrix)); |
82 | 55 | } |
83 | 56 |
|
84 | 57 | @Test |
85 | | - void testEmptyMatrix() { |
86 | | - double[][] empty = {}; |
| 58 | + void testTwoByTwoMatrix() { |
| 59 | + double[][] matrix = {{4, 3}, {6, 3}}; |
87 | 60 |
|
88 | | - assertThrows(IllegalArgumentException.class, () -> LUDecomposition.decompose(empty)); |
89 | | - } |
| 61 | + LUDecomposition.Result result = LUDecomposition.decompose(matrix); |
90 | 62 |
|
91 | | - @Test |
92 | | - void testSingularMatrix() { |
93 | | - double[][] singular = { |
94 | | - {1, 2, 3}, |
95 | | - {2, 4, 6}, |
96 | | - {3, 6, 9} |
97 | | - }; |
| 63 | + double[][] expectedL = {{1.0, 0.0}, {1.5, 1.0}}; |
| 64 | + |
| 65 | + double[][] expectedU = {{4.0, 3.0}, {0.0, -1.5}}; |
98 | 66 |
|
99 | | - assertThrows(IllegalArgumentException.class, () -> LUDecomposition.decompose(singular)); |
| 67 | + assertMatrixEquals(expectedL, result.getL()); |
| 68 | + assertMatrixEquals(expectedU, result.getU()); |
100 | 69 | } |
101 | 70 |
|
102 | 71 | private void assertMatrixEquals(double[][] expected, double[][] actual) { |
|
0 commit comments