Given a square matrix, find adjoint and inverse of the matrix.
We strongly recommend you to refer below as a prerequisite of this.
Determinant of a Matrix
What is Adjoint?
Adjoint (or Adjugate) of a matrix is the matrix obtained by taking 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).
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]
- Product of a square matrix A with its adjoint yields a diagonal matrix, where each diagonal entry is equal to determinant of A.
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
How to find Adjoint?
We follow 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 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 equation.
Below are implementation for finding adjoint and inverse of a matrix.
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/amp/ for details of getCofactor() and determinant().
This article is contributed by Ashutosh Kumar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Decimal Equivalent of Gray Code and its Inverse
- Find sum of inverse of the divisors when sum of divisors and the number is given
- Find the value of P and modular inverse of Q modulo 998244353
- Chinese Remainder Theorem | Set 2 (Inverse Modulo based Implementation)
- Fast method to calculate inverse square root of a floating point number in IEEE 754 format
- Modular multiplicative inverse from 1 to n
- Fast inverse square root
- Program to implement Inverse Interpolation using Lagrange Formula
- Check if the given array is same as its inverse permutation
- Count array elements having modular inverse under given prime number P equal to itself
- Nearest smaller number to N having multiplicative inverse under modulo N equal to that number
- XOR of array elements whose modular inverse with a given number exists
- Modular multiplicative inverse
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix
- Program to check diagonal matrix and scalar matrix
- Check if matrix can be converted to another matrix by transposing square sub-matrices
- Maximum trace possible for any sub-matrix of the given matrix
- Create matrix whose sum of diagonals in each sub matrix is even
- Construct a square Matrix whose parity of diagonal sum is same as size of matrix
- Minimize count of adjacent row swaps to convert given Matrix to a Lower Triangular Matrix