
function gjinvA = gaussjordaninvAApivotmax(A)

printf('avec pivot max')
printf("\nce programme utilise 2 matrice nxn (une matrice nx2n)")

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),
A(:,n+1:2*n)=diag(ones(1,n)),
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),
                  A([k,l],k:n+k-1)=A([l,k],k:n+k-1),
                  aux=perm(k);perm(k)=perm(l),perm(l)=aux
                  de=-de,
                  print(6,A),
    end,

   printf('pivot max = %f',A(k,k)),
   de=de*A(k,k),


   p=A(k,k)  // pivot
   printf(' '),
   printf('pivot=%f',p),

   A(k,[k:n+k]) = A(k,[k:n+k])/p, // nouvelle ligne k

   for i = [1:k-1,k+1:n] ,   // nouvelle ligne i 
       A(i,[k:n+k]) = A(i,[k:n+k]) - A(i,k)*A(k,[k:n+k]), 
   end,

   printf('etape %d',k),
   print(6,A),
end,

printf('determinant=%f',de),
print(6,perm)
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),
                                   A(:,[n+k,n+j])=A(:,[n+j,n+k]),
                                  perm(k)=perm(j)
                                  break
                 end,
             end,
    end,
end,

gjinvA=A(:,n+1:2*n), // c'est l'inverse

// gaussjordaninvAApivotmax(A)
// A=[2,1,-4;3,3,-5;4,5,-2]

