-
Notifications
You must be signed in to change notification settings - Fork 21.2k
Expand file tree
/
Copy pathLUDecompositionTest.java
More file actions
106 lines (84 loc) · 3.45 KB
/
LUDecompositionTest.java
File metadata and controls
106 lines (84 loc) · 3.45 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
package com.thealgorithms.maths;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
public class LUDecompositionTest {
private static final double DELTA = 1e-9;
@Test
public void testDecomposeSimpleMatrix() {
double[][] matrix = {{2, 1, 1}, {4, 3, 3}, {8, 7, 9}};
double[][] lu = LUDecomposition.decompose(matrix);
double[][] lower = LUDecomposition.getLowerMatrix(lu);
double[][] upper = LUDecomposition.getUpperMatrix(lu);
assertArrayEquals(new double[] {1, 1, 1}, new double[] {lower[0][0], lower[1][1], lower[2][2]}, DELTA);
double[][] product = multiply(lower, upper);
assertArrayEquals(new double[] {2, 1, 1, 4, 3, 3, 8, 7, 9}, flatten(product), DELTA);
}
@Test
public void testDecomposeTwoByTwo() {
double[][] matrix = {{1, 2}, {3, 4}};
double[][] lu = LUDecomposition.decompose(matrix);
double[][] lower = LUDecomposition.getLowerMatrix(lu);
double[][] upper = LUDecomposition.getUpperMatrix(lu);
double[][] product = multiply(lower, upper);
assertArrayEquals(new double[] {1, 2, 3, 4}, flatten(product), DELTA);
}
@Test
public void testDecomposeIdentityMatrix() {
double[][] matrix = {{1, 0}, {0, 1}};
double[][] lu = LUDecomposition.decompose(matrix);
double[][] lower = LUDecomposition.getLowerMatrix(lu);
double[][] upper = LUDecomposition.getUpperMatrix(lu);
assertArrayEquals(new double[] {1, 0, 0, 1}, flatten(lower), DELTA);
assertArrayEquals(new double[] {1, 0, 0, 1}, flatten(upper), DELTA);
}
@Test
public void testDecomposeNonSquareMatrixThrows() {
double[][] matrix = {{1, 2, 3}, {4, 5, 6}};
assertThrows(IllegalArgumentException.class, () -> LUDecomposition.decompose(matrix));
}
@Test
public void testDecomposeSingularMatrixThrows() {
double[][] matrix = {{0, 1}, {1, 0}};
assertThrows(ArithmeticException.class, () -> LUDecomposition.decompose(matrix));
}
@Test
public void testSolveLinearSystem() {
double[][] matrix = {{2, 1, 1}, {4, 3, 3}, {8, 7, 9}};
double[] b = {8, 20, 46};
double[][] lu = LUDecomposition.decompose(matrix);
double[] solution = LUDecomposition.solve(lu, b);
assertArrayEquals(new double[] {1, 3, 3}, solution, DELTA);
}
@Test
public void testSolveTwoByTwoSystem() {
double[][] matrix = {{2, 1}, {1, 3}};
double[] b = {5, 7};
double[][] lu = LUDecomposition.decompose(matrix);
double[] solution = LUDecomposition.solve(lu, b);
assertArrayEquals(new double[] {1.6, 1.8}, solution, DELTA);
}
private static double[][] multiply(double[][] a, double[][] b) {
int n = a.length;
double[][] result = new double[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
result[i][j] += a[i][k] * b[k][j];
}
}
}
return result;
}
private static double[] flatten(double[][] matrix) {
int n = matrix.length;
double[] result = new double[n * n];
int idx = 0;
for (double[] row : matrix) {
for (double val : row) {
result[idx++] = val;
}
}
return result;
}
}