Adjoint of a Matrix is the transpose of the co-factors of the given matrix. It is the core Mathematics concept used in calculating inverse of a matrix. In this Article, I am going to share with you the code of adjoint of n x n matrix in C++ where n=1,2 or 3.
What is Adjoint of n x n Matrix
The adjoint of an n x n matrix is known as the adjugate or classical adjoint. It is a matrix that is related to the original matrix and is used in various mathematical operations, especially in finding the inverse of a matrix. The adjoint is denoted as “adj(A)” for a matrix A.
To find the adjoint of an n x n matrix A, you need to follow these steps:
- Calculate the cofactor matrix of A.
- For each element A[i][j] of A, calculate the cofactor C[i][j]. The cofactor of an element is the determinant of the matrix obtained by removing the i-th row and j-th column from A, multiplied by (-1)^(i+j). In mathematical notation:
C[i][j] = (-1)^(i+j) * det(M[i][j]),
where M[i][j] is the (n-1) x (n-1) matrix obtained by removing the i-th row and j-th column from A.
- Transpose the cofactor matrix.
- Swap the rows and columns of the cofactor matrix obtained in step 1 to get the adjoint matrix.
Mathematically, if C is the cofactor matrix of A, then the adjoint matrix adj(A) is given by:
adj(A) = transpose(C)
The adjoint of a matrix plays a crucial role in finding the inverse of a matrix. Specifically, if A is an invertible (non-singular) matrix, then you can find the inverse of A as follows:
A^(-1) = (1 / det(A)) * adj(A)
where det(A) is the determinant of A.
Keep in mind that the adjoint is only defined for square matrices (n x n matrices), and it’s used primarily in linear algebra and matrix operations.
Code in C++
//ADJOINT of matrices :)
#include<iostream>
#include<math.h>
#include<conio.h>
using namespace std;
int main()
{
int n;
system("color b4 ");
do
{
cout<<"\n\n\tEnter Size of Matrix : \n\n";
cout<<"\t1-One x One "<<endl;
cout<<"\t2-Two x Two"<<endl;
cout<<"\t3-Three x Three "<<endl<<"\t";
cin>>n;
}
while(n>4 || n<1) ;
//system("CLS");
//system("color F3");
int mat[n][n],adj[n][n];
cout<<"\n\n\tEnter Elements now \n";
int i,j;
//enter elements
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<"\tElement ["<<i+1<<"]["<<j+1<<"] : ";cin>>mat[i][j];
}
}
cout<<"\n\t";
if(n==1)
{
adj[0][0]=mat[0][0];
}
//2x2 matrix determinent
if(n==2)
{
//2X2 matrix determinent
adj[0][0]=mat[1][1];
adj[0][1]=-1*mat[0][1];
adj[1][0]=-1*mat[1][0];
adj[1][1]=mat[0][0];
}
if(n==3)
{
//3X3 matrix adjoint
//cofactor 11
adj[0][0]=pow(-1,1+1)*(mat[1][1]*mat[2][2]-mat[1][2]*mat[2][1]);
//cofactor 12
adj[0][1]=pow(-1,1+2)*(mat[1][0]*mat[2][2]-mat[1][2]*mat[2][0]);
//cofactor 13
adj[0][2]=pow(-1,1+3)*(mat[1][0]*mat[2][1]-mat[1][1]*mat[2][0]);
//cofactor 21
adj[1][0]=pow(-1,2+1)*(mat[0][1]*mat[2][2]-mat[0][2]*mat[2][1]);
//cofactor 22
adj[1][1]=pow(-1,2+2)*(mat[0][0]*mat[2][2]-mat[0][2]*mat[2][0]);
//cofactor 23
adj[1][2]=pow(-1,2+3)*(mat[0][0]*mat[2][1]-mat[0][1]*mat[2][0]);
//cofactor 31
adj[2][0]=pow(-1,3+1)*(mat[0][1]*mat[1][2]-mat[0][2]*mat[1][1]);
//cofactor 32
adj[2][1]=pow(-1,3+2)*(mat[0][0]*mat[1][2]-mat[0][2]*mat[1][0]);
//cofactor 33
adj[2][2]=pow(-1,3+3)*(mat[0][0]*mat[1][1]-mat[0][1]*mat[1][0]);
}
//print matrix adjoint
cout<<"\n\tADJOINT OF "<<n<<" x "<<n<<" MATRIX IS \n\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<"\t"<<adj[j][i]<<"\t";
}
cout<<endl;
}
getch();
}
Output
