matrix inverstion code in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sheff
    New Member
    • May 2010
    • 21

    matrix inverstion code in C

    HI everybody, I was trying to use the following code to invert a matrix, everytime I try to run this code, the only error msg that comes up is

    '' inv030710.c:83: error: expected initializer before ‘int’ '' in this line of the code

    void Gauss( RK8** a, RK8 ** b, int n)


    int main()

    {




    I do not really understand the problem, can you please let me know how to sort out this probleml.

    thank you alla nd looking forward to hearing from you.









    Code:
    # include <stdio.h>
    #include<malloc.h>
    #include<stdio.h>
    #include<stdlib.h>
    #include <time.h>
    # include <string.h>
    # include <math.h>
    # define RK8 double 
    
    
    
    
    
    
    void nrerror(char error_text[] )
      {
    
      fprintf(stderr,"Numerical Recipes run-time error...\n");
      fprintf(stderr,"%s\n",error_text);
      fprintf(stderr,"...now exiting to system...\n");
      exit(1);
    
      }
    
    double *dvector( int nl,int nh)
      {
    
      double *v;
    
      v=(double *)malloc((unsigned) (nh-nl+1)*sizeof(double));
      if (!v) nrerror("allocation failure in dvector()");
      return v-nl;
    
      }
    
    
    void free_dvector(double *v,int nl,int nh)
      {
    
      free((char*) (v+nl));
      nh++;
    
      }
    
    
    double **dmatrix( int nrl, int nrh, int ncl, int nch )
      {
    
      int i;
      double **m;
    
      m=(double **) malloc((unsigned) (nrh-nrl+1)*sizeof(double*));
      if (!m) nrerror("allocation failure 1 in dmatrix()");
      m -= nrl;
    
      for(i=nrl;i<=nrh;i++) 
        {
        m[i]=(double *) malloc((unsigned) (nch-ncl+1)*sizeof(double));
        if (!m[i]) nrerror("allocation failure 2 in dmatrix()");
        m[i] -= ncl;
        }
      return m;
    
      }
    
    
    
    void free_dmatrix( double **m, int nrl, int nrh, int ncl, int nch )
      {
      int i;
    
      for(i = nrh;i >= nrl; i--) free((char*) (m[i]+ncl));
      free((char*) (m+nrl));
      
      };
    
    
    void Gauss( RK8** a, RK8 ** b,  int n)
    
    
    int main()
    
    {
    
    
    double **first;
    first = dmatrix( 0, 2, 0, 2 );
    
    double **second;
    second = dmatrix( 0, 2, 0, 2 );
    
    
     first[3][3]={{1.,2.,6.},{3.,4., 5.},{3.,4.,7.}};
    
    second[3][3]={{0.,0.,0.},{0.,0., 0.},{0.,0.,0.}};
    
    Gauss( first , second , 3);
    
    
    }
    
    void Gauss (RK8 ** a, RK8 ** b, int n)
    {
      RK8 d, temp = 0, c;
      int i, j, k, m, nn, *ipvt;
    
      if ((ipvt = (int *) malloc (n * sizeof (int))) == NULL)
          memflt ();
    
      nn = n;
      for (i = 0; i < nn; i++)
        ipvt[i] = i;
    
      for (k = 0; k < nn; k++)
        {
          temp = 0.;
          m = k;
          for (i = k; i < nn; i++)
            {
              d = a[k][i];
              if (fabs (d) > temp)
                {
                  temp = fabs (d);
                  m = i;
                }
            }
          if (m != k)
            {
              j = ipvt[k];
              ipvt[k] = ipvt[m];
              ipvt[m] = j;
              for (j = 0; j < nn; j++)
                {
                  temp = a[j][k];
                  a[j][k] = a[j][m];
                  a[j][m] = temp;
                }
            }
          d = 1 / a[k][k];
          for (j = 0; j < k; j++)
            {
              c = a[j][k] * d;
              for (i = 0; i < nn; i++)
                a[j][i] -= a[k][i] * c;
              a[j][k] = c;
            }
          for (j = k + 1; j < nn; j++)
            {
              c = a[j][k] * d;
              for (i = 0; i < nn; i++)
                a[j][i] -= a[k][i] * c;
              a[j][k] = c;
            }
          for (i = 0; i < nn; i++)
            a[k][i] = -a[k][i] * d;
          a[k][k] = d;
        }
    
      for (i = 0; i < nn; i++)
        memcpy (b[ipvt[i]], a[i], sizeof (RK8) * nn);
    
      free (ipvt);
    }
    Last edited by Banfa; Jul 4 '10, 10:40 PM. Reason: Added [code] ... [/code] tags
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This is bizarre code. It's not C because variables are defined in the middle of functions. It's not C++ because there are no C++ keywords in this code.

    Variable names are reused. Nothing will compile using either C or C++ compiler.

    I suggest using code that will compile.

    Comment

    • sheff
      New Member
      • May 2010
      • 21

      #3
      hi weaknessforcats , thank u for your reply, I am really sure what you mean by bizarre code?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        In main():

        Code:
        int main()
        
        {
        
        
        double **first;
        first = dmatrix( 0, 2, 0, 2 );
        
        double **second;
        second = dmatrix( 0, 2, 0, 2 );
        In C all variables need to be defined at the beginning of the function. You can't define second in the middle of the function.


        Code:
        first[3][3]={{1.,2.,6.},{3.,4., 5.},{3.,4.,7.}};
        
        second[3][3]={{0.,0.,0.},{0.,0., 0.},{0.,0.,0.}};
        Once a variable is defined, you can't reuse the name to define new arrays. This syntax: ]={{1.,2.,6.},{3 .,4., 5.},{3.,4.,7.}} is used to initialize a new array with values. The syntax doesnpt work of the variable is aleady defined. AND it was defined two lines up.

        Code:
        void Gauss( RK8** a, RK8 ** b, int n)
        
        
        int main()
        Function prototype missing a semi-colon.

        If this was to be C++, there are no C++ headers being used:

        Code:
        # include <stdio.h>
        #include<malloc.h>
        #include<stdio.h>
        #include<stdlib.h>
        #include <time.h>
        # include <string.h>
        # include <math.h>
        etc, etc...

        Comment

        • sheff
          New Member
          • May 2010
          • 21

          #5
          HI weaknessforcats , THANK you very much for ur replies,

          I just wanna direct your attention to the fact that we here first = dmatrix( 0, 2, 0, 2 );
          we only tell the complier that our matrix '' first '' has got 3 rows and 3 columns ,

          while in the following line we are identifying the values that are given to the matrix

          first[3][3]={{1.,2.,6.},{3 .,4., 5.},{3.,4.,7.}} ;



          I put the missing semi colum as you mentioned but ended up having more error msgs I aint got a clue what to do, I mean did u try to make the corrections you mentioned and see the result? can you do that and let me know if u r getting the right results plse,




          {


          double **first;
          first = dmatrix( 0, 2, 0, 2 );

          double **second;
          second = dmatrix( 0, 2, 0, 2 );





          first[3][3]={{1.,2.,6.},{3 .,4., 5.},{3.,4.,7.}} ;

          second[3][3]={{0.,0.,0.},{0 .,0., 0.},{0.,0.,0.}} ;

          Gauss( first , second , 3);


          }

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by weaknessforcats
            In C all variables need to be defined at the beginning of the function. You can't define second in the middle of the function.
            Erm, I think you can in C99

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              This code:

              Code:
              first[3][3]={{1.,2.,6.},{3.,4., 5.},{3.,4.,7.}};
              creates a matrix named first as a two-dimensional 3x3 array with initial values.

              You can only only use this syntx if the array does not already exist.

              However, first is already defined as a double**. You can't use initializat syntax on an array that already exists.

              You will need to individually initialize the array elements.

              Comment

              Working...