I developed this program in the second semester of my graduation. Initially, I searched for codes available on the internet and I found a few, but none of them produced the correct output, the implication being, the algorithms were flawed. Afterwards, I began developing an algorithm of my own, using of course the rudimentary knowledge of abstract mathematics, I learnt in high school. For calculating the Inverse and Adjoint of a matrix, the most important function, required is the one that calculates and returns the co-factors of individual elements in an array. Trying my hand at a few matrices, I discovered this interesting pattern that emerged.
The basic formula, that I find for co-factors was that if, in a function, say Give_Cof( ), that accepted two arguments int i and int j, the cofactor for that position is calculated as follows:
int cofactor =
A[(i+1)%3][(j+1)%3] * A[(i+2)%3][(j+2)%3] - A[(i+1)%3][(j+2)%3] * A[(i+2)%3][(j+1)%3];
I thought that the codes would work, but they generated erroneous results. for, I had not considered that co factors are different from minors in the sense that
Cofactor = (-1)^i+j * Minor.
I tried a different code, factoring the sign that was to be coalesced,
int cofactor =
(A[(i+1)%3][(j+1)%3] * A[(i+2)%3][(j+2)%3] - A[(i+1)%3][(j+2)%3] * A[(i+2)%3][(j+1)%3]) * pow(-1,(i+j)); //the function pow requires the header file math.h h to be included, in order to work
But even that didn't correct the function. After that, I tried applying that formula on different matrices, and the defect in the algorithm came to light. I observed that for odd positions, the formula fetched the Left hand operand after the right hand operand, which was creating the problem in the first place. Whereas, the code worked just fine for the elements at even positions i.e (i+j)%2 == 0.
The following function summarizes my findings:
int give_Cof(int A[][], int i, int j) //A[][3] is a 3 X 3, two dimensional array, i, j denote the position
{ // of the element
int cofactor;
if((i + j)%2 == 0)
{
cofactor = A[(m+1)%3][(n+1)%3] * A[(m+2)%3][(n+2)%3] -
A[(m+1)%3][(n+2)%3] * A[(m+2)%3][(n+1)%3];
}
else
{
cofactor = -(-A[(m+1)%3][(n+1)%3] * A[(m+2)%3][(n+2)%3] +
A[(m+1)%3][(n+2)%3] * A[(m+2)%3][(n+1)%3]);
}
return cofactor;
}
The complete, executable program, follows;
#include <iostream>
using namespace std;
int min(int A[][3], int m, int n)
{
int minor;
if((m+n)%2 == 0)
{
minor = A[(m+1)%3][(n+1)%3] * A[(m+2)%3][(n+2)%3] -
A[(m+1)%3][(n+2)%3] * A[(m+2)%3][(n+1)%3];
}
else
{
minor = (-A[(m+1)%3][(n+1)%3] * A[(m+2)%3][(n+2)%3] +
A[(m+1)%3][(n+2)%3] * A[(m+2)%3][(n+1)%3]);
}
return minor;
}
int give_Cof(int A[][3], int m, int n)
{
int cofactor;
if((m+n)%2 == 0)
{
cofactor = min(A,m,n);
}
else
{
cofactor = -min(A,m,n);
}
return cofactor;
}
class Matrix
{
private:
int A[3][3];
public:
void getMatrix()
{
for(int i = 0; i < 3; i++)
{
cout<<"\nEnter the Elements of row "<<i+1<<"\t";
for(int j = 0; j < 3; j++)
cin>>A[i][j];
}
}
void showMatrix()
{
for(int i = 0; i < 3; i++)
{
cout<<endl;
for(int j = 0; j < 3; j++)
cout<<"\t"<<A[i][j];
}
}
int Determinant()
{
int x = A[0][0] * Cofactor(0,0);
int y = A[0][1] * Cofactor(0,1);
int z = A[0][2] * Cofactor(0,2);
int det = x + y + z;
return det;
}
int Minor(int m, int n)
{
int minor = min(A,m,n);
return minor;
}
int Cofactor(int m, int n)
{
int cof = give_Cof(A,m,n);
return cof;
}
void Transpose()
{
int Tran[3][3];
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
Tran[i][j] = A[j][i];
for(int i = 0; i < 3; i++)
{
cout<<endl;
for(int j = 0; j < 3; j++)
cout<<"\t"<<Tran[i][j];
}
}
void Adj()
{
int Adjt[3][3], Adj[3][3];
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
Adjt[i][j] = Cofactor(i,j);
for(int i = 0; i < 3; i++)
{
cout<<endl;
for(int j = 0; j < 3; j++)
{
Adj[i][j] = Adjt[j][i];
cout<<"\t"<<Adj[i][j];
}
}
}
void Inverse()
{
int Adjt[3][3], Adj[3][3];
float Inv[3][3];
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
Adjt[i][j] = Cofactor(i,j);
for(int i = 0; i < 3; i++)
{
cout<<endl;
for(int j = 0; j < 3; j++)
{
Adj[i][j] = Adjt[j][i];
Inv[i][j] = Adj[i][j];
Inv[i][j] = Inv[i][j]/Determinant();
cout<<"\t"<<Inv[i][j];
}
}
}
};
int main(int argc, char **argv)
{
Matrix M;
bool ch = true;
while(ch == true)
{
cout<<"\n\n\n****** Matrix Operation Menu ******";
cout<<"\n***********************************";
cout<<"\n 1. Enter The Matrix ";
cout<<"\n 2. Display The Matrix";
cout<<"\n 3. Display the determinant of the Matrix";
cout<<"\n 4. Display the Minors of the Matrix";
cout<<"\n 5. Display the Cofactors of the Matrix";
cout<<"\n 6. Display the Transpose of the Matrix";
cout<<"\n 7. Display the Adjoint of the Matrix";
cout<<"\n 8. Display the Inverse of the Matrix";
cout<<"\n 9. Exit";
cout<<"\nEnter your choice :\t";
int x;
cin>>x;
switch(x)
{
case 1:
M.getMatrix();
break;
case 2:
M.showMatrix();
break;
case 3:
cout<<"\nThe Determinant of the Matrix is :\t"<<M.Determinant();
break;
case 4:
cout<<"\nMinors of the Matrix are : ";
for(int i = 0; i < 3; i++)
{
cout<<endl;
for(int j = 0; j < 3; j++)
cout<<"\t"<<M.Minor(i,j);
}
break;
case 5:
cout<<"\nCofactors of the Matrix are : ";
for(int i = 0; i < 3; i++)
{
cout<<endl;
for(int j = 0; j < 3; j++)
cout<<"\t"<<M.Cofactor(i,j);
}
break;
case 6:
M.Transpose();
break;
case 7:
M.Adj();
break;
case 8:
M.Inverse();
break;
case 9:
ch = false;
break;
default:
cout<<"\nInvalid Entry!!!";
}
}
return 0;
}
A sample output might enhance the understanding of the program, and that's why , I have provide one, below:
****** Matrix Operation Menu ******
***********************************
1. Enter The Matrix
2. Display The Matrix
3. Display the determinant of the Matrix
4. Display the Minors of the Matrix
5. Display the Cofactors of the Matrix
6. Display the Transpose of the Matrix
7. Display the Adjoint of the Matrix
8. Display the Inverse of the Matrix
9. Exit
Enter your choice : 1
Enter the Elements of row 1 1 3 3
Enter the Elements of row 2 1 4 3
Enter the Elements of row 3 1 3 4
****** Matrix Operation Menu ******
***********************************
1. Enter The Matrix
2. Display The Matrix
3. Display the determinant of the Matrix
4. Display the Minors of the Matrix
5. Display the Cofactors of the Matrix
6. Display the Transpose of the Matrix
7. Display the Adjoint of the Matrix
8. Display the Inverse of the Matrix
9. Exit
Enter your choice : 2
1 3 3
1 4 3
1 3 4
****** Matrix Operation Menu ******
***********************************
1. Enter The Matrix
2. Display The Matrix
3. Display the determinant of the Matrix
4. Display the Minors of the Matrix
5. Display the Cofactors of the Matrix
6. Display the Transpose of the Matrix
7. Display the Adjoint of the Matrix
8. Display the Inverse of the Matrix
9. Exit
Enter your choice : 3
The Determinant of the Matrix is : 1
****** Matrix Operation Menu ******
***********************************
1. Enter The Matrix
2. Display The Matrix
3. Display the determinant of the Matrix
4. Display the Minors of the Matrix
5. Display the Cofactors of the Matrix
6. Display the Transpose of the Matrix
7. Display the Adjoint of the Matrix
8. Display the Inverse of the Matrix
9. Exit
Enter your choice : 4
Minors of the Matrix are :
7 1 -1
3 1 0
-3 0 1
****** Matrix Operation Menu ******
***********************************
1. Enter The Matrix
2. Display The Matrix
3. Display the determinant of the Matrix
4. Display the Minors of the Matrix
5. Display the Cofactors of the Matrix
6. Display the Transpose of the Matrix
7. Display the Adjoint of the Matrix
8. Display the Inverse of the Matrix
9. Exit
Enter your choice : 5
Cofactors of the Matrix are :
7 -1 -1
-3 1 0
-3 0 1
****** Matrix Operation Menu ******
***********************************
1. Enter The Matrix
2. Display The Matrix
3. Display the determinant of the Matrix
4. Display the Minors of the Matrix
5. Display the Cofactors of the Matrix
6. Display the Transpose of the Matrix
7. Display the Adjoint of the Matrix
8. Display the Inverse of the Matrix
9. Exit
Enter your choice : 6
1 1 1
3 4 3
3 3 4
****** Matrix Operation Menu ******
***********************************
1. Enter The Matrix
2. Display The Matrix
3. Display the determinant of the Matrix
4. Display the Minors of the Matrix
5. Display the Cofactors of the Matrix
6. Display the Transpose of the Matrix
7. Display the Adjoint of the Matrix
8. Display the Inverse of the Matrix
9. Exit
Enter your choice : 7
7 -3 -3
-1 1 0
-1 0 1
****** Matrix Operation Menu ******
***********************************
1. Enter The Matrix
2. Display The Matrix
3. Display the determinant of the Matrix
4. Display the Minors of the Matrix
5. Display the Cofactors of the Matrix
6. Display the Transpose of the Matrix
7. Display the Adjoint of the Matrix
8. Display the Inverse of the Matrix
9. Exit
Enter your choice : 8
7 -3 -3
-1 1 0
-1 0 1
****** Matrix Operation Menu ******
***********************************
1. Enter The Matrix
2. Display The Matrix
3. Display the determinant of the Matrix
4. Display the Minors of the Matrix
5. Display the Cofactors of the Matrix
6. Display the Transpose of the Matrix
7. Display the Adjoint of the Matrix
8. Display the Inverse of the Matrix
9. Exit
Enter your choice : 9
RUN SUCCESSFUL (total time: 1m 44s)
The basic formula, that I find for co-factors was that if, in a function, say Give_Cof( ), that accepted two arguments int i and int j, the cofactor for that position is calculated as follows:
int cofactor =
A[(i+1)%3][(j+1)%3] * A[(i+2)%3][(j+2)%3] - A[(i+1)%3][(j+2)%3] * A[(i+2)%3][(j+1)%3];
I thought that the codes would work, but they generated erroneous results. for, I had not considered that co factors are different from minors in the sense that
Cofactor = (-1)^i+j * Minor.
I tried a different code, factoring the sign that was to be coalesced,
int cofactor =
(A[(i+1)%3][(j+1)%3] * A[(i+2)%3][(j+2)%3] - A[(i+1)%3][(j+2)%3] * A[(i+2)%3][(j+1)%3]) * pow(-1,(i+j)); //the function pow requires the header file math.h h to be included, in order to work
But even that didn't correct the function. After that, I tried applying that formula on different matrices, and the defect in the algorithm came to light. I observed that for odd positions, the formula fetched the Left hand operand after the right hand operand, which was creating the problem in the first place. Whereas, the code worked just fine for the elements at even positions i.e (i+j)%2 == 0.
The following function summarizes my findings:
int give_Cof(int A[][], int i, int j) //A[][3] is a 3 X 3, two dimensional array, i, j denote the position
{ // of the element
int cofactor;
if((i + j)%2 == 0)
{
cofactor = A[(m+1)%3][(n+1)%3] * A[(m+2)%3][(n+2)%3] -
A[(m+1)%3][(n+2)%3] * A[(m+2)%3][(n+1)%3];
}
else
{
cofactor = -(-A[(m+1)%3][(n+1)%3] * A[(m+2)%3][(n+2)%3] +
A[(m+1)%3][(n+2)%3] * A[(m+2)%3][(n+1)%3]);
}
return cofactor;
}
The complete, executable program, follows;
#include <iostream>
using namespace std;
int min(int A[][3], int m, int n)
{
int minor;
if((m+n)%2 == 0)
{
minor = A[(m+1)%3][(n+1)%3] * A[(m+2)%3][(n+2)%3] -
A[(m+1)%3][(n+2)%3] * A[(m+2)%3][(n+1)%3];
}
else
{
minor = (-A[(m+1)%3][(n+1)%3] * A[(m+2)%3][(n+2)%3] +
A[(m+1)%3][(n+2)%3] * A[(m+2)%3][(n+1)%3]);
}
return minor;
}
int give_Cof(int A[][3], int m, int n)
{
int cofactor;
if((m+n)%2 == 0)
{
cofactor = min(A,m,n);
}
else
{
cofactor = -min(A,m,n);
}
return cofactor;
}
class Matrix
{
private:
int A[3][3];
public:
void getMatrix()
{
for(int i = 0; i < 3; i++)
{
cout<<"\nEnter the Elements of row "<<i+1<<"\t";
for(int j = 0; j < 3; j++)
cin>>A[i][j];
}
}
void showMatrix()
{
for(int i = 0; i < 3; i++)
{
cout<<endl;
for(int j = 0; j < 3; j++)
cout<<"\t"<<A[i][j];
}
}
int Determinant()
{
int x = A[0][0] * Cofactor(0,0);
int y = A[0][1] * Cofactor(0,1);
int z = A[0][2] * Cofactor(0,2);
int det = x + y + z;
return det;
}
int Minor(int m, int n)
{
int minor = min(A,m,n);
return minor;
}
int Cofactor(int m, int n)
{
int cof = give_Cof(A,m,n);
return cof;
}
void Transpose()
{
int Tran[3][3];
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
Tran[i][j] = A[j][i];
for(int i = 0; i < 3; i++)
{
cout<<endl;
for(int j = 0; j < 3; j++)
cout<<"\t"<<Tran[i][j];
}
}
void Adj()
{
int Adjt[3][3], Adj[3][3];
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
Adjt[i][j] = Cofactor(i,j);
for(int i = 0; i < 3; i++)
{
cout<<endl;
for(int j = 0; j < 3; j++)
{
Adj[i][j] = Adjt[j][i];
cout<<"\t"<<Adj[i][j];
}
}
}
void Inverse()
{
int Adjt[3][3], Adj[3][3];
float Inv[3][3];
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
Adjt[i][j] = Cofactor(i,j);
for(int i = 0; i < 3; i++)
{
cout<<endl;
for(int j = 0; j < 3; j++)
{
Adj[i][j] = Adjt[j][i];
Inv[i][j] = Adj[i][j];
Inv[i][j] = Inv[i][j]/Determinant();
cout<<"\t"<<Inv[i][j];
}
}
}
};
int main(int argc, char **argv)
{
Matrix M;
bool ch = true;
while(ch == true)
{
cout<<"\n\n\n****** Matrix Operation Menu ******";
cout<<"\n***********************************";
cout<<"\n 1. Enter The Matrix ";
cout<<"\n 2. Display The Matrix";
cout<<"\n 3. Display the determinant of the Matrix";
cout<<"\n 4. Display the Minors of the Matrix";
cout<<"\n 5. Display the Cofactors of the Matrix";
cout<<"\n 6. Display the Transpose of the Matrix";
cout<<"\n 7. Display the Adjoint of the Matrix";
cout<<"\n 8. Display the Inverse of the Matrix";
cout<<"\n 9. Exit";
cout<<"\nEnter your choice :\t";
int x;
cin>>x;
switch(x)
{
case 1:
M.getMatrix();
break;
case 2:
M.showMatrix();
break;
case 3:
cout<<"\nThe Determinant of the Matrix is :\t"<<M.Determinant();
break;
case 4:
cout<<"\nMinors of the Matrix are : ";
for(int i = 0; i < 3; i++)
{
cout<<endl;
for(int j = 0; j < 3; j++)
cout<<"\t"<<M.Minor(i,j);
}
break;
case 5:
cout<<"\nCofactors of the Matrix are : ";
for(int i = 0; i < 3; i++)
{
cout<<endl;
for(int j = 0; j < 3; j++)
cout<<"\t"<<M.Cofactor(i,j);
}
break;
case 6:
M.Transpose();
break;
case 7:
M.Adj();
break;
case 8:
M.Inverse();
break;
case 9:
ch = false;
break;
default:
cout<<"\nInvalid Entry!!!";
}
}
return 0;
}
A sample output might enhance the understanding of the program, and that's why , I have provide one, below:
****** Matrix Operation Menu ******
***********************************
1. Enter The Matrix
2. Display The Matrix
3. Display the determinant of the Matrix
4. Display the Minors of the Matrix
5. Display the Cofactors of the Matrix
6. Display the Transpose of the Matrix
7. Display the Adjoint of the Matrix
8. Display the Inverse of the Matrix
9. Exit
Enter your choice : 1
Enter the Elements of row 1 1 3 3
Enter the Elements of row 2 1 4 3
Enter the Elements of row 3 1 3 4
****** Matrix Operation Menu ******
***********************************
1. Enter The Matrix
2. Display The Matrix
3. Display the determinant of the Matrix
4. Display the Minors of the Matrix
5. Display the Cofactors of the Matrix
6. Display the Transpose of the Matrix
7. Display the Adjoint of the Matrix
8. Display the Inverse of the Matrix
9. Exit
Enter your choice : 2
1 3 3
1 4 3
1 3 4
****** Matrix Operation Menu ******
***********************************
1. Enter The Matrix
2. Display The Matrix
3. Display the determinant of the Matrix
4. Display the Minors of the Matrix
5. Display the Cofactors of the Matrix
6. Display the Transpose of the Matrix
7. Display the Adjoint of the Matrix
8. Display the Inverse of the Matrix
9. Exit
Enter your choice : 3
The Determinant of the Matrix is : 1
****** Matrix Operation Menu ******
***********************************
1. Enter The Matrix
2. Display The Matrix
3. Display the determinant of the Matrix
4. Display the Minors of the Matrix
5. Display the Cofactors of the Matrix
6. Display the Transpose of the Matrix
7. Display the Adjoint of the Matrix
8. Display the Inverse of the Matrix
9. Exit
Enter your choice : 4
Minors of the Matrix are :
7 1 -1
3 1 0
-3 0 1
****** Matrix Operation Menu ******
***********************************
1. Enter The Matrix
2. Display The Matrix
3. Display the determinant of the Matrix
4. Display the Minors of the Matrix
5. Display the Cofactors of the Matrix
6. Display the Transpose of the Matrix
7. Display the Adjoint of the Matrix
8. Display the Inverse of the Matrix
9. Exit
Enter your choice : 5
Cofactors of the Matrix are :
7 -1 -1
-3 1 0
-3 0 1
****** Matrix Operation Menu ******
***********************************
1. Enter The Matrix
2. Display The Matrix
3. Display the determinant of the Matrix
4. Display the Minors of the Matrix
5. Display the Cofactors of the Matrix
6. Display the Transpose of the Matrix
7. Display the Adjoint of the Matrix
8. Display the Inverse of the Matrix
9. Exit
Enter your choice : 6
1 1 1
3 4 3
3 3 4
****** Matrix Operation Menu ******
***********************************
1. Enter The Matrix
2. Display The Matrix
3. Display the determinant of the Matrix
4. Display the Minors of the Matrix
5. Display the Cofactors of the Matrix
6. Display the Transpose of the Matrix
7. Display the Adjoint of the Matrix
8. Display the Inverse of the Matrix
9. Exit
Enter your choice : 7
7 -3 -3
-1 1 0
-1 0 1
****** Matrix Operation Menu ******
***********************************
1. Enter The Matrix
2. Display The Matrix
3. Display the determinant of the Matrix
4. Display the Minors of the Matrix
5. Display the Cofactors of the Matrix
6. Display the Transpose of the Matrix
7. Display the Adjoint of the Matrix
8. Display the Inverse of the Matrix
9. Exit
Enter your choice : 8
7 -3 -3
-1 1 0
-1 0 1
****** Matrix Operation Menu ******
***********************************
1. Enter The Matrix
2. Display The Matrix
3. Display the determinant of the Matrix
4. Display the Minors of the Matrix
5. Display the Cofactors of the Matrix
6. Display the Transpose of the Matrix
7. Display the Adjoint of the Matrix
8. Display the Inverse of the Matrix
9. Exit
Enter your choice : 9
RUN SUCCESSFUL (total time: 1m 44s)
I hope this helps....
No comments:
Post a Comment