The inverse of a square matrix A is the matrix A^-1, such that A A^-1 = I, where
ID: 3814803 • Letter: T
Question
The inverse of a square matrix A is the matrix A^-1, such that A A^-1 = I, where I, is the Identity Matrix. A square matrix A has an inverse iff (if and only if) the determinant |A| notequalto 0. The matrix having an inverse is called a nonsingular, or invertible matrix. A = [a_11 a_12 a_13 a_21 a_22 a_23 a_21 a_22 a_23 a_31 a_32 a_33] The inverse of the above matrix is given by: A^-1 = 1/det(A) [|a_22 a_23 a_32 a_33 | | a_13 a_12 a_33 a_32 | |a_12 a_13 a_22 a_23| |a_23 a_2 a_33 a_31| |a_11 a_13 a_31 a_33| | a_13 a_11 a_23 a_21| |a_21 a_22 a_31 a_32| |a_12 a_11 a_32 a_31| |a_11 a_12 a_21 a_22|] b. Using equations 1 and 2, develop a C++ program using Two Dimensional arrays and pointers in a function named matrix_Inverse_p tr() to return the inverse of a nonsingular matrix. The program should have the capability of identifying a singular or non-singular matrix. Write all the functions using pointers only.Explanation / Answer
c++ code:
#if !defined(MATRIX_H)
#define MATRIX_H
#include <stdio.h>
#include <iostream>
#include <tchar.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
class InverseMatrix
{
private:
int rows;
int cols;
char name[128];
InverseMatrix();
public:
double **info;
InverseMatrix( char *name, int rows, int cols) :
rows(rows), cols(cols)
{
strcpy(name, name);
info = new double*[rows];
for (int i = 0; i < rows; i++)
info[i] = new double[cols];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
info[i][j] = 0.0;
}
}
}
InverseMatrix(const InverseMatrix &other)
{
strcpy(name, other.name);
rows = other.rows;
cols = other.cols;
info = new double*[rows];
for (int i = 0; i < rows; i++)
info[i] = new double[cols];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
info[i][j] = other.info[i][j];
}
}
}
~InverseMatrix()
{
for (int i = 0; i < rows; i++)
delete[] info[i];
delete[] info;
rows = cols = 0;
}
void Matrix_Setname( char *name)
{
strcpy(name, name);
}
char* Matrix_Getname()
{
return name;
}
void Matrix_getInput()
{
std::cin >> *this;
}
void FIll_Input()
{
std::cout << " Enter Input For Matrix : " << name << " Rows: "
<< rows << " Cols: " << cols << " ";
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
std::cout << "Input For Row: " << i + 1 << " Col: " << j
+ 1 << " = ";
std::cin >> info[i][j];
std::cout << info[i][j] << " ";
}
std::cout << " ";
}
std::cout << " ";
}
double Matrix_Det_Merminant()
{
double Det_M = 0;
double **p = info;
switch (rows)
{
case 2:
{
Det_M = p[0][0] * p[1][1] - p[0][1] * p[1][0];
return Det_M;
}
break;
case 3:
{
double a = p[0][0];
double b = p[0][1];
double c = p[0][2];
double d = p[1][0];
double e = p[1][1];
double f = p[1][2];
double g = p[2][0];
double h = p[2][1];
double i = p[2][2];
double Det_M = (a * e * i + b * f * g + c * d * h);
Det_M = Det_M - a * f * h;
Det_M = Det_M - b * d * i;
Det_M = Det_M - c * e * g;
return Det_M;
}
break;
case 4:
{
InverseMatrix *tmp[4];
for (int i = 0; i < 4; i++)
tmp[i] = new InverseMatrix("", 3, 3);
for (int k = 0; k < 4; k++)
{
for (int i = 1; i < 4; i++)
{
int j1 = 0;
for (int j = 0; j < 4; j++)
{
if (k == j)
continue;
tmp[k]->info[i - 1][j1++]
= this->info[i][j];
}
}
}
double Det_M = this->info[0][0] * tmp[0]->Matrix_Det_Merminant()
- this->info[0][1] * tmp[1]->Matrix_Det_Merminant()
+ this->info[0][2] * tmp[2]->Matrix_Det_Merminant()
- this->info[0][3] * tmp[3]->Matrix_Det_Merminant();
return Det_M;
}
break;
case 5:
{
InverseMatrix *tmp[5];
for (int i = 0; i < 5; i++)
tmp[i] = new InverseMatrix("", 4, 4);
for (int k = 0; k < 5; k++)
{
for (int i = 1; i < 5; i++)
{
int j1 = 0;
for (int j = 0; j < 5; j++)
{
if (k == j)
continue;
tmp[k]->info[i - 1][j1++]
= this->info[i][j];
}
}
}
double Det_M = this->info[0][0] * tmp[0]->Matrix_Det_Merminant()
- this->info[0][1] * tmp[1]->Matrix_Det_Merminant()
+ this->info[0][2] * tmp[2]->Matrix_Det_Merminant()
- this->info[0][3] * tmp[3]->Matrix_Det_Merminant()
+ this->info[0][4] * tmp[4]->Matrix_Det_Merminant();
return Det_M;
}
default:
{
int dimm = rows;
InverseMatrix **tmp = new InverseMatrix*[dimm];
for (int i = 0; i < dimm; i++)
tmp[i] = new InverseMatrix("", dimm - 1, dimm - 1);
for (int k = 0; k < dimm; k++)
{
for (int i = 1; i < dimm; i++)
{
int j1 = 0;
for (int j = 0; j < dimm; j++)
{
if (k == j)
continue;
tmp[k]->info[i - 1][j1++]
= this->info[i][j];
}
}
}
double Det_M = 0;
for (int k = 0; k < dimm; k++)
{
if ((k % 2) == 0)
Det_M = Det_M + (this->info[0][k]
* tmp[k]->Matrix_Det_Merminant());
else
Det_M = Det_M - (this->info[0][k]
* tmp[k]->Matrix_Det_Merminant());
}
for (int i = 0; i < dimm; i++)
delete tmp[i];
delete[] tmp;
return Det_M;
}
break;
}
}
InverseMatrix& operator =(const InverseMatrix &other)
{
if (this->rows != other.rows || this->cols != other.cols)
{
std::cout
<< "WARNING: Assignment is taking place with by changing the number of rows and columns of the matrix";
}
for (int i = 0; i < rows; i++)
delete[] info[i];
delete[] info;
rows = cols = 0;
strcpy(name, other.name);
rows = other.rows;
cols = other.cols;
info = new double*[rows];
for (int i = 0; i < rows; i++)
info[i] = new double[cols];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
info[i][j] = other.info[i][j];
}
}
return *this;
}
InverseMatrix CoFactor()
{
InverseMatrix Matrix_cofactor("COF", rows, cols);
if (rows != cols)
return Matrix_cofactor;
if (rows < 2)
return Matrix_cofactor;
else if (rows == 2)
{
Matrix_cofactor.info[0][0] = info[1][1];
Matrix_cofactor.info[0][1] = -info[1][0];
Matrix_cofactor.info[1][0] = -info[0][1];
Matrix_cofactor.info[1][1] = info[0][0];
return Matrix_cofactor;
}
else if (rows >= 3)
{
int dimm = rows;
InverseMatrix ***tmp = new InverseMatrix**[dimm];
for (int i = 0; i < dimm; i++)
tmp[i] = new InverseMatrix*[dimm];
for (int i = 0; i < dimm; i++)
for (int j = 0; j < dimm; j++)
tmp[i][j] = new InverseMatrix("", dimm - 1, dimm - 1);
for (int k1 = 0; k1 < dimm; k1++)
{
for (int k2 = 0; k2 < dimm; k2++)
{
int i1 = 0;
for (int i = 0; i < dimm; i++)
{
int j1 = 0;
for (int j = 0; j < dimm; j++)
{
if (k1 == i || k2 == j)
continue;
tmp[k1][k2]->info[i1][j1++]
= this->info[i][j];
}
if (k1 != i)
i1++;
}
}
}
bool flagPositive = true;
for (int k1 = 0; k1 < dimm; k1++)
{
flagPositive = ((k1 % 2) == 0);
for (int k2 = 0; k2 < dimm; k2++)
{
if (flagPositive == true)
{
Matrix_cofactor.info[k1][k2]
= tmp[k1][k2]->Matrix_Det_Merminant();
flagPositive = false;
}
else
{
Matrix_cofactor.info[k1][k2]
= -tmp[k1][k2]->Matrix_Det_Merminant();
flagPositive = true;
}
}
}
for (int i = 0; i < dimm; i++)
for (int j = 0; j < dimm; j++)
delete tmp[i][j];
for (int i = 0; i < dimm; i++)
delete[] tmp[i];
delete[] tmp;
}
return Matrix_cofactor;
}
InverseMatrix Adjoint()
{
InverseMatrix Matrix_cofactor("COF", rows, cols);
InverseMatrix adj("ADJ", rows, cols);
if (rows != cols)
return adj;
Matrix_cofactor = this->CoFactor();
// adjoint is transpose of a Matrix_cofactor of a matrix
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
adj.info[j][i] = Matrix_cofactor.info[i][j];
}
}
return adj;
}
InverseMatrix Transpose()
{
InverseMatrix trans("TR", cols, rows);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
trans.info[j][i] = info[i][j];
}
}
return trans;
}
InverseMatrix Inverse()
{
InverseMatrix Matrix_cofactor("COF", rows, cols);
InverseMatrix inv("INV", rows, cols);
if (rows != cols)
return inv;
// to find out Matrix_Det_Merminant
double Det_M = Matrix_Det_Merminant();
Matrix_cofactor = this->CoFactor();
// inv = transpose of Matrix_cofactor / Matrix_Det_Merminant
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
inv.info[j][i] = Matrix_cofactor.info[i][j] / Det_M;
}
}
return inv;
}
InverseMatrix operator +(const InverseMatrix &other)
{
if (this->rows != other.rows || this->cols != other.cols)
{
std::cout
<< "Addition could not take place because number of rows and columns are different between the two matrices";
return *this;
}
InverseMatrix result("", rows, cols);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
result.info[i][j] = this->info[i][j]
+ other.info[i][j];
}
}
return result;
}
InverseMatrix operator -(const InverseMatrix &other)
{
if (this->rows != other.rows || this->cols != other.cols)
{
std::cout
<< "Subtraction could not take place because number of rows and columns are different between the two matrices";
return *this;
}
InverseMatrix result("", rows, cols);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
result.info[i][j] = this->info[i][j]
- other.info[i][j];
}
}
return result;
}
InverseMatrix operator *(const InverseMatrix &other)
{
if (this->cols != other.rows)
{
std::cout
<< "Multiplication could not take place because number of columns of 1st Matrix and number of rows in 2nd Matrix are different";
return *this;
}
InverseMatrix result("", this->rows, other.cols);
for (int i = 0; i < this->rows; i++)
{
for (int j = 0; j < other.cols; j++)
{
for (int k = 0; k < this->cols; k++)
{
result.info[i][j] += this->info[i][k]
* other.info[k][j];
}
}
}
return result;
}
bool operator ==(const InverseMatrix &other)
{
if (this->rows != other.rows || this->cols != other.cols)
{
std::cout
<< "Comparision could not take place because number of rows and columns are different between the two matrices";
return false;
}
InverseMatrix result("", rows, cols);
bool bEqual = true;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (this->info[i][j] != other.info[i][j])
bEqual = false;
}
}
return bEqual;
}
friend std::istream& operator >>(std::istream &is, InverseMatrix &m);
friend std::ostream& operator <<(std::ostream &os, const InverseMatrix &m);
};
std::istream& operator >>(std::istream &is, InverseMatrix &m)
{
std::cout << " Enter Input For Matrix : " << m.name << " Rows: "
<< m.rows << " Cols: " << m.cols << " ";
for (int i = 0; i < m.rows; i++)
{
for (int j = 0; j < m.cols; j++)
{
std::cout << "Input For Row: " << i + 1 << " Col: " << j + 1
<< " = ";
is >> m.info[i][j];
}
std::cout << " ";
}
std::cout << " ";
return is;
}
std::ostream& operator <<(std::ostream &os, const InverseMatrix &m)
{
os << " Matrix : " << m.name << " Rows: " << m.rows << " Cols: "
<< m.cols << " ";
for (int i = 0; i < m.rows; i++)
{
os << " | ";
for (int j = 0; j < m.cols; j++)
{
char buf[32];
double data = m.info[i][j];
if (m.info[i][j] > -0.00001 && m.info[i][j] < 0.00001)
data = 0;
sprintf(buf, "%10.2lf ", data);
os << buf;
}
os << "| ";
}
os << " ";
return os;
}
#endif
int main()
{
InverseMatrix a("A", 5, 5);
//std::cin >> a;
a.FIll_Input();
InverseMatrix aadj = a.Inverse();
std::cout << a;
std::cout << aadj;
InverseMatrix unit = (a * aadj);
unit.Matrix_Setname("A * A-Inv");
std::cout << unit;
}
SAMPLE OUTPUT:
Enter Input For Matrix : Rows: 5 Cols: 5
Input For Row: 1 Col: 1 = 56
Input For Row: 1 Col: 2 = 23
Input For Row: 1 Col: 3 = 12
Input For Row: 1 Col: 4 = 2
Input For Row: 1 Col: 5 = 3
Input For Row: 2 Col: 1 = 5
Input For Row: 2 Col: 2 = 2
Input For Row: 2 Col: 3 = 3
Input For Row: 2 Col: 4 = 9
Input For Row: 2 Col: 5 = 6
Input For Row: 3 Col: 1 = 12
Input For Row: 3 Col: 2 = 21
Input For Row: 3 Col: 3 = 32
Input For Row: 3 Col: 4 = 23
Input For Row: 3 Col: 5 = 63
Input For Row: 4 Col: 1 = 56
Input For Row: 4 Col: 2 = 12
Input For Row: 4 Col: 3 = 20
Input For Row: 4 Col: 4 = 01
Input For Row: 4 Col: 5 = 20
Input For Row: 5 Col: 1 = 90
Input For Row: 5 Col: 2 = 59
Input For Row: 5 Col: 3 = 89
Input For Row: 5 Col: 4 = 65
Input For Row: 5 Col: 5 = 15
Matrix A_____> Rows: 5 Cols: 5
| 56.00 23.00 12.00 2.00 3.00 |
| 5.00 2.00 3.00 9.00 6.00 |
| 12.00 21.00 32.00 23.00 63.00 |
| 56.00 12.00 20.00 1.00 20.00 |
| 90.00 59.00 89.00 65.00 15.00 |
Matrix INVERSE(A) :------- Rows: 5 Cols: 5
| 0.00 0.04 -0.01 0.02 -0.00 |
| 0.06 -0.06 0.02 -0.06 -0.00 |
| -0.04 -0.09 -0.00 0.03 0.01 |
| -0.00 0.12 -0.01 -0.01 0.00 |
| 0.00 0.01 0.01 0.01 -0.01 |
Matrix : Inv(A)*A:--- Rows: 5 Cols: 5
| 1.00 0.00 0.00 0.00 0.00 |
| 0.00 1.00 0.00 0.00 0.00 |
| 0.00 0.00 1.00 0.00 0.00 |
| 0.00 0.00 0.00 1.00 0.00 |
| 0.00 0.00 0.00 0.00 1.00 |
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.