Partage
  • Partager sur Facebook
  • Partager sur Twitter

inverse d'une matrice

erreur segmentation

    29 mars 2017 à 12:00:58

    bonjour j'ai créé un petit programme pour calculer l'inverse d'une matrice 3*3 mais j'ai une erreur quand j'essaie de rentrer manuellement des valeurs dans la matrice; si quelqu'un a une solution je lui en serais gré!! :)

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct {
      int dim;
      double ** matrice;
    } Matrice;
    
    void initMatrice(int dim, Matrice * matrice)
    {
      int i, j;
    
      matrice->dim = dim;
      matrice->matrice = (double **)malloc(sizeof(double) * dim);
    
      for (i = 0; i < dim; i++)
        {
          matrice->matrice[i] = (double *)malloc(sizeof(double) * dim);
          for (j = 0; j < dim; j++)
    	{
    	  matrice->matrice[i][j] = 0;
    	}
        }
    }
    
    void printMatrice(Matrice * matrice)
    {
      int i, j;
      printf("\n");
    
      for (i = 0; i < matrice->dim; i++)
        {
          printf("|");
          for (j = 0; j < matrice->dim; j++)
    	{
    	  printf(" %.1f ", matrice->matrice[i][j]);
    	}
          printf("|\n");
        }
    }
    
    Matrice * creerMatrice(int dim, double tab[])
    {
      int i = 0, j = 0, k = 0;
      Matrice * m = (Matrice *)malloc(sizeof(Matrice));
      initMatrice(dim, m);
    
      while (k < dim*dim)
        {
          m->matrice[i][j] = tab[k];
          j++;
          k++;
          if (j == dim)
    	{
    	  j = 0;
    	  i++;
    	}
        }
    
      return m;
    }
    
    Matrice * extraireSousMatrice(Matrice * m, int y, int x)
    {
      int dim = (m->dim - 1);
      double * tab = (double *) malloc(sizeof(double) * dim * dim);
    
      int i, j, k = 0;
    
      for(i = 0; i < m->dim; i++)
        {
          for(j = 0; j < m->dim; j++)
    	{
    	  if(j != y && i != x)
    	    {
    	      tab[k] = m->matrice[i][j];
    	      k++;
    	    }
    	}
        }
    
      Matrice * subM = creerMatrice(dim, tab);
      free(tab);
    
      return subM;
    }
    
    int pow(int n, int p)
    {
      int i;
      int res = 1;
      for(i = 1; i <= p; i++)
        {
          res *= n;
        }
    
      return res;
    }
    
    int det(Matrice * m)
    {
      if(m->dim == 1)
        {
          return m->matrice[0][0];
        }
      else
        {
          double d = 0;
          int i;
          for(i = 0; i < m->dim; i++)
    	{
    	  Matrice * subM = extraireSousMatrice(m, 0, i);
    
    	  int s = pow(-1, i);
    	  d += m->matrice[i][0] * det(subM) * s;
    	  free(subM);
    	}
    
          return d;
        }
    }
    
    Matrice * tr(Matrice * m)
    {
      int i, j;
      Matrice * transposee = (Matrice *) malloc(sizeof(Matrice));
      initMatrice(m->dim, transposee);
    
      for(i = 0; i < m->dim; i++)
        {
          for(j = 0; j < m->dim; j++)
    	{
    	  transposee->matrice[i][j] = m->matrice[j][i];
    	}
        }
    
      return transposee;
    }
    
    Matrice * commatrice(Matrice * m)
    {
      int i, j, k = 0;
      double * tab = (double *) malloc(sizeof(double) * m->dim * m->dim);
    
      for(i = 0; i < m->dim; i++)
        {
          for(j = 0; j < m->dim; j++)
    	{
    	  Matrice * subM = extraireSousMatrice(m, j, i);
    	  tab[k] = pow(-1, i+j) * det(subM);
    	  k++;
    	  free(subM);
    	}
        }
    
      Matrice * comm = creerMatrice(m->dim, tab);
      return comm;
    }
    
    void diviser(Matrice * m, double n)
    {
      int i, j;
      if(n == 0)
        {
          printf("ERREUR : Division par 0 (La matrice n'est peut etre pas inversible)");
          exit(-1);
        }
    
      for (i = 0; i < m->dim; i++)
        {
          for (j = 0; j < m->dim; j++)
    	{
    	  m->matrice[i][j] = m->matrice[i][j] / n;
    	}
        }
    }
    
    Matrice * dupliquerMatrice(Matrice * m)
    {
      int i, j;
      Matrice * dup = (Matrice *) malloc(sizeof(Matrice));
      initMatrice(m->dim, dup);
    
      for(i = 0; i < m->dim; i++)
        {
          for(j = 0; j < m->dim; j++)
    	{
    	  dup->matrice[i][j] = m->matrice[i][j];
    	}
        }
    
      return dup;
    }
    
    Matrice * inverse(Matrice * m)
    {
      Matrice * comm = commatrice(m);
      Matrice * transposeeCommatrice = tr(comm);
      Matrice * inv = dupliquerMatrice(transposeeCommatrice);
      diviser(inv, det(m));
    
      free(comm);
      free(transposeeCommatrice);
    
      return inv;
    }
    
    int main()
    {
      double a11, a12, a13, a21, a22, a23, a31, a32, a33;
      int *pa11, *pa12, *pa13, *pa21, *pa22, *pa23, *pa31, *pa32, *pa33;
      double tab[] = {*pa11, *pa12, *pa13, *pa21, *pa22, *pa23, *pa31, *pa32, *pa33};
      printf("matrice 3X3 ");
        
      printf("a11=");
      scanf("%lf",&a11);
      
      printf("a12=");
      scanf("%lf",&a12);
      
      printf("a13=");
      scanf("%lf",&a13);
      
      printf("a21=");
      scanf("%lf",&a21);
      
      printf("a22=");
      scanf("%lf",&a22);
      
      printf("a23=");
      scanf("%lf",&a23);
      
      printf("a31=");
      scanf("%lf",&a31);
      
      printf("a32=");
      scanf("%lf",&a32);
      
      printf("a33=");
      scanf("%lf",&a33);
    
      *pa11=&a11;
      *pa12=&a12;
      *pa13=&a13;
      *pa21=&a21;
      *pa22=&a22;
      *pa23=&a23;
      *pa31=&a31;
      *pa32=&a32;
      *pa33=&a33;
     
     
      printf("a11=%d\n",*pa11);
      printf("a12=%d\n",*pa12);
      printf("a13=%d\n",*pa13);
      printf("a21=%d\n",*pa21);
      printf("a22=%d\n",*pa22);
      printf("a23=%d\n",*pa23);
      printf("a31=%d\n",*pa31);
      printf("a32=%d\n",*pa32);
      printf("a33=%d\n",*pa33);
      
      Matrice * m = creerMatrice(3, tab);
      Matrice * inv = inverse(m);
    
      printf("Matrice m :");
      printMatrice(m);
    
      printf("\n");
    
      printf("Inverse de m :");
      printMatrice(inv);
    
      free(inv);
    
      return 1;
    }
     
    



    • Partager sur Facebook
    • Partager sur Twitter
      29 mars 2017 à 12:12:29

      Yop,

      Ligne 213 tu mets dans le tableau les valeurs pointées par les paXX, mais ils pointent sur rien.

      Ligns 243 à 251 tu assignes l'adresse d'un double à un int (qui n'existe toujours pas).

      -
      Edité par Gam' 29 mars 2017 à 12:13:45

      • Partager sur Facebook
      • Partager sur Twitter
        29 mars 2017 à 19:01:19

        Pourquoi presque 300 lignes code pour une inverse 3x3 utilise le pivot de gauss pour trouver la matrice inverse c est plus rapide et surtout c'est valable pour nxm
        • Partager sur Facebook
        • Partager sur Twitter
          4 avril 2017 à 19:36:07

          salut @siapolo oui j'ai déjà un programme avec Gauss mais dans le cadre d'un TP faut qu'on trouve plusieurs méthodes différentes.

          @Gam' à la base j'avais fait ça comme programme :

          #include <stdio.h>
          #include <stdlib.h>
          #include <string.h>
           
          typedef struct {
            int dim;
            double ** matrice;
          } Matrice;
           
          void initMatrice(int dim, Matrice * matrice)
          {
            int i, j;
           
            matrice->dim = dim;
            matrice->matrice = (double **)malloc(sizeof(double) * dim);
           
            for (i = 0; i < dim; i++)
              {
                matrice->matrice[i] = (double *)malloc(sizeof(double) * dim);
                for (j = 0; j < dim; j++)
              {
                matrice->matrice[i][j] = 0;
              }
              }
          }
           
          void printMatrice(Matrice * matrice)
          {
            int i, j;
            printf("\n");
           
            for (i = 0; i < matrice->dim; i++)
              {
                printf("|");
                for (j = 0; j < matrice->dim; j++)
              {
                printf(" %.1f ", matrice->matrice[i][j]);
              }
                printf("|\n");
              }
          }
           
          Matrice * creerMatrice(int dim, double tab[])
          {
            int i = 0, j = 0, k = 0;
            Matrice * m = (Matrice *)malloc(sizeof(Matrice));
            initMatrice(dim, m);
           
            while (k < dim*dim)
              {
                m->matrice[i][j] = tab[k];
                j++;
                k++;
                if (j == dim)
              {
                j = 0;
                i++;
              }
              }
           
            return m;
          }
           
          Matrice * extraireSousMatrice(Matrice * m, int y, int x)
          {
            int dim = (m->dim - 1);
            double * tab = (double *) malloc(sizeof(double) * dim * dim);
           
            int i, j, k = 0;
           
            for(i = 0; i < m->dim; i++)
              {
                for(j = 0; j < m->dim; j++)
              {
                if(j != y && i != x)
                  {
                    tab[k] = m->matrice[i][j];
                    k++;
                  }
              }
              }
           
            Matrice * subM = creerMatrice(dim, tab);
            free(tab);
           
            return subM;
          }
           
          int pow(int n, int p)
          {
            int i;
            int res = 1;
            for(i = 1; i <= p; i++)
              {
                res *= n;
              }
           
            return res;
          }
           
          int det(Matrice * m)
          {
            if(m->dim == 1)
              {
                return m->matrice[0][0];
              }
            else
              {
                double d = 0;
                int i;
                for(i = 0; i < m->dim; i++)
              {
                Matrice * subM = extraireSousMatrice(m, 0, i);
           
                int s = pow(-1, i);
                d += m->matrice[i][0] * det(subM) * s;
                free(subM);
              }
           
                return d;
              }
          }
           
          Matrice * tr(Matrice * m)
          {
            int i, j;
            Matrice * transposee = (Matrice *) malloc(sizeof(Matrice));
            initMatrice(m->dim, transposee);
           
            for(i = 0; i < m->dim; i++)
              {
                for(j = 0; j < m->dim; j++)
              {
                transposee->matrice[i][j] = m->matrice[j][i];
              }
              }
           
            return transposee;
          }
           
          Matrice * commatrice(Matrice * m)
          {
            int i, j, k = 0;
            double * tab = (double *) malloc(sizeof(double) * m->dim * m->dim);
           
            for(i = 0; i < m->dim; i++)
              {
                for(j = 0; j < m->dim; j++)
              {
                Matrice * subM = extraireSousMatrice(m, j, i);
                tab[k] = pow(-1, i+j) * det(subM);
                k++;
                free(subM);
              }
              }
           
            Matrice * comm = creerMatrice(m->dim, tab);
            return comm;
          }
           
          void diviser(Matrice * m, double n)
          {
            int i, j;
            if(n == 0)
              {
                printf("ERREUR : Division par 0 (La matrice n'est peut etre pas inversible)");
                exit(-1);
              }
           
            for (i = 0; i < m->dim; i++)
              {
                for (j = 0; j < m->dim; j++)
              {
                m->matrice[i][j] = m->matrice[i][j] / n;
              }
              }
          }
           
          Matrice * dupliquerMatrice(Matrice * m)
          {
            int i, j;
            Matrice * dup = (Matrice *) malloc(sizeof(Matrice));
            initMatrice(m->dim, dup);
           
            for(i = 0; i < m->dim; i++)
              {
                for(j = 0; j < m->dim; j++)
              {
                dup->matrice[i][j] = m->matrice[i][j];
              }
              }
           
            return dup;
          }
           
          Matrice * inverse(Matrice * m)
          {
            Matrice * comm = commatrice(m);
            Matrice * transposeeCommatrice = tr(comm);
            Matrice * inv = dupliquerMatrice(transposeeCommatrice);
            diviser(inv, det(m));
           
            free(comm);
            free(transposeeCommatrice);
           
            return inv;
          }
           
          int main()
          {
            double a11, a12, a13, a21, a22, a23, a31, a32, a33;
           
            printf("matrice 3X3 ");
               
            printf("a11=");
            scanf("%lf",&a11);
             
            printf("a12=");
            scanf("%lf",&a12);
             
            printf("a13=");
            scanf("%lf",&a13);
             
            printf("a21=");
            scanf("%lf",&a21);
             
            printf("a22=");
            scanf("%lf",&a22);
             
            printf("a23=");
            scanf("%lf",&a23);
             
            printf("a31=");
            scanf("%lf",&a31);
             
            printf("a32=");
            scanf("%lf",&a32);
             
            printf("a33=");
            scanf("%lf",&a33);
           
           double tab[] = {a11, a12, a13, a21, a22, a23, a31, a32, a33};
           
             
            Matrice * m = creerMatrice(3, tab);
            Matrice * inv = inverse(m);
           
            printf("Matrice m :");
            printMatrice(m);
           
            printf("\n");
           
            printf("Inverse de m :");
            printMatrice(inv);
           
            free(inv);
           
            return 1;
          }

          sauf que justement quand je demande de rentrer dans mon tableau directement mes aXX le programme rentre leur adresse (il prend comme si c'étaient des &aXX)
          du coup j'ai essayé de construire une sorte de pointeur qui pointerai sur l'adresse des aXX (je me suis dit si le programme prend les aXX comme des adresses alors peut-être qu'en créant des pointeurs sur l'adresse ça me donnerait leur valeur (genre adresse d'adresse=valeur) mais ça marche pas... je comprends pas pourquoi il se contente pas de rentrer les valeurs dans le tableau...

          • Partager sur Facebook
          • Partager sur Twitter

          inverse d'une matrice

          × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
          × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
          • Editeur
          • Markdown