LU Decomposition คือการย่อยเมทริกซ์ให้เป็นผลคูณของเมทริกซ์สามเหลี่ยมล่าง (Lower Triangular Matrix - L) และเมทริกซ์สามเหลี่ยมบน (Upper Triangular Matrix - U) โดยที่ L มีค่า 0 ทุกค่าที่อยู่เหนือแนวทแยง และ U มีค่า 0 ทุกค่าที่อยู่ใต้แนวทแยง
ตัวอย่าง LU Decomposition
Given matrix (A): 1 1 0 2 1 3 3 1 1 L matrix (Lower): 1 0 0 2 -1 0 3 -2 -5 U matrix (Upper): 1 1 0 0 1 -3 0 0 1
ตรวจสอบ: L × U = A ✓
โค้ดโปรแกรม LU Decomposition
#include <iostream>
using namespace std;
void LUdecomposition(float a[10][10], float l[10][10], float u[10][10], int n) {
int i, j, k;
for (i = 0; i < n; i++) {
// คำนวณคอลัมน์ที่ i ของ L
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][k] * u[k][i];
}
}
// คำนวณแถวที่ i ของ U
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] -= (l[i][k] * u[k][j]) / l[i][i];
}
}
}
}
int main() {
float a[10][10], l[10][10], u[10][10];
int n, i, j;
cout << "Enter size of square matrix: ";
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:" << endl;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
cout << l[i][j] << " ";
cout << endl;
}
cout << "U Decomposition:" << 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: 1 0 0 2 -1 0 3 -2 -5 U Decomposition: 1 1 0 0 1 -3 0 0 1
การประยุกต์ใช้
- แก้ระบบสมการเชิงเส้น Ax = b
- หา determinant ของเมทริกซ์ (det(A) = det(L) × det(U))
- หา inverse matrix
- ใช้ในวิธีเชิงตัวเลขและวิศวกรรมคำนวณ