Ta có: Det(A)=Det(L)*Det(U) mà Det(L) = 1 nên Det(A) = Det(U).
[Code Turbo C++]
#include "conio.h"
#include "iostream.h"
#define max 100
/*Nhap ma tran*/
void Nhap(float A[max][max],int n) {
for(int i = 0; i<n; i++)
for(int j = 0; j<n; j++) {
cout<<"a["<<i<<"]["<<j<<"] = ";
cin>>A[i][j];
}
}
/*Xuat ma tran*/
void XuatMaTran(float A[max][max], int n) {
cout<<"\n";
for(int i=0 ; i<n; i++){
cout<<endl;
for(int j=0 ; j<n; j++)
cout<<A[i][j]<<"\t";
}
}
/*Phan ra A = LU*/
void PhanRaLU(float A[max][max], float L[max][max], float U[max][max], int n) {
for(int k = 0; k<n; k++) {
U[k][k] = A[k][k];
L[k][k] = 1;
for(int i=k+1; i<n; i++) {
L[i][k] = A[i][k]/U[k][k];
U[k][i] = A[k][i];
U[i][k] = 0;
L[k][i] = 0;
}
for(i = k+1; i<n; i++)
for(int j = k+1; j<n; j++)
A[i][j] = A[i][j]-L[i][k]*U[k][j];
}
}
//dinh thuc ma tran tam giac
double DinhThucMaTranTamGiac(float A[max][max], int n) {
double temp = 1;
for(int i = 0; i<n; i++)
temp*=A[i][i];
return temp;
}
/*chuong trinh chinh*/
void main() {
int n;
float A[max][max],L[max][max],U[max][max];
clrscr();
cout<<"Nhap n = ";
cin>>n;
cout<<"Nhap ma tran A\n";
Nhap(A,n);
cout<<"Ma tran A vua nhap";
XuatMaTran(A,n);
PhanRaLU(A,L,U,n);
cout<<"\nMa tran L";
XuatMaTran(L,n);
cout<<"\nMa tran U";
XuatMaTran(U,n);
cout<<"\nDet(A) = Det(L)*Det(U) = "<<DinhThucMaTranTamGiac(U,n);
getch();
}