Skip to content

Commit 100462d

Browse files
authored
Added LU Decomposition Algorithm for matrix (#6834)
* Added LU decomposition algorthm * Added LU decomposition algorthim * Added LU decomposition algorthim * Added LU decomposition algorthim * Added LU decomposition algorthim * Added LU decomposition algorthim * Added LU decomposition algorthim * Added LU decomposition algorthim * Added LU decomposition algorthim
1 parent 82ff14c commit 100462d

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
* Reference:
13+
* https://en.wikipedia.org/wiki/lu_decomposition
14+
*/
15+
public final class LUDecomposition {
16+
17+
private LUDecomposition() {
18+
}
19+
20+
/**
21+
* A helper class to store both l and u matrices
22+
*/
23+
public static class LU {
24+
double[][] l;
25+
double[][] u;
26+
27+
LU(double[][] l, double[][] u) {
28+
this.l = l;
29+
this.u = u;
30+
}
31+
}
32+
33+
/**
34+
* Performs LU Decomposition on a square matrix a
35+
*
36+
* @param a input square matrix
37+
* @return LU object containing l and u matrices
38+
*/
39+
public static LU decompose(double[][] a) {
40+
int n = a.length;
41+
double[][] l = new double[n][n];
42+
double[][] u = new double[n][n];
43+
44+
for (int i = 0; i < n; i++) {
45+
// upper triangular matrix
46+
for (int k = i; k < n; k++) {
47+
double sum = 0;
48+
for (int j = 0; j < i; j++) {
49+
sum += l[i][j] * u[j][k];
50+
}
51+
u[i][k] = a[i][k] - sum;
52+
}
53+
54+
// lower triangular matrix
55+
for (int k = i; k < n; k++) {
56+
if (i == k) {
57+
l[i][i] = 1; // diagonal as 1
58+
} else {
59+
double sum = 0;
60+
for (int j = 0; j < i; j++) {
61+
sum += l[k][j] * u[j][i];
62+
}
63+
l[k][i] = (a[k][i] - sum) / u[i][i];
64+
}
65+
}
66+
}
67+
68+
return new LU(l, u);
69+
}
70+
71+
/**
72+
* Utility function to print a matrix
73+
*
74+
* @param m matrix to print
75+
*/
76+
public static void printMatrix(double[][] m) {
77+
for (double[] row : m) {
78+
System.out.print("[");
79+
for (int j = 0; j < row.length; j++) {
80+
System.out.printf("%7.3f", row[j]);
81+
if (j < row.length - 1) {
82+
System.out.print(", ");
83+
}
84+
}
85+
System.out.println("]");
86+
}
87+
}
88+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.thealgorithms.matrix;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class LUDecompositionTest {
8+
9+
@Test
10+
public void testLUDecomposition() {
11+
double[][] a = {{4, 3}, {6, 3}};
12+
13+
// Perform LU decomposition
14+
LUDecomposition.LU lu = LUDecomposition.decompose(a);
15+
double[][] l = lu.l;
16+
double[][] u = lu.u;
17+
18+
// Reconstruct a from l and u
19+
double[][] reconstructed = multiplyMatrices(l, u);
20+
21+
// Assert that reconstructed matrix matches original a
22+
for (int i = 0; i < a.length; i++) {
23+
assertArrayEquals(a[i], reconstructed[i], 1e-9);
24+
}
25+
}
26+
27+
// Helper method to multiply two matrices
28+
private double[][] multiplyMatrices(double[][] a, double[][] b) {
29+
int n = a.length;
30+
double[][] c = new double[n][n];
31+
for (int i = 0; i < n; i++) {
32+
for (int j = 0; j < n; j++) {
33+
for (int k = 0; k < n; k++) {
34+
c[i][j] += a[i][k] * b[k][j];
35+
}
36+
}
37+
}
38+
return c;
39+
}
40+
}

0 commit comments

Comments
 (0)