Sunday, September 18, 2011

Numerical Methods: Determinant of nxn matrix using C

Source Code:

#include<stdio.h>
int main(){
    float  matrix[10][10], ratio, det;
    int i, j, k, n;
    printf("Enter order of matrix: ");
    scanf("%d", &n);
    printf("Enter the matrix: \n");
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            scanf("%f", &matrix[i][j]);
        }
    }
    /* Conversion of matrix to upper triangular */
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            if(j>i){
                ratio = matrix[j][i]/matrix[i][i];
                for(k = 0; k < n; k++){
                    matrix[j][k] -= ratio * matrix[i][k];
                }
            }
        }
    }
    det = 1; //storage for determinant
    for(i = 0; i < n; i++)
        det *= matrix[i][i];
    printf("The determinant of matrix is: %.2f\n\n", det);
    return 0;
}

13 comments:

  1. if diagonal i.e matrix[1][1]=0 then ratio will be infinity ?

    ReplyDelete
    Replies
    1. This code fails in this case, you must perform partial or complete pivoting

      Delete
    2. You can always check matrix[1][1]==0, if so, add a whole column to matrix[i][1].

      Delete
    3. Above source code is working or not?

      Delete
  2. can you explain that 'raio' is what to do?

    ReplyDelete
    Replies
    1. Ratio actually prevents the division by zero error. If you put matrix[j][i]/matrix[i][i] instead of ratio inside third nested for loop, it throws division by zero run time error.

      Delete
    2. This is how you reduce the matrix to an upper triangular, therefore the determinant is just the multiplication of diagonal elements.

      matrix[i][j] = matrix[i][j] - matrix[k][j]*ratio
      //this reduces rows using the previous row, until matrix is diagonal.

      Delete
  3. /* u can simplify ur alghorithme by the following code*/


    #include

    int main(){

    float matrix[10][10];

    int i, j, k, n;

    printf("Enter order of matrix: ");

    scanf("%d", &n);

    printf("Enter the matrix: \n");

    for(i = 0; i < n; i++){

    for(j = 0; j < n; j++){

    scanf("%f", &matrix[i][j]);

    }

    }

    /* Calculate directly the determinante */

    for(k = 1; k < n; k++)

    for(i = k+1; i <= n; i++)

    for(j = k+1; j <= n; j++){

    matrix[i][j] = matrix[i][j]matrix[k][k]-matrix[i][k]matrix[k][j];
    if(k>=2)
    matrix[i][j]=matrix[i][j]/matrix[k][k];

    }
    printf("The determinant of matrix is: %.2f\n\n", matrix[n][n]);

    return 0;

    }

    ReplyDelete
    Replies
    1. Or you could optimize the algorithm and get this:

      #include

      int main(){
      int i, j, n;
      printf("Enter order of matrix: ");
      scanf("%d", &n);
      float*m=(float*)malloc(n*n*sizeof(float));
      printf("Enter the matrix: \n");
      for(i = 0; i < n; i++){
      for(j = 0; j < n; j++){
      scanf("%f", &m[i*n+j]);
      }
      }
      float ary[n-1];
      float* subc=&ary[-1];
      float det=m[0];
      for(i=1;i<n;++i){
      subc[i]=m[i]/det;
      }
      for(i=1;i<n-1;++i){
      float dc=m[i*(n+1)]-subc[i];
      for(j=i+1;j<n;++j){
      subc[j]+=(m[j*n+i]-subc[j])/rc;
      }
      det*=dc;
      }
      det*=(m[i*(n+1)]-subc[i]);
      free(m);
      printf("The determinant of matrix is: %.2f\n\n", det);
      return 0;
      }

      Delete
    2. What is rc in the line "subc[j]+=(m[j*n+i]-subc[j])/rc;?"

      Delete
  4. this programme has some error in upper triangulr

    ReplyDelete