Advertisement
Guest User

Untitled

a guest
Mar 30th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define TAILLE 20
  4.  
  5. struct matrice {
  6.     double** values;
  7.     int height; // i
  8.     int length; // j
  9. };
  10.  
  11. typedef struct matrice matrice;
  12.  
  13. /* fonctions annexes */
  14. matrice* init(int hauteur, int largeur)
  15. {
  16.     int i,j;
  17.     matrice* res=malloc(sizeof(matrice));
  18.     res->height=hauteur;
  19.     res->length=largeur;
  20.     res->values=malloc(hauteur*sizeof(double*));
  21.     for (i=0;i<hauteur;i++)
  22.     {
  23.         res->values[i]=malloc(largeur*sizeof(double));
  24.         for (j=0;j<largeur;j++)
  25.         {  
  26.             res->values[i][j]=0;
  27.         }
  28.     }
  29.     return res;
  30. }
  31. /* fonction d'affichage de matrice de taille height*length */
  32. void affichage_matrice(matrice* mat)
  33. {
  34.     int i,j;
  35.     for (i=0;i<mat->height;i++)
  36.     {
  37.         for (j=0;j<mat->length;j++)
  38.         {
  39.             printf("%.3f ",mat->values[i][j]);
  40.         }
  41.         printf("\n");
  42.     }
  43. }
  44.  
  45.  
  46. matrice* copier_matrice(matrice* mat)
  47. {
  48.     int i,j;
  49.     matrice* res = init(mat->height,mat->length);
  50.     for(i=0;i<res->height;i++)
  51.     {
  52.         for(j=0;j<res->length;j++)
  53.         {
  54.             res->values[i][j]=mat->values[i][j];
  55.         }
  56.     }
  57.     return res;
  58. }
  59.  
  60. /** Exercice 1.a
  61.     La matrice A doit être triangulaire inférieure */
  62. matrice* solinf(matrice* L,matrice* b)
  63. {
  64.     matrice* res=init(L->height,1);
  65.     int i,j;
  66.     int error=0;
  67.     if (b->height != L->height || b->length != 1) {error = 1;}
  68.     else
  69.     {
  70.         if (L->height != L->length) {error = 2;}
  71.         else
  72.         {
  73.             for (i=0;i<L->height;i++)
  74.             {
  75.                 for (j=i+1;j<L->length;j++)
  76.                 {
  77.                     if (L->values[i][j] != 0) error=3;
  78.                 }
  79.             }
  80.         }
  81.     }
  82.     switch(error)
  83.     {
  84.         case 1:
  85.           fprintf(stderr,"[Erreur] la matrice n'est pas carrée\n");
  86.           break;
  87.         case 2:
  88.           fprintf(stderr,"[Erreur] La matrice b n'a pas les bonnes dimensions\n");
  89.           break;
  90.         case 3:
  91.           fprintf(stderr,"[Erreur] la matrice n'est pas triangulaire inférieure\n");
  92.           break;
  93.     }
  94.  
  95.     // La matrice ne doit pas contenir de 0 sur la diagonale
  96.     for (i=0;i<L->height;i++)
  97.     {
  98.         res->values[i][0]=0;
  99.         for (j=0;j<i;j++)
  100.         {
  101.             res->values[i][0]+=L->values[i][j]*res->values[j][0];
  102.         }
  103.         res->values[i][0]=(b->values[i][0]-res->values[i][0])/L->values[i][i];
  104.     }
  105.     return res;
  106. }
  107.  
  108. /** Exercice 1.b
  109.     La matrice A doit être triangulaire supérieure */
  110. matrice* solsup(matrice* U,matrice* b)
  111. {
  112.     matrice* res=init(U->height,1);
  113.     int i,j;
  114.     int error=0;
  115.     if (b->height != U->height || b->length != 1) {error = 1;}
  116.     else
  117.     {
  118.         if (U->height != U->length) {error = 2;}
  119.         else
  120.         {
  121.             for (j=0;j<U->length;j++)
  122.             {
  123.                 for (i=j+1;i<U->height;i++)
  124.                 {
  125.                     if (U->values[i][j] != 0) error=3;
  126.                 }
  127.             }
  128.         }
  129.     }
  130.     switch(error)
  131.     {
  132.         case 1:
  133.           fprintf(stderr,"[Erreur] la matrice n'est pas carrée\n");
  134.           break;
  135.         case 2:
  136.           fprintf(stderr,"[Erreur] La matrice b n'a pas les bonnes dimensions\n");
  137.           break;
  138.         case 3:
  139.           fprintf(stderr,"[Erreur] la matrice n'est pas triangulaire supérieure\n");
  140.           break;
  141.     }
  142.  
  143.     // La matrice ne doit pas contenir de 0 sur la diagonale
  144.     for (i=U->height-1;i>=0;i--)
  145.     {
  146.         res->values[i][0]=0;
  147.         for (j=i+1;j<U->height;j++)
  148.         {
  149.             res->values[i][0]+=U->values[i][j]*res->values[j][0];
  150.         }
  151.         res->values[i][0]=(b->values[i][0]-res->values[i][0])/U->values[i][i];
  152.     }
  153.     return res;
  154. }
  155.  
  156. /* Exercice 2.a */
  157. matrice** trigGauss(matrice* A, matrice* b)
  158. {
  159.     matrice** tab = (matrice**)malloc(2*sizeof(matrice*));
  160.     matrice* mat = copier_matrice(A);
  161.     matrice* vect = copier_matrice(b);
  162.     int i,j,k,c;
  163.     for(k=0;k<mat->height-1;k++)
  164.     {
  165.         if (mat->values[k][k]==0) printf("[Erreur] Une des valeurs sur la diagonale est nulle, utilisez la méthode du pivot\n");
  166.         for(i=k+1;i<mat->height;i++)
  167.         {
  168.             c=mat->values[i][k]/mat->values[k][k];
  169.             vect->values[i][0]=vect->values[i][0]-c*vect->values[k][0];
  170.             mat->values[i][k]=0;
  171.             for(j=k+1;j<mat->length;j++)
  172.             {
  173.                 mat->values[i][j]=mat->values[i][j]-c*mat->values[k][j];
  174.             }
  175.         }
  176.     }
  177.     tab[0]=mat;
  178.     tab[1]=vect;
  179.     return tab;
  180. }
  181.  
  182. /* Exercice 2.b */
  183. matrice* ResolutionGauss(matrice* A, matrice* b)
  184. {
  185.     matrice** gauss=trigGauss(A,b);
  186.     matrice* res=solsup(gauss[0],gauss[1]);
  187.     return res;
  188. }
  189.  
  190. /* Exercice 3.a */
  191. matrice** FactLU(matrice* A)
  192. {
  193.     matrice** tab = (matrice**)malloc(2*sizeof(matrice*));
  194.     matrice* L=init(A->height,A->length);
  195.     matrice* U=init(A->height,A->length); // Initialisation à la matrice nulle
  196.     int i,j,k;
  197.     double sum;
  198.     for(i=0;i<A->height;i++)
  199.     {
  200.         L->values[i][i]=1; // Initialisation à la matrice identitée
  201.     }
  202.     for(i=0;i<A->height-1;i++)
  203.     {
  204.         for(j=i;j<A->height;j++)
  205.         {
  206.             sum = 0;
  207.             for(k=0;k<i+1;k++)
  208.             {
  209.                 sum=sum+L->values[i][k]*U->values[k][j];
  210.             }
  211.             U->values[i][j]=A->values[i][j]-sum;
  212.         }
  213.         for(j=i+1;j<A->height;j++)
  214.         {
  215.             sum = 0;
  216.             for(k=0;k<i+1;k++)
  217.             {
  218.                 sum=sum+L->values[j][k]*U->values[k][i];
  219.             }
  220.             L->values[j][i]=(A->values[j][i]-sum)/U->values[i][i];
  221.         }
  222.     }
  223.     sum=0;
  224.     for(k=0;k<A->height-1;k++)
  225.     {
  226.         sum=sum+L->values[L->height-1][k]*U->values[k][U->length-1];
  227.     }
  228.     U->values[U->height-1][U->length-1]=A->values[A->height-1][A->length-1] - sum;
  229.     tab[0]=L;
  230.     tab[1]=U;
  231.     return tab;
  232. }
  233.  
  234. matrice* ResolutionLU(matrice* A, matrice* b)
  235. {
  236.     matrice** tab=FactLU(A);
  237.     matrice* res=init(A->height,1);
  238.     res=solinf(tab[0],b);
  239.     res=solsup(tab[1],res);
  240.     return res;
  241. }
  242.  
  243. /* Similaire à ResolutionLU, permet de réduire le nombre de calculs pour inverser une matrice */
  244. matrice* ResolutionLU2(matrice** tab,matrice* b)
  245. {
  246.     matrice* res=solinf(tab[0],b);
  247.     res=solsup(tab[1],res);
  248.     return res;
  249. }
  250.  
  251. matrice* inverse(matrice* A)
  252. {
  253.     matrice* mat;
  254.     matrice* res=init(A->height,A->length);
  255.     matrice* b=init(A->height,1);
  256.     matrice** tab=FactLU(A);
  257.     int i,j;
  258.     for(i=0;i<A->height;i++)
  259.     {
  260.         b->values[i][0]=1; /* Initialisation du vecteur unitaire */
  261.         mat=ResolutionLU2(tab,b);
  262.         for(j=0;j<A->height;j++)
  263.         {
  264.             res->values[j][i]=mat->values[j][0];
  265.         }
  266.         b->values[i][0]=0;
  267.     }
  268.     return res;
  269. }
  270.  
  271.  
  272. /* Initialisation 1.a */
  273. matrice* exo1a_L(matrice* mat)
  274. {
  275.     mat->values[0][0]=1;
  276.     mat->values[0][1]=0;
  277.     mat->values[0][2]=0;
  278.     mat->values[1][0]=2;
  279.     mat->values[1][1]=3;
  280.     mat->values[1][2]=0;
  281.     mat->values[2][0]=1;
  282.     mat->values[2][1]=4;
  283.     mat->values[2][2]=-1;
  284.     return mat;
  285. }
  286.  
  287. matrice* exo1a_b(matrice* mat)
  288. {
  289.     mat->values[0][0]=1;
  290.     mat->values[1][0]=8;
  291.     mat->values[2][0]=10;
  292.     return mat;
  293. }
  294.  
  295. /* Initialisation 1.b */
  296. matrice* exo1b_U(matrice* mat)
  297. {
  298.     mat->values[0][0]=1;
  299.     mat->values[0][1]=2;
  300.     mat->values[0][2]=3;
  301.     mat->values[1][0]=0;
  302.     mat->values[1][1]=4;
  303.     mat->values[1][2]=8;
  304.     mat->values[2][0]=0;
  305.     mat->values[2][1]=0;
  306.     mat->values[2][2]=5;
  307.     return mat;
  308. }
  309.  
  310. matrice* exo1b_b(matrice* mat)
  311. {
  312.     mat->values[0][0]=6;
  313.     mat->values[1][0]=16;
  314.     mat->values[2][0]=15;
  315.     return mat;
  316. }
  317.  
  318. /* Initialisation 2.a */
  319. matrice* exo2a_A(matrice* mat)
  320. {
  321.     mat->values[0][0]=3;
  322.     mat->values[0][1]=1;
  323.     mat->values[0][2]=2;
  324.     mat->values[1][0]=3;
  325.     mat->values[1][1]=2;
  326.     mat->values[1][2]=6;
  327.     mat->values[2][0]=6;
  328.     mat->values[2][1]=1;
  329.     mat->values[2][2]=-1;
  330.     return mat;
  331. }
  332.  
  333. matrice* exo2a_b(matrice* mat)
  334. {
  335.     mat->values[0][0]=2;
  336.     mat->values[1][0]=1;
  337.     mat->values[2][0]=4;
  338.     return mat;
  339. }
  340.  
  341. /* Initialisation 2.b */
  342. matrice* exo2b_A(matrice* mat)
  343. {
  344.     mat->values[0][0]=1;
  345.     mat->values[0][1]=2;
  346.     mat->values[0][2]=3;
  347.     mat->values[1][0]=5;
  348.     mat->values[1][1]=2;
  349.     mat->values[1][2]=1;
  350.     mat->values[2][0]=3;
  351.     mat->values[2][1]=-1;
  352.     mat->values[2][2]=1;
  353.     return mat;
  354. }
  355.  
  356. matrice* exo2b_b(matrice* mat)
  357. {
  358.     mat->values[0][0]=5;
  359.     mat->values[1][0]=5;
  360.     mat->values[2][0]=6;
  361.     return mat;
  362. }
  363. /* Initialisation 3.a */
  364.  
  365. matrice* exo3a_A(matrice* mat)
  366. {
  367.     mat->values[0][0]=3;
  368.     mat->values[0][1]=1;
  369.     mat->values[0][2]=2;
  370.     mat->values[1][0]=3;
  371.     mat->values[1][1]=2;
  372.     mat->values[1][2]=6;
  373.     mat->values[2][0]=6;
  374.     mat->values[2][1]=1;
  375.     mat->values[2][2]=-1;
  376.     return mat;
  377. }
  378.  
  379. /* Initialisation 3.b */
  380.  
  381. matrice* exo3b_A(matrice* mat)
  382. {
  383.     mat->values[0][0]=1;
  384.     mat->values[0][1]=2;
  385.     mat->values[0][2]=3;
  386.     mat->values[1][0]=5;
  387.     mat->values[1][1]=2;
  388.     mat->values[1][2]=1;
  389.     mat->values[2][0]=3;
  390.     mat->values[2][1]=-1;
  391.     mat->values[2][2]=1;
  392.     return mat;
  393. }
  394.  
  395. matrice* exo3b_b(matrice* mat)
  396. {
  397.     mat->values[0][0]=5;
  398.     mat->values[1][0]=5;
  399.     mat->values[2][0]=6;
  400.     return mat;
  401. }
  402.  
  403. /* Initialisation 4 */
  404.  
  405. matrice* exo4_A(matrice* mat)
  406. {
  407.     mat->values[0][0]=1;
  408.     mat->values[0][1]=2;
  409.     mat->values[0][2]=3;
  410.     mat->values[1][0]=5;
  411.     mat->values[1][1]=2;
  412.     mat->values[1][2]=1;
  413.     mat->values[2][0]=3;
  414.     mat->values[2][1]=-1;
  415.     mat->values[2][2]=1;
  416.     return mat;
  417. }
  418.  
  419. int main()
  420. {
  421.     printf("   Exercice 1.a : matrice triangulaire inférieure\n   Valeur de x attendue : 1 2 -1\n");
  422.     matrice* L = init(3,3);
  423.     L = exo1a_L(L);
  424.     matrice* b = init(3,1);
  425.     b = exo1a_b(b);
  426.     matrice* x = solinf(L,b);
  427.     affichage_matrice(x);
  428.  
  429.     printf("   Exercice 1.b : matrice triangulaire supérieure\n   Valeur de x attendue : 1 -2 3\n");
  430.     matrice* U = init(3,3);
  431.     U = exo1b_U(U);
  432.     b = exo1b_b(b);
  433.     x = solsup(U,b);
  434.     affichage_matrice(x);
  435.  
  436.     printf("   Exercice 2.a : Algorithme d'élimination de Gauss\n   Valeurs de A et x attendues :\n  1  0  0     2\nA 0  3  0  x -3\n  0  0 -1     5\n\n");
  437.     matrice* A = init(3,3);
  438.     A = exo2a_A(A);
  439.     b = exo2a_b(b);
  440.     matrice** tab = trigGauss(L,b);
  441.     affichage_matrice(tab[0]);
  442.     affichage_matrice(tab[1]);
  443.  
  444.     printf("   Exercice 2.b : Algorithme de résolution de Gauss\n   Valeur de x attendue : 16/30 19/30 32/30\n");
  445.     A = exo2b_A(A);
  446.     b = exo2b_b(b);
  447.     x = ResolutionGauss(A,b);
  448.     affichage_matrice(x);
  449.  
  450.     printf("   Exercice 3.a : Algorithme de factorisation par méthode Doolittle\n   Valeurs de L et U attendues :\n  1  0  0     3  1  2\nL 1  1  0   U 0  1  4\n  2 -1  1     0  0 -1\n\n");
  451.     A = exo3a_A(A);
  452.     tab=FactLU(A);
  453.     printf("Matrice L : \n");
  454.     affichage_matrice(tab[0]);
  455.     printf("Matrice U : \n");
  456.     affichage_matrice(tab[1]);
  457.  
  458.     printf("   Exercice 3.b : Alorithme de résolution par méthode Doolittle\n   Valeur de x attendue : 1 -1 2\n");
  459.     A = exo3b_A(A);
  460.     b = exo3b_b(b);
  461.     x = ResolutionLU(A,b);
  462.     affichage_matrice(x);
  463.  
  464.     printf("   Exercice 4 : Inversion de matrice\n   Valeur de A attendue :\n-0.088  0.147  0.117\n 0.059  0.235 -0.411\n 0.323 -0.206  0.235\n\n");
  465.     A = exo4_A(A);
  466.     A = inverse(A);
  467.     affichage_matrice(A);
  468.     return 0;
  469. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement