행렬의 LU 분해는 하부 삼각 행렬과 상부 삼각 행렬의 곱으로 행렬을 생성합니다. LU의 LU 행렬 분해는 Lower Upper를 의미합니다.
행렬의 LU 분해의 예는 다음과 같습니다. -
Given matrix is: 1 1 0 2 1 3 3 1 1 The L matrix is: 1 0 0 2 -1 0 3 -2 -5 The U matrix is: 1 1 0 0 1 -3 0 0 1
행렬의 LU 분해를 수행하는 프로그램은 다음과 같습니다. -
예시
#include<iostream>
using namespace std;
void LUdecomposition(float a[10][10], float l[10][10], float u[10][10], int n) {
int i = 0, j = 0, k = 0;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (j < i)
l[j][i] = 0;
else {
l[j][i] = a[j][i];
for (k = 0; k < i; k++) {
l[j][i] = l[j][i] - l[j][k] * u[k][i];
}
}
}
for (j = 0; j < n; j++) {
if (j < i)
u[i][j] = 0;
else if (j == i)
u[i][j] = 1;
else {
u[i][j] = a[i][j] / l[i][i];
for (k = 0; k < i; k++) {
u[i][j] = u[i][j] - ((l[i][k] * u[k][j]) / l[i][i]);
}
}
}
}
}
int main() {
float a[10][10], l[10][10], u[10][10];
int n = 0, i = 0, j = 0;
cout << "Enter size of square matrix : "<<endl;
cin >> n;
cout<<"Enter matrix values: "<endl;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
cin >> a[i][j];
LUdecomposition(a, l, u, n);
cout << "L Decomposition is as follows..."<<endl;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
cout<<l[i][j]<<" ";
}
cout << endl;
}
cout << "U Decomposition is as follows..."<<endl;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
cout<<u[i][j]<<" ";
}
cout << endl;
}
return 0;
} 출력
위 프로그램의 출력은 다음과 같습니다.
Enter size of square matrix : 3 Enter matrix values: 1 1 0 2 1 3 3 1 1 L Decomposition is as follows... 1 0 0 2 -1 0 3 -2 -5 U Decomposition is as follows... 1 1 0 0 1 -3 0 0 1
위의 프로그램에서 함수 LU 분해는 주어진 행렬의 L 및 U 분해를 찾습니다. 이것은 L 및 U 분해를 계산하고 행렬 a[][]의 l[][] 및 u[][] 행렬에 저장하는 중첩 for 루프를 사용하여 수행됩니다.
이것을 보여주는 코드 조각은 다음과 같습니다 -
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (j < i)
l[j][i] = 0;
else {
l[j][i] = a[j][i];
for (k = 0; k < i; k++) {
l[j][i] = l[j][i] - l[j][k] * u[k][i];
}
}
}
for (j = 0; j < n; j++) {
if (j < i)
u[i][j] = 0;
else if (j == i)
u[i][j] = 1;
else {
u[i][j] = a[i][j] / l[i][i];
for (k = 0; k < i; k++) {
u[i][j] = u[i][j] - ((l[i][k] * u[k][j]) / l[i][i]);
}
}
}
} main() 함수에서 행렬의 크기와 요소는 사용자로부터 얻습니다. 이것은 다음과 같이 주어집니다 -
cout << "Enter size of square matrix : "<<endl; cin >> n; cout<<"Enter matrix values: "<endl; for (i = 0; i < n; i++) for (j = 0; j < n; j++) cin >> a[i][j];
그런 다음 LU 분해 기능이 호출되고 L 및 U 분해가 표시됩니다. 이것은 다음과 같습니다. -
LUdecomposition(a, l, u, n);
cout << "L Decomposition is as follows..."<<endl;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
cout<<l[i][j]<<" ";
}
cout << endl;
}
cout << "U Decomposition is as follows..."<<endl;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
cout<u[i][j]<<" ";
}
cout << endl;
}