Mathc matrices/Fichiers h : vim2

Un livre de Wikilivres.


Bibliothèque


Installer ce fichier dans votre répertoire de travail.

vim2.h
/* ------------------------------------ */
/*  Save as :   vim2.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]

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

        The first element of the matrix is z = A[1][1]

        For a 10x10 matrix the last element is z = A[10][10]

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

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

        double **T;

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

        f_mR(T);

   -------------------------------------------------------------------------- */
   
/* ------------------------------------ */
/* Same as i_RC_mR() but work with the 
   functions.   
          
   ex :   i_mR(rsize_R(M),csize_R(M));  */
/* ------------------------------------ */ 
double **i_mR(
int      r,
int      c
)
{
double **A;
int     ar;
int     ac;
int      i;

            if(r<R1||c<C1)
            {
            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;
         ac = c + 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=R1; i<ar; i++) A[i] = A[R0]+i*ac;

/* ------- Save the numbers of rows and columns -------- */

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

/* ----------- Put zero in the column zero ------------- */
    for(r=R2; r<A[R_SIZE][C0]; r++)

        A[r][C0] = 0.;

/* ----------- Index the columns ------------- */
    for(c=C1; c<A[C_SIZE][C0]; c++)

        A[R0][c] = c;

    m0_mR(A);

return(A);
}
/* ------------------------------------ */
/* ------------------------------------ */   
void f_mR(
double **A
)
{
  if(A)  free(A[0]);

  free(A);
}
/* ------------------------------------ */
/* ------------------------------------ */
/* Same as i_mR() but work with the size     
   in memory 
   
   ex :  i_RC_mR(M[R_SIZE][C0],M[C_SIZE][C0] */
/* ------------------------------------ */   
double **i_RC_mR(
int      R,
int      C
)
{
return(i_mR(--R,--C));
}
/* ------------------------------------ */ 
/* ------------------------------------ */


La fonction double **A = i_mR(Rx, Cx); permet de construire une matrice de Rx lignes et de Cx colonnes.


La fonction f_mR(A); libérera l'espace donné à la matrice.


La fonction i_RC_mR( A[R_SIZE][C0], A[C_SIZE][C0]); doit être utiliser lorsque l'on veut directement récupérer la taille des matrices dans la mémoire.


Par contre, il faudra utiliser la fonction double **Ab = i_Abr_Ac_bc_mR(RAb, CA, Cb ); lorsque l'on travaillera avec les fonctions Gauss-Jordan. Cela permetra de préciser le nombre de colonnes de A (CA) et le nombre de colonnes de b (Cb), ce que l'on ne peut pas faire avec le fonction i_mR();