Adjoint and Inverse of a Matrix
Last Updated :
01 Nov, 2024
Given a square matrix, find the adjoint and inverse of the matrix. We strongly recommend you to refer determinant of matrix as a prerequisite for this.
Adjoint (or Adjugate) of a matrix is the matrix obtained by taking the transpose of the cofactor matrix of a given square matrix is called its Adjoint or Adjugate matrix. The Adjoint of any square matrix ‘A’ (say) is represented as Adj(A).
Example:
5 -2 2 7
1 0 0 3
-3 1 5 0
3 -1 -9 4
For instance, the cofactor of the top left corner '5' is
+ |0 0 3|
...|1 5 0| = 3(1 * -9 - (-1) * 5) = -12.
...|-1 -9 4|
(The minor matrix is formed by deleting the row
and column of the given entry.)
As another sample, the cofactor of the top row corner '-2' is
-|1 0 3|
...|-3 5 0| = - [1 (20 - 0) - 0 + 3 (27 - 15)] = -56.
...|3 -9 4|
Proceeding like this, we obtain the matrix
[-12 -56 4 4]
[76 208 4 4]
[-60 -82 -2 20]
[-36 -58 -10 12]
Finally, to get the adjoint, just take the previous
matrix's transpose:
[-12 76 -60 -36]
[-56 208 -82 -58]
[4 4 -2 -10]
[4 4 20 12]
Important properties:
Product of a square matrix A with its adjoint yields a diagonal matrix, where each diagonal entry is equal to determinant of A.
i.e.
A.adj(A) = det(A).I
I => Identity matrix of same order as of A.
det(A) => Determinant value of A
A non-zero square matrix ‘A’ of order n is said to be invertible if there exists a unique square matrix ‘B’ of order n such that,
A.B = B.A = I
The matrix 'B' is said to be inverse of 'A'.
i.e., B = A-1
- adj(AB) = (adj B).(adj A)
- adj( k A) = kn-1 adj(A)
- A-1 = (adj A) / |A|
- (A-1)-1 = A
- (AB)-1 = B-1A-1
How to find Adjoint?
We follow the definition given above.
Let A[N][N] be input matrix.
1) Create a matrix adj[N][N] store the adjoint matrix.
2) For every entry A[i][j] in input matrix where 0 <= i < N
and 0 <= j < N.
a) Find cofactor of A[i][j]
b) Find sign of entry. Sign is + if (i+j) is even else
sign is odd.
c) Place the cofactor at adj[j][i]How to find Inverse?
Inverse of a matrix exists only if the matrix is non-singular i.e., determinant should not be 0.
Using determinant and adjoint, we can easily find the inverse of a square matrix using the below formula,
If det(A) != 0
A-1 = adj(A)/det(A)
Else
"Inverse doesn't exist" Inverse is used to find the solution to a system of linear equations.
Below are implementations for finding adjoint and inverse of a matrix.
C++
#include <iostream>
#include <vector>
using namespace std;
// Function to get cofactor of mat[p][q] in cof[][]. n is
// current dimension of mat[][]
void getCof(vector<vector<int>>& mat, vector<vector<int>>& cof,
int p, int q, int n) {
int i = 0, j = 0;
for (int row = 0; row < n; row++) {
for (int col = 0; col < n; col++) {
if (row != p && col != q) {
cof[i][j++] = mat[row][col];
if (j == n - 1) {
j = 0;
i++;
}
}
}
}
}
// Recursive function for finding determinant
// of matrix mat of dimension n
int getDet(vector<vector<int>>& mat, int n) {
if (n == 1) return mat[0][0];
int det = 0;
// To store cofactors
vector<vector<int>> cof(mat.size(), vector<int>(mat.size()));
int sign = 1;
for (int f = 0; f < n; f++) {
getCof(mat, cof, 0, f, n);
det += sign * mat[0][f] * getDet(cof, n - 1);
sign = -sign;
}
return det;
}
// Function to get adjoint of mat in adj
void adjoint(vector<vector<int>>& mat, vector<vector<int>>& adj) {
int n = mat.size();
if (n == 1) {
adj[0][0] = 1;
return;
}
int sign = 1;
vector<vector<int>> cof(n, vector<int>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
getCof(mat, cof, i, j, n);
sign = ((i + j) % 2 == 0) ? 1 : -1;
adj[j][i] = sign * getDet(cof, n - 1);
}
}
}
// Function to calculate and store inverse, returns
// false if matrix is singular
bool inverse(vector<vector<int>>& mat, vector<vector<float>>& inv) {
int n = mat.size();
int det = getDet(mat, n);
if (det == 0) {
cout << "Singular matrix, can't find its inverse";
return false;
}
vector<vector<int>> adj(n, vector<int>(n));
adjoint(mat, adj);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
inv[i][j] = adj[i][j] / float(det);
return true;
}
int main() {
vector<vector<int>> mat = { { 5, -2, 2, 7 },
{ 1, 0, 0, 3 },
{ -3, 1, 5, 0 },
{ 3, -1, -9, 4 } };
int n = mat.size();
vector<vector<int>> adj(n, vector<int>(n)); // To store adjoint
vector<vector<float>> inv(n, vector<float>(n)); // To store inverse
// Print the input matrix
cout << "Input matrix is:\n";
for (auto& row : mat) {
for (int val : row) cout << val << " ";
cout << endl;
}
// Print the adjoint matrix
cout << "\nThe Adjoint is:\n";
adjoint(mat, adj);
for (auto& row : adj) {
for (int val : row) cout << val << " ";
cout << endl;
}
// Print the inverse matrix if it exists
cout << "\nThe Inverse is:\n";
if (inverse(mat, inv)) {
for (auto& row : inv) {
for (float val : row) cout << val << " ";
cout << endl;
}
}
return 0;
}
C
#include <stdio.h>
#define N 4
// Function to get cofactor of mat[p][q] in cof[][]. n is
// current dimension of mat[][]
void getCof(int mat[N][N], int cof[N][N], int p, int q, int n) {
int i = 0, j = 0;
for (int row = 0; row < n; row++) {
for (int col = 0; col < n; col++) {
if (row != p && col != q) {
cof[i][j++] = mat[row][col];
if (j == n - 1) {
j = 0;
i++;
}
}
}
}
}
// Recursive function for finding determinant of matrix mat of dimension n
int getDet(int mat[N][N], int n) {
if (n == 1) return mat[0][0];
int det = 0;
int cof[N][N];
int sign = 1;
for (int f = 0; f < n; f++) {
getCof(mat, cof, 0, f, n);
det += sign * mat[0][f] * getDet(cof, n - 1);
sign = -sign;
}
return det;
}
// Function to get adjoint of mat in adj
void adjoint(int mat[N][N], double adj[N][N]) {
if (N == 1) {
adj[0][0] = 1;
return;
}
int sign = 1;
int cof[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
getCof(mat, cof, i, j, N);
sign = ((i + j) % 2 == 0) ? 1 : -1;
adj[j][i] = sign * getDet(cof, N - 1);
}
}
}
// Function to calculate and store inverse, returns 0 if matrix is singular
int inverse(int mat[N][N], double inv[N][N]) {
int det = getDet(mat, N);
if (det == 0) {
printf("Singular matrix, can't find its inverse\n");
return 0;
}
double adj[N][N];
adjoint(mat, adj);
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
inv[i][j] = adj[i][j] / det;
return 1;
}
int main() {
int mat[N][N] = { { 5, -2, 2, 7 },
{ 1, 0, 0, 3 },
{ -3, 1, 5, 0 },
{ 3, -1, -9, 4 } };
double adj[N][N];
double inv[N][N];
// Print the input matrix
printf("Input matrix is:\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
// Print the adjoint matrix
printf("\nThe Adjoint is:\n");
adjoint(mat, adj);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%lf ", adj[i][j]);
}
printf("\n");
}
// Print the inverse matrix if it exists
printf("\nThe Inverse is:\n");
if (inverse(mat, inv)) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%lf ", inv[i][j]);
}
printf("\n");
}
}
return 0;
}
Java
// Importing required classes
import java.util.Arrays;
import java.util.Scanner;
public class GfG {
// Function to get cofactor of mat[p][q] in cof[][]
static void getCof(int[][] mat, int[][] cof, int p, int q, int n) {
int i = 0, j = 0;
for (int row = 0; row < n; row++) {
for (int col = 0; col < n; col++) {
if (row != p && col != q) {
cof[i][j++] = mat[row][col];
if (j == n - 1) {
j = 0;
i++;
}
}
}
}
}
// Recursive function for finding determinant of
// matrix mat of dimension n
static int getDet(int[][] mat, int n) {
if (n == 1) return mat[0][0];
int det = 0;
int[][] cof = new int[n][n];
int sign = 1;
for (int f = 0; f < n; f++) {
getCof(mat, cof, 0, f, n);
det += sign * mat[0][f] * getDet(cof, n - 1);
sign = -sign;
}
return det;
}
// Function to get adjoint of mat in adj
static void adjoint(int[][] mat, int[][] adj) {
int n = mat.length;
if (n == 1) {
adj[0][0] = 1;
return;
}
int sign;
int[][] cof = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
getCof(mat, cof, i, j, n);
sign = ((i + j) % 2 == 0) ? 1 : -1;
adj[j][i] = sign * getDet(cof, n - 1);
}
}
}
// Function to calculate and store inverse, returns
// false if matrix is singular
static boolean inverse(int[][] mat, float[][] inv) {
int n = mat.length;
int det = getDet(mat, n);
if (det == 0) {
System.out.println("Singular matrix, can't find its inverse");
return false;
}
int[][] adj = new int[n][n];
adjoint(mat, adj);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
inv[i][j] = (float) adj[i][j] / det;
return true;
}
public static void main(String[] args) {
int[][] mat = { { 5, -2, 2, 7 }, { 1, 0, 0, 3 },
{ -3, 1, 5, 0 }, { 3, -1, -9, 4 } };
int n = mat.length;
int[][] adj = new int[n][n]; // To store adjoint
float[][] inv = new float[n][n]; // To store inverse
// Print the input matrix
System.out.println("Input matrix is:");
for (int[] row : mat) {
System.out.println(Arrays.toString(row));
}
// Print the adjoint matrix
System.out.println("\nThe Adjoint is:");
adjoint(mat, adj);
for (int[] row : adj) {
System.out.println(Arrays.toString(row));
}
// Print the inverse matrix if it exists
System.out.println("\nThe Inverse is:");
if (inverse(mat, inv)) {
for (float[] row : inv) {
System.out.println(Arrays.toString(row));
}
}
}
}
Python
# Function to get cofactor of mat[p][q] in cof[][]
def get_cof(mat, cof, p, q, n):
i = 0
j = 0
for row in range(n):
for col in range(n):
if row != p and col != q:
cof[i][j] = mat[row][col]
j += 1
if j == n - 1:
j = 0
i += 1
# Recursive function for finding determinant
# of matrix mat of dimension n
def get_det(mat, n):
if n == 1:
return mat[0][0]
det = 0
cof = [[0] * n for _ in range(n)] # To store cofactors
sign = 1
for f in range(n):
get_cof(mat, cof, 0, f, n)
det += sign * mat[0][f] * get_det(cof, n - 1)
sign = -sign
return det
# Function to get adjoint of mat in adj
def adjoint(mat, adj):
n = len(mat)
if n == 1:
adj[0][0] = 1
return
sign = 1
cof = [[0] * n for _ in range(n)]
for i in range(n):
for j in range(n):
get_cof(mat, cof, i, j, n)
sign = 1 if (i + j) % 2 == 0 else -1
adj[j][i] = sign * get_det(cof, n - 1)
# Function to calculate and store inverse, returns
# false if matrix is singular
def inverse(mat):
n = len(mat)
det = get_det(mat, n)
if det == 0:
print("Singular matrix, can't find its inverse")
return None
adj = [[0] * n for _ in range(n)]
adjoint(mat, adj)
inv = [[adj[i][j] / det for j in range(n)] for i in range(n)]
return inv
if __name__ == '__main__':
mat = [[5, -2, 2, 7], [1, 0, 0, 3], [-3, 1, 5, 0], [3, -1, -9, 4]]
n = len(mat)
adj = [[0] * n for _ in range(n)] # To store adjoint
# Print the input matrix
print("Input matrix is:")
for row in mat:
print(row)
# Print the adjoint matrix
print("\nThe Adjoint is:")
adjoint(mat, adj)
for row in adj:
print(row)
# Print the inverse matrix if it exists
print("\nThe Inverse is:")
inv = inverse(mat)
if inv:
for row in inv:
print(row)
C#
using System;
class MatrixOperations {
// Function to get cofactor of mat[p][q] in cof[][]. n is
// current dimension of mat[][]
static void GetCof(int[,] mat, int[,] cof, int p, int q, int n) {
int i = 0, j = 0;
for (int row = 0; row < n; row++) {
for (int col = 0; col < n; col++) {
if (row != p && col != q) {
cof[i, j++] = mat[row, col];
if (j == n - 1) {
j = 0;
i++;
}
}
}
}
}
// Recursive function for finding determinant of matrix mat of dimension n
static int GetDet(int[,] mat, int n) {
if (n == 1) return mat[0, 0];
int det = 0;
// To store cofactors
int[,] cof = new int[n, n];
int sign = 1;
for (int f = 0; f < n; f++) {
GetCof(mat, cof, 0, f, n);
det += sign * mat[0, f] * GetDet(cof, n - 1);
sign = -sign;
}
return det;
}
// Function to get adjoint of mat in adj
static void Adjoint(int[,] mat, double[,] adj) {
int n = mat.GetLength(0);
if (n == 1) {
adj[0, 0] = 1;
return;
}
int sign = 1;
int[,] cof = new int[n, n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
GetCof(mat, cof, i, j, n);
sign = ((i + j) % 2 == 0) ? 1 : -1;
adj[j, i] = sign * GetDet(cof, n - 1);
}
}
}
// Function to calculate and store inverse, returns false if matrix is singular
static bool Inverse(int[,] mat, double[,] inv) {
int n = mat.GetLength(0);
int det = GetDet(mat, n);
if (det == 0) {
Console.WriteLine("Singular matrix, can't find its inverse");
return false;
}
double[,] adj = new double[n, n];
Adjoint(mat, adj);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
inv[i, j] = adj[i, j] / det;
return true;
}
static void Main() {
int[,] mat = new int[,] { { 5, -2, 2, 7 },
{ 1, 0, 0, 3 },
{ -3, 1, 5, 0 },
{ 3, -1, -9, 4 } };
int n = mat.GetLength(0);
double[,] adj = new double[n, n]; // To store adjoint
double[,] inv = new double[n, n]; // To store inverse
// Print the input matrix
Console.WriteLine("Input matrix is:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
Console.Write(mat[i, j] + " ");
}
Console.WriteLine();
}
// Print the adjoint matrix
Console.WriteLine("\nThe Adjoint is:");
Adjoint(mat, adj);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
Console.Write(adj[i, j] + " ");
}
Console.WriteLine();
}
// Print the inverse matrix if it exists
Console.WriteLine("\nThe Inverse is:");
if (Inverse(mat, inv)) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
Console.Write(inv[i, j] + " ");
}
Console.WriteLine();
}
}
}
}
JavaScript
// Function to get cofactor of mat[p][q] in cof[][]. n is
// current dimension of mat[][]
function getCof(mat, cof, p, q, n) {
let i = 0, j = 0;
for (let row = 0; row < n; row++) {
for (let col = 0; col < n; col++) {
if (row !== p && col !== q) {
cof[i][j++] = mat[row][col];
if (j === n - 1) {
j = 0;
i++;
}
}
}
}
}
// Recursive function for finding determinant
// of matrix mat of dimension n
function getDet(mat, n) {
if (n === 1) return mat[0][0];
let det = 0;
// To store cofactors
let cof = Array.from({ length: mat.length }, () => Array(mat.length).fill(0));
let sign = 1;
for (let f = 0; f < n; f++) {
getCof(mat, cof, 0, f, n);
det += sign * mat[0][f] * getDet(cof, n - 1);
sign = -sign;
}
return det;
}
// Function to get adjoint of mat in adj
function adjoint(mat, adj) {
let n = mat.length;
if (n === 1) {
adj[0][0] = 1;
return;
}
let sign = 1;
let cof = Array.from({ length: n }, () => Array(n).fill(0));
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
getCof(mat, cof, i, j, n);
sign = ((i + j) % 2 === 0) ? 1 : -1;
adj[j][i] = sign * getDet(cof, n - 1);
}
}
}
// Function to calculate and store inverse, returns
// false if matrix is singular
function inverse(mat, inv) {
let n = mat.length;
let det = getDet(mat, n);
if (det === 0) {
console.log("Singular matrix, can't find its inverse");
return false;
}
let adj = Array.from({ length: n }, () => Array(n).fill(0));
adjoint(mat, adj);
for (let i = 0; i < n; i++)
for (let j = 0; j < n; j++)
inv[i][j] = adj[i][j] / det;
return true;
}
let mat = [ [ 5, -2, 2, 7 ],
[ 1, 0, 0, 3 ],
[ -3, 1, 5, 0 ],
[ 3, -1, -9, 4 ] ];
let n = mat.length;
let adj = Array.from({ length: n }, () => Array(n).fill(0)); // To store adjoint
let inv = Array.from({ length: n }, () => Array(n).fill(0)); // To store inverse
// Print the input matrix
console.log("Input matrix is:");
mat.forEach(row => {
console.log(row.join(' '));
});
// Print the adjoint matrix
console.log("\nThe Adjoint is:");
adjoint(mat, adj);
adj.forEach(row => {
console.log(row.join(' '));
});
// Print the inverse matrix if it exists
console.log("\nThe Inverse is:");
if (inverse(mat, inv)) {
inv.forEach(row => {
console.log(row.join(' '));
});
}
OutputInput matrix is:
5 -2 2 7
1 0 0 3
-3 1 5 0
3 -1 -9 4
The Adjoint is:
-12 76 -60 -36
-56 208 -82 -58
4 4 -2 -10
4 4 20 12
The Inverse is:
-0.136364 0.863636 -0.681818 -0.409091
-0.636364 2.36364 -0.931818 -0.659091
0.0454545 0.0454545 -0.0227273 -0.113636
0.0454545 0.0454545 0.227273 0.136364
Please refer determinant of matrix for details of getCofactor() and determinant().