Inverse matrice
Discutez d'informatique ici !
-
magyy
- Membre Naturel
- Messages: 39
- Enregistré le: 25 Mai 2015, 13:56
-
par magyy » 30 Avr 2016, 21:21
Bonsoir,
je dois écrire un programme qui permet de déterminer l'inverse d'une matrice en me basant sur un programme qui permet de résoudre un système linéaire, mais je ne sais pas comment faire.
Voici le code du programme de résolution de systeme linéaire en utilisant la methode du pivot de Gauss:
- Code: Tout sélectionner
#include <stdio.h>
#include <stdlib.h>
int n,k,drapeau;
float *A, *b,*x;int c,y;
int num(int i,int j)
{
return (i-1)*n+j-1;
}
//saisie de la matrice et du vecteur b
void* saisie(int n,float *A,float *b)
{ int i,j;
printf("Donner A ligne/ligne\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%f",&A[num(i,j)]);
}
}
printf("Donner les coordonnees de b\n");
for(i=1;i<=n;i++)
{
scanf("%f",&b[i]);
}
}
//Recherche du pivot
int recherche_pivot(int k,float *A)
{
int i,i0;
i=k;
i0=-1;
do
{
if(A[num(i,k)]==0)
i=i+1;
}while((A[num(i,k)]==0)&&(i!=n+1));
if(i!=n+1)
i0=i;
return i0;
}
//Permutation
void permutation(int k,int i0,float *A,float *b)
{
int j;float tampon;
for(j=1;j<=n;j++)
{
tampon=A[num(k,j)];
A[num(k,j)]=A[num(i0,j)];
A[num(i0,j)]=tampon;
}
for(j=1;j<=n;j++)
{
tampon=b[k];
b[k]=b[i0];
b[i0]=tampon;
}
}
//Elimination
void elimination(int k,float *A,float *b)
{
float r;int i,j;
for(i=k+1;i<=n;i++)
{
r=A[num(i,k)]/A[num(k,k)];
for(j=k;j<=n;j++)
{
A[num(i,j)]=A[num(i,j)]-r*A[num(k,j)];
}
b[i]=b[i]-r*b[k];
}
}
//remontée
void remontee(float *A,float *b,float *x)
{
int i,j;
x[n]=b[n]/A[num(n,n)];
for (i=n-1;i>=1;i=i-1)
{
x[i]=b[i];
for(j=i+1;j<=n;j++)
{
x[i]=x[i]-A[num(i,j)]*x[j];
x[i]=x[i]/A[num(i,i)];
}
}
}
//affichage du vecteur x
void affiche_vecteur(float *x)
{
int i;
for(i=1;i<=n;i++)
{
printf("%f\n",x[i]);
}
}
int main()
{
printf("Donner n\n");
scanf("%d",&n);
y=n*n;
A=malloc(y*sizeof(float));
b=malloc(n*sizeof(float));
x=malloc(n*sizeof(float));
drapeau=0;
saisie(n,A,b);
k=1;
do
{
c=recherche_pivot(k,A);
if(c==k)
{
elimination(k,A,b);
k=k+1;
}
else
{
if(c!=-1)
{
permutation(k,c,A,b);
elimination(k,A,b);
k=k+1;
}
else
drapeau=1;
}
}while((k!=n)&&(drapeau!=1));
if((k==n)&&(A[num(n,n)]!=0))
{
remontee(A,b,&x);
affiche_vecteur(&x);
}
else
printf("Pas de solution unique");
return 0;
}
Merci d'avance!
-
Ben314
- Le Ben
- Messages: 21483
- Enregistré le: 11 Nov 2009, 23:53
-
par Ben314 » 02 Mai 2016, 14:48
Salut,
Si on écrit l'inverse de A sous la forme
où les Vi sont les vecteurs colonnes, que peut-on dire du produit de la matrice A par le vecteur colonne Vi ?
Donc quel est le système linéaire qui permet de trouver Vi ?
Qui n'entend qu'un son n'entend qu'une sonnerie. Signé : Sonfucius
-
magyy
- Membre Naturel
- Messages: 39
- Enregistré le: 25 Mai 2015, 13:56
-
par magyy » 04 Mai 2016, 22:04
Le produit d'une matrice et de son inverse donne la matrice identité .
Le systeme c'est AA^-1=I
-
magyy
- Membre Naturel
- Messages: 39
- Enregistré le: 25 Mai 2015, 13:56
-
par magyy » 05 Mai 2016, 02:34
J'ai modifié mon code, mais j'ai un probleme au niveau de ma fonction "remontée":
Voici le code:
- Code: Tout sélectionner
def main():
pass
if __name__ == '__main__':
main()
def MatVide(taille):
a=taille+1
m = [float]*a
i=1
while(i<a):
m[i] = [float]*a
i=i+1
return m
def saisie( n,A):
print("Donner A ligne/ligne\n")
i=1
while i<=n:
j=1
while j<=n:
print("Donner A[",i,"][",j,"]")
A[i][j] = float(input())
j=j+1
i=i+1
#Recherche du pivot
def recherche_pivot( k,A):
i=k
i0=-1
while((A[i][k]==0) and (i!=n+1)):
if(A[i][k]==0):
i=i+1
if(i!=n+1):
i0=i
return i0
#Permutation
def permutation(k, i0,A,b):
for j in range(n):
tampon=A[k][j]
A[k][j]=A[i0][j]
A[i0][j]=tampon
for j in range(n):
tampon=b[k][j]
b[k][j]=b[i0][j]
b[i0][j]=tampon
#Elimination
def elimination(k,A,b):
i=k+1
while i<=n:
j=k
r=A[i][k]/A[k][k]
while j<=n:
A[i][j]=A[i][j]-r*A[k][j]
b[i][j]=b[i][j]-r*b[k][j]
j=j+1
i=i+1
#remontee
def remontee(A,b,x):
x[n][n]=b[n][n]/A[n][n]
i=n
j=n-1
while(i>=1):
x[i][j]=b[i][j]
while(j<n):
x[i][j]=x[i][j]-A[i][j]*x[i][j]
x[i][j]=x[i][j]/A[i][i]
j=j+1
i=i-1
j=n
#affichage de la matrice
def affiche_matrice(x,n):
print("L'inverse est:")
i=1
while(i<=n):
j=1
while(j<=n):
print(x[i][j])
j=j+1
i=i+1
print("Donner n\n")
n=int(input())
drapeau=0
A = MatVide(n)
x= MatVide(n)
b = MatVide(n)
saisie(n,A)
k=1
#determination de la matrice identité
i=1
while(i<=n):
j=1
while(j<=n):
b[i][j]=0
j=j+1
i=i+1
i=1
while(i<=n):
j=1
while(j<=n):
if(j==i):
b[i][j]=1
j=j+1
i=i+1
while((k!=n) and (drapeau!=1)):
c=recherche_pivot(k,A)
if(c==k):
elimination(k,A,b)
k=k+1
else:
if(c!=-1):
permutation(k,c,A,b)
elimination(k,A,b)
k=k+1
else:
drapeau=1
if((k==n) and (A[n][n]!=0)):
remontee(A,b,x)
print("La matrice est inversible\n")
affiche_matrice(x,n)
else:
print("La matrice n'est pas inversible\n")
if(drapeau==1):
print("La matrice n'est pas inversible.")
Utilisateurs parcourant ce forum : Aucun utilisateur enregistré et 1 invité