Mathc complexes/Fichiers c : xi mr


Application


Installer et compiler ce fichier dans votre répertoire de travail.

ai_mz.c
/* ------------------------------------ */
/*  Save as :   ai_mz.c                 */
/* ------------------------------------ */
#include "w_a.h"
/* ---------------------------------------------------------------------------
 Do    : Dynamically allocate a multidimensional array.

            (see : FAQ of the comp.lang.c group)

        You can keep the array's contents contiguous,

                 r0    r1    r2    ... rn
                 R_000|C_xxx|0_xxx|...|0_xxx


                R =  Number of rows.
                C =  Number of columns.

        The declaration of the sizes into the matrices, it is my work.

        So be careful.

        The first row and the first column are not used.

                      *********************

        The size of the row    of the matrix is into  A[R_SIZE][C0] = A[0][0]
        The size of the column of the matrix is into  A[C_SIZE][C0] = A[1][0]

                      *********************

                           Complex Matices

        The first element of the matrix is z =(A[1][1*C2],A[1][1*C2+C1])

        For a 10x10 matrix the last element is z = (A[10][10*C2],A[10][10*C2+C1])

                      *********************

        If you want to duplicate a matrix A, you must use :

        double **T;

        T = i_duplicate_mZ(  A[R_SIZE][C0],
                             A[C_SIZE][C0]);

        f_mZ(T);

   -------------------------------------------------------------------------- */
/* ------------------------------------ */
double **xi_mZ(
int      r,
int      c
)
{
double **A;
int     ar;
int     ac;
int      i;

            if(r<1||c<1)
            {
            printf(" The size of the matrix must be positive integers.\n\n");
            printf(" double **i_mR(); \n\n");
            fflush(stdout);
            getchar();
            exit(EXIT_FAILURE);
            }

         ar = r    + C1;
         ac = c*C2 + C1;

         A = malloc(ar * sizeof(**A));
             if(!A)
            {
            printf(" I was unable to allocate the memory you requested.\n\n");
            printf(" double **i_mR(); \n\n");
            printf(" **A = malloc(ar * sizeof(*A));\n\n");
            fflush(stdout);
            getchar();
            exit(EXIT_FAILURE);
            }

      A[0] = malloc(ar * ac * sizeof(**A) );
             if(!A[0])
            {
            printf(" I was unable to allocate the memory you requested.\n\n");
            printf(" double **i_mR();\n\n");
            printf(" A[0] = malloc(ar * ac * sizeof(**A) );\n\n");
            fflush(stdout);
            getchar();
            exit(EXIT_FAILURE);
            }

	for(i=1; i<ar; i++) A[i] = A[0]+i*ac;

/* ----------- Give a value to the zero row and the zero column ------------- */

    A[R_SIZE][C0] = ar;
    A[C_SIZE][C0] = ac;

    for(r=R2; r<A[R_SIZE][C0]; r++)

        A[r][C0] = 0.;

    for(c=C1, i=1; c<A[C_SIZE][C0]; c+=C2, i++)
       {  
		   A[R0][c]    = i; 
		   A[R0][c+C1] = 0; }

    m0_mZ(A);

return(A);
}
/* ------------------------------------ */
void fun(int r,int c)
{
double **A     = r_mZ(i_mZ(r,c),9);

  clrscrn();

  printf(" A[R%d,C%d]:          (p_mZ();)\n",rsize_mZ(A),csize_mZ(A));
  p_mZ(A,5,0, 4,0, C6);

  printf("\n\n\n");
  printf(" A[R%d,C%d]:          (pall_mZ();)\n",rsize_mZ(A),csize_mZ(A));
  pall_mZ(A,5,0, 4,0);
 
  f_mZ(A);
}
/* ------------------------------------ */
int main(void)
{
time_t t;

  srand(time(&t));
  
   do 
        fun(rp_I(R4),rp_I(C5));
        
    while(stop_w());
        
  return 0;
}


La fonction i_mZ(); existant déjà dans la librairie je l'ai ici renommé xi_mZ();

Nous connaissons la fonction pall_mZ(); qui permet afficher la colonne zéro et la ligne zéro.


Étudions quelques sorties écran.

Exemple de sortie écran :

  A[R4,C3]:          (p_mZ();)

   -1  +4i    -1  -7i    +6  +2i 
   +8  -9i    -7  +6i    -3  +8i 
   +2  -3i    +2  +6i    +4  +8i 
   +4  -5i    -3  +4i    -7  +4i 




 A[R4,C3]:          (pall_mZ();)

   +5    +1   0     +2   0     +3   0  

   +7    -1  +4i    -1  -7i    +6  +2i 
   +0    +8  -9i    -7  +6i    -3  +8i 
   +0    +2  -3i    +2  +6i    +4  +8i 
   +0    +4  -5i    -3  +4i    -7  +4i 


 Press return to continue
 Press X      to stop


Nous allons étudier la fin de la fonction.

/* ----------- Give a value to the zero row and the zero column ------------- */

    A[R_SIZE][C0] = ar; /* On copie le nombre de  lignes  dans la case A[R0;C0]  */
    A[C_SIZE][C0] = ac; /* On copie le nombre de colonnes dans la case A[R1;C0]  */

    for(r=R2; r<A[R_SIZE][C0]; r++)/* On met des zéro dans la colonne zéro      */
                                    /* à partir de la ligne deux                */ 
        A[r][C0] = 0.;

    for(c=C1, i=1; c<A[C_SIZE][C0]; c+=C2, i++)
       {                            /* On crée un index dans la ligne zéro      */
		   A[R0][c]    = i; 
		   A[R0][c+C1] = 0; }

    m0_mZ(A);             /* On initialise la matrice comme une matrice zéro   */

return(A);                /* On retourne l'adresse de la matrice au programme   */
}


Passons maintenant au début de la fonction.

            if(r<1||c<1)             /* On vérifie que les tailles des lignes */ 
            {                        /* et des colonnes sont positives        */

            printf(" The size of the matrix must be positive integers.\n\n");
            printf(" double **i_mR(); \n\n");
            fflush(stdout);
            getchar();
            exit(EXIT_FAILURE);
            }

         ar = r    + R1;         /* On rajoute  une  ligne, la  ligne  zéro */
         ac = c*C2 + C1;         /* On rajoute une colonne, la colonne zéro */


Maintenant vérifions si on a suffisamment d'espace pour le nombre de lignes

         A = malloc(ar * sizeof(**A));   /* Donne de l'espace pour ar lignes */
             if(!A)                       /* Si cela à échoué...             */ 
            {
            printf(" I was unable to allocate the memory you requested.\n\n");
            printf(" double **i_mR(); \n\n");
            printf(" **A = malloc(ar * sizeof(*A));\n\n");
            fflush(stdout);
            getchar();
            exit(EXIT_FAILURE);

sizeof(**A) indique que l'on travaille avec des doubles. On crée donc un espace pour ar double.


Maintenant vérifions si on a suffisamment d'espace pour le nombre de colonnes

      A[0] = malloc(ar * ac * sizeof(**A) );  /* Donne de l'espace pour ar*ac colonnes */
             if(!A[0])                        /* Si cela à échoué...                   */ 
            {
            printf(" I was unable to allocate the memory you requested.\n\n");
            printf(" double **i_mR();\n\n");
            printf(" A[0] = malloc(ar * ac * sizeof(**A) );\n\n");
            fflush(stdout);
            getchar();
            exit(EXIT_FAILURE);
            }

	for(i=1; i<ar; i++) A[i] = A[0]+i*ac;  /* On associe à chaque ligne les ac colonnes */