|
| 1 | +package com.thealgorithms.matrix; |
| 2 | + |
| 3 | +/** |
| 4 | + * LU Decomposition Algorithm |
| 5 | + * -------------------------- |
| 6 | + * Decomposes a square matrix A into a product of two matrices: |
| 7 | + * A = L * U |
| 8 | + * where: |
| 9 | + * - L is a lower triangular matrix with 1s on its diagonal |
| 10 | + * - U is an upper triangular matrix |
| 11 | + * |
| 12 | + * This algorithm is widely used in: |
| 13 | + * - Solving systems of linear equations (Ax = b) |
| 14 | + * - Finding matrix inverses |
| 15 | + * - Computing determinants efficiently |
| 16 | + * |
| 17 | + * Time Complexity: O(n³) |
| 18 | + * |
| 19 | + * Reference: |
| 20 | + * https://en.wikipedia.org/wiki/LU_decomposition |
| 21 | + * |
| 22 | + * Example: |
| 23 | + * >>> double[][] A = { |
| 24 | + * >>> {2, -1, -2}, |
| 25 | + * >>> {-4, 6, 3}, |
| 26 | + * >>> {-4, -2, 8} |
| 27 | + * >>> }; |
| 28 | + * >>> LUDecomposition.LU result = LUDecomposition.decompose(A); |
| 29 | + * >>> LUDecomposition.printMatrix(result.L); |
| 30 | + * >>> LUDecomposition.printMatrix(result.U); |
| 31 | + * |
| 32 | + * Expected Output: |
| 33 | + * L = |
| 34 | + * [1.000, 0.000, 0.000] |
| 35 | + * [-2.000, 1.000, 0.000] |
| 36 | + * [-2.000, -1.000, 1.000] |
| 37 | + * |
| 38 | + * U = |
| 39 | + * [2.000, -1.000, -2.000] |
| 40 | + * [0.000, 4.000, -1.000] |
| 41 | + * [0.000, 0.000, 3.000] |
| 42 | + */ |
| 43 | + |
| 44 | +public class LUDecomposition { |
| 45 | + |
| 46 | + /** |
| 47 | + * A helper class to store both L and U matrices |
| 48 | + */ |
| 49 | + public static class LU { |
| 50 | + double[][] L; |
| 51 | + double[][] U; |
| 52 | + |
| 53 | + LU(double[][] L, double[][] U) { |
| 54 | + this.L = L; |
| 55 | + this.U = U; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Performs LU Decomposition on a square matrix A |
| 61 | + * @param A input square matrix |
| 62 | + * @return LU object containing L and U matrices |
| 63 | + */ |
| 64 | + public static LU decompose(double[][] A) { |
| 65 | + int n = A.length; |
| 66 | + double[][] L = new double[n][n]; |
| 67 | + double[][] U = new double[n][n]; |
| 68 | + |
| 69 | + for (int i = 0; i < n; i++) { |
| 70 | + // Upper Triangular Matrix |
| 71 | + for (int k = i; k < n; k++) { |
| 72 | + double sum = 0; |
| 73 | + for (int j = 0; j < i; j++) { |
| 74 | + sum += L[i][j] * U[j][k]; |
| 75 | + } |
| 76 | + U[i][k] = A[i][k] - sum; |
| 77 | + } |
| 78 | + |
| 79 | + // Lower Triangular Matrix |
| 80 | + for (int k = i; k < n; k++) { |
| 81 | + if (i == k) { |
| 82 | + L[i][i] = 1; // Diagonal as 1 |
| 83 | + } else { |
| 84 | + double sum = 0; |
| 85 | + for (int j = 0; j < i; j++) { |
| 86 | + sum += L[k][j] * U[j][i]; |
| 87 | + } |
| 88 | + L[k][i] = (A[k][i] - sum) / U[i][i]; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return new LU(L, U); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Utility function to print a matrix |
| 98 | + * @param M matrix to print |
| 99 | + */ |
| 100 | + public static void printMatrix(double[][] M) { |
| 101 | + for (double[] row : M) { |
| 102 | + System.out.print("["); |
| 103 | + for (int j = 0; j < row.length; j++) { |
| 104 | + System.out.printf("%7.3f", row[j]); |
| 105 | + if (j < row.length - 1) |
| 106 | + System.out.print(", "); |
| 107 | + } |
| 108 | + System.out.println("]"); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Demonstration (doctest) |
| 114 | + */ |
| 115 | + public static void main(String[] args) { |
| 116 | + double[][] A = { |
| 117 | + {2, -1, -2}, |
| 118 | + {-4, 6, 3}, |
| 119 | + {-4, -2, 8} |
| 120 | + }; |
| 121 | + |
| 122 | + LU result = decompose(A); |
| 123 | + |
| 124 | + System.out.println("L matrix:"); |
| 125 | + printMatrix(result.L); |
| 126 | + |
| 127 | + System.out.println("\nU matrix:"); |
| 128 | + printMatrix(result.U); |
| 129 | + } |
| 130 | +} |
0 commit comments