function gjinvA = gaussjordaninvAA(A)

printf("gauss-jordan-inverse sans 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
   if A(k,k)==0 then // recherche d'un pivot non nul
       printf('pivotnul k=%d',k),
       if k<n then i=k+1,
                   while(A(i,k)==0 & i<n), i=i+1, 
                   end,
               else i=k,
        end,
        if A(i,k)==0 then gjinvA='matrice singuliere', return 
                     else printf('on echange les lignes %d et %d',k,i),
                          A([k,i],k:n+k-1)=A([i,k],k:n+k-1),
                          aux=perm(k);perm(k)=perm(i),perm(i)=aux 
                          de=-de,
                          print(6,A),
        end,
    end,
   
   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),
   de=de*p,
end,

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),
                                   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

// gaussjordaninvAA(A)
// A=[2,1,-4;3,3,-5;4,5,-2]

