function gjinvA = gaussjordaninv(A)

printf("gauss-jordan-inverse sans 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
   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],:)=A([i,k],:),
                          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,[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),
   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(:,[k,j])=A(:,[j,k]),
                                  perm(k)=perm(j)
                                  break
                 end,
             end,
    end,
end,

gjinvA=A, // c'est l'inverse

// gaussjordaninv(A)
// A=[2,1,-4;3,3,-5;4,5,-2]

