在数值分析和线性代数中,LU分解(其中LU代表“ lower upper”,也称为LU因式分解)将矩阵分解为下三角矩阵和上三角矩阵的乘积。计算机通常使用LU分解求解线性方程组的平方系统,这也是求矩阵求逆或计算矩阵行列式时的关键步骤。 LU分解是由数学家Tadeusz Banachiewicz在1938年提出的。
设A为方阵。 LU分解是指按适当的行和/或列排序或置换将A分解为两个因子,即下三角矩阵L和上三角矩阵U, A = LU 。
Doolittle算法:
始终可以将方矩阵分解为下三角矩阵和上三角矩阵。也就是说, [A] = [L] [U]
杜利特尔(Doolittle)的方法提供了一种将A分解为LU的替代方法,而无需经历高斯消除的麻烦。
对于一般的n×n矩阵A,我们假设存在LU分解,并明确写出L和U的形式。然后,我们根据由A = LU所需的乘法所得的方程式,系统地求解L和U中的项。
U矩阵的项由下式给出:
以及L矩阵的术语:
范例:
Input :
Output :
C++
// CPP Program to decompose a matrix into
// lower and upper traingular matrix
#include
using namespace std;
const int MAX = 100;
void luDecomposition(int mat[][MAX], int n)
{
int lower[n][n], upper[n][n];
memset(lower, 0, sizeof(lower));
memset(upper, 0, sizeof(upper));
// Decomposing matrix into Upper and Lower
// triangular matrix
for (int i = 0; i < n; i++)
{
// Upper Triangular
for (int k = i; k < n; k++)
{
// Summation of L(i, j) * U(j, k)
int sum = 0;
for (int j = 0; j < i; j++)
sum += (lower[i][j] * upper[j][k]);
// Evaluating U(i, k)
upper[i][k] = mat[i][k] - sum;
}
// Lower Triangular
for (int k = i; k < n; k++)
{
if (i == k)
lower[i][i] = 1; // Diagonal as 1
else
{
// Summation of L(k, j) * U(j, i)
int sum = 0;
for (int j = 0; j < i; j++)
sum += (lower[k][j] * upper[j][i]);
// Evaluating L(k, i)
lower[k][i]
= (mat[k][i] - sum) / upper[i][i];
}
}
}
// setw is for displaying nicely
cout << setw(6)
<< " Lower Triangular"
<< setw(32)
<< "Upper Triangular" << endl;
// Displaying the result :
for (int i = 0; i < n; i++)
{
// Lower
for (int j = 0; j < n; j++)
cout << setw(6) << lower[i][j] << "\t";
cout << "\t";
// Upper
for (int j = 0; j < n; j++)
cout << setw(6) << upper[i][j] << "\t";
cout << endl;
}
}
// Driver code
int main()
{
int mat[][MAX]
= { { 2, -1, -2 }, { -4, 6, 3 }, { -4, -2, 8 } };
luDecomposition(mat, 3);
return 0;
}
Java
// Java Program to decompose a matrix into
// lower and upper traingular matrix
class GFG {
static int MAX = 100;
static String s = "";
static void luDecomposition(int[][] mat, int n)
{
int[][] lower = new int[n][n];
int[][] upper = new int[n][n];
// Decomposing matrix into Upper and Lower
// triangular matrix
for (int i = 0; i < n; i++)
{
// Upper Triangular
for (int k = i; k < n; k++)
{
// Summation of L(i, j) * U(j, k)
int sum = 0;
for (int j = 0; j < i; j++)
sum += (lower[i][j] * upper[j][k]);
// Evaluating U(i, k)
upper[i][k] = mat[i][k] - sum;
}
// Lower Triangular
for (int k = i; k < n; k++)
{
if (i == k)
lower[i][i] = 1; // Diagonal as 1
else
{
// Summation of L(k, j) * U(j, i)
int sum = 0;
for (int j = 0; j < i; j++)
sum += (lower[k][j] * upper[j][i]);
// Evaluating L(k, i)
lower[k][i]
= (mat[k][i] - sum) / upper[i][i];
}
}
}
// setw is for displaying nicely
System.out.println(setw(2) + " Lower Triangular"
+ setw(10) + "Upper Triangular");
// Displaying the result :
for (int i = 0; i < n; i++)
{
// Lower
for (int j = 0; j < n; j++)
System.out.print(setw(4) + lower[i][j]
+ "\t");
System.out.print("\t");
// Upper
for (int j = 0; j < n; j++)
System.out.print(setw(4) + upper[i][j]
+ "\t");
System.out.print("\n");
}
}
static String setw(int noOfSpace)
{
s = "";
for (int i = 0; i < noOfSpace; i++)
s += " ";
return s;
}
// Driver code
public static void main(String arr[])
{
int mat[][] = { { 2, -1, -2 },
{ -4, 6, 3 },
{ -4, -2, 8 } };
luDecomposition(mat, 3);
}
}
/* This code contributed by PrinciRaj1992 */
Python3
# Python3 Program to decompose
# a matrix into lower and
# upper traingular matrix
MAX = 100
def luDecomposition(mat, n):
lower = [[0 for x in range(n)]
for y in range(n)]
upper = [[0 for x in range(n)]
for y in range(n)]
# Decomposing matrix into Upper
# and Lower triangular matrix
for i in range(n):
# Upper Triangular
for k in range(i, n):
# Summation of L(i, j) * U(j, k)
sum = 0
for j in range(i):
sum += (lower[i][j] * upper[j][k])
# Evaluating U(i, k)
upper[i][k] = mat[i][k] - sum
# Lower Triangular
for k in range(i, n):
if (i == k):
lower[i][i] = 1 # Diagonal as 1
else:
# Summation of L(k, j) * U(j, i)
sum = 0
for j in range(i):
sum += (lower[k][j] * upper[j][i])
# Evaluating L(k, i)
lower[k][i] = int((mat[k][i] - sum) /
upper[i][i])
# setw is for displaying nicely
print("Lower Triangular\t\tUpper Triangular")
# Displaying the result :
for i in range(n):
# Lower
for j in range(n):
print(lower[i][j], end="\t")
print("", end="\t")
# Upper
for j in range(n):
print(upper[i][j], end="\t")
print("")
# Driver code
mat = [[2, -1, -2],
[-4, 6, 3],
[-4, -2, 8]]
luDecomposition(mat, 3)
# This code is contributed by mits
C#
// C# Program to decompose a matrix into
// lower and upper traingular matrix
using System;
class GFG {
static int MAX = 100;
static String s = "";
static void luDecomposition(int[, ] mat, int n)
{
int[, ] lower = new int[n, n];
int[, ] upper = new int[n, n];
// Decomposing matrix into Upper and Lower
// triangular matrix
for (int i = 0; i < n; i++)
{
// Upper Triangular
for (int k = i; k < n; k++)
{
// Summation of L(i, j) * U(j, k)
int sum = 0;
for (int j = 0; j < i; j++)
sum += (lower[i, j] * upper[j, k]);
// Evaluating U(i, k)
upper[i, k] = mat[i, k] - sum;
}
// Lower Triangular
for (int k = i; k < n; k++)
{
if (i == k)
lower[i, i] = 1; // Diagonal as 1
else
{
// Summation of L(k, j) * U(j, i)
int sum = 0;
for (int j = 0; j < i; j++)
sum += (lower[k, j] * upper[j, i]);
// Evaluating L(k, i)
lower[k, i]
= (mat[k, i] - sum) / upper[i, i];
}
}
}
// setw is for displaying nicely
Console.WriteLine(setw(2) + " Lower Triangular"
+ setw(10) + "Upper Triangular");
// Displaying the result :
for (int i = 0; i < n; i++)
{
// Lower
for (int j = 0; j < n; j++)
Console.Write(setw(4) + lower[i, j] + "\t");
Console.Write("\t");
// Upper
for (int j = 0; j < n; j++)
Console.Write(setw(4) + upper[i, j] + "\t");
Console.Write("\n");
}
}
static String setw(int noOfSpace)
{
s = "";
for (int i = 0; i < noOfSpace; i++)
s += " ";
return s;
}
// Driver code
public static void Main(String[] arr)
{
int[, ] mat = { { 2, -1, -2 },
{ -4, 6, 3 },
{ -4, -2, 8 } };
luDecomposition(mat, 3);
}
}
// This code is contributed by Princi Singh
PHP
输出:
Lower Triangular Upper Triangular
1 0 0 2 -1 -2
-2 1 0 0 4 -1
-2 -1 1 0 0 3