0% found this document useful (0 votes)
61 views3 pages

Inverse Matrix

This C++ program contains functions to calculate the inverse of a matrix using LU decomposition and back substitution. It initializes matrices, inputs a matrix from the user, calculates the inverse using LU decomposition, and prints the inverse matrix. It also multiplies the original matrix with the inverse and prints the result to check if it is the identity matrix.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views3 pages

Inverse Matrix

This C++ program contains functions to calculate the inverse of a matrix using LU decomposition and back substitution. It initializes matrices, inputs a matrix from the user, calculates the inverse using LU decomposition, and prints the inverse matrix. It also multiplies the original matrix with the inverse and prints the result to check if it is the identity matrix.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <iostream>

#include <iomanip>
#include "Strassen'sAlgorithm.hh"

using namespace std;

double **initializeMatrix(int n){


double **M = new double *[n];
for(int i=0; i<n; i++){
M[i] = new double[n];
for(int j=0; j<n; j++){
M[i][j] = 0;
}
}

return M;
}

void inputMatrix(double **M, int n){


for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
cin >> M[i][j];
}
}
}

void printMatrix(double **M, int n){


for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
cout << fixed << setprecision(4)<< M[i][j] << " ";
}
cout << '\n';
}
}

void LUDecomposition(double **M, double **L, double **U, int n){

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


for (int k = i; k < n; k++) {
double sum = 0;
for (int j = 0; j < i; j++) {
sum += (L[i][j] * U[j][k]);
}
U[i][k] = M[i][k] - sum;
}
for (int k = i; k < n; k++) {
if (i == k)
L[i][i] = 1;
else {
double sum = 0;
for (int j = 0; j < i; j++) {
sum += (L[k][j] * U[j][i]);
}
L[k][i] = (M[k][i] - sum) / U[i][i];
}
}
}

}
double **inverseMatrix(double **M, int n) {
double **inv = initializeMatrix(n);
double **L = initializeMatrix(n);
double **U = initializeMatrix(n);

LUDecomposition(M, L, U, n);

double **identity = initializeMatrix(n);


for(int i=0; i<n; i++) identity[i][i] = 1;

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


// Solve Ly = I
double *y = new double[n];
for(int j=0; j<n; j++){
double sum = 0;
for(int k=0; k<j; k++){
sum += L[j][k] * y[k];
}
y[j] = (identity[j][i] - sum) / L[j][j];
}
// Solve Ux = y
for(int j=n-1; j>=0; j--){
double sum = 0;
for(int k=j+1; k<n; k++){
sum += U[j][k] * inv[k][i];
}
inv[j][i] = (y[j] - sum) / U[j][j];
}
delete[] y;
}
return inv;
}

int main() {

int n;

cout << "Enter the dimension of the matrix: ";


cin >> n;
cout << "Enter the matrix:" << endl;

double **A = initializeMatrix(n);

inputMatrix(A, n);

double **A_inv = inverseMatrix(A, n);

cout << "The inverse matrix is:" << endl;


printMatrix(A_inv, n);
cout << endl;

double **checkMatrix = initializeMatrix(n);

cout << "The result of StrassenMultiple:" << endl;

StrassenMultiple(A, A_inv, checkMatrix, n);


printMatrix(checkMatrix, n);
return 0;
}

You might also like