Member • Dec 26, 2008
Find the way to find inverse matrix urgent
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 .
Member • Dec 26, 2008
Member • Dec 27, 2008
Member • Dec 30, 2008
Member • Dec 31, 2008
Member • Jan 2, 2009
#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[0][0]; } 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[0][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[0][0] = matrix[0][0]; } 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:
Member • Jan 2, 2009
Administrator • Jan 2, 2009