Skip to content

Commit 1fc0a95

Browse files
committed
fix: Resolve checkstyle violations in LUDecomposition
1 parent 6ee8b17 commit 1fc0a95

File tree

1 file changed

+39
-48
lines changed

1 file changed

+39
-48
lines changed

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

Lines changed: 39 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,13 @@
11
package com.thealgorithms.matrix;
22

33
/**
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-
package com.thealgorithms.matrix;
29-
30-
/**
31-
* LU Decomposition algorithm for square matrices
4+
* LU Decomposition algorithm for square matrices.
325
* Decomposes a matrix A into L (lower triangular) and U (upper triangular)
336
* such that A = L * U
34-
*
35-
* Time Complexity: O(n^3)
36-
* Space Complexity: O(n^2)
37-
*
7+
*
8+
* <p>Time Complexity: O(n^3)
9+
* <p>Space Complexity: O(n^2)
10+
*
3811
* @author Raghu0703
3912
* @see <a href="https://en.wikipedia.org/wiki/LU_decomposition">LU Decomposition</a>
4013
*/
@@ -43,15 +16,15 @@ private LUDecomposition() {
4316
}
4417

4518
/**
46-
* Performs LU decomposition on a square matrix using Doolittle's method
47-
*
19+
* Performs LU decomposition on a square matrix using Doolittle's method.
20+
*
4821
* @param matrix The input square matrix
4922
* @return A Result object containing L and U matrices
5023
* @throws IllegalArgumentException if matrix is not square or singular
5124
*/
5225
public static Result decompose(double[][] matrix) {
5326
int n = matrix.length;
54-
27+
5528
// Validate input
5629
if (n == 0) {
5730
throw new IllegalArgumentException("Matrix cannot be empty");
@@ -61,15 +34,15 @@ public static Result decompose(double[][] matrix) {
6134
throw new IllegalArgumentException("Matrix must be square");
6235
}
6336
}
64-
37+
6538
double[][] l = new double[n][n];
6639
double[][] u = new double[n][n];
67-
40+
6841
// Initialize L with identity matrix
6942
for (int i = 0; i < n; i++) {
7043
l[i][i] = 1.0;
7144
}
72-
45+
7346
// Perform LU decomposition using Doolittle's method
7447
for (int j = 0; j < n; j++) {
7548
// Calculate U matrix elements
@@ -80,41 +53,59 @@ public static Result decompose(double[][] matrix) {
8053
}
8154
u[i][j] = matrix[i][j] - sum;
8255
}
83-
56+
8457
// Calculate L matrix elements
8558
for (int i = j + 1; i < n; i++) {
8659
double sum = 0.0;
8760
for (int k = 0; k < j; k++) {
8861
sum += l[i][k] * u[k][j];
8962
}
90-
63+
9164
if (Math.abs(u[j][j]) < 1e-10) {
92-
throw new IllegalArgumentException("Matrix is singular or nearly singular");
65+
throw new IllegalArgumentException(
66+
"Matrix is singular or nearly singular"
67+
);
9368
}
94-
69+
9570
l[i][j] = (matrix[i][j] - sum) / u[j][j];
9671
}
9772
}
98-
73+
9974
return new Result(l, u);
10075
}
101-
76+
10277
/**
103-
* Result class to hold L and U matrices from decomposition
78+
* Result class to hold L and U matrices from decomposition.
10479
*/
10580
public static class Result {
10681
private final double[][] lMatrix;
10782
private final double[][] uMatrix;
108-
83+
84+
/**
85+
* Constructor for Result.
86+
*
87+
* @param l Lower triangular matrix
88+
* @param u Upper triangular matrix
89+
*/
10990
public Result(double[][] l, double[][] u) {
11091
this.lMatrix = l;
11192
this.uMatrix = u;
11293
}
113-
94+
95+
/**
96+
* Gets the lower triangular matrix.
97+
*
98+
* @return L matrix
99+
*/
114100
public double[][] getL() {
115101
return lMatrix;
116102
}
117-
103+
104+
/**
105+
* Gets the upper triangular matrix.
106+
*
107+
* @return U matrix
108+
*/
118109
public double[][] getU() {
119110
return uMatrix;
120111
}

0 commit comments

Comments
 (0)