K<T> := FunctionField(RationalField());
R<X> := PolynomialRing(K);
S<X> := FieldOfFractions(R);


TCtFrac := function(P,Q)
  A := R!P;
  B := R!Q;
  TCF := [ ];
  while Degree(Parent(P)!B) gt 0 do
    b := LeadingCoefficient(B);
    P0, r := Quotrem(b*A,B);
    TCF cat:= [P0/b];
    A := B;
    B := -b*r;
  end while;
  TCF cat:= [A/B];
  return TCF;
end function;


MonoidSum := function(f,g)
  A1 := R!Numerator(f);
  B1 := R!Denominator(f);
  A2 := R!Numerator(g);
  B2 := R!Denominator(g);
  D1, U1, V1 := Xgcd(A1,B1);
  D2, U2, V2 := Xgcd(A2,B2);
  A3 := A1*A2-V1*B2;
  B3 := B1*A2+U1*B2;
  return S!(A3/B3);
end function;
