locked
Sarrus |A|, Minors Parser for matrix with n>5 RRS feed

  • Question

  • #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    int n;
    int i,j, im,jm, iminor,jminor, iinvm,jinvm;
    #define MAX 20
    double X[MAX][MAX];
    double Invm[MAX][MAX];


    double temp[MAX][MAX];
    double temp1[MAX][MAX];
     //double minor(int i, int j,int n, long double a1[MAX][MAX] );
    void eliminate (int i, int j,int n,  double a1[MAX][MAX] );
    double adop(int i, int j ,int n, double xinp1[MAX][MAX]);


    double  determ( double  a[MAX][MAX], int n);


     double  determ( double  a[MAX][MAX], int n) {
      int  p, h, k, i, j;
        int det=0;
      double temp[MAX][MAX];
      if(n==1) {
        return a[0][0];
      } else if(n==2) {
        det=(a[0][0]*a[1][1]-a[0][1]*a[1][0]);
        return det;
      }


     else {
        for(p=0;p<n;p++) {
         
         eliminate (0, p, n,   a ); // get minor matrix
        
         det=det+a[0][p]*adop(0, p ,n-1, temp);
        }
        return det;
      }

    }


    void eliminate (int i, int j,int n,  double a2[MAX][MAX] )
    {
    printf("\n Eliminate  %d  %d  ",i,j) ;  
     double y=0;


      int   h, k, i1, j1;
    im=i;
    jm=j;
    iminor=n-1;
    jminor=n-1;

          h = 0;
          k = 0;   
          h = 0;
          k = 0;
          for(i1=0;i1<n;i1++) {
           if(i1==i) {
                continue;
              }
             
            for( j1=0;j1<n;j1++) {
              if(j1==j) {
                continue;
              }
              //printf("\n h= %d k= %d , i1= %d j1= %d ,a1=%lf ",h,k, i1,j1, a2[i1][j1]) ;  
              temp[h][k] = a2[i1][j1];
              //printf("  temp[%d][%d]=%lf n-1=%d ",h,k,  temp[h][k],n-1) ;
              k++;
              if(k==n-1) {
                         h++;
                         k = 0;
                         }
            }
          } // zero line
           
           
       
     // OutputX( n-1,temp); 
     return;
    }

    void eliminate1 (int i, int j,int n,  double a2[MAX][MAX])
    {
    //printf("\n Eliminate 1  %d  %d  ",i,j) ;  
     double y=0;


      int   h, k, i1, j1;
    im=i;
    jm=j;
    iminor=n-1;
    jminor=n-1;

          h = 0;
          k = 0;   
          h = 0;
          k = 0;
          for(i1=0;i1<n;i1++) {
           if(i1==i) {
                continue;
              }
             
            for( j1=0;j1<n;j1++) {
              if(j1==j) {
                continue;
              }
              //printf("\n h= %d k= %d , i1= %d j1= %d ,a1=%lf ",h,k, i1,j1, a2[i1][j1]) ;  
               temp1[h][k] = a2[i1][j1];
              //printf("  temp1[%d][%d]=%lf n-1=%d ",h,k,  temp1[h][k],n-1) ;
              k++;
              if(k==n-1) {
                         h++;
                         k = 0;
                         }
            }
          } // zero line
           
           
       
      //OutputX( n-1,temp1); 
     return;
    }


     double minor(int i, int j,int n,  double a1[MAX][MAX] )
    {
     double y=0;

     
      int  p, h, k, i1, j1;
    im=i;
    jm=j;
    iminor=n-1;
    jminor=n-1;

           
          eliminate1 ( i, j,  n,   a1 );
           
     // printf("\n Minor %d %d , n= %d \n",i,j , n-1) ;    
      OutputX( n-1,temp1);     
           
           
    y=determ(temp1, n-1);
    return y;
    }
     
     double adop(int i, int j ,int n, double xinp1[MAX][MAX])
    {
    double y1= pow(-1,(i+j+2))*minor(i,j,n,xinp1 );
    printf ("\n  Adop=%lf ", y1); 
    return  y1;
     
    }

    /*
    void invmatrix(int n, double xinp[MAX][MAX])
    {

    int a,b;
    iinvm=n;
    jinvm=n;
     double denom=determ(xinp,n);

    if (denom==0)
    {
    printf ("\n Error");
    }

    for (b=0;b<n;b++)
    {
    for (a=0;a<n;a++)
    {
    Invm[b][a]=(adop(a,b,n,xinp))/denom;

    }}

    */
    void InputX(void)
    {

     
    printf ("\n Input square matrix Xn*n");
    printf ("\n Input number of elements n  ");
    scanf  ("%d",&n);

    for (i=0;i<n;i++){
    for (j=0;j<n;j++){
    printf ("Input X[%d][%d]  ",i,j);
    scanf ("%lf",&X[i][j]);
    }}


    return;

    }


    void OutputX( int n, double X[MAX][MAX])
    {

     
     
    getch();
    for (i=0;i<n;i++){
    for (j=0;j<n;j++){
    printf ("\n  X[%d][%d]= %lf ",i,j,X[i][j]);
     
    }}

    }


    /*

    0 4  7 -1
    3 2  1 2
    2 3 -2 4
    5 -2 1 3


    detA=209

    A11=-39
    A12=-8
    A13=41
    A14=46

    detA=sum (1,4; (A1,i* x1,i))


    */


    void geteval(void)
    {
     n=4;
    X[0][0]=0;
    X[0][1]=4;
    X[0][2]=7;
    X[0][3]=-1;
    X[1][0]=3;
    X[1][1]=2;
    X[1][2]=1;
    X[1][3]=2;
    X[2][0]=2;
    X[2][1]=3;
    X[2][2]=-2;
    X[2][3]=4;
    X[3][0]=5;
    X[3][1]=-2;
    X[3][2]=1;
    X[3][3]=3;
    return; 
    }
    void geteval1(void)
    { n=3;
      X[0][0]= 2.000000;
      X[0][1]= 1.000000;
      X[0][2]= 2.000000;
      X[1][0]= 3.000000;
      X[1][1]= -2.000000;
      X[1][2]= 4.000000;
      X[2][0]= -2.000000;
      X[2][1]= 1.000000;
      X[2][2]= 3.000000;
    return;
    }

    void main (void)
    {
    double y1;
    label1: 
    //InputX();
    geteval();


    OutputX( n,X);
    y1=determ( X,  n) ;
    //y1=adop(0,0, n,X) ;
    //printf ("\n  Adop=%lf ", y1);
    printf ("\n |A|=%lf ", y1);
    getch();
    goto label1;

    getch();
    }

    /*

    X[0][0]= 0.000000 X[0][1]= 4.000000 X[0][2]= 7.000000 X[0][3]= -1.00000
    X[1][0]= 3.000000 X[1][1]= 2.000000 X[1][2]= 1.000000 X[1][3]= 2.000000
    X[2][0]= 2.000000 X[2][1]= 3.000000 X[2][2]= -2.00000 X[2][3]= 4.000000
    X[3][0]= 5.000000 X[3][1]= -2.00000 X[3][2]= 1.000000 X[3][3]= 3.000000
     
    adop(0,0, n,X) ;
    Minor
       X[0][0]= 2.000000;   X[0][1]= 1.000000;   X[0][2]= 2.000000;
       X[1][0]= 3.000000;   X[1][1]= -2.000000;   X[1][2]= 4.000000;
       X[2][0]= -2.000000;   X[2][1]= 1.000000;  X[2][2]= 3.000000;
      
    X[0][0]= -2.000000 X[0][1]= 4.000000
    X[1][0]= 1.000000 X[1][1]= 3.000000


    */


    • Edited by USERPC01 Tuesday, February 17, 2015 1:03 AM
    Tuesday, February 17, 2015 12:57 AM

All replies

  • How to export temp[][] as array from function as private variable without class? Hows to create multiple copy of subroutine with independent private variables (new subroutine name) with reference on  insuroutine new copy of subroutine without error ?
    Tuesday, February 17, 2015 1:02 AM
  • How to use  "new", "malloc", private: factorization, Gauss method using only

    "multiruned" subhierarhical decomposition by line(column) and Sarrus's rule ?

    Tuesday, February 17, 2015 1:11 AM
  •  http://www.linuxquestions.org/questions/programming-9/matrix-determinant-program-in-c-and-c-executable-25-times-larger-than-the-other-649405/

    examples

    Tuesday, February 17, 2015 1:49 PM
  • //#include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <math.h>
    // #include <cmath>
    //using namespace std;

     //double ** submatrix( double **matrix, int order, int i, int j);
     //double determinant( double **matrix, int order);

     


     double** submatrix( double **matrix, int order, int i, int j)
    {

      double **subm;
     int p, q;    // Indexes for matrix
     int a = 0, b;  // Indexes for subm
     //subm = new  double* [order - 1];

             subm = (double **) malloc((order-1)*sizeof(double *));

     for(p = 0; p < order; p++) {
      if(p==i) continue;    //Skip ith row
       
                        //subm[a] = new  double[order - 1];
                          subm[a]=(double *) malloc((order-1)*sizeof(double));
       b = 0;

      for(q = 0; q< order; q++) {
        if(q==j) continue;  //Skip jth column
       subm[a][b++] = matrix[p][q];
      }
      a++; //Increment row index
     }
     return subm;
    }

     double determinant( double **matrix, int order)
    {      
             double determ = 0;
     if(order == 1)
      return **matrix; //Return the element if the matrix is of order one


            if(order == 2){
                      determ=(matrix[0][0]*matrix[1][1]-matrix[0][1]*matrix[1][0]);
                      return determ;
                          }
     int i;

     
     for(i = 0; i < order; i++)
      determ +=  (pow(-1.0,i)) * matrix[i][0] * determinant(submatrix(matrix,order, i, 0), order - 1);
    // delete  matrix;
     return determ;
    }

    double algdop(double **matrix, int order, int i,int j)
    {
    return (pow(-1,i+j))*matrix[i][0] * determinant(submatrix(matrix,order, i, j), order - 1);
    }


    int main()
    {
     int i, j, order;

      printf("Enter the order of the matrix: ");
      scanf("%d",&order);
            //Allocate memory for pointers (each pointer represents a row)
            //matrix= new  double* [order];

     double **matrix =(double **) malloc(order*sizeof(double *)); 
     
              
            printf("Enter the elements of the matrix: \n");
     
     
     for(i = 0; i < order; i++) {

       //matrix[i] = new  double[order];
                            //Each row contains 'order' number of elements

                            matrix[i]=(double *) malloc(order * sizeof(double ));

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

       
       
       
       
       
       
       
     }
        printf("\nOk  " );
     
            double result=determinant(matrix, order);

         printf("\nParsing determinant Ok  " );
     printf("\nDeterminant: %le  ", result);
     
     return 0;
    }


    • Edited by USERPC01 Tuesday, February 17, 2015 4:28 PM
    Tuesday, February 17, 2015 4:26 PM