Skip to content

Commit 238063e

Browse files
committed
Added LU decomposition algorthim
1 parent ed53379 commit 238063e

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

src/main/java/com/thealgorithms/matrix/LUDecomposition.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
* LU Decomposition Algorithm
55
* --------------------------
66
* Decomposes a square matrix A into a product of two matrices:
7-
* A = L * U
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
9+
* - L is a lower triangular matrix with 1s on its diagonal
10+
* - U is an upper triangular matrix
1111
*
1212
* This algorithm is widely used in:
13-
* - Solving systems of linear equations (Ax = b)
14-
* - Finding matrix inverses
15-
* - Computing determinants efficiently
13+
* - Solving systems of linear equations (Ax = b)
14+
* - Finding matrix inverses
15+
* - Computing determinants efficiently
1616
*
1717
* Time Complexity: O(n³)
1818
*
@@ -21,9 +21,9 @@
2121
*
2222
* Example:
2323
* >>> double[][] A = {
24-
* >>> {2, -1, -2},
25-
* >>> {-4, 6, 3},
26-
* >>> {-4, -2, 8}
24+
* >>> {2, -1, -2},
25+
* >>> {-4, 6, 3},
26+
* >>> {-4, -2, 8}
2727
* >>> };
2828
* >>> LUDecomposition.LU result = LUDecomposition.decompose(A);
2929
* >>> LUDecomposition.printMatrix(result.L);
@@ -58,6 +58,7 @@ public static class LU {
5858

5959
/**
6060
* Performs LU Decomposition on a square matrix A
61+
*
6162
* @param A input square matrix
6263
* @return LU object containing L and U matrices
6364
*/
@@ -95,15 +96,15 @@ public static LU decompose(double[][] A) {
9596

9697
/**
9798
* Utility function to print a matrix
99+
*
98100
* @param M matrix to print
99101
*/
100102
public static void printMatrix(double[][] M) {
101103
for (double[] row : M) {
102104
System.out.print("[");
103105
for (int j = 0; j < row.length; j++) {
104106
System.out.printf("%7.3f", row[j]);
105-
if (j < row.length - 1)
106-
System.out.print(", ");
107+
if (j < row.length - 1) System.out.print(", ");
107108
}
108109
System.out.println("]");
109110
}
@@ -113,11 +114,7 @@ public static void printMatrix(double[][] M) {
113114
* Demonstration (doctest)
114115
*/
115116
public static void main(String[] args) {
116-
double[][] A = {
117-
{2, -1, -2},
118-
{-4, 6, 3},
119-
{-4, -2, 8}
120-
};
117+
double[][] A = {{2, -1, -2}, {-4, 6, 3}, {-4, -2, 8}};
121118

122119
LU result = decompose(A);
123120

0 commit comments

Comments
 (0)