 GFG App
Open App Browser
Continue

# Adjoint and Inverse of a Matrix

Given a square matrix, find the adjoint and inverse of the matrix.
We strongly recommend you to refer below as a prerequisite for this.

`Determinant of a Matrix`

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:

```Below example and explanation are taken from here.
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```
• A-1 = (adj A) / |A|
• (A-1)-1 = A
• (AB)-1 = B-1A-1

We follow the definition given above.

```Let A[N][N] be input 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
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++

 `// C++ program to find adjoint and inverse of a matrix` `#include ` `using` `namespace` `std;` `#define N 4`   `// Function to get cofactor of A[p][q] in temp[][]. n is` `// current dimension of A[][]` `void` `getCofactor(``int` `A[N][N], ``int` `temp[N][N], ``int` `p, ``int` `q,` `                 ``int` `n)` `{` `    ``int` `i = 0, j = 0;`   `    ``// Looping for each element of the matrix` `    ``for` `(``int` `row = 0; row < n; row++) {` `        ``for` `(``int` `col = 0; col < n; col++) {` `            ``//  Copying into temporary matrix only those` `            ``//  element which are not in given row and` `            ``//  column` `            ``if` `(row != p && col != q) {` `                ``temp[i][j++] = A[row][col];`   `                ``// Row is filled, so increase row index and` `                ``// reset col index` `                ``if` `(j == n - 1) {` `                    ``j = 0;` `                    ``i++;` `                ``}` `            ``}` `        ``}` `    ``}` `}`   `/* Recursive function for finding determinant of matrix.` `   ``n is current dimension of A[][]. */` `int` `determinant(``int` `A[N][N], ``int` `n)` `{` `    ``int` `D = 0; ``// Initialize result`   `    ``//  Base case : if matrix contains single element` `    ``if` `(n == 1)` `        ``return` `A;`   `    ``int` `temp[N][N]; ``// To store cofactors`   `    ``int` `sign = 1; ``// To store sign multiplier`   `    ``// Iterate for each element of first row` `    ``for` `(``int` `f = 0; f < n; f++) {` `        ``// Getting Cofactor of A[f]` `        ``getCofactor(A, temp, 0, f, n);` `        ``D += sign * A[f] * determinant(temp, n - 1);`   `        ``// terms are to be added with alternate sign` `        ``sign = -sign;` `    ``}`   `    ``return` `D;` `}`   `// Function to get adjoint of A[N][N] in adj[N][N].` `void` `adjoint(``int` `A[N][N], ``int` `adj[N][N])` `{` `    ``if` `(N == 1) {` `        ``adj = 1;` `        ``return``;` `    ``}`   `    ``// temp is used to store cofactors of A[][]` `    ``int` `sign = 1, temp[N][N];`   `    ``for` `(``int` `i = 0; i < N; i++) {` `        ``for` `(``int` `j = 0; j < N; j++) {` `            ``// Get cofactor of A[i][j]` `            ``getCofactor(A, temp, i, j, N);`   `            ``// sign of adj[j][i] positive if sum of row` `            ``// and column indexes is even.` `            ``sign = ((i + j) % 2 == 0) ? 1 : -1;`   `            ``// Interchanging rows and columns to get the` `            ``// transpose of the cofactor matrix` `            ``adj[j][i] = (sign) * (determinant(temp, N - 1));` `        ``}` `    ``}` `}`   `// Function to calculate and store inverse, returns false if` `// matrix is singular` `bool` `inverse(``int` `A[N][N], ``float` `inverse[N][N])` `{` `    ``// Find determinant of A[][]` `    ``int` `det = determinant(A, N);` `    ``if` `(det == 0) {` `        ``cout << ``"Singular matrix, can't find its inverse"``;` `        ``return` `false``;` `    ``}`   `    ``// Find adjoint` `    ``int` `adj[N][N];` `    ``adjoint(A, adj);`   `    ``// Find Inverse using formula "inverse(A) =` `    ``// adj(A)/det(A)"` `    ``for` `(``int` `i = 0; i < N; i++)` `        ``for` `(``int` `j = 0; j < N; j++)` `            ``inverse[i][j] = adj[i][j] / ``float``(det);`   `    ``return` `true``;` `}`   `// Generic function to display the matrix.  We use it to` `// display both adjoin and inverse. adjoin is integer matrix` `// and inverse is a float.` `template` `<``class` `T> ``void` `display(T A[N][N])` `{` `    ``for` `(``int` `i = 0; i < N; i++) {` `        ``for` `(``int` `j = 0; j < N; j++)` `            ``cout << A[i][j] << ``" "``;` `        ``cout << endl;` `    ``}` `}`   `// Driver program` `int` `main()` `{` `    ``int` `A[N][N] = { { 5, -2, 2, 7 },` `                    ``{ 1, 0, 0, 3 },` `                    ``{ -3, 1, 5, 0 },` `                    ``{ 3, -1, -9, 4 } };`   `    ``int` `adj[N][N]; ``// To store adjoint of A[][]`   `    ``float` `inv[N][N]; ``// To store inverse of A[][]`   `    ``cout << ``"Input matrix is :\n"``;` `    ``display(A);`   `    ``cout << ``"\nThe Adjoint is :\n"``;` `    ``adjoint(A, adj);` `    ``display(adj);`   `    ``cout << ``"\nThe Inverse is :\n"``;` `    ``if` `(inverse(A, inv))` `        ``display(inv);`   `    ``return` `0;` `}`

## Java

 `// Java program to find adjoint and inverse of a matrix` `class` `GFG` `{` `    `  `static` `final` `int` `N = ``4``;`   `// Function to get cofactor of A[p][q] in temp[][]. n is current` `// dimension of A[][]` `static` `void` `getCofactor(``int` `A[][], ``int` `temp[][], ``int` `p, ``int` `q, ``int` `n)` `{` `    ``int` `i = ``0``, j = ``0``;`   `    ``// Looping for each element of the matrix` `    ``for` `(``int` `row = ``0``; row < n; row++)` `    ``{` `        ``for` `(``int` `col = ``0``; col < n; col++)` `        ``{` `            ``// Copying into temporary matrix only those element` `            ``// which are not in given row and column` `            ``if` `(row != p && col != q)` `            ``{` `                ``temp[i][j++] = A[row][col];`   `                ``// Row is filled, so increase row index and` `                ``// reset col index` `                ``if` `(j == n - ``1``)` `                ``{` `                    ``j = ``0``;` `                    ``i++;` `                ``}` `            ``}` `        ``}` `    ``}` `}`   `/* Recursive function for finding determinant of matrix.` `n is current dimension of A[][]. */` `static` `int` `determinant(``int` `A[][], ``int` `n)` `{` `    ``int` `D = ``0``; ``// Initialize result`   `    ``// Base case : if matrix contains single element` `    ``if` `(n == ``1``)` `        ``return` `A[``0``][``0``];`   `    ``int` `[][]temp = ``new` `int``[N][N]; ``// To store cofactors`   `    ``int` `sign = ``1``; ``// To store sign multiplier`   `    ``// Iterate for each element of first row` `    ``for` `(``int` `f = ``0``; f < n; f++)` `    ``{` `        ``// Getting Cofactor of A[f]` `        ``getCofactor(A, temp, ``0``, f, n);` `        ``D += sign * A[``0``][f] * determinant(temp, n - ``1``);`   `        ``// terms are to be added with alternate sign` `        ``sign = -sign;` `    ``}`   `    ``return` `D;` `}`   `// Function to get adjoint of A[N][N] in adj[N][N].` `static` `void` `adjoint(``int` `A[][],``int` `[][]adj)` `{` `    ``if` `(N == ``1``)` `    ``{` `        ``adj[``0``][``0``] = ``1``;` `        ``return``;` `    ``}`   `    ``// temp is used to store cofactors of A[][]` `    ``int` `sign = ``1``;` `    ``int` `[][]temp = ``new` `int``[N][N];`   `    ``for` `(``int` `i = ``0``; i < N; i++)` `    ``{` `        ``for` `(``int` `j = ``0``; j < N; j++)` `        ``{` `            ``// Get cofactor of A[i][j]` `            ``getCofactor(A, temp, i, j, N);`   `            ``// sign of adj[j][i] positive if sum of row` `            ``// and column indexes is even.` `            ``sign = ((i + j) % ``2` `== ``0``)? ``1``: -``1``;`   `            ``// Interchanging rows and columns to get the` `            ``// transpose of the cofactor matrix` `            ``adj[j][i] = (sign)*(determinant(temp, N-``1``));` `        ``}` `    ``}` `}`   `// Function to calculate and store inverse, returns false if` `// matrix is singular` `static` `boolean` `inverse(``int` `A[][], ``float` `[][]inverse)` `{` `    ``// Find determinant of A[][]` `    ``int` `det = determinant(A, N);` `    ``if` `(det == ``0``)` `    ``{` `        ``System.out.print(``"Singular matrix, can't find its inverse"``);` `        ``return` `false``;` `    ``}`   `    ``// Find adjoint` `    ``int` `[][]adj = ``new` `int``[N][N];` `    ``adjoint(A, adj);`   `    ``// Find Inverse using formula "inverse(A) = adj(A)/det(A)"` `    ``for` `(``int` `i = ``0``; i < N; i++)` `        ``for` `(``int` `j = ``0``; j < N; j++)` `            ``inverse[i][j] = adj[i][j]/(``float``)det;`   `    ``return` `true``;` `}`   `// Generic function to display the matrix. We use it to display` `// both adjoin and inverse. adjoin is integer matrix and inverse` `// is a float.`   `static` `void` `display(``int` `A[][])` `{` `    ``for` `(``int` `i = ``0``; i < N; i++)` `    ``{` `        ``for` `(``int` `j = ``0``; j < N; j++)` `            ``System.out.print(A[i][j]+ ``" "``);` `        ``System.out.println();` `    ``}` `}` `static` `void` `display(``float` `A[][])` `{` `    ``for` `(``int` `i = ``0``; i < N; i++)` `    ``{` `        ``for` `(``int` `j = ``0``; j < N; j++)` `            ``System.out.printf(``"%.6f "``,A[i][j]);` `        ``System.out.println();` `    ``}` `}`   `// Driver program` `public` `static` `void` `main(String[] args)` `{` `    ``int` `A[][] = { {``5``, -``2``, ``2``, ``7``},` `                    ``{``1``, ``0``, ``0``, ``3``},` `                    ``{-``3``, ``1``, ``5``, ``0``},` `                    ``{``3``, -``1``, -``9``, ``4``}};`   `    ``int` `[][]adj = ``new` `int``[N][N]; ``// To store adjoint of A[][]`   `    ``float` `[][]inv = ``new` `float``[N][N]; ``// To store inverse of A[][]`   `    ``System.out.print(``"Input matrix is :\n"``);` `    ``display(A);`   `    ``System.out.print(``"\nThe Adjoint is :\n"``);` `    ``adjoint(A, adj);` `    ``display(adj);`   `    ``System.out.print(``"\nThe Inverse is :\n"``);` `    ``if` `(inverse(A, inv))` `        ``display(inv);`   `}` `}`   `// This code is contributed by Rajput-Ji`

## Python3

 `# Python3 program to find adjoint and` `# inverse of a matrix` `N ``=` `4`   `# Function to get cofactor of` `# A[p][q] in temp[][]. n is current` `# dimension of A[][]` `def` `getCofactor(A, temp, p, q, n):`   `    ``i ``=` `0` `    ``j ``=` `0`   `    ``# Looping for each element of the matrix` `    ``for` `row ``in` `range``(n):`   `        ``for` `col ``in` `range``(n):`   `            ``# Copying into temporary matrix only those element` `            ``# which are not in given row and column` `            ``if` `(row !``=` `p ``and` `col !``=` `q):`   `                ``temp[i][j] ``=` `A[row][col]` `                ``j ``+``=` `1`   `                ``# Row is filled, so increase row index and` `                ``# reset col index` `                ``if` `(j ``=``=` `n ``-` `1``):` `                    ``j ``=` `0` `                    ``i ``+``=` `1`     `# Recursive function for finding determinant of matrix.` `#  n is current dimension of A[][].` `def` `determinant(A, n):`   `    ``D ``=` `0`   `# Initialize result`   `    ``# Base case : if matrix contains single element` `    ``if` `(n ``=``=` `1``):` `        ``return` `A[``0``][``0``]`   `    ``temp ``=` `[]   ``# To store cofactors` `    ``for` `i ``in` `range``(N):` `        ``temp.append([``None` `for` `_ ``in` `range``(N)])`   `    ``sign ``=` `1`   `# To store sign multiplier`   `    ``# Iterate for each element of first row` `    ``for` `f ``in` `range``(n):`   `        ``# Getting Cofactor of A[f]` `        ``getCofactor(A, temp, ``0``, f, n)` `        ``D ``+``=` `sign ``*` `A[``0``][f] ``*` `determinant(temp, n ``-` `1``)`   `        ``# terms are to be added with alternate sign` `        ``sign ``=` `-``sign`   `    ``return` `D`     `# Function to get adjoint of A[N][N] in adj[N][N].` `def` `adjoint(A, adj):`   `    ``if` `(N ``=``=` `1``):` `        ``adj[``0``][``0``] ``=` `1` `        ``return`   `    ``# temp is used to store cofactors of A[][]` `    ``sign ``=` `1` `    ``temp ``=` `[]   ``# To store cofactors` `    ``for` `i ``in` `range``(N):` `        ``temp.append([``None` `for` `_ ``in` `range``(N)])`   `    ``for` `i ``in` `range``(N):` `        ``for` `j ``in` `range``(N):` `            ``# Get cofactor of A[i][j]` `            ``getCofactor(A, temp, i, j, N)`   `            ``# sign of adj[j][i] positive if sum of row` `            ``# and column indexes is even.` `            ``sign ``=` `[``1``, ``-``1``][(i ``+` `j) ``%` `2``]`   `            ``# Interchanging rows and columns to get the` `            ``# transpose of the cofactor matrix` `            ``adj[j][i] ``=` `(sign)``*``(determinant(temp, N``-``1``))`     `# Function to calculate and store inverse, returns false if` `# matrix is singular` `def` `inverse(A, inverse):`   `    ``# Find determinant of A[][]` `    ``det ``=` `determinant(A, N)` `    ``if` `(det ``=``=` `0``):` `        ``print``(``"Singular matrix, can't find its inverse"``)` `        ``return` `False`   `    ``# Find adjoint` `    ``adj ``=` `[]` `    ``for` `i ``in` `range``(N):` `        ``adj.append([``None` `for` `_ ``in` `range``(N)])` `    ``adjoint(A, adj)`   `    ``# Find Inverse using formula "inverse(A) = adj(A)/det(A)"` `    ``for` `i ``in` `range``(N):` `        ``for` `j ``in` `range``(N):` `            ``inverse[i][j] ``=` `adj[i][j] ``/` `det`   `    ``return` `True`     `# Generic function to display the` `# matrix. We use it to display` `# both adjoin and inverse. adjoin` `# is integer matrix and inverse` `# is a float.` `def` `display(A):` `    ``for` `i ``in` `range``(N):` `        ``for` `j ``in` `range``(N):` `            ``print``(A[i][j], end``=``" "``)` `        ``print``()`     `def` `displays(A):` `    ``for` `i ``in` `range``(N):` `        ``for` `j ``in` `range``(N):` `            ``print``(``round``(A[i][j], ``6``), end``=``" "``)` `        ``print``()`     `# Driver program`   `A ``=` `[[``5``, ``-``2``, ``2``, ``7``], [``1``, ``0``, ``0``, ``3``], [``-``3``, ``1``, ``5``, ``0``], [``3``, ``-``1``, ``-``9``, ``4``]]` `adj ``=` `[``None` `for` `_ ``in` `range``(N)]` `inv ``=` `[``None` `for` `_ ``in` `range``(N)]`   `for` `i ``in` `range``(N):` `    ``adj[i] ``=` `[``None` `for` `_ ``in` `range``(N)]` `    ``inv[i] ``=` `[``None` `for` `_ ``in` `range``(N)]`     `print``(``"Input matrix is :"``)` `display(A)`   `print``(``"\nThe Adjoint is :"``)` `adjoint(A, adj)` `display(adj)`   `print``(``"\nThe Inverse is :"``)` `if` `(inverse(A, inv)):` `    ``displays(inv)`   `# This code is contributed by phasing17`

## C#

 `// C# program to find adjoint and inverse of a matrix` `using` `System;` `using` `System.Collections.Generic;`   `class` `GFG` `{` `    `  `static` `readonly` `int` `N = 4;`   `// Function to get cofactor of A[p,q] in [,]temp. n is current` `// dimension of [,]A` `static` `void` `getCofactor(``int` `[,]A, ``int` `[,]temp, ``int` `p, ``int` `q, ``int` `n)` `{` `    ``int` `i = 0, j = 0;`   `    ``// Looping for each element of the matrix` `    ``for` `(``int` `row = 0; row < n; row++)` `    ``{` `        ``for` `(``int` `col = 0; col < n; col++)` `        ``{` `            ``// Copying into temporary matrix only those element` `            ``// which are not in given row and column` `            ``if` `(row != p && col != q)` `            ``{` `                ``temp[i, j++] = A[row, col];`   `                ``// Row is filled, so increase row index and` `                ``// reset col index` `                ``if` `(j == n - 1)` `                ``{` `                    ``j = 0;` `                    ``i++;` `                ``}` `            ``}` `        ``}` `    ``}` `}`   `/* Recursive function for finding determinant of matrix.` `n is current dimension of [,]A. */` `static` `int` `determinant(``int` `[,]A, ``int` `n)` `{` `    ``int` `D = 0; ``// Initialize result`   `    ``// Base case : if matrix contains single element` `    ``if` `(n == 1)` `        ``return` `A[0, 0];`   `    ``int` `[,]temp = ``new` `int``[N, N]; ``// To store cofactors`   `    ``int` `sign = 1; ``// To store sign multiplier`   `    ``// Iterate for each element of first row` `    ``for` `(``int` `f = 0; f < n; f++)` `    ``{` `        ``// Getting Cofactor of A[0,f]` `        ``getCofactor(A, temp, 0, f, n);` `        ``D += sign * A[0, f] * determinant(temp, n - 1);`   `        ``// terms are to be added with alternate sign` `        ``sign = -sign;` `    ``}` `    ``return` `D;` `}`   `// Function to get adjoint of A[N,N] in adj[N,N].` `static` `void` `adjoint(``int` `[,]A, ``int` `[,]adj)` `{` `    ``if` `(N == 1)` `    ``{` `        ``adj[0, 0] = 1;` `        ``return``;` `    ``}`   `    ``// temp is used to store cofactors of [,]A` `    ``int` `sign = 1;` `    ``int` `[,]temp = ``new` `int``[N, N];`   `    ``for` `(``int` `i = 0; i < N; i++)` `    ``{` `        ``for` `(``int` `j = 0; j < N; j++)` `        ``{` `            ``// Get cofactor of A[i,j]` `            ``getCofactor(A, temp, i, j, N);`   `            ``// sign of adj[j,i] positive if sum of row` `            ``// and column indexes is even.` `            ``sign = ((i + j) % 2 == 0)? 1: -1;`   `            ``// Interchanging rows and columns to get the` `            ``// transpose of the cofactor matrix` `            ``adj[j, i] = (sign) * (determinant(temp, N - 1));` `        ``}` `    ``}` `}`   `// Function to calculate and store inverse, returns false if` `// matrix is singular` `static` `bool` `inverse(``int` `[,]A, ``float` `[,]inverse)` `{` `    ``// Find determinant of [,]A` `    ``int` `det = determinant(A, N);` `    ``if` `(det == 0)` `    ``{` `        ``Console.Write(``"Singular matrix, can't find its inverse"``);` `        ``return` `false``;` `    ``}`   `    ``// Find adjoint` `    ``int` `[,]adj = ``new` `int``[N, N];` `    ``adjoint(A, adj);`   `    ``// Find Inverse using formula "inverse(A) = adj(A)/det(A)"` `    ``for` `(``int` `i = 0; i < N; i++)` `        ``for` `(``int` `j = 0; j < N; j++)` `            ``inverse[i, j] = adj[i, j]/(``float``)det;`   `    ``return` `true``;` `}`   `// Generic function to display the matrix. We use it to display` `// both adjoin and inverse. adjoin is integer matrix and inverse` `// is a float.` `static` `void` `display(``int` `[,]A)` `{` `    ``for` `(``int` `i = 0; i < N; i++)` `    ``{` `        ``for` `(``int` `j = 0; j < N; j++)` `            ``Console.Write(A[i, j]+ ``" "``);` `        ``Console.WriteLine();` `    ``}` `}` `static` `void` `display(``float` `[,]A)` `{` `    ``for` `(``int` `i = 0; i < N; i++)` `    ``{` `        ``for` `(``int` `j = 0; j < N; j++)` `            ``Console.Write(``"{0:F6} "``, A[i, j]);` `        ``Console.WriteLine();` `    ``}` `}`   `// Driver program` `public` `static` `void` `Main(String[] args)` `{` `    ``int` `[,]A = { {5, -2, 2, 7},` `                    ``{1, 0, 0, 3},` `                    ``{-3, 1, 5, 0},` `                    ``{3, -1, -9, 4}};`   `    ``int` `[,]adj = ``new` `int``[N, N]; ``// To store adjoint of [,]A`   `    ``float` `[,]inv = ``new` `float``[N, N]; ``// To store inverse of [,]A`   `    ``Console.Write(``"Input matrix is :\n"``);` `    ``display(A);`   `    ``Console.Write(``"\nThe Adjoint is :\n"``);` `    ``adjoint(A, adj);` `    ``display(adj);`   `    ``Console.Write(``"\nThe Inverse is :\n"``);` `    ``if` `(inverse(A, inv))` `        ``display(inv);` `}` `}`   `// This code is contributed by 29AjayKumar`

## Javascript

 ``

Output:

```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 https://www..geeksforgeeks.org/determinant-of-a-matrix/ for details of getCofactor() and determinant().