function gjinvA = gaussjordaninvpivotmax(A) // avec pivot max

printf('avec pivot max')

n=size(A,'c') // calcul de la taille de A et vérif carrée
if ~ size(A,'r')==n  then printf('matrice non carree'), end

print(6,A),

de=1, // pour calculer le determinant en meme temps

perm=[1:n] // prem(i)=i c'est pour mémoriser les permutations
           // perm(i)=l signifiera qu'il y a eu permutation des lignes i et l

for k = 1 : n ,   // etape k
   amax=0, l=k, // recherche du pivot maximal
    for i=k:n
        absA=abs(A(i,k))
        if absA>amax then l=i, amax=absA, end
    end
    if amax==0 then X='matrice singuliere', return, end
    if l<> k then printf('pour pivot max on echange les lignes %d et %d',k,l),
                  aux=A(k,:),A(k,:)=A(l,:),A(l,:)=aux
                  aux=perm(k);perm(k)=perm(l),perm(l)=aux
                  de=-de,
                  print(6,A),
    end,
   
   p=A(k,k)  // pivot max
   printf(' '),
   printf('pivot max = %f',p),
   de=de*p

   A(k,[1:k-1,k+1:n]) = A(k,[1:k-1,k+1:n])/p, // nouvelle ligne k

   for i = [1:k-1,k+1:n] ,   // nouvelle ligne i 
       A(i,[1:k-1,k+1:n]) = A(i,[1:k-1,k+1:n]) - A(i,k)*A(k,[1:k-1,k+1:n]), 
   end,

   A([1:k-1,k+1:n],k) = -A([1:k-1,k+1:n],k)/p,
   A(k,k) = 1/p, 
   
   printf('etape %d',k),
   print(6,A),
end,
   // pour l'affichage qu'on y voie clair !
   // AA=[eye(n,n),eye(n,n)], AA(1:n,[k+1:n,n+1:n+k])=A
   // AA=[eye(n,n),eye(n,n)], AA(1:n,k+1:n+k)=A(1:n,[k+1:n,1:k])
   // print(6,AA),

printf('determinant=%f',de),

for j=1:n, // permutations eventuelles des colonnes
    if perm(j)<>j then for k=j+1:n, 
                if perm(k)==j then printf('echange des colonnes %d et %d',k,j),
                                  aux=A(:,k),A(:,k)=A(:,j),A(:,j)=aux
                                  perm(k)=perm(j)
                                  break
                 end,
             end,
    end,
end,

print(6,A),
gjinvA=A, // c'est l'inverse


