restart;
with(linalg);taille:=proc(A)
local t,u,n,p;
t:=[op(2,evalm(A))];
n:=op(2,t[1]);p:=op(2,t[2]);
[n,p];
end:diagonale:=proc(X)
local i,j,n,B;
n:=taille(X)[2];
B:=array(1..n,1..n);
for i from 1 to n do
for j from 1 to n do B[i,j]:=0; od;
B[i,i]:=X[1,i];
od;
evalm(B);
end:filtre:=proc(X,r)
local i,j,t,B,C;
t:=taille(X);
B:=array(1..r,1..t[2]-r);
C:=array(1..r,1..r);
for i from 1 to r do
for j from 1 to r do C[i,j]:=X[i,j]; od;
for j from 1 to t[2]-r do B[i,j]:=X[i,j+r]; od;
od;
evalm(C),evalm(B);
end:Permut_ligne:=proc(i,j,A)
local B,k,t;
t:=taille(A);
B:=array(1..t[1],1..t[2]);
B:=evalm(A);
for k from 1 to t[2] do
B[i,k]:=A[j,k]; B[j,k]:=A[i,k];
od;
evalm(B);
end: Dilat_ligne:=proc(a,i,A)
local k,t,B;
t:=taille(A);
B:=array(1..t[1],1..t[2]);
B:=evalm(A);
for k from 1 to t[2] do
B[i,k]:=simplify(a*A[i,k]);
od;
evalm(B);
end:Transvect_ligne:=proc(i,a,j,A)
local k,t,B;
t:=taille(A);
B:=array(1..t[1],1..t[2]);
B:=evalm(A);
for k from 1 to t[2] do
B[i,k]:=simplify(A[i,k]+a*A[j,k]);
od;
evalm(B);
end:CherchePivot_colonne:=proc(A,i)
local t,k;
t:=taille(A);
if is(i>t[1])then RETURN(IMPOSSIBLE); fi;
for k from i to t[1] do
if is(A[k,i]<>0) then RETURN(k); fi;
od;
RETURN(0);
end:Permut_colonne:=proc(i,j,A)
local B,k,t;
t:=taille(A);
B:=array(1..t[1],1..t[2]);
B:=evalm(A);
for k from 1 to t[1] do
B[k,i]:=A[k,j]; B[k,j]:=A[k,i];
od;
evalm(B);
end: zero_sur_pivot_historique:=proc(i,A)
local t,B,k,hist;
hist:=NULL;
t:=taille(A);
B:=array(1..t[1],1..t[2]);
B:=evalm(A);
if is(i>t[2]) or is(i>t[1]) or is(B[i,i]<>1) then RETURN(IMPOSSIBLE); fi;
for k from 1 to i-1 do
if B[k,i]<>0 then hist:=hist,["Transvection_ligne",k,-B[k,i],i]; fi; hist contient la s\351quence des op\351rations \351l\351mentaires effectu\351es sur B
B:=Transvect_ligne(k,-B[k,i],i,B);
od;
evalm(B),[hist];
end:
zero_sous_pivot_historique:=proc(i,A)
local t,B,k,hist,r;
hist:=NULL;
t:=taille(A);
B:=array(1..t[1],1..t[2]);
B:=evalm(A);
if is(i>t[2]) or is(i>t[1]) or is(B[i,i]=0) then RETURN(IMPOSSIBLE); fi;
if (B[i,i]<>1 ) then hist:=hist,["Dilate_ligne",1/B[i,i],i]; fi;
B:=Dilat_ligne(1/B[i,i],i,B);
for k from i+1 to t[1] do
if B[k,i]<>0 then hist:=hist,["Transvection_ligne",k,-B[k,i],i]; fi; hist contient la s\351quence des op\351rations \351l\351mentaires effectu\351es sur B
B:=Transvect_ligne(k,-B[k,i],i,B);
od;
evalm(B),[hist];
end:Gauss_historique:=proc(A,opt)
local T,i,j,t,k,hist,r,temp;
hist:=NULL;
t:=taille(A);
T:=array(1..t[1],1..t[2]);
T:=evalm(A);
for j from 1 to t[2] do
i:=CherchePivot_colonne(T,j);
if opt="colonnes" then
k:=j+1;
while(i=0 and k<t[2]+1) do
hist:=hist,["Echange_colonne",j,k];
T:=Permut_colonne(j,k,T);
i:=CherchePivot_colonne(T,j);
k:=k+1;
od;
fi;
if i<>0 then T:=Permut_ligne(i,j,T);
if (i<>j) then hist:=hist,["Echange_ligne",i,j]; fi;
temp:=zero_sous_pivot_historique(j,T);
T:=temp[1];
hist:=hist,op(temp[2]);
fi;
od;
if opt="CRAMER" then
if (sum(T[k,k],k=1..t[1])<>t[1]) then RETURN(IMPOSSIBLE) fi;
for j from t[1] to 1 by -1 do
temp:=zero_sur_pivot_historique(j,T);
T:=temp[1];
hist:=hist,op(temp[2]);
od;
fi;
[hist];
end:Ginverse:=proc(A)
local T,B,j,k,q,t,m,hist,texte;
t:=taille(A);
T:=array(1..t[1],1..t[2]);
B:=array(1..t[1],1..t[2]);
for j from 1 to t[1] do
for k from 1 to t[1] do T[j,k]:=0; od; tous les coefficients de la ligne j sont nulls
T[j,j]:=1; sauf celui d'indice j
od;
On a a obtenu jusqu'ici la matrice identit\351
B:=evalm(A);
hist:=Gauss_historique(A,"CRAMER");
m:=nops(hist);
print(B, "........" , T);
for j from 1 to m do
q:=hist[j];
if q[1]="Echange_ligne" then T:=Permut_ligne(q[2],q[3],T);B:=Permut_ligne(q[2],q[3],B);texte:='L'[q[2]],"<--->",'L'[q[3]];
elif q[1]="Echange_colonne" then T:=Permut_colonne(q[2],q[3],T);B:=Permut_colonne(q[2],q[3],B);texte:='C'[q[2]],"<--->",'C'[q[3]];
elif q[1]="Dilate_ligne" then T:=Dilat_ligne(q[2],q[3],T);B:=Dilat_ligne(q[2],q[3],B);texte:='L'[q[3]],"<----",q[2]*'L'[q[3]];
elif q[1]="Transvection_ligne" then T:=Transvect_ligne(q[2],q[3],q[4],T);B:=Transvect_ligne(q[2],q[3],q[4],B);texte:='L'[q[2]],"<----",'L'[q[2]]+q[3]*'L'[q[4]];
fi;
print(B,texte,T);
od;
end:Grang:=proc(A)
local B,j,k,q,t,m,hist,r,texte;
t:=taille(A);
B:=array(1..t[1],1..t[2]);
B:=evalm(A);
hist:=Gauss_historique(A,"colonnes");
m:=nops(hist);
print("........" , B);
for j from 1 to m do
q:=hist[j];
if q[1]="Echange_ligne" then B:=Permut_ligne(q[2],q[3],B);texte:='L'[q[2]],"<--->",'L'[q[3]];
elif q[1]="Echange_colonne" then B:=Permut_colonne(q[2],q[3],B);texte:='C'[q[2]],"<--->",'C'[q[3]];
elif q[1]="Dilate_ligne" then B:=Dilat_ligne(q[2],q[3],B);texte:='L'[q[3]],"<----",q[2]*'L'[q[3]];
elif q[1]="Transvection_ligne" then B:=Transvect_ligne(q[2],q[3],q[4],B);texte:='L'[q[2]],"<----",'L'[q[2]]+q[3]*'L'[q[4]];
fi;
print(texte,B);
od;
end:Grang_inverse:=proc(A,C)
local B,j,k,q,t,m,hist,r,texte;
t:=taille(C);
B:=array(1..t[1],1..t[2]);
B:=evalm(C);
hist:=Gauss_historique(A,"colonnes");
m:=nops(hist);
print("........" , B);
for j from m to 1 by -1 do
q:=hist[j];
if q[1]="Echange_ligne" then B:=Permut_ligne(q[2],q[3],B);texte:='L'[q[2]],"<--->",'L'[q[3]];
elif q[1]="Echange_colonne" then B:=Permut_colonne(q[2],q[3],B);texte:='C'[q[2]],"<--->",'C'[q[3]];
elif q[1]="Dilate_ligne" then B:=Dilat_ligne(1/q[2],q[3],B);texte:='L'[q[3]],"<----",1/q[2]*'L'[q[3]];
elif q[1]="Transvection_ligne" then B:=Transvect_ligne(q[2],-q[3],q[4],B);texte:='L'[q[2]],"<----",'L'[q[2]]-q[3]*'L'[q[4]];
fi;
print(texte,B);
od;
end:Gsyst_cramer:=proc(A,X,Y)
local B,Z,Xt,j,k,q,t,m,hist,r,texte;
t:=taille(A);
B:=array(1..t[1],1..t[2]);
B:=evalm(A);
Z:=array(1..t[1],1..1);
Z:=evalm(transpose([Y]));
Xt:=array(1..t[2],1..1);
Xt:=evalm(transpose([X]));
hist:=Gauss_historique(A,"CRAMER");
m:=nops(hist);
print("........" ,evalm(B&*Xt),"=",Z);
for j from 1 to m do
q:=hist[j];
if q[1]="Echange_ligne" then Z:=Permut_ligne(q[2],q[3],Z);B:=Permut_ligne(q[2],q[3],B);texte:='L'[q[2]],"<--->",'L'[q[3]];
elif q[1]="Echange_colonne" then Xt:=Permut_colonne(q[2],q[3],Xt);B:=Permut_colonne(q[2],q[3],B);texte:='C'[q[2]],"<--->",'C'[q[3]];
elif q[1]="Dilate_ligne" then Z:=Dilat_ligne(q[2],q[3],Z);B:=Dilat_ligne(q[2],q[3],B);texte:='L'[q[3]],"<----",q[2]*'L'[q[3]];
elif q[1]="Transvection_ligne" then Z:=Transvect_ligne(q[2],q[3],q[4],Z);B:=Transvect_ligne(q[2],q[3],q[4],B);texte:='L'[q[2]],"<----",'L'[q[2]]+q[3]*'L'[q[4]];
fi;
print(texte,evalm(B&*Xt),"=",Z);
od;
end:Gsyst:=proc(A,X,Y)
local B,Z,Xt,Xd,j,k,q,t,m,r,hist,texte,param,F,Bc,Zc,Xtc;
t:=taille(A);
B:=array(1..t[1],1..t[2]);
B:=evalm(A);
Z:=array(1..t[1],1..1);
Z:=evalm(transpose([Y]));
Xt:=evalm([X]);
Xd:=diagonale(Xt);
hist:=Gauss_historique(A,"colonnes");
m:=nops(hist);
print("........" ,evalm(B&*Xd),"=",Z);
for j from 1 to m do
q:=hist[j];
if q[1]="Echange_ligne" then Z:=Permut_ligne(q[2],q[3],Z);B:=Permut_ligne(q[2],q[3],B);texte:='L'[q[2]],"<--->",'L'[q[3]];
elif q[1]="Echange_colonne" then Xt:=Permut_colonne(q[2],q[3],Xt);B:=Permut_colonne(q[2],q[3],B);texte:='C'[q[2]],"<--->",'C'[q[3]];
elif q[1]="Dilate_ligne" then Z:=Dilat_ligne(q[2],q[3],Z);B:=Dilat_ligne(q[2],q[3],B);texte:='L'[q[3]],"<----",q[2]*'L'[q[3]];
elif q[1]="Transvection_ligne" then Z:=Transvect_ligne(q[2],q[3],q[4],Z);B:=Transvect_ligne(q[2],q[3],q[4],B);texte:='L'[q[2]],"<----",'L'[q[2]]+q[3]*'L'[q[4]];
fi;
Xd:=diagonale(Xt);
print(texte,evalm(B&*Xd),"=",Z);
od;
r:=add(B[i,i],i=1..min(t[1],t[2]));
print("Conclusion: Sous la condition d'avoir la compatibilit\351 des",t[1]-r, "derni\350res \351quations");
for j from r+1 to t[1] do print(Z[j,1]=0); od;
param:=seq(Xt[1,j],j=r+1..t[2]);
print("En prenant alors ",param, "comme param\351tres");
print("On est donc ramen\351 \340 la resolution du syst\350me de CRAMER");
F:=filtre(B,r);
Bc:=F[1];Zc:=evalm(transpose([[seq(Z[j,1],j=1..r)]])-F[2]&*transpose([[param]]));
Xtc:=transpose([[seq(Xt[1,j],j=1..r)]]);
#CRAMER
hist:=Gauss_historique(Bc,"CRAMER");
m:=nops(hist);
print("........" ,evalm(Bc&*Xtc),"=",Zc);
for j from 1 to m do
q:=hist[j];
if q[1]="Echange_ligne" then Zc:=Permut_ligne(q[2],q[3],Zc);Bc:=Permut_ligne(q[2],q[3],Bc);texte:='L'[q[2]],"<--->",'L'[q[3]];
elif q[1]="Echange_colonne" then Xtc:=Permut_ligne(q[2],q[3],Xtc);Bc:=Permut_colonne(q[2],q[3],Bc);texte:='C'[q[2]],"<--->",'C'[q[3]];
elif q[1]="Dilate_ligne" then Zc:=Dilat_ligne(q[2],q[3],Zc);Bc:=Dilat_ligne(q[2],q[3],Bc);texte:='L'[q[3]],"<----",q[2]*'L'[q[3]];
elif q[1]="Transvection_ligne" then Zc:=Transvect_ligne(q[2],q[3],q[4],Zc);Bc:=Transvect_ligne(q[2],q[3],q[4],Bc);texte:='L'[q[2]],"<----",'L'[q[2]]+q[3]*'L'[q[4]];
fi;
print(texte,evalm(Bc&*Xtc),"=",Zc);
od;
end:R\351solution d'un syst\350meA:=array(1..4,1..4): for i from 1 to 4 do for j from 1 to 4 do A[i,j]:=i+j: od:od:A=evalm(A);Gsyst(A,[x,y,z,t],[6,7,8,9]);Calcul du rangA:=array(1..4,1..4): for i from 1 to 4 do for j from 1 to 4 do A[i,j]:=min(i-1,j-1): od:od:A=evalm(A);Grang(A);R\351solution d'un syst\350me de CRAMERA:=array(1..4,1..4): for i from 1 to 4 do for j from 1 to 4 do A[i,j]:=min(i,j): od:od:A=evalm(A);Gsyst_cramer(A,[x1,x2,x3,x4],[y1,y2,y3,y4]);Calcul de l'inverse d'une matriceGinverse(A);