From ed53379b686891fafc451b4e0423b5c5ba09c23e Mon Sep 17 00:00:00 2001 From: Sourav Pati Date: Sun, 19 Oct 2025 13:20:42 +0530 Subject: [PATCH 1/9] Added LU decomposition algorthm --- .../thealgorithms/matrix/LUDecomposition.java | 130 ++++++++++++++++++ 1 file changed, 130 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..e335feee9525 --- /dev/null +++ b/src/main/java/com/thealgorithms/matrix/LUDecomposition.java @@ -0,0 +1,130 @@ +package com.thealgorithms.matrix; + +/** + * LU Decomposition Algorithm + * -------------------------- + * Decomposes a square matrix A into a product of two matrices: + * A = L * U + * where: + * - L is a lower triangular matrix with 1s on its diagonal + * - U is an upper triangular matrix + * + * This algorithm is widely used in: + * - Solving systems of linear equations (Ax = b) + * - Finding matrix inverses + * - Computing determinants efficiently + * + * Time Complexity: O(n³) + * + * Reference: + * https://en.wikipedia.org/wiki/LU_decomposition + * + * Example: + * >>> double[][] A = { + * >>> {2, -1, -2}, + * >>> {-4, 6, 3}, + * >>> {-4, -2, 8} + * >>> }; + * >>> LUDecomposition.LU result = LUDecomposition.decompose(A); + * >>> LUDecomposition.printMatrix(result.L); + * >>> LUDecomposition.printMatrix(result.U); + * + * Expected Output: + * L = + * [1.000, 0.000, 0.000] + * [-2.000, 1.000, 0.000] + * [-2.000, -1.000, 1.000] + * + * U = + * [2.000, -1.000, -2.000] + * [0.000, 4.000, -1.000] + * [0.000, 0.000, 3.000] + */ + +public class LUDecomposition { + + /** + * A helper class to store both L and U matrices + */ + public static class LU { + double[][] L; + double[][] U; + + LU(double[][] L, double[][] U) { + this.L = L; + this.U = U; + } + } + + /** + * Performs LU Decomposition on a square matrix A + * @param A input square matrix + * @return LU object containing L and U matrices + */ + public static LU decompose(double[][] A) { + int n = A.length; + double[][] L = new double[n][n]; + double[][] U = 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 += L[i][j] * U[j][k]; + } + U[i][k] = A[i][k] - sum; + } + + // Lower Triangular Matrix + for (int k = i; k < n; k++) { + if (i == k) { + L[i][i] = 1; // Diagonal as 1 + } else { + double sum = 0; + for (int j = 0; j < i; j++) { + sum += L[k][j] * U[j][i]; + } + L[k][i] = (A[k][i] - sum) / U[i][i]; + } + } + } + + return new LU(L, U); + } + + /** + * Utility function to print a matrix + * @param M matrix to print + */ + public static void printMatrix(double[][] M) { + for (double[] row : M) { + System.out.print("["); + for (int j = 0; j < row.length; j++) { + System.out.printf("%7.3f", row[j]); + if (j < row.length - 1) + System.out.print(", "); + } + System.out.println("]"); + } + } + + /** + * Demonstration (doctest) + */ + public static void main(String[] args) { + double[][] A = { + {2, -1, -2}, + {-4, 6, 3}, + {-4, -2, 8} + }; + + LU result = decompose(A); + + System.out.println("L matrix:"); + printMatrix(result.L); + + System.out.println("\nU matrix:"); + printMatrix(result.U); + } +} From 238063e269d55e3305ee1b181be07c2beb54c4e6 Mon Sep 17 00:00:00 2001 From: Sourav Pati Date: Sun, 19 Oct 2025 13:44:42 +0530 Subject: [PATCH 2/9] Added LU decomposition algorthim --- .../thealgorithms/matrix/LUDecomposition.java | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/thealgorithms/matrix/LUDecomposition.java b/src/main/java/com/thealgorithms/matrix/LUDecomposition.java index e335feee9525..480da38663ae 100644 --- a/src/main/java/com/thealgorithms/matrix/LUDecomposition.java +++ b/src/main/java/com/thealgorithms/matrix/LUDecomposition.java @@ -4,15 +4,15 @@ * LU Decomposition Algorithm * -------------------------- * Decomposes a square matrix A into a product of two matrices: - * A = L * U + * A = L * U * where: - * - L is a lower triangular matrix with 1s on its diagonal - * - U is an upper triangular matrix + * - L is a lower triangular matrix with 1s on its diagonal + * - U is an upper triangular matrix * * This algorithm is widely used in: - * - Solving systems of linear equations (Ax = b) - * - Finding matrix inverses - * - Computing determinants efficiently + * - Solving systems of linear equations (Ax = b) + * - Finding matrix inverses + * - Computing determinants efficiently * * Time Complexity: O(n³) * @@ -21,9 +21,9 @@ * * Example: * >>> double[][] A = { - * >>> {2, -1, -2}, - * >>> {-4, 6, 3}, - * >>> {-4, -2, 8} + * >>> {2, -1, -2}, + * >>> {-4, 6, 3}, + * >>> {-4, -2, 8} * >>> }; * >>> LUDecomposition.LU result = LUDecomposition.decompose(A); * >>> LUDecomposition.printMatrix(result.L); @@ -58,6 +58,7 @@ public static class LU { /** * Performs LU Decomposition on a square matrix A + * * @param A input square matrix * @return LU object containing L and U matrices */ @@ -95,6 +96,7 @@ public static LU decompose(double[][] A) { /** * Utility function to print a matrix + * * @param M matrix to print */ public static void printMatrix(double[][] M) { @@ -102,8 +104,7 @@ public static void printMatrix(double[][] M) { System.out.print("["); for (int j = 0; j < row.length; j++) { System.out.printf("%7.3f", row[j]); - if (j < row.length - 1) - System.out.print(", "); + if (j < row.length - 1) System.out.print(", "); } System.out.println("]"); } @@ -113,11 +114,7 @@ public static void printMatrix(double[][] M) { * Demonstration (doctest) */ public static void main(String[] args) { - double[][] A = { - {2, -1, -2}, - {-4, 6, 3}, - {-4, -2, 8} - }; + double[][] A = {{2, -1, -2}, {-4, 6, 3}, {-4, -2, 8}}; LU result = decompose(A); From b9f35bf4f4a85adf219f7b6dae40d2d88531506f Mon Sep 17 00:00:00 2001 From: Sourav Pati Date: Sun, 19 Oct 2025 14:15:01 +0530 Subject: [PATCH 3/9] Added LU decomposition algorthim --- src/main/java/com/thealgorithms/matrix/LUDecomposition.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/matrix/LUDecomposition.java b/src/main/java/com/thealgorithms/matrix/LUDecomposition.java index 480da38663ae..f5564166efaf 100644 --- a/src/main/java/com/thealgorithms/matrix/LUDecomposition.java +++ b/src/main/java/com/thealgorithms/matrix/LUDecomposition.java @@ -104,7 +104,9 @@ public static void printMatrix(double[][] M) { System.out.print("["); for (int j = 0; j < row.length; j++) { System.out.printf("%7.3f", row[j]); - if (j < row.length - 1) System.out.print(", "); + if (j < row.length - 1) { + System.out.print(", "); + } } System.out.println("]"); } From fffe0432ee9cc8662378c332ddb3130140fbe56e Mon Sep 17 00:00:00 2001 From: Sourav Pati Date: Sun, 19 Oct 2025 14:26:50 +0530 Subject: [PATCH 4/9] Added LU decomposition algorthim --- .../thealgorithms/matrix/LUDecomposition.java | 105 +++++++----------- 1 file changed, 39 insertions(+), 66 deletions(-) diff --git a/src/main/java/com/thealgorithms/matrix/LUDecomposition.java b/src/main/java/com/thealgorithms/matrix/LUDecomposition.java index f5564166efaf..f7da7bbf3bef 100644 --- a/src/main/java/com/thealgorithms/matrix/LUDecomposition.java +++ b/src/main/java/com/thealgorithms/matrix/LUDecomposition.java @@ -1,106 +1,79 @@ package com.thealgorithms.matrix; /** - * LU Decomposition Algorithm + * LU Decomposition algorithm * -------------------------- - * Decomposes a square matrix A into a product of two matrices: - * A = L * U + * Decomposes a square matrix a into a product of two matrices: + * a = l * u * where: - * - L is a lower triangular matrix with 1s on its diagonal - * - U is an upper triangular matrix - * - * This algorithm is widely used in: - * - Solving systems of linear equations (Ax = b) - * - Finding matrix inverses - * - Computing determinants efficiently - * - * Time Complexity: O(n³) + * - l is a lower triangular matrix with 1s on its diagonal + * - u is an upper triangular matrix * * Reference: - * https://en.wikipedia.org/wiki/LU_decomposition - * - * Example: - * >>> double[][] A = { - * >>> {2, -1, -2}, - * >>> {-4, 6, 3}, - * >>> {-4, -2, 8} - * >>> }; - * >>> LUDecomposition.LU result = LUDecomposition.decompose(A); - * >>> LUDecomposition.printMatrix(result.L); - * >>> LUDecomposition.printMatrix(result.U); - * - * Expected Output: - * L = - * [1.000, 0.000, 0.000] - * [-2.000, 1.000, 0.000] - * [-2.000, -1.000, 1.000] - * - * U = - * [2.000, -1.000, -2.000] - * [0.000, 4.000, -1.000] - * [0.000, 0.000, 3.000] + * https://en.wikipedia.org/wiki/lu_decomposition */ - public class LUDecomposition { + private LUDecomposition() {} + /** - * A helper class to store both L and U matrices + * A helper class to store both l and u matrices */ public static class LU { - double[][] L; - double[][] U; + double[][] l; + double[][] u; - LU(double[][] L, double[][] U) { - this.L = L; - this.U = U; + LU(double[][] l, double[][] u) { + this.l = l; + this.u = u; } } /** - * Performs LU Decomposition on a square matrix A + * Performs LU Decomposition on a square matrix a * - * @param A input square matrix - * @return LU object containing L and U matrices + * @param a input square matrix + * @return LU object containing l and u matrices */ - public static LU decompose(double[][] A) { - int n = A.length; - double[][] L = new double[n][n]; - double[][] U = new double[n][n]; + public static LU decompose(double[][] a) { + int n = a.length; + double[][] l = new double[n][n]; + double[][] u = new double[n][n]; for (int i = 0; i < n; i++) { - // Upper Triangular Matrix + // upper triangular matrix for (int k = i; k < n; k++) { double sum = 0; for (int j = 0; j < i; j++) { - sum += L[i][j] * U[j][k]; + sum += l[i][j] * u[j][k]; } - U[i][k] = A[i][k] - sum; + u[i][k] = a[i][k] - sum; } - // Lower Triangular Matrix + // lower triangular matrix for (int k = i; k < n; k++) { if (i == k) { - L[i][i] = 1; // Diagonal as 1 + l[i][i] = 1; // diagonal as 1 } else { double sum = 0; for (int j = 0; j < i; j++) { - sum += L[k][j] * U[j][i]; + sum += l[k][j] * u[j][i]; } - L[k][i] = (A[k][i] - sum) / U[i][i]; + l[k][i] = (a[k][i] - sum) / u[i][i]; } } } - return new LU(L, U); + return new LU(l, u); } /** * Utility function to print a matrix * - * @param M matrix to print + * @param m matrix to print */ - public static void printMatrix(double[][] M) { - for (double[] row : M) { + public static void printMatrix(double[][] m) { + for (double[] row : m) { System.out.print("["); for (int j = 0; j < row.length; j++) { System.out.printf("%7.3f", row[j]); @@ -116,14 +89,14 @@ public static void printMatrix(double[][] M) { * Demonstration (doctest) */ public static void main(String[] args) { - double[][] A = {{2, -1, -2}, {-4, 6, 3}, {-4, -2, 8}}; + double[][] a = {{2, -1, -2}, {-4, 6, 3}, {-4, -2, 8}}; - LU result = decompose(A); + LU result = decompose(a); - System.out.println("L matrix:"); - printMatrix(result.L); + System.out.println("l matrix:"); + printMatrix(result.l); - System.out.println("\nU matrix:"); - printMatrix(result.U); + System.out.println("\nu matrix:"); + printMatrix(result.u); } -} +} \ No newline at end of file From 5ce7ce0798a7f96e2e51dcb8d3b9fb2e29d828a9 Mon Sep 17 00:00:00 2001 From: Sourav Pati Date: Sun, 19 Oct 2025 14:28:41 +0530 Subject: [PATCH 5/9] Added LU decomposition algorthim --- src/main/java/com/thealgorithms/matrix/LUDecomposition.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/matrix/LUDecomposition.java b/src/main/java/com/thealgorithms/matrix/LUDecomposition.java index f7da7bbf3bef..c396c0f48be8 100644 --- a/src/main/java/com/thealgorithms/matrix/LUDecomposition.java +++ b/src/main/java/com/thealgorithms/matrix/LUDecomposition.java @@ -14,7 +14,8 @@ */ public class LUDecomposition { - private LUDecomposition() {} + private LUDecomposition() { + } /** * A helper class to store both l and u matrices @@ -99,4 +100,4 @@ public static void main(String[] args) { System.out.println("\nu matrix:"); printMatrix(result.u); } -} \ No newline at end of file +} From d992ac5b98dbf2501eea2198781db00559b3392c Mon Sep 17 00:00:00 2001 From: Sourav Pati Date: Sun, 19 Oct 2025 14:32:17 +0530 Subject: [PATCH 6/9] Added LU decomposition algorthim --- src/main/java/com/thealgorithms/matrix/LUDecomposition.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/matrix/LUDecomposition.java b/src/main/java/com/thealgorithms/matrix/LUDecomposition.java index c396c0f48be8..a619200f664a 100644 --- a/src/main/java/com/thealgorithms/matrix/LUDecomposition.java +++ b/src/main/java/com/thealgorithms/matrix/LUDecomposition.java @@ -12,7 +12,7 @@ * Reference: * https://en.wikipedia.org/wiki/lu_decomposition */ -public class LUDecomposition { +public final class LUDecomposition { private LUDecomposition() { } From 1202d090622b47bfb56acfb8518bedc193a4ed8d Mon Sep 17 00:00:00 2001 From: Sourav Pati Date: Sun, 19 Oct 2025 14:55:58 +0530 Subject: [PATCH 7/9] Added LU decomposition algorthim --- .../thealgorithms/matrix/LUDecomposition.java | 15 ------- .../matrix/LUDecompositionTest.java | 43 +++++++++++++++++++ 2 files changed, 43 insertions(+), 15 deletions(-) create mode 100644 src/test/java/com/thealgorithms/matrix/LUDecompositionTest.java diff --git a/src/main/java/com/thealgorithms/matrix/LUDecomposition.java b/src/main/java/com/thealgorithms/matrix/LUDecomposition.java index a619200f664a..e41aaa201338 100644 --- a/src/main/java/com/thealgorithms/matrix/LUDecomposition.java +++ b/src/main/java/com/thealgorithms/matrix/LUDecomposition.java @@ -85,19 +85,4 @@ public static void printMatrix(double[][] m) { System.out.println("]"); } } - - /** - * Demonstration (doctest) - */ - public static void main(String[] args) { - double[][] a = {{2, -1, -2}, {-4, 6, 3}, {-4, -2, 8}}; - - LU result = decompose(a); - - System.out.println("l matrix:"); - printMatrix(result.l); - - System.out.println("\nu matrix:"); - printMatrix(result.u); - } } diff --git a/src/test/java/com/thealgorithms/matrix/LUDecompositionTest.java b/src/test/java/com/thealgorithms/matrix/LUDecompositionTest.java new file mode 100644 index 000000000000..3540cb9e191c --- /dev/null +++ b/src/test/java/com/thealgorithms/matrix/LUDecompositionTest.java @@ -0,0 +1,43 @@ +package com.thealgorithms.matrix; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import org.junit.jupiter.api.Test; + +public class LUDecompositionTest { + + @Test + public void testLUDecomposition() { + double[][] A = { + {4, 3}, + {6, 3} + }; + + // Perform LU decomposition + LUDecomposition.LU lu = LUDecomposition.decompose(A); + double[][] L = lu.l; + double[][] U = lu.u; + + // Reconstruct A from L and U + double[][] reconstructed = multiplyMatrices(L, U); + + // Assert that reconstructed matrix matches original A + for (int i = 0; i < A.length; i++) { + assertArrayEquals(A[i], reconstructed[i], 1e-9); + } + } + + // Helper method to multiply two matrices + private double[][] multiplyMatrices(double[][] A, double[][] B) { + int n = A.length; + double[][] C = 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++) { + C[i][j] += A[i][k] * B[k][j]; + } + } + } + return C; + } +} From f2b078a3c1b6fee029e98e9b53288846f6e47790 Mon Sep 17 00:00:00 2001 From: Sourav Pati Date: Sun, 19 Oct 2025 14:56:46 +0530 Subject: [PATCH 8/9] Added LU decomposition algorthim --- .../java/com/thealgorithms/matrix/LUDecompositionTest.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/test/java/com/thealgorithms/matrix/LUDecompositionTest.java b/src/test/java/com/thealgorithms/matrix/LUDecompositionTest.java index 3540cb9e191c..5114e95a07d8 100644 --- a/src/test/java/com/thealgorithms/matrix/LUDecompositionTest.java +++ b/src/test/java/com/thealgorithms/matrix/LUDecompositionTest.java @@ -8,10 +8,7 @@ public class LUDecompositionTest { @Test public void testLUDecomposition() { - double[][] A = { - {4, 3}, - {6, 3} - }; + double[][] A = {{4, 3}, {6, 3}}; // Perform LU decomposition LUDecomposition.LU lu = LUDecomposition.decompose(A); From 6c59d78d233f68d7675ce80a82bf1f10d3df9e0a Mon Sep 17 00:00:00 2001 From: Sourav Pati Date: Sun, 19 Oct 2025 15:01:34 +0530 Subject: [PATCH 9/9] Added LU decomposition algorthim --- .../matrix/LUDecompositionTest.java | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/test/java/com/thealgorithms/matrix/LUDecompositionTest.java b/src/test/java/com/thealgorithms/matrix/LUDecompositionTest.java index 5114e95a07d8..d3cc6d64bf42 100644 --- a/src/test/java/com/thealgorithms/matrix/LUDecompositionTest.java +++ b/src/test/java/com/thealgorithms/matrix/LUDecompositionTest.java @@ -8,33 +8,33 @@ public class LUDecompositionTest { @Test public void testLUDecomposition() { - double[][] A = {{4, 3}, {6, 3}}; + double[][] a = {{4, 3}, {6, 3}}; // Perform LU decomposition - LUDecomposition.LU lu = LUDecomposition.decompose(A); - double[][] L = lu.l; - double[][] U = lu.u; + LUDecomposition.LU lu = LUDecomposition.decompose(a); + double[][] l = lu.l; + double[][] u = lu.u; - // Reconstruct A from L and U - double[][] reconstructed = multiplyMatrices(L, U); + // Reconstruct a from l and u + double[][] reconstructed = multiplyMatrices(l, u); - // Assert that reconstructed matrix matches original A - for (int i = 0; i < A.length; i++) { - assertArrayEquals(A[i], reconstructed[i], 1e-9); + // Assert that reconstructed matrix matches original a + for (int i = 0; i < a.length; i++) { + assertArrayEquals(a[i], reconstructed[i], 1e-9); } } // Helper method to multiply two matrices - private double[][] multiplyMatrices(double[][] A, double[][] B) { - int n = A.length; - double[][] C = new double[n][n]; + private double[][] multiplyMatrices(double[][] a, double[][] b) { + int n = a.length; + double[][] c = 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++) { - C[i][j] += A[i][k] * B[k][j]; + c[i][j] += a[i][k] * b[k][j]; } } } - return C; + return c; } }