forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLUDecomposition.java
More file actions
113 lines (99 loc) · 3.06 KB
/
LUDecomposition.java
File metadata and controls
113 lines (99 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.thealgorithms.matrix;
/**
* LU Decomposition algorithm for square matrices.
* Decomposes a matrix A into L (lower triangular) and U (upper triangular)
* such that A = L * U
*
* <p>Time Complexity: O(n^3)
* <p>Space Complexity: O(n^2)
*
* @author Raghu0703
* @see <a href="https://en.wikipedia.org/wiki/LU_decomposition">LU Decomposition</a>
*/
public final class LUDecomposition {
private LUDecomposition() {
}
/**
* Performs LU decomposition on a square matrix using Doolittle's method.
*
* @param matrix The input square matrix
* @return A Result object containing L and U matrices
* @throws IllegalArgumentException if matrix is not square or singular
*/
public static Result decompose(double[][] matrix) {
int n = matrix.length;
// Validate input
if (n == 0) {
throw new IllegalArgumentException("Matrix cannot be empty");
}
for (double[] row : matrix) {
if (row.length != n) {
throw new IllegalArgumentException("Matrix must be square");
}
}
double[][] l = new double[n][n];
double[][] u = new double[n][n];
// Initialize L with identity matrix
for (int i = 0; i < n; i++) {
l[i][i] = 1.0;
}
// Perform LU decomposition using Doolittle's method
for (int j = 0; j < n; j++) {
// Calculate U matrix elements
for (int i = 0; i <= j; i++) {
double sum = 0.0;
for (int k = 0; k < i; k++) {
sum += l[i][k] * u[k][j];
}
u[i][j] = matrix[i][j] - sum;
}
// Calculate L matrix elements
for (int i = j + 1; i < n; i++) {
double sum = 0.0;
for (int k = 0; k < j; k++) {
sum += l[i][k] * u[k][j];
}
if (Math.abs(u[j][j]) < 1e-10) {
throw new IllegalArgumentException(
"Matrix is singular or nearly singular"
);
}
l[i][j] = (matrix[i][j] - sum) / u[j][j];
}
}
return new Result(l, u);
}
/**
* Result class to hold L and U matrices from decomposition.
*/
public static class Result {
private final double[][] lMatrix;
private final double[][] uMatrix;
/**
* Constructor for Result.
*
* @param l Lower triangular matrix
* @param u Upper triangular matrix
*/
public Result(double[][] l, double[][] u) {
this.lMatrix = l;
this.uMatrix = u;
}
/**
* Gets the lower triangular matrix.
*
* @return L matrix
*/
public double[][] getL() {
return lMatrix;
}
/**
* Gets the upper triangular matrix.
*
* @return U matrix
*/
public double[][] getU() {
return uMatrix;
}
}
}