|
1 | 1 | package com.thealgorithms.matrix; |
2 | 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; |
| 3 | +/** |
| 4 | + * LU Decomposition is a matrix factorization technique that decomposes a matrix A |
| 5 | + * into the product of a lower triangular matrix L and an upper triangular matrix U. |
| 6 | + * This is useful for solving systems of linear equations, computing determinants, |
| 7 | + * and inverting matrices. |
| 8 | + * |
| 9 | + * @author Hardvan |
| 10 | + */ |
| 11 | +public final class LUDecomposition { |
| 12 | + private LUDecomposition() { |
| 13 | + } |
7 | 14 |
|
8 | | -class LUDecompositionTest { |
9 | | - private static final double EPSILON = 1e-10; |
| 15 | + public static class Result { |
| 16 | + private final double[][] l; |
| 17 | + private final double[][] u; |
10 | 18 |
|
11 | | - @Test |
12 | | - void testBasicLUDecomposition() { |
13 | | - double[][] matrix = {{2, -1, -2}, {-4, 6, 3}, {-4, -2, 8}}; |
14 | | - LUDecomposition.Result result = LUDecomposition.decompose(matrix); |
| 19 | + public Result(double[][] l, double[][] u) { |
| 20 | + this.l = l; |
| 21 | + this.u = u; |
| 22 | + } |
15 | 23 |
|
16 | | - double[][] expectedL = {{1.0, 0.0, 0.0}, {-2.0, 1.0, 0.0}, {-2.0, -1.0, 1.0}}; |
17 | | - double[][] expectedU = {{2.0, -1.0, -2.0}, {0.0, 4.0, -1.0}, {0.0, 0.0, 3.0}}; |
| 24 | + public double[][] getL() { |
| 25 | + return l; |
| 26 | + } |
18 | 27 |
|
19 | | - assertMatrixEquals(expectedL, result.getL()); |
20 | | - assertMatrixEquals(expectedU, result.getU()); |
| 28 | + public double[][] getU() { |
| 29 | + return u; |
| 30 | + } |
21 | 31 | } |
22 | 32 |
|
23 | | - @Test |
24 | | - void testIdentityMatrix() { |
25 | | - double[][] matrix = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}; |
26 | | - LUDecomposition.Result result = LUDecomposition.decompose(matrix); |
27 | | - |
28 | | - assertMatrixEquals(matrix, result.getL()); |
29 | | - assertMatrixEquals(matrix, result.getU()); |
30 | | - } |
| 33 | + /** |
| 34 | + * Performs LU decomposition on the given matrix. |
| 35 | + * |
| 36 | + * @param matrix The input matrix to decompose |
| 37 | + * @return Result object containing L and U matrices |
| 38 | + * @throws IllegalArgumentException if matrix is not square, empty, or singular |
| 39 | + */ |
| 40 | + public static Result decompose(double[][] matrix) { |
| 41 | + if (matrix == null || matrix.length == 0) { |
| 42 | + throw new IllegalArgumentException("Matrix cannot be null or empty"); |
| 43 | + } |
31 | 44 |
|
32 | | - @Test |
33 | | - void testSingularMatrix() { |
34 | | - double[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; |
35 | | - assertThrows(IllegalArgumentException.class, () -> LUDecomposition.decompose(matrix)); |
36 | | - } |
| 45 | + int n = matrix.length; |
| 46 | + if (matrix[0].length != n) { |
| 47 | + throw new IllegalArgumentException("Matrix must be square"); |
| 48 | + } |
37 | 49 |
|
38 | | - @Test |
39 | | - void testEmptyMatrix() { |
40 | | - double[][] matrix = {}; |
41 | | - assertThrows(IllegalArgumentException.class, () -> LUDecomposition.decompose(matrix)); |
42 | | - } |
| 50 | + double[][] l = new double[n][n]; |
| 51 | + double[][] u = new double[n][n]; |
43 | 52 |
|
44 | | - @Test |
45 | | - void testNonSquareMatrix() { |
46 | | - double[][] matrix = {{1, 2, 3}, {4, 5, 6}}; |
47 | | - assertThrows(IllegalArgumentException.class, () -> LUDecomposition.decompose(matrix)); |
48 | | - } |
| 53 | + for (int i = 0; i < n; i++) { |
| 54 | + l[i][i] = 1.0; |
49 | 55 |
|
50 | | - @Test |
51 | | - void testTwoByTwoMatrix() { |
52 | | - double[][] matrix = {{4, 3}, {6, 3}}; |
53 | | - LUDecomposition.Result result = LUDecomposition.decompose(matrix); |
| 56 | + for (int j = i; j < n; j++) { |
| 57 | + double sum = 0.0; |
| 58 | + for (int k = 0; k < i; k++) { |
| 59 | + sum += l[i][k] * u[k][j]; |
| 60 | + } |
| 61 | + u[i][j] = matrix[i][j] - sum; |
| 62 | + } |
54 | 63 |
|
55 | | - double[][] expectedL = {{1.0, 0.0}, {1.5, 1.0}}; |
56 | | - double[][] expectedU = {{4.0, 3.0}, {0.0, -1.5}}; |
| 64 | + for (int j = i + 1; j < n; j++) { |
| 65 | + double sum = 0.0; |
| 66 | + for (int k = 0; k < i; k++) { |
| 67 | + sum += l[j][k] * u[k][i]; |
| 68 | + } |
57 | 69 |
|
58 | | - assertMatrixEquals(expectedL, result.getL()); |
59 | | - assertMatrixEquals(expectedU, result.getU()); |
60 | | - } |
| 70 | + if (Math.abs(u[i][i]) < 1e-10) { |
| 71 | + throw new IllegalArgumentException("Matrix is singular or nearly singular"); |
| 72 | + } |
61 | 73 |
|
62 | | - private void assertMatrixEquals(double[][] expected, double[][] actual) { |
63 | | - for (int i = 0; i < expected.length; i++) { |
64 | | - assertArrayEquals(expected[i], actual[i], EPSILON); |
| 74 | + l[j][i] = (matrix[j][i] - sum) / u[i][i]; |
| 75 | + } |
65 | 76 | } |
| 77 | + |
| 78 | + return new Result(l, u); |
66 | 79 | } |
67 | 80 | } |
0 commit comments