function X = gauss0pivotmax(A,B)     // avec recherche du pivot maximal

printf('gauss0 avec pivot maximal')

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

// copie de B dans la colonne n+1 de A
A(:,n+1) = B
print(6,A),

ech=1 // pour le nombre d'echanges (parite)

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],:)=A([l,k],:),
                  ech=-ech,
                  print(6,A),
    end,
  
   printf('pivot max = %f',A(k,k)),

   for i = k+1 : n ,   // nouvelle ligne i
       A(i,k:n+1) = A(i,k:n+1) - A(i,k)*A(k,k:n+1)/A(k,k)
   end,
   // ou mieux A(k+1:n,k:n+1) = A(k+1:n,k:n+1) - A(k+1:n,k)*A(k,k:n+1)/A(k,k)
   printf('etape %d',k),
   print(6,A),
end,

printf('determinant=%f', ech*prod(diag(A))),

// remontée, calcul des xi dans le vecteur X
X(n) = A(n,n+1)/A(n,n), 
for i=[n-1:-1:1],   // remontee : calcul des xi
    X(i) = (A(i,n+1) - A(i,i+1:n)*X(i+1:n))/A(i,i),
end,
printf('solution'),

// appel gauss0pmax(A,B) ou X=gauss0pmax(A,B)
// A=[2,1,-4;3,3,-5;4,5,-2], B=[8;14;16]       premier exemple du cours
// A=[2,1,-4;4,2,-7;2,1,-1], B=[8;15;9]        impossible
// A=[2,1,-4;4,2,-7;2,1,-1], B=[8;15;5]        indetermine
// A=[2,1,-4;4,2,-7;2,2,-1], B=[8;15;5]        echange de lignes obligatoire
