From 0f0bb842d90359c24aa22c5d60117625eabf4c6b Mon Sep 17 00:00:00 2001 From: Thanay Bodda Date: Mon, 20 Oct 2025 13:12:19 +0300 Subject: [PATCH] feat: Add LU Decomposition algorithm --- .../thealgorithms/matrix/LUDecomposition.java | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/main/java/com/thealgorithms/matrix/LUDecomposition.java diff --git a/src/main/java/com/thealgorithms/matrix/LUDecomposition.java b/src/main/java/com/thealgorithms/matrix/LUDecomposition.java new file mode 100644 index 000000000000..73ade228f633 --- /dev/null +++ b/src/main/java/com/thealgorithms/matrix/LUDecomposition.java @@ -0,0 +1,92 @@ +// File: src/main/java/com/thealgorithms/matrix/LUDecomposition.java +package com.thealgorithms.matrix; + +import java.util.Arrays; + +/** + * Implements the LU Decomposition algorithm for a square matrix. + * + * LU decomposition factors a matrix as the product of a lower triangular matrix (L) + * and an upper triangular matrix (U). This implementation uses the Doolittle algorithm, + * where the diagonal elements of the lower triangular matrix L are all 1s. + */ +public class LUDecomposition { + + /** + * Decomposes a given square matrix into Lower (L) and Upper (U) matrices. + * + * @param matrix The square matrix to be decomposed. + * @return An array containing L and U matrices. result[0] is L, result[1] is U. + * @throws IllegalArgumentException if the matrix is not square. + */ + public static double[][][] decompose(double[][] matrix) { + int n = matrix.length; + if (n == 0 || matrix[0].length != n) { + throw new IllegalArgumentException("Matrix must be square."); + } + + double[][] lower = new double[n][n]; + double[][] upper = new double[n][n]; + + for (int i = 0; i < n; i++) { + // Upper Triangular Matrix + for (int k = i; k < n; k++) { + double sum = 0; + for (int j = 0; j < i; j++) { + sum += (lower[i][j] * upper[j][k]); + } + upper[i][k] = matrix[i][k] - sum; + } + + // Lower Triangular Matrix + for (int k = i; k < n; k++) { + if (i == k) { + lower[i][i] = 1; // Diagonal of L is 1 + } else { + double sum = 0; + for (int j = 0; j < i; j++) { + sum += (lower[k][j] * upper[j][i]); + } + lower[k][i] = (matrix[k][i] - sum) / upper[i][i]; + } + } + } + + return new double[][][] {lower, upper}; + } + + /** + * A utility function to print a matrix. + * @param matrix The matrix to be printed. + */ + public static void printMatrix(double[][] matrix) { + for (double[] row : matrix) { + System.out.println(Arrays.toString(row)); + } + } + + /** + * Main method to demonstrate the LU Decomposition. + * @param args Command-line arguments (not used). + */ + public static void main(String[] args) { + double[][] matrix = { + {2, -1, -2}, + {-4, 6, 3}, + {-4, -2, 8} + }; + + System.out.println("Original Matrix:"); + printMatrix(matrix); + + double[][][] luMatrices = decompose(matrix); + double[][] lower = luMatrices[0]; + double[][] upper = luMatrices[1]; + + System.out.println("\nLower Triangular Matrix (L):"); + printMatrix(lower); + + System.out.println("\nUpper Triangular Matrix (U):"); + printMatrix(upper); + } +} \ No newline at end of file