Wednesday, June 12, 2013

Determinant Calculation of Nth Order Square Matrix| Cramer's Rule Implementation in C

Determinant calculation of a square matrix is widely used in solving many applied Mathematics problems. Some of them are while solving a set of linear equations using Cramer's rule (iff they have unique solutions), calculation of inverse matrix, Eigen values/ Eigen vectors problem and so on.

The determinant of a square matrix A∈Rn×n is the real number det(A) defined as follows:

det(A) = SUMperm [sign(ν1,ν2,...,νn)a1ν1a2ν2...anνn]

The summation is over all n! permutations(ν1,ν2,...,νn) of the integers 1,2,...,n, and sign(ν1,ν2,...,νn) = +1 or−1 depending on whether the n-tuple (ν1,ν2,...,νn) is an even or odd permutation of (1,2,...,n), respectively. An even (odd) permutation is obtained by an even (odd) number of exchanges of two adjacent elements in the array(1,2,...,n). A matrix A∈Rn×n is said to be non-singular when its determinant
det(A) is nonzero.
Here, we've presented the determinant calculator of a square matrix in C:

 void readmatrix(int m[][8], int s){  
  int i,j;  
  printf("Enter elements of the matrix: \n");  
     scanf("%d", &m[i][j]);  
 int determinant(int m[][8],int s){  
  int i, j, k, det,cramermatrix[15][15], product, plussum = 0, minussum = 0;  
  if (s == 2)  
   det = m[0][0]*m[1][1] - m[0][1]*m[1][0];  
      cramermatrix[i][j] = m[i][j];  
       cramermatrix[i][j] = m[i][j-s];  
     product = 1;  
     for(j = 0; j <s; j++)  
       product = product*cramermatrix[j][i+j];  
     plussum += product;  
     product = 1;  
     for(j = 0; j <s; j++)  
       product = product*cramermatrix[j][i-j];  
     minussum += product;  
   det = plussum - minussum;  
  return det;  
 int main(){  
  int matrix[8][8], det, size;  
  printf("Enter the order of square matrix (n*n) e.g. 4 : ");  
  scanf("%d", &size);  
  readmatrix(matrix, size);  
  det = determinant(matrix, size);  
  printf("\n The determinant of the given square matrix is\n det = %d\n\n", det);  
  return 0;  

Sample Output:

 Enter the order of square matrix (n*n) e.g. 4: 3  
 Enter the elements of the matrix:  
 3 4 5  
 0 1 1  
 0 1 -1  
 Determinant of the given matrix is   
 det = -6  

Enjoy coding !!!

No comments :

Post a Comment

Related Posts Plugin for WordPress, Blogger...