Skip to content

Commit 18bbc9f

Browse files
authored
Update LUDecompositionTest.java
1 parent db6f72c commit 18bbc9f

File tree

1 file changed

+27
-58
lines changed

1 file changed

+27
-58
lines changed

src/test/java/com/thealgorithms/matrix/LUDecompositionTest.java

Lines changed: 27 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,101 +2,70 @@
22

33
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
44
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
56
import org.junit.jupiter.api.Test;
67

78
class LUDecompositionTest {
89

9-
private static final double EPSILON = 1e-6;
10+
private static final double EPSILON = 1e-10;
1011

1112
@Test
1213
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}};
1815

1916
LUDecomposition.Result result = LUDecomposition.decompose(matrix);
2017

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}};
2619

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}};
3221

3322
assertMatrixEquals(expectedL, result.getL());
3423
assertMatrixEquals(expectedU, result.getU());
3524
}
3625

3726
@Test
3827
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}};
4429

45-
LUDecomposition.Result result = LUDecomposition.decompose(identity);
30+
LUDecomposition.Result result = LUDecomposition.decompose(matrix);
4631

47-
assertMatrixEquals(identity, result.getL());
48-
assertMatrixEquals(identity, result.getU());
32+
assertMatrixEquals(matrix, result.getL());
33+
assertMatrixEquals(matrix, result.getU());
4934
}
5035

5136
@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}};
5939

60-
double[][] expectedL = {
61-
{1.0, 0.0},
62-
{1.5, 1.0}
63-
};
40+
assertThrows(IllegalArgumentException.class, () -> LUDecomposition.decompose(matrix));
41+
}
6442

65-
double[][] expectedU = {
66-
{4.0, 3.0},
67-
{0.0, -1.5}
68-
};
43+
@Test
44+
void testEmptyMatrix() {
45+
double[][] matrix = {};
6946

70-
assertMatrixEquals(expectedL, result.getL());
71-
assertMatrixEquals(expectedU, result.getU());
47+
assertThrows(IllegalArgumentException.class, () -> LUDecomposition.decompose(matrix));
7248
}
7349

7450
@Test
7551
void testNonSquareMatrix() {
76-
double[][] nonSquare = {
77-
{1, 2, 3},
78-
{4, 5, 6}
79-
};
52+
double[][] matrix = {{1, 2, 3}, {4, 5, 6}};
8053

81-
assertThrows(IllegalArgumentException.class, () -> LUDecomposition.decompose(nonSquare));
54+
assertThrows(IllegalArgumentException.class, () -> LUDecomposition.decompose(matrix));
8255
}
8356

8457
@Test
85-
void testEmptyMatrix() {
86-
double[][] empty = {};
58+
void testTwoByTwoMatrix() {
59+
double[][] matrix = {{4, 3}, {6, 3}};
8760

88-
assertThrows(IllegalArgumentException.class, () -> LUDecomposition.decompose(empty));
89-
}
61+
LUDecomposition.Result result = LUDecomposition.decompose(matrix);
9062

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}};
9866

99-
assertThrows(IllegalArgumentException.class, () -> LUDecomposition.decompose(singular));
67+
assertMatrixEquals(expectedL, result.getL());
68+
assertMatrixEquals(expectedU, result.getU());
10069
}
10170

10271
private void assertMatrixEquals(double[][] expected, double[][] actual) {

0 commit comments

Comments
 (0)