Skip to content

Commit fffe043

Browse files
committed
Added LU decomposition algorthim
1 parent b9f35bf commit fffe043

File tree

1 file changed

+39
-66
lines changed

1 file changed

+39
-66
lines changed
Lines changed: 39 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,79 @@
11
package com.thealgorithms.matrix;
22

33
/**
4-
* LU Decomposition Algorithm
4+
* LU Decomposition algorithm
55
* --------------------------
6-
* Decomposes a square matrix A into a product of two matrices:
7-
* A = L * U
6+
* Decomposes a square matrix a into a product of two matrices:
7+
* a = l * u
88
* 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³)
9+
* - l is a lower triangular matrix with 1s on its diagonal
10+
* - u is an upper triangular matrix
1811
*
1912
* 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]
13+
* https://en.wikipedia.org/wiki/lu_decomposition
4214
*/
43-
4415
public class LUDecomposition {
4516

17+
private LUDecomposition() {}
18+
4619
/**
47-
* A helper class to store both L and U matrices
20+
* A helper class to store both l and u matrices
4821
*/
4922
public static class LU {
50-
double[][] L;
51-
double[][] U;
23+
double[][] l;
24+
double[][] u;
5225

53-
LU(double[][] L, double[][] U) {
54-
this.L = L;
55-
this.U = U;
26+
LU(double[][] l, double[][] u) {
27+
this.l = l;
28+
this.u = u;
5629
}
5730
}
5831

5932
/**
60-
* Performs LU Decomposition on a square matrix A
33+
* Performs LU Decomposition on a square matrix a
6134
*
62-
* @param A input square matrix
63-
* @return LU object containing L and U matrices
35+
* @param a input square matrix
36+
* @return LU object containing l and u matrices
6437
*/
65-
public static LU decompose(double[][] A) {
66-
int n = A.length;
67-
double[][] L = new double[n][n];
68-
double[][] U = new double[n][n];
38+
public static LU decompose(double[][] a) {
39+
int n = a.length;
40+
double[][] l = new double[n][n];
41+
double[][] u = new double[n][n];
6942

7043
for (int i = 0; i < n; i++) {
71-
// Upper Triangular Matrix
44+
// upper triangular matrix
7245
for (int k = i; k < n; k++) {
7346
double sum = 0;
7447
for (int j = 0; j < i; j++) {
75-
sum += L[i][j] * U[j][k];
48+
sum += l[i][j] * u[j][k];
7649
}
77-
U[i][k] = A[i][k] - sum;
50+
u[i][k] = a[i][k] - sum;
7851
}
7952

80-
// Lower Triangular Matrix
53+
// lower triangular matrix
8154
for (int k = i; k < n; k++) {
8255
if (i == k) {
83-
L[i][i] = 1; // Diagonal as 1
56+
l[i][i] = 1; // diagonal as 1
8457
} else {
8558
double sum = 0;
8659
for (int j = 0; j < i; j++) {
87-
sum += L[k][j] * U[j][i];
60+
sum += l[k][j] * u[j][i];
8861
}
89-
L[k][i] = (A[k][i] - sum) / U[i][i];
62+
l[k][i] = (a[k][i] - sum) / u[i][i];
9063
}
9164
}
9265
}
9366

94-
return new LU(L, U);
67+
return new LU(l, u);
9568
}
9669

9770
/**
9871
* Utility function to print a matrix
9972
*
100-
* @param M matrix to print
73+
* @param m matrix to print
10174
*/
102-
public static void printMatrix(double[][] M) {
103-
for (double[] row : M) {
75+
public static void printMatrix(double[][] m) {
76+
for (double[] row : m) {
10477
System.out.print("[");
10578
for (int j = 0; j < row.length; j++) {
10679
System.out.printf("%7.3f", row[j]);
@@ -116,14 +89,14 @@ public static void printMatrix(double[][] M) {
11689
* Demonstration (doctest)
11790
*/
11891
public static void main(String[] args) {
119-
double[][] A = {{2, -1, -2}, {-4, 6, 3}, {-4, -2, 8}};
92+
double[][] a = {{2, -1, -2}, {-4, 6, 3}, {-4, -2, 8}};
12093

121-
LU result = decompose(A);
94+
LU result = decompose(a);
12295

123-
System.out.println("L matrix:");
124-
printMatrix(result.L);
96+
System.out.println("l matrix:");
97+
printMatrix(result.l);
12598

126-
System.out.println("\nU matrix:");
127-
printMatrix(result.U);
99+
System.out.println("\nu matrix:");
100+
printMatrix(result.u);
128101
}
129-
}
102+
}

0 commit comments

Comments
 (0)