Inverse de matrice dynamique

Signaler
Messages postés
6
Date d'inscription
jeudi 23 janvier 2003
Statut
Membre
Dernière intervention
9 octobre 2003
-
Messages postés
540
Date d'inscription
dimanche 23 mars 2003
Statut
Membre
Dernière intervention
5 octobre 2007
-
Aidez nous please c pr programmer en langage Votre texte ICIC l inverse de la matrice dynamique merci bcp !!!!!!

4 réponses

Messages postés
6
Date d'inscription
jeudi 23 janvier 2003
Statut
Membre
Dernière intervention
9 octobre 2003

-------------------------------
Réponse au message :
-------------------------------

> Aidez nous please c pr programmer en langage Votre texte ICIC l inverse de la matrice dynamique merci bcp !!!!!!
Messages postés
6
Date d'inscription
jeudi 23 janvier 2003
Statut
Membre
Dernière intervention
9 octobre 2003

-------------------------------
Réponse au message :
-------------------------------

>
>
>
>
> -------------------------------
> Réponse au message :
> -------------------------------
>
> > Aidez nous please c pr programmer en langage Votre texte ICIC l inverse de la matrice dynamique merci bcp !!!!!!
>
Messages postés
540
Date d'inscription
dimanche 23 mars 2003
Statut
Membre
Dernière intervention
5 octobre 2007
1
#include <stdio.h>
#include <malloc.h>

typedef struct {double *v; int dim;} matrix;

void destruct(matrix m)
{
free(m.v);
}

void create(matrix *m, int dim)
{
m->dim= dim;
m->v= (double *)calloc(dim, sizeof(double));
}

double getVal(matrix m, int x, int y)
{
if( x >= m.dim )
{
printf("%d is an invalid x index to get value (dim=%d)\n", x, m.dim);
return 0.0;
}

if( y >= m.dim )
{
printf("%d is an invalid y index to get value (dim=%d)\n", y, m.dim);
return 0.0;
}

return m.v[(m.dim * y) + x];
}

void setVal(matrix m, int x, int y, double v)
{
if( x >= m.dim )
{
printf("%d is an invalid x index to set value (dim=%d)\n", x, m.dim);
return;
}

if( y >= m.dim )
{
printf("%d is an invalid y index to set value (dim=%d)\n", y, m.dim);
return;
}

m.v[(m.dim * y) + x]= v;
}

double getSignatureDim(int i)
{
return (i % 2)?-1.0:1.0;
}

double getSignature(int x, int y)
{
return getSignatureDim(x) * getSignatureDim(y);
}

matrix extract(matrix m, int x, int y)
{
matrix res;
int i, j, k, l, dim= m.dim;

create(&res, dim - 1);
for(j= l= 0; j < dim; j++)
{
for(i= k= 0; i < dim; i++)
{
if( (i != x) && (j != y) )
setVal(res, k, l, getVal(m, i, j));

if( i != x )
k++;
}

if( j != y )
l++;
}

return res;
}

double getDet(matrix m)
{
int dim= m.dim;

if( !dim )
{
printf("It's an empty matrix\n");
return 0.0;
}

if( dim == 1 )
return *m.v;
else
{
int i, j;
double res= 0.0;

for(j= 0; j < dim; j++)
for(i= 0; i < dim; i++)
{
matrix a= extract(m, i, j);
res+= getVal(m, i, j) * getSignature(i, j) * getDet(a);
destruct(a);
}

return res;
}
}

matrix inverse(matrix m)
{
matrix res= {0};
int dim= m.dim;

create(&res, dim);

if( dim == 1 )
setVal(res, 0, 0, 1.0 / getVal(m, 0, 0));
else
{
int i, j;
double det= getDet(m);

if( !det )
{
printf("This matrix is not inversable\n");
return res;
}

for(j= 0; j < dim; j++)
for(i= 0; i < dim; i++)
{
matrix a= extract(m, i, j);
setVal(res, i, j, getVal(m, i, j) * getSignature(i, j) * getDet(a) / det);
destruct(a);
}
}

return res;
}

int main()
{
matrix m;

create(&m, 2);

setVal(m, 0, 0, 1.0);
setVal(m, 0, 1, 2.0);
setVal(m, 1, 0, 3.0);
setVal(m, 1, 1, 4.0);

printf("%f\t", getVal(m, 0, 0));
printf("%f\n", getVal(m, 0, 1));
printf("%f\t", getVal(m, 1, 0));
printf("%f\n", getVal(m, 1, 1));
printf("\n");

m= inverse(m);

printf("%f\t", getVal(m, 0, 0));
printf("%f\n", getVal(m, 0, 1));
printf("%f\t", getVal(m, 1, 0));
printf("%f\n", getVal(m, 1, 1));

return 0;
}

Core Breaker :)
Messages postés
540
Date d'inscription
dimanche 23 mars 2003
Statut
Membre
Dernière intervention
5 octobre 2007
1
Désolé je me suis trompé.
Voici le bon:

#include <stdio.h>
#include <malloc.h>

typedef struct {double *v; int dim;} matrix;

void destruct(matrix m)
{
free(m.v);
}

void create(matrix *m, int dim)
{
m->dim= dim;
m->v= (double *)calloc(dim * dim, sizeof(double));
}

double getVal(matrix m, int x, int y)
{
if( x >= m.dim )
{
printf("%d is an invalid x index to get value (dim=%d)\n", x, m.dim);
return 0.0;
}

if( y >= m.dim )
{
printf("%d is an invalid y index to get value (dim=%d)\n", y, m.dim);
return 0.0;
}

return m.v[(m.dim * y) + x];
}

void printM(matrix m)
{
int i, j;
printf("\n---------------\nmat:\n");
for(i=0; i < m.dim; i++)
{
for(j=0; j < m.dim; j++)
printf("%f\t", getVal(m, i, j));
printf("\n");
}
printf("---------------\n\n");
}

void setVal(matrix m, int x, int y, double v)
{
if( x >= m.dim )
{
printf("%d is an invalid x index to set value (dim=%d)\n", x, m.dim);
return;
}

if( y >= m.dim )
{
printf("%d is an invalid y index to set value (dim=%d)\n", y, m.dim);
return;
}

m.v[(m.dim * y) + x]= v;
}

double getSignatureDim(int i)
{
return ( i % 2 )?-1.0:1.0;
}

double getSignature(int x, int y)
{
return getSignatureDim(x) * getSignatureDim(y);
}

matrix extract(matrix m, int x, int y)
{
matrix res;
int i, j, k, l, dim= m.dim;

create(&res, dim - 1);
for(j= l= 0; j < dim; j++)
{
for(i= k= 0; i < dim; i++)
{
if( (i != x) && (j != y) )
setVal(res, k, l, getVal(m, i, j));

if( i != x )
k++;
}

if( j != y )
l++;
}

return res;
}

double getDet(matrix m)
{
int dim= m.dim;

if( !dim )
{
printf("It's an empty matrix\n");
return 0.0;
}

if( dim == 1 )
return *m.v;
else
{
int i, j;
double res= 0.0;

for(i= 0; i < dim; i++)
{
matrix a= extract(m, i, 0);
res+= getVal(m, i, 0) * getSignatureDim(i) * getDet(a);
destruct(a);
}

return res;
}
}

matrix transpose(matrix m)
{
int dim= m.dim;

if( dim > 1 )
{
int i, j;
double tmp;

for(j= 0; j < dim; j++)
for(i= j + 1; i < dim; i++)
{
tmp= getVal(m, i, j);
setVal(m, i, j, getVal(m, j, i));
setVal(m, j, i, tmp);
}
}

return m;
}

matrix inverse(matrix m)
{
matrix res= {0};
int dim= m.dim;

create(&res, dim);

if( dim == 1 )
setVal(res, 0, 0, 1.0 / getVal(m, 0, 0));
else
{
int i, j;
double det= getDet(m);

if( !det )
{
printf("This matrix is not inversable\n");
return res;
}

for(j= 0; j < dim; j++)
for(i= 0; i < dim; i++)
{
matrix a= extract(m, i, j);
setVal(res, i, j, getSignature(i, j) * getDet(a) / det);
destruct(a);
}
}

return transpose(res);
}

int main()
{
matrix m;

create(&m, 2);

setVal(m, 0, 0, 1.0);
setVal(m, 0, 1, 2.0);
setVal(m, 1, 0, 3.0);
setVal(m, 1, 1, 4.0);

printf("%f\t", getVal(m, 0, 0));
printf("%f\n", getVal(m, 0, 1));
printf("%f\t", getVal(m, 1, 0));
printf("%f\n", getVal(m, 1, 1));
printf("det:%f\n", getDet(m));
printf("\n");

m= inverse(m);

printf("%f\t", getVal(m, 0, 0));
printf("%f\n", getVal(m, 0, 1));
printf("%f\t", getVal(m, 1, 0));
printf("%f\n", getVal(m, 1, 1));

return 0;
}

Core Breaker :)