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:

 #include<stdio.h>  
 void readmatrix(int m[][8], int s){  
  int i,j;  
  printf("Enter elements of the matrix: \n");  
  for(i=0;i<s;i++)  
   for(j=0;j<s;j++)  
     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];  
  else  
  {  
    for(i=0;i<s;i++)  
     for(j=0;j<s;j++)  
      cramermatrix[i][j] = m[i][j];  
    for(i=0;i<s;i++)  
     for(j=s;j<(2*s-1);j++)  
       cramermatrix[i][j] = m[i][j-s];  
   for(i=0;i<s;i++){  
     product = 1;  
     for(j = 0; j <s; j++)  
       product = product*cramermatrix[j][i+j];  
     plussum += product;  
     }  
   for(i=s-1;i<(2*s-1);i++){  
     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 !!!


2 comments :

  1. For more news, please head over to our website,mmopixel has an awesome offer for you. You can purchase GW2 Gold price or GW2 Legendary Weapons from us whenever you need any of them.

    ReplyDelete
  2. SEGA GENESIS - GAN-GAMING
    SEGA GENESIS. septcasino GENESIS-HANDS. https://sol.edu.kg/ Genesis (JP-EU). NA. NA. gri-go.com NA. SEGA GENESIS-HANDS. NA. SEGA GENESIS. NA. GENESIS-HANDS. https://tricktactoe.com/ NA. worrione

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...