0% found this document useful (0 votes)
3 views8 pages

Chap - 2 (Part - 1)

The document provides two C++ programs for matrix decomposition methods: Cholesky Decomposition and LDL^T Decomposition. Both programs include functions to check if a matrix is symmetric and positive definite, perform the decomposition, and reconstruct the original matrix. The main function prompts the user for matrix input, validates it, and displays the results of the decomposition and reconstruction.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

Chap - 2 (Part - 1)

The document provides two C++ programs for matrix decomposition methods: Cholesky Decomposition and LDL^T Decomposition. Both programs include functions to check if a matrix is symmetric and positive definite, perform the decomposition, and reconstruct the original matrix. The main function prompts the user for matrix input, validates it, and displays the results of the decomposition and reconstruction.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Bài 1.

Cholesky Decomposition

Code:
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

bool check_doixung(const vector< vector<double> >& matrix) {


int n = matrix.size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
if (matrix[i][j] != matrix[j][i]) return false;
}
}
return true;
}

bool check_xacdinh_duong(const vector< vector<double> >& matrix) {


int n = matrix.size();
vector< vector<double> > subMatrix(n, vector<double>(n));

for (int k = 0; k < n; k++) {


double det = 1.0;
for (int i = 0; i <= k; i++) {
for (int j = 0; j <= k; j++) {
subMatrix[i][j] = matrix[i][j];
}
}

for (int i = 0; i <= k; i++)


det *= subMatrix[i][i];

if (det <= 0) return false;


}
return true;
}
vector< vector<double> > choleskyDecomposition(const vector< vector<double> >&
matrix) {
int n = matrix.size();
vector< vector<double> > lower(n, vector<double>(n, 0));

for (int i = 0; i < n; i++) {


for (int j = 0; j <= i; j++) {
double sum = 0;

if (j == i) {
for (int k = 0; k < j; k++)
sum += lower[j][k] * lower[j][k];
lower[j][j] = sqrt(matrix[j][j] - sum);
} else {
for (int k = 0; k < j; k++)
sum += lower[i][k] * lower[j][k];
lower[i][j] = (matrix[i][j] - sum) / lower[j][j];
}
}
}
return lower;
}

vector< vector<double> > transpose(const vector< vector<double> >& matrix) {


int n = matrix.size();
vector< vector<double> > transposed(n, vector<double>(n, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
transposed[j][i] = matrix[i][j];
}
}
return transposed;
}

vector< vector<double> > multiply(const vector< vector<double> >& A, const


vector< vector<double> >& B) {
int n = A.size();
vector< vector<double> > result(n, vector<double>(n, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
result[i][j] += A[i][k] * B[k][j];
}
}
}
return result;
}

void print(const vector< vector<double> >& matrix) {


for (int i = 0; i < matrix.size(); i++) {
for (int j = 0; j < matrix[i].size(); j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
}

int main() {
int n;
cout << "Nhap kich thuoc ma tran: ";
cin >> n;

vector< vector<double> > matrix(n, vector<double>(n));


cout << "Nhap cac phan tu cua ma tran:\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> matrix[i][j];
}
}

if (!check_doixung(matrix)) {
cout << "Ma tran khong doi xung!" << endl;
return 1;
}

if (!check_xacdinh_duong(matrix)) {
cout << "Ma tran khong xac dinh duong!" << endl;
return 1;
}

vector< vector<double> > L = choleskyDecomposition(matrix);


cout << "Ma tran L:" << endl;
print(L);

vector< vector<double> > Lt = transpose(L);


cout << "Ma tran L^T:" << endl;
print(Lt);

vector< vector<double> > A_reconstructed = multiply(L, Lt);


cout << "Ma tran A (L * L^T):" << endl;
print(A_reconstructed);

return 0;
}

Màn hình kết quả:


Bài 2. Cholesky

Code:
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

bool check_doixung(const vector< vector<double> >& matrix) {


int n = matrix.size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
if (matrix[i][j] != matrix[j][i]) return false;
}
}
return true;
}

void LDLtDecomposition(const vector< vector<double> >& matrix, vector<


vector<double> >& L, vector<double>& D) {
int n = matrix.size();
L.assign(n, vector<double>(n, 0));
D.assign(n, 0);

for (int i = 0; i < n; i++) {


for (int j = 0; j < i; j++) {
double sum = matrix[i][j];
for (int k = 0; k < j; k++) {
sum -= L[i][k] * L[j][k] * D[k];
}
L[i][j] = sum / D[j]; // C?p nh?t L[i][j] dúng công th?c
}

double sum = matrix[i][i];


for (int k = 0; k < i; k++) {
sum -= L[i][k] * L[i][k] * D[k];
}
D[i] = sum;
L[i][i] = 1.0;
}
}

vector< vector<double> > transpose(const vector< vector<double> >& matrix) {


int n = matrix.size();
vector< vector<double> > transposed(n, vector<double>(n, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
transposed[j][i] = matrix[i][j];
}
}
return transposed;
}

vector< vector<double> > multiplyWithD(const vector< vector<double> >& L, const


vector<double>& D) {
int n = L.size();
vector< vector<double> > result(n, vector<double>(n, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
result[i][j] = L[i][j] * D[j];
}
}
return result;
}

vector< vector<double> > multiply(const vector< vector<double> >& A, const


vector< vector<double> >& B) {
int n = A.size();
vector< vector<double> > result(n, vector<double>(n, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
result[i][j] += A[i][k] * B[k][j];
}
}
}
return result;
}
void print(const vector< vector<double> >& matrix) {
int n = matrix.size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < matrix[i].size(); j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
}

void printVector(const vector<double>& vec) {


for (size_t i = 0; i < vec.size(); i++) {
cout << vec[i] << " ";
}
cout << endl;
}

int main() {
int n;
cout << "Nhap kich thuoc ma tran: ";
cin >> n;

vector< vector<double> > matrix(n, vector<double>(n));


cout << "Nhap cac phan tu cua ma tran:\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> matrix[i][j];
}
}

if (!check_doixung(matrix)) {
cout << "Ma tran khong doi xung!" << endl;
return 1;
}

vector< vector<double> > L;


vector<double> D;
LDLtDecomposition(matrix, L, D);

cout << "Ma tran L:" << endl;


print(L);
cout << "Vector D:" << endl;
printVector(D);

vector< vector<double> > Lt = transpose(L);


cout << "Ma tran L^T:" << endl;
print(Lt);

vector< vector<double> > LD = multiplyWithD(L, D);


vector< vector<double> > A_reconstructed = multiply(LD, Lt);

cout << "Ma tran A kiem thu (L * D * L^T):" << endl;


print(A_reconstructed);

return 0;
}

Màn hình kết quả:

You might also like