Advertisement
ArfIsAToe

calcul matrice inverse

Nov 15th, 2020
804
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.     float pow(float n,int p)
  4.     {
  5.         if (p==0)
  6.         {
  7.             return 1;
  8.         }else
  9.         {
  10.             return pow(n,p-1)*n;
  11.         }
  12.     }
  13.     float mineur(float m[3][3],int cl,int cc)
  14.     {
  15.         float num1,num2;
  16.         int posnum=0;
  17.         for (int l =0;l<3;l++)
  18.         {
  19.             for (int c =0;c<3;c++)
  20.             {
  21.                 if(l==cl)
  22.                 {  // printf("Break\n");
  23.                     break;
  24.                 }else if(c==cc)
  25.                 {  // printf("continued\n");
  26.                     continue;
  27.                 }else
  28.                 {
  29.                     posnum++;
  30.                     switch (posnum)
  31.                     {
  32.                     case 1:
  33.                         num1=m[l][c];
  34.                         break;
  35.                     case 2:
  36.                         num2=m[l][c];
  37.                         break;
  38.                     case 3:
  39.                         num2*=m[l][c];
  40.                         break;
  41.                     case 4:
  42.                         num1*=m[l][c];
  43.                         break;
  44.                     }
  45.                 }
  46.             }
  47.         }
  48.         return num1-num2;
  49.     }
  50.     float cofacteur(float m[3][3],int l,int c)
  51.     {
  52.             return pow(-1,l+c+2)*mineur(m,l,c);
  53.     }
  54.     void aff(float m[3][3])
  55.     {
  56.         for(int l =0; l<3;l++)
  57.          {
  58.              for(int c = 0; c<3;c++)
  59.              {
  60.                  printf("%.0f\t",m[l][c]);
  61.              }
  62.              printf("\n");
  63.          }
  64.     }
  65. int main()
  66. {
  67.  
  68. /// input  matrice
  69.  float m[3][3],M[3][3];
  70.  for(int l=0; l<3;l++)
  71.  {
  72.      for(int c = 0; c<3;c++)
  73.      {
  74.          scanf("%f",&m[l][c]);
  75.      }
  76.  }
  77. system("CLS"); /// to clear the input screen for a cleaner output
  78. printf("Martice:\n");
  79. aff(m);
  80.  
  81.  
  82. /// calcul du determinant
  83.  float det=m[0][0]*(m[1][1]*m[2][2]-m[1][2]*m[2][1])-
  84.            m[1][0]*(m[0][1]*m[2][2]-m[0][2]*m[2][1])+
  85.            m[2][0]*(m[0][1]*m[1][2]-m[0][2]*m[1][1]);
  86.         printf("Det(M)= %.2f\n",det);
  87.  
  88.  
  89. /// creation d'une matrice des cofacteurs aka comatrice
  90. for(int l=0; l<3;l++)
  91.  {
  92.      for(int c = 0; c<3;c++)
  93.      {
  94.         M[l][c]=cofacteur(m,l,c);
  95.      }
  96.  }
  97.  printf("\n\nMatrice Cofacteur {Com(m)}:\n");
  98.  aff(M);
  99.  ////printf("\n\n");
  100.  /// symétrie du matrice
  101.  float x;
  102.  for(int l=0; l<3;l++)
  103.  {
  104.      for(int c = 0; c<2;c++)
  105.      {
  106.             if(l<c)
  107.             {
  108.                 continue;
  109.             }
  110.             x=M[l][c];
  111.             M[l][c]=M[c][l];
  112.             M[c][l]=x;
  113.      }
  114.      printf("\n");
  115.  }          /*
  116.  printf("\n\nInverse du Matrice Cofacteur {t(Com(m))}:\n");
  117.  aff(M);    */
  118.  
  119.  printf("l'inverse du matrice tapee est m^-1=1/%.1f  *\n",det);
  120.  aff(M);
  121. //printf("\n\n%.0f\t%.0f\t%.0f\t%.0f\t",m[2][1],mineur(m,2,1),pow(-1,2+1+2),cofacteur(m,2,1));
  122. }
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement