1. Home >
2. Apps >
3. Groups >

# Find the way to find inverse matrix urgent

Question asked by tveswaran in #Coffee Room on Dec 26, 2008 tveswaran · Dec 26, 2008
Rank E1 - BEGINNER
a=
17 17 5
21 18 21
2 2 19

the ans is

4 9 15
15 17 6
24 0 17

i want to how this comes . Posted in: #Coffee Room Ashutosh_shukla · Dec 27, 2008
Rank D1 - MASTER
Hey if you want the mathematics method that we had in course then refer to any textbook with a chapter on matrices.But for coding part if you are

😕 then

Check this thing out
Inverse = Adjoint divided by the determinant of matrix

Now how to find the adjoint again if you are 😕 then

Adjoint = Transpose of the matrix of cofactors.

Now how to find the cofactors again if you are 😕 then

cofactor of element is minor of the element multiplied by (-1) raised to (i+j)

Now how to find the minor again if you are 😕 then

minor is the value of the determinant formed by skipping the ith row and jth column then taking remaining rows and columns.

I guess i have clarified it enough.
The hard task is to find the determinant in generalized manner.
You have two loops one inside the another and then again two loops inside them to find the determinant of remaining rows and columns.Can you do this??????????????I have a big doubt Try it out even if you get the code working then also it would be of the complexity n^4 which is not advisable I think i posted quite a big suggestion sorry for that but i am really 😁 tveswaran · Dec 30, 2008
Rank E1 - BEGINNER
thanx dude.... but i know the method wat u tld above... but i need to know how the inverse come for given matrix.......
i already tried ur method....but its not give the solution....... Ashutosh_shukla · Dec 31, 2008
Rank D1 - MASTER
I forgot to specify that this method works only for the matrices with non zero determinant
see if your input satisfies this criteria Rank C2 - EXPERT
Below is my sample code to determine inverse of a matrix of order n.

```  #include "stdio.h"

#define MAX_MATRIX_ORDER 10

void print_matrix(int matrix[MAX_MATRIX_ORDER][MAX_MATRIX_ORDER], int order) {
int i = 0, j = 0;
for(i = 0; i < order; i++) {
for(j = 0; j < order; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}

long calculate_determinant(int matrix[MAX_MATRIX_ORDER][MAX_MATRIX_ORDER], int order) {
int submatrix[MAX_MATRIX_ORDER][MAX_MATRIX_ORDER] = { 0 };
int determinant = 0;
int sign = 1;
int i = 0, j = 0, k = 0;

if(order == 1) {
determinant = matrix;
} else {
for(i = 0; i < order; i++) {
for(j = 0; j < (order - 1); j++) {
for(k = 0; k < (order - 1); k++) {
if(k < i) {
submatrix[j][k] = matrix[j + 1][k];
} else {
submatrix[j][k] = matrix[j + 1][k + 1];
}
}
}
determinant += sign*matrix[i]*calculate_determinant(submatrix, order -1);
sign = -sign;
}
}

return determinant;
}

void determine_cofactor_matrix(int matrix[MAX_MATRIX_ORDER][MAX_MATRIX_ORDER], int cofactor_matrix[MAX_MATRIX_ORDER][MAX_MATRIX_ORDER], int order) {
int submatrix[MAX_MATRIX_ORDER][MAX_MATRIX_ORDER] = { 0 };
int sign_x = 1, sign_y = 1;
int index_x = 0, index_y = 0;
int i = 0, j = 0, k = 0, l = 0;

if(order == 1) {
cofactor_matrix = matrix;
} else {
for(i = 0; i < order; i++) {
for(j = 0; j < order; j++) {
index_x = 0;
index_y = 0;
for(k = 0; k < order; k++) {
if(k != i) {
for(l = 0; l < order; l++) {
if(l != j) {
submatrix[index_x][index_y] = matrix[k][l];
index_y++;
}
}
index_x++;
index_y = 0;
}
}
cofactor_matrix[i][j] = sign_x*sign_y*calculate_determinant(submatrix, order -1);
sign_y = -sign_y;
}
sign_x = -sign_x;
sign_y = 1;
}
}
}

void determine_transpose_matrix(int matrix[MAX_MATRIX_ORDER][MAX_MATRIX_ORDER], int order) {
int i = 0, j = 0, temp = 0;
for(i = 0; i < order; i++) {
for(j = 0; j <= i; j++) {
temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
}

int main() {
int matrix[MAX_MATRIX_ORDER][MAX_MATRIX_ORDER] = { 0 };
int order = 0;
long determinant = 0;
int inverse_matrix[MAX_MATRIX_ORDER][MAX_MATRIX_ORDER] = { 0 };
int i = 0, j = 0;

scanf("%d", &order);
if(order > MAX_MATRIX_ORDER) {
printf("Matrix order too high\n");
return 0;
} else if(order < 1) {
printf("Invalid matrix order\n");
return 0;
}

for(i = 0; i < order; i++) {
for(j = 0; j < order; j++) {
scanf("%d", &matrix[i][j]);
}
}

determinant = calculate_determinant(matrix, order);
printf("Matrix:\n");
print_matrix(matrix, order);
if(determinant == 0) {
printf("Determinant of matrix is ), inverse can't be find.");
} else {
determine_cofactor_matrix(matrix, inverse_matrix, order);
determine_transpose_matrix(inverse_matrix, order);
}
printf("Inverse matrix: [A]*(1/%d)\n[A] =\n", determinant);
print_matrix(inverse_matrix, order);

return 0;
}
```
Compile the file with above code and run as:
a.exe < input.txt > output.txt [on windows]
a.out < input.txt > output.txt [on linux]

Here,
a.exe or a.out are the executables generated after compilation

input.txt is the file containing input, e.g., if the matrix of order 3 is:
1 2 3
0 4 5
1 0 6
Then input.txt will be
3
1 2 3
0 4 5
1 0 6

output.txt is the output file containing result, for given example output.txt will contain
Matrix:
1 2 3
0 4 5
1 0 6
Inverse matrix: [A]*(1/22)
[A] =
24 -12 -2
5 3 -5
-4 2 4

Please let me know if any clarification on the code is required.  