forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLUDecompositionTest.java
More file actions
107 lines (84 loc) · 2.56 KB
/
LUDecompositionTest.java
File metadata and controls
107 lines (84 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.thealgorithms.matrix;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
class LUDecompositionTest {
private static final double EPSILON = 1e-6;
@Test
void testBasicLUDecomposition() {
double[][] matrix = {
{2, -1, -2},
{-4, 6, 3},
{-4, -2, 8}
};
LUDecomposition.Result result = LUDecomposition.decompose(matrix);
double[][] expectedL = {
{1.0, 0.0, 0.0},
{-2.0, 1.0, 0.0},
{-2.0, -1.0, 1.0}
};
double[][] expectedU = {
{2.0, -1.0, -2.0},
{0.0, 4.0, -1.0},
{0.0, 0.0, 3.0}
};
assertMatrixEquals(expectedL, result.getL());
assertMatrixEquals(expectedU, result.getU());
}
@Test
void testIdentityMatrix() {
double[][] identity = {
{1, 0, 0},
{0, 1, 0},
{0, 0, 1}
};
LUDecomposition.Result result = LUDecomposition.decompose(identity);
assertMatrixEquals(identity, result.getL());
assertMatrixEquals(identity, result.getU());
}
@Test
void testTwoByTwoMatrix() {
double[][] matrix = {
{4, 3},
{6, 3}
};
LUDecomposition.Result result = LUDecomposition.decompose(matrix);
double[][] expectedL = {
{1.0, 0.0},
{1.5, 1.0}
};
double[][] expectedU = {
{4.0, 3.0},
{0.0, -1.5}
};
assertMatrixEquals(expectedL, result.getL());
assertMatrixEquals(expectedU, result.getU());
}
@Test
void testNonSquareMatrix() {
double[][] nonSquare = {
{1, 2, 3},
{4, 5, 6}
};
assertThrows(IllegalArgumentException.class, () -> LUDecomposition.decompose(nonSquare));
}
@Test
void testEmptyMatrix() {
double[][] empty = {};
assertThrows(IllegalArgumentException.class, () -> LUDecomposition.decompose(empty));
}
@Test
void testSingularMatrix() {
double[][] singular = {
{1, 2, 3},
{2, 4, 6},
{3, 6, 9}
};
assertThrows(IllegalArgumentException.class, () -> LUDecomposition.decompose(singular));
}
private void assertMatrixEquals(double[][] expected, double[][] actual) {
for (int i = 0; i < expected.length; i++) {
assertArrayEquals(expected[i], actual[i], EPSILON);
}
}
}