« Introduction au langage C/Matrices complexes 2 » : différence entre les versions
Contenu supprimé Contenu ajouté
m Robot : Remplacement de texte automatisé (-\bE(tud) +É\1) |
m Robot : Remplacement de texte automatisé (- n'est pas + n’est pas , - Aujourd'hui + Aujourd’hui , - d'euros + d’euros , - d'agir + d’agir , - l'apparence + l’apparence ); changements de type cosmétique |
||
Ligne 23 : | Ligne 23 : | ||
<source lang="c"> |
<source lang="c"> |
||
/* ------------------------------ */ |
/* ------------------------------ */ |
||
/* Save as : c01.c |
/* Save as : c01.c */ |
||
/* ------------------------------ */ |
/* ------------------------------ */ |
||
#include |
#include <stdio.h> |
||
#include |
#include <stdlib.h> |
||
#include |
#include <stddef.h> |
||
#include |
#include <ctype.h> |
||
#include |
#include <time.h> |
||
#include |
#include <math.h> |
||
/* ------------------------------ */ |
/* ------------------------------ */ |
||
#define C0 |
#define C0 0 |
||
#define C1 |
#define C1 1 |
||
#define C2 |
#define C2 2 |
||
#define C3 |
#define C3 3 |
||
#define C4 |
#define C4 4 |
||
#define C5 |
#define C5 5 |
||
#define R0 |
#define R0 0 |
||
#define R1 |
#define R1 1 |
||
#define R2 |
#define R2 2 |
||
#define R3 |
#define R3 3 |
||
#define R4 |
#define R4 4 |
||
#define R5 |
#define R5 5 |
||
#define RC0 |
#define RC0 0 |
||
#define RC1 |
#define RC1 1 |
||
#define RC2 |
#define RC2 2 |
||
#define RC3 |
#define RC3 3 |
||
#define RC4 |
#define RC4 4 |
||
#define OF |
#define OF 0 |
||
#define R_SIZE |
#define R_SIZE 0 |
||
#define C_SIZE |
#define C_SIZE 1 |
||
#define C_SIZE_A |
#define C_SIZE_A 2 |
||
#define ERROR_E |
#define ERROR_E +1.E-010 |
||
#define FIRST |
#define FIRST 1 |
||
#define FILENAME |
#define FILENAME 11 |
||
#ifndef PI |
#ifndef PI |
||
#define PI |
#define PI 3.14159265359 |
||
#endif |
#endif |
||
/* ------------------------------ */ |
/* ------------------------------ */ |
||
double **i_mZ( |
double **i_mZ( |
||
int |
int r, |
||
int |
int c |
||
) |
) |
||
{ |
{ |
||
double **A; |
double **A; |
||
int |
int ar; |
||
int |
int ac; |
||
int |
int i; |
||
if(r<1||c<1) |
if(r<1||c<1) |
||
Ligne 116 : | Ligne 116 : | ||
for(i=1; i<ar; i++) A[i] = A[0]+i*ac; |
for(i=1; i<ar; i++) A[i] = A[0]+i*ac; |
||
/* |
/* Memorize the matrix size*/ |
||
A[R_SIZE][OF] = ar;/* A[R_SIZE][OF] = A[0][0] */ |
A[R_SIZE][OF] = ar;/* A[R_SIZE][OF] = A[0][0] */ |
||
A[C_SIZE][OF] = ac;/* A[C_SIZE][OF] = A[1][0] */ |
A[C_SIZE][OF] = ac;/* A[C_SIZE][OF] = A[1][0] */ |
||
/* |
/* I initialize the zero row and the zero column */ |
||
for(r=2; r<A[R_SIZE][OF]; r++) A[r][0] = 0.; |
for(r=2; r<A[R_SIZE][OF]; r++) A[r][0] = 0.; |
||
for(c=1; c<A[C_SIZE][OF]; c++) A[0][c] = 0.; |
for(c=1; c<A[C_SIZE][OF]; c++) A[0][c] = 0.; |
||
Ligne 159 : | Ligne 159 : | ||
/* ------------------------------ */ |
/* ------------------------------ */ |
||
double **i_mZ( |
double **i_mZ( |
||
int |
int r, |
||
int |
int c |
||
) |
) |
||
{ |
{ |
||
double **A; |
double **A; |
||
int |
int ar; |
||
int |
int ac; |
||
int |
int i; |
||
if(r<R1||c<C1) |
if(r<R1||c<C1) |
||
Ligne 230 : | Ligne 230 : | ||
for(i=1; i<ar; i++) A[i] = A[0]+i*ac; |
for(i=1; i<ar; i++) A[i] = A[0]+i*ac; |
||
/* |
/* Pour un tableau de 6 colonnes et 4 lignes. |
||
A |
A |
||
Ligne 239 : | Ligne 239 : | ||
123456 123456 123456 123456 123456 |
123456 123456 123456 123456 123456 |
||
A[0] A[1] A[2] A[3] |
A[0] A[1] A[2] A[3] |
||
*/ |
*/ |
||
/* |
/* On copie la taille de la matrice dans la matrice. */ |
||
A[R_SIZE][OF] = ar;/* A[R_SIZE][OF] = A[0][0] */ |
A[R_SIZE][OF] = ar;/* A[R_SIZE][OF] = A[0][0] */ |
||
A[C_SIZE][OF] = ac;/* A[C_SIZE][OF] = A[1][0] */ |
A[C_SIZE][OF] = ac;/* A[C_SIZE][OF] = A[1][0] */ |
||
/* |
/* On met zéro dans la ligne zéro et dans la colonne zéro. */ |
||
for(r=2; r<A[R_SIZE][OF]; r++) A[r][0] = 0.; |
for(r=2; r<A[R_SIZE][OF]; r++) A[r][0] = 0.; |
||
for(c=1; c<A[C_SIZE][OF]; c++) A[0][c] = 0.; |
for(c=1; c<A[C_SIZE][OF]; c++) A[0][c] = 0.; |
||
Ligne 256 : | Ligne 256 : | ||
Important : |
Important : |
||
* Vous |
* Vous n’est pas obliger de comprendre ce code pour utiliser les matrices. |
||
* Dans la librairie je mets à zéro tous les éléments de la matrice avec la fonction m_0_mZ(A); |
* Dans la librairie je mets à zéro tous les éléments de la matrice avec la fonction m_0_mZ(A); |
||
Version du 18 mai 2016 à 21:43
Introduction (deuxième partie)
- Un exemple complet à tester (sans sortie).
- Puis nous étudirons la fonction i_mZ().
Le code
Exemple indépendant de la librairie
- Ce fichier est complet.
- Il créé une matrice et la détruit.
- Certain include et define sont inutiles pour le code présenter, mais indispensable si on décide de rajouter certaine fonction de la librairie.
/* ------------------------------ */
/* Save as : c01.c */
/* ------------------------------ */
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <ctype.h>
#include <time.h>
#include <math.h>
/* ------------------------------ */
#define C0 0
#define C1 1
#define C2 2
#define C3 3
#define C4 4
#define C5 5
#define R0 0
#define R1 1
#define R2 2
#define R3 3
#define R4 4
#define R5 5
#define RC0 0
#define RC1 1
#define RC2 2
#define RC3 3
#define RC4 4
#define OF 0
#define R_SIZE 0
#define C_SIZE 1
#define C_SIZE_A 2
#define ERROR_E +1.E-010
#define FIRST 1
#define FILENAME 11
#ifndef PI
#define PI 3.14159265359
#endif
/* ------------------------------ */
double **i_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;
/* Dynamically allocate an array of "ar" rows
and "ac" columns. */
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;
/* Memorize the matrix size*/
A[R_SIZE][OF] = ar;/* A[R_SIZE][OF] = A[0][0] */
A[C_SIZE][OF] = ac;/* A[C_SIZE][OF] = A[1][0] */
/* I initialize the zero row and the zero column */
for(r=2; r<A[R_SIZE][OF]; r++) A[r][0] = 0.;
for(c=1; c<A[C_SIZE][OF]; c++) A[0][c] = 0.;
return(A);
}
/* ------------------------------------ */
void f_mZ(
double **A
)
{
if(A) free(A[0]);
free(A);
}
/* ------------------------------------ */
int main(void)
{
double **A = i_mZ(3,5);
f_mZ(A);
printf("\n Press return to continue");
getchar();
return 0;
}
Étudions la fonction i_mZ()
- Elle retourne un double pointeur de double (double **i_mZ)
- Il entre le nombre de lignes (r) et le nombre de colonnes (c).
/* ------------------------------ */
double **i_mZ(
int r,
int c
)
{
double **A;
int ar;
int ac;
int i;
if(r<R1||c<C1)
{
printf(" Si le nombre de lignes ou de colonnes est inférieur à un tout s'arrête.\n\n");
printf(" double **i_mR(); \n\n");
fflush(stdout);
getchar();
exit(EXIT_FAILURE);
}
/* Je me réserve la colonne zéro et la ligne zéro,
pour mémoriser la taille de la matrice,
je rajoute donc une ligne et une colonne
On travail avec des complexes. On multiplie
le nombre de colonnes par 2 */
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);
}
/* Ces deux commandes alloue un block mémoire de double.
Le premier malloc offre un block de (ar) éléments.
A pointe sur le premier de ces éléments.
Cela correspond a un espace pour un tableau A[ar]. (ar nombres de lignes, ac nombre de colonne)
Le deuxième malloc offre un block de (ar*ac) élément pour les éléments du tableau.
A[0] le premier élément du tableau pointe aussi sur A.
A[0] pointe sur A.
A[1] pointe sur la deuxième ligne.
A[2] pointe sur la troisième ligne.
...
C'est ce que fait la boucle for ci-dessous.
On incrémente chaque A[i] de (i*colonnes) à partir de l'adresse de départ.
*/
for(i=1; i<ar; i++) A[i] = A[0]+i*ac;
/* Pour un tableau de 6 colonnes et 4 lignes.
A
******************
A[0]
A
123456 123456 123456 123456 123456
A[0] A[1] A[2] A[3]
*/
/* On copie la taille de la matrice dans la matrice. */
A[R_SIZE][OF] = ar;/* A[R_SIZE][OF] = A[0][0] */
A[C_SIZE][OF] = ac;/* A[C_SIZE][OF] = A[1][0] */
/* On met zéro dans la ligne zéro et dans la colonne zéro. */
for(r=2; r<A[R_SIZE][OF]; r++) A[r][0] = 0.;
for(c=1; c<A[C_SIZE][OF]; c++) A[0][c] = 0.;
return(A);
}
Important :
- Vous n’est pas obliger de comprendre ce code pour utiliser les matrices.
- Dans la librairie je mets à zéro tous les éléments de la matrice avec la fonction m_0_mZ(A);