-
Notifications
You must be signed in to change notification settings - Fork 0
/
LU-Decomposition.cpp
62 lines (51 loc) · 1.49 KB
/
LU-Decomposition.cpp
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
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
void luDecomposition(const vector<vector<double>>& A, vector<vector<double>>& L, vector<vector<double>>& U) {
int n = A.size();
for (int i = 0; i < n; ++i) {
// Upper Triangular Matrix U
for (int k = i; k < n; ++k) {
double sum = 0;
for (int j = 0; j < i; ++j)
sum += (L[i][j] * U[j][k]);
U[i][k] = A[i][k] - sum;
}
// Lower Triangular Matrix L
for (int k = i; k < n; ++k) {
if (i == k)
L[i][i] = 1; // Diagonal as 1
else {
double sum = 0;
for (int j = 0; j < i; ++j)
sum += (L[k][j] * U[j][i]);
L[k][i] = (A[k][i] - sum) / U[i][i];
}
}
}
}
void printMatrix(const vector<vector<double>>& matrix) {
int n = matrix.size();
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j)
cout << setw(10) << matrix[i][j] << " ";
cout << endl;
}
}
int main() {
vector<vector<double>> A = {
{4, 3, 0},
{3, 7, -1},
{0, -1, 4}
};
int n = A.size();
vector<vector<double>> L(n, vector<double>(n, 0));
vector<vector<double>> U(n, vector<double>(n, 0));
luDecomposition(A, L, U);
cout << "Lower Triangular Matrix L:" << endl;
printMatrix(L);
cout << "Upper Triangular Matrix U:" << endl;
printMatrix(U);
return 0;
}