|
| 1 | +package com.thealgorithms.maths; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | + |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +public class LUDecompositionTest { |
| 9 | + |
| 10 | + private static final double DELTA = 1e-9; |
| 11 | + |
| 12 | + @Test |
| 13 | + public void testDecomposeSimpleMatrix() { |
| 14 | + double[][] matrix = {{2, 1, 1}, {4, 3, 3}, {8, 7, 9}}; |
| 15 | + double[][] lu = LUDecomposition.decompose(matrix); |
| 16 | + |
| 17 | + double[][] lower = LUDecomposition.getLowerMatrix(lu); |
| 18 | + double[][] upper = LUDecomposition.getUpperMatrix(lu); |
| 19 | + |
| 20 | + assertArrayEquals(new double[] {1, 1, 1}, new double[] {lower[0][0], lower[1][1], lower[2][2]}, DELTA); |
| 21 | + |
| 22 | + double[][] product = multiply(lower, upper); |
| 23 | + assertArrayEquals(new double[] {2, 1, 1, 4, 3, 3, 8, 7, 9}, flatten(product), DELTA); |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + public void testDecomposeTwoByTwo() { |
| 28 | + double[][] matrix = {{1, 2}, {3, 4}}; |
| 29 | + double[][] lu = LUDecomposition.decompose(matrix); |
| 30 | + |
| 31 | + double[][] lower = LUDecomposition.getLowerMatrix(lu); |
| 32 | + double[][] upper = LUDecomposition.getUpperMatrix(lu); |
| 33 | + |
| 34 | + double[][] product = multiply(lower, upper); |
| 35 | + assertArrayEquals(new double[] {1, 2, 3, 4}, flatten(product), DELTA); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testDecomposeIdentityMatrix() { |
| 40 | + double[][] matrix = {{1, 0}, {0, 1}}; |
| 41 | + double[][] lu = LUDecomposition.decompose(matrix); |
| 42 | + |
| 43 | + double[][] lower = LUDecomposition.getLowerMatrix(lu); |
| 44 | + double[][] upper = LUDecomposition.getUpperMatrix(lu); |
| 45 | + |
| 46 | + assertArrayEquals(new double[] {1, 0, 0, 1}, flatten(lower), DELTA); |
| 47 | + assertArrayEquals(new double[] {1, 0, 0, 1}, flatten(upper), DELTA); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testDecomposeNonSquareMatrixThrows() { |
| 52 | + double[][] matrix = {{1, 2, 3}, {4, 5, 6}}; |
| 53 | + assertThrows(IllegalArgumentException.class, () -> LUDecomposition.decompose(matrix)); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testDecomposeSingularMatrixThrows() { |
| 58 | + double[][] matrix = {{0, 1}, {1, 0}}; |
| 59 | + assertThrows(ArithmeticException.class, () -> LUDecomposition.decompose(matrix)); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testSolveLinearSystem() { |
| 64 | + double[][] matrix = {{2, 1, 1}, {4, 3, 3}, {8, 7, 9}}; |
| 65 | + double[] b = {8, 20, 46}; |
| 66 | + double[][] lu = LUDecomposition.decompose(matrix); |
| 67 | + double[] solution = LUDecomposition.solve(lu, b); |
| 68 | + |
| 69 | + assertArrayEquals(new double[] {1, 3, 3}, solution, DELTA); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testSolveTwoByTwoSystem() { |
| 74 | + double[][] matrix = {{2, 1}, {1, 3}}; |
| 75 | + double[] b = {5, 7}; |
| 76 | + double[][] lu = LUDecomposition.decompose(matrix); |
| 77 | + double[] solution = LUDecomposition.solve(lu, b); |
| 78 | + |
| 79 | + assertArrayEquals(new double[] {1.6, 1.8}, solution, DELTA); |
| 80 | + } |
| 81 | + |
| 82 | + private static double[][] multiply(double[][] a, double[][] b) { |
| 83 | + int n = a.length; |
| 84 | + double[][] result = new double[n][n]; |
| 85 | + for (int i = 0; i < n; i++) { |
| 86 | + for (int j = 0; j < n; j++) { |
| 87 | + for (int k = 0; k < n; k++) { |
| 88 | + result[i][j] += a[i][k] * b[k][j]; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + return result; |
| 93 | + } |
| 94 | + |
| 95 | + private static double[] flatten(double[][] matrix) { |
| 96 | + int n = matrix.length; |
| 97 | + double[] result = new double[n * n]; |
| 98 | + int idx = 0; |
| 99 | + for (double[] row : matrix) { |
| 100 | + for (double val : row) { |
| 101 | + result[idx++] = val; |
| 102 | + } |
| 103 | + } |
| 104 | + return result; |
| 105 | + } |
| 106 | +} |
0 commit comments