Deg [f_, var_] := Length [ CoefficientList [ f, var ] ] - 1 ;

Lcf [f_, var_] := CoefficientList [f, var] [[Deg[f,var] + 1]];

GCDPolies [f_, g_, var_] :=
Module [{matrix, linesofmatrix, q, gcdline},
   matrix = {{f,1,0},{g,0,1}};
   linesofmatrix = 2;
   While [Deg [ matrix [[linesofmatrix]] [[1]], var] >= 0,
      q = PolynomialQuotient [matrix [[linesofmatrix - 1]] [[1]],
                               matrix [[linesofmatrix]] [[1]],
                               var];
      matrix = Append [matrix, Expand [Together[ 
                                     matrix [[linesofmatrix - 1]] -
                                      q * matrix [[linesofmatrix]]
                                               ]
                                      ]
                      ];
      linesofmatrix++;
   ];
   gcdline = matrix [[linesofmatrix - 1]];
   {matrix, gcdline}
]


PoliesExtendedGCD [f_, g_, var_] :=
   Module [{lastline},
      lastline = GCDPolies[f,g,var][[2]];
      {lastline[[1]], {lastline[[2]], lastline[[3]]}}];



