/*
Groebner Basis Test
AKS 24/11/95.
*/


SetAssertions(true);
VERB := true;
VERB := false;

//gon();

function create_ideal(P, k, l, d, rand)
    n := Rank(P);
    return ideal<P |
	[
	    &+[rand() * &*[P.i^Random(0, d): i in [1..n]]: i in [1..l]]:
	    i in [1..k]
	]
    >;
end function;

procedure ideal_test(K, n, k, l, d, rand)
    if VERB then
	"\n******************\n";
    end if;

    "*** Groebner test:", K, n, k, l, d;

    //SetVerbose("GroebnerWalk", true);
    //SetVerbose("Groebner", true);

    P := PolynomialRing(K, n, "grevlex");
    AssignNames(~P, [CodeToString(i + 96): i in [1 .. n]]);

    I := create_ideal(P, k, l, d, rand);
    J := create_ideal(P, k, l, d, rand);

    if VERB then
	"I:", I;
	"J:", J;
    end if;

    Groebner(I);
    Groebner(J);

    _ := IsZeroDimensional(I);
    _ := IsZeroDimensional(J);

    if VERB then
	"Groebner I:", I;
	"Groebner J:", J;

	"IsZeroDimensional(I):", IsZeroDimensional(I);
	"IsZeroDimensional(J):", IsZeroDimensional(J);
    end if;

    S := I + J;
    M := I meet J;
    if VERB then
	"M:", M;
    end if;
    T := I * J;
    if VERB then
	"T:", T;
    end if;
    C := ColonIdeal(I, J);
    if VERB then
	"C:", C;
    end if;

    assert I subset S;
    assert J subset S;
    assert M subset I;
    assert M subset J;
    assert T subset M;
    if VERB then
	"T eq M:", T eq M;
    end if;

    assert IsProper(I) eq (1 notin I);
    assert IsZero(I) eq (#GroebnerBasis(I) eq 0);

    F := &+Basis(I);
    G := &+Basis(J);
    H := &+Basis(M);

    assert F in I;
    assert G in J;
    assert H in M;
    assert IsInRadical(F, I);
    assert IsInRadical(G, J);
    assert IsInRadical(H, M);

    assert NormalForm(F, I) eq 0;
    assert NormalForm(G, J) eq 0;
    assert NormalForm(F, J) in S;

    NF := NormalForm([F, G, F*G + 1], J);

// "J:", J; "NF:", NF; "NFJ:", NormalForm(F, J);
    assert NF[1] eq NormalForm(F, J);
    assert NF[2] eq 0;
    assert NF[3] eq NormalForm(P!1, J);

    if F ne 0 and G ne 0 then
	assert SPolynomial(F, G) in S;
    end if;

    C := Coordinates(I, F);
    assert &+[P | C[i]*BasisElement(I, i): i in [1..#C]] eq F;
end procedure;

rand := func<K| func<|Random(K)>>;
ideal_testr := proc<K, n, k, l, d | ideal_test(K, n, k, l, d, rand(K))>;

    //ideal_testr(GF(NextPrime(2^32)), 3, 2, 3, 3);
    //error "done";

ideal_test(RationalField(), 2, 2, 3, 2, func<|Random(-10, 10)>);

ideal_testr(GF(2), 10, 3, 3, 1);
ideal_testr(GF(2), 6, 2, 5, 1);
ideal_testr(GF(2), 3, 2, 5, 1);
ideal_testr(GF(3), 5, 2, 3, 1);

for q in
    [2^i: i in [2 .. 67] cat [100, 200]] cat
    [4, 5, 7, 9, 13, 16, 23, 3^5] cat
    [2965819, 11863279,
     PreviousPrime(Round(2^28.5)), PreviousPrime(2^29),
     PreviousPrime(2^32), NextPrime(2^32),
     PreviousPrime(2^60), PreviousPrime(2^63),
     NextPrime(10^100)
     ]
do
    //GetSeed();
    ideal_testr(GF(q), 3, 2, 3, 3);
end for;

ideal_testr(GF(2^1000), 3, 3, 3, 2);

SetEchoInput(true);

K := GF(35098201);
P<x,y> := PolynomialRing(K, 2);
B := [y + 1 + x^5 + x^10, 1 + x^34];
I := ideal<P | B>;
E := EliminationIdeal(I, 1);
B := Basis(E);
G := GroebnerBasis(I);
assert G[2] eq B[1];

B := [y + 1 + x^5 + x^10, 1 + x^34];
I := ideal<P | B>;
Groebner(I: Al := "Direct");
assert Basis(I) eq G;


P<x,y,z>:=PolynomialRing(Rationals(),3);
assert MinimalAlgebraGenerators([x + y + 1, (x+y + z)^2, z^2, x*z, y*z, z^6])
    eq [x+y+1, (x+y + z)^2, x*z, y*z];
assert MinimalAlgebraGenerators([x+y+1, (x+y)*z, z + 1]) eq [x+y+1, z + 1];


/*
GB over Z test.
*/

P<x1,x2,x3,x4> := PolynomialRing(IntegerRing(), 4);
f1 := 7*x1*x2^3*x3;
f2 := x1*x2*x3^3;
g1 := 64*x1^2 - 62*x1*x2 + 57*x1*x3 + 61*x1*x4 - 100*x2^2 + 5*x2*x3 +
50*x2*x4 - 23*x3^2 - 81*x3*x4 + 59*x4^2;
g2 := 70*x1^2 + 63*x1*x2 - 21*x1*x3 + 84*x1*x4 + 51*x2^2 - 20*x2*x3 +
38*x2*x4 - 29*x3^2 - 11*x3*x4 + 8*x4^2;
I := Ideal([g1, g2]);
Groebner(I);
h := f1*g1+f2*g2;
assert h in I;


// ER, Feb 2013

P<a60,a61,a62,a63,a64,a65,a66,a40,a41,a42,a43,a44,a30,a31,a32,a33,a20,a21,
a22,a10,a11>
:=PolynomialAlgebra(Integers(),
[6,6,6,6,6,6,6, 4,4,4,4,4, 3,3,3,3, 2,2,2, 1,1],"grevlexw",
[6,6,6,6,6,6,6, 4,4,4,4,4, 3,3,3,3, 2,2,2, 1,1]);

a6:=Polynomial([a60,a61,a62,a63,a64,a65,a66]);
a4:=Polynomial([a40,a41,a42,a43,a44]);
a3:=Polynomial([a30,a31,a32,a33]);
a2:=Polynomial([a20,a21,a22]);
a1:=Polynomial([a10,a11]);

c4:=-48*a4+16*a2^2+a1^4-24*a1*a3+8*a2*a1^2;
c6:=864*a6+216*a3^2+48*a2^2*a1^2+12*a2*a1^4-288*a4*a2-72*a4*a1^2-36*a1^3*a3+64*a2^3
+a1^6-144*a1*a3*a2;

eqIV:=[Coefficient(c4,i):i in [2..4]]
   cat [Coefficient(c6,i):i in [3..6]];
idl:=Ideal(eqIV cat [a10,a11,a30,a31,a32,a33]);
assert 16*a44*a21*a22 + 16*a43*a22^2 - 16*a21*a22^3 in idl;
GB := GroebnerBasis(idl);
assert #GB eq 78;
assert [LeadingTerm(f): f in Basis(idl)] eq
    [ 32*a64^2*a20*a22^3, 32*a64^2*a21*a22^3, 32*a64*a65*a21*a22^3, 
    32*a65^2*a21*a22^3, 32*a64^2*a22^4, 32*a64*a65*a22^4, 32*a65^2*a22^4, 
    16*a64*a42*a21^3, 16*a65*a42*a21^3, 16*a66*a42*a21^3, 
    16*a64*a43*a20*a21*a22, 16*a65*a43*a20*a21*a22, 16*a66*a43*a20*a21*a22, 
    16*a64*a43*a20*a22^2, 16*a65*a43*a20*a22^2, 16*a66*a43*a20*a22^2, 
    16*a64*a42*a22^3, 16*a65*a42*a22^3, 16*a64*a43*a22^3, 16*a65*a43*a22^3, 
    16*a64*a44*a22^3, 16*a65*a44*a22^3, 32*a65*a21^2*a22^3, 16*a42^2*a21^3, 
    16*a42*a43*a20*a21*a22, 16*a43^2*a20*a21*a22, 96*a64*a20*a21^2*a22, 
    16*a42*a43*a20*a22^2, 16*a43^2*a20*a22^2, 16*a43^2*a21*a22^2, 
    96*a64*a20*a21*a22^2, 96*a65*a20*a21*a22^2, 96*a64*a21^2*a22^2, 
    16*a42^2*a22^3, 16*a42*a43*a22^3, 16*a43^2*a22^3, 16*a42*a44*a22^3, 
    16*a43*a44*a22^3, 16*a44^2*a22^3, 32*a65*a20*a22^3, 96*a66*a20*a22^3, 
    32*a64*a21^3, 32*a65*a21^3, 32*a66*a21^3, 96*a65*a21^2*a22, 
    96*a66*a21^2*a22, 96*a66*a21*a22^2, 96*a64*a22^3, 96*a65*a22^3, 
    288*a63*a21^2, 288*a64*a21^2, 288*a65*a21^2, 288*a66*a21^2, 288*a63*a21*a22,
    288*a64*a21*a22, 288*a65*a21*a22, 288*a66*a21*a22, 288*a63*a22^2, 
    288*a64*a22^2, 288*a65*a22^2, 288*a66*a22^2, 16*a44*a20*a22^2, 16*a43*a21^2,
    16*a44*a21^2, 16*a44*a21*a22, 864*a63, 864*a64, 864*a65, 864*a66, 48*a42, 
    48*a43, 48*a44, a30, a31, a32, a33, a10, a11 ];

// May 2015 (GB over ANF)

SetEchoInput(false);

"GB over ANF";

procedure test_gb_anf(M1, M2)

    "test_gb_anf:";
    IndentPush();
    Factorization(M1);
    Factorization(M2);
    IndentPop();

    Z := IntegerRing();
    P<x> := PolynomialRing(Z);
    K<w> := NumberField(x^2 + 1);
    P<x,y,z,s,t> := PolynomialRing(K, 5, "grevlex");
    f := w*x + M1*z + M2*1;
    g := y+z+1;

    B := [f+g, f+2*g, g];
    GB := GroebnerBasis(B);
    assert GB eq [x - w*M1*z - w*M2, y + z + 1];

    B := [f*g, g];
    GB := GroebnerBasis(B);
    assert GB eq [g];

    B := [f*g, f];
    GB := GroebnerBasis(B);
    assert GB eq [Normalize(f)];

    B := [f^2, x*g];
    GB := GroebnerBasis(B);
    assert GB eq 
    [
	y*z^2 + z^3 + 2*M2/M1*y*z + (M1 + 2*M2)/M1*z^2 + M2^2/M1^2*y +
	    (2*M1*M2 + M2^2)/M1^2*z + M2^2/M1^2,
	x^2 - 2*w*M1*x*z - M1^2*z^2 - 2*w*M2*x - 2*M1*M2*z - M2^2, x*y +
	x*z + x
    ];

    B := [f^2, M2*x*y*g + M1];
    assert GroebnerBasis(B) eq 
    [
	y^2*z^2 + y*z^3 + 2*M2/M1*y^2*z + (M1 + 2*M2)/M1*y*z^2 + M2^2/M1^2*y^2 + 
	    (2*M1*M2 + M2^2)/M1^2*y*z + 1/(M1*M2)*x + M2^2/M1^2*y + -2*w/M2*z + 
	    -2*w/M1,
	x*y^2 + x*y*z + x*y + M1/M2,
	x^2 + -2*w*M1*x*z + -M1^2*z^2 + -2*w*M2*x + -2*M1*M2*z + -M2^2
    ];

    B := [M1*x^2*y + M2, w*M2*x*y + M1*z + 1];
    assert GroebnerBasis(B) eq 
    [
	x*y + -w*M1/M2*z + -w/M2,
	x*z + 1/M1*x + -w*M2^2/M1^2,
	z^2 + -M2^3/M1^3*y + 2/M1*z + 1/M1^2
    ];

    B := [M1*M2*x^2*y + M2+w, (w+1)*M2*x*y^3 + M1*z + 1, M1*M2*x^3*y + z + w];
    assert GroebnerBasis(B) eq 
    [
	z^4 + (3*w*M1 + 1)/M1*z^3 + (-3*M1 + 3*w)/M1*z^2 + ((w + 1)*M2^5 +
	    (5*w - 5)*M2^4 + (-10*w - 10)*M2^3 + (-10*w + 10)*M2^2 + (5*w +
	    5)*M2 + (w - 1))/(M1^3*M2)*y + (-w*M1 - 3)/M1*z + -w/M1,
	y*z^2 + 2*w*y*z + -1*y + (M2^3 + 3*w*M2^2 - 3*M2 - w)/(M1*M2),
	y^2 + 1/2*(w - 1)*M1^2/(M2^2 + 2*w*M2 - 1)*z^2 + (1/2*(-w - 1)*M1^2 +
	    1/2*(w - 1)*M1)/(M2^2 + 2*w*M2 - 1)*z + 1/2*(-w - 1)*M1/(M2^2 +
	    2*w*M2 - 1),
	x + -1/(M2 + w)*z + -w/(M2 + w)

    ];

    B := [M1*M2*x^3*y^4 + M2*x*y+w, (w+1)*M2*x*y^3 + M2*y + M1*z + 1, M1*M2*x^3*
    y^2 + z + w];
    assert GroebnerBasis(B) eq 
    [
	y*z^3 + (2*w*M1^2 + 1/2*(5*w + 5)*M1*M2^2 + M1 - 3*M2^3)/M1^2*y*z^2 + 
	    1/2*(3*w + 3)*M2*z^3 + (-w*M1*M2^2 + 1/2*(3*w + 3)*M2^4 + M2^2)/M1^2*x*y
	    + -4*M2^2/M1^3*y^2 + (-2*w*M1^2*M2 + 1/2*(w + 1)*M1*M2^3 + 2*M1*M2 + 
	    3*M2^4)/M1^2*x*z + (-M1^3 + 1/2*(5*w - 5)*M1^2*M2^2 + 2*w*M1^2 - 
	    3*w*M1*M2^3 + 1/2*(5*w + 5)*M1*M2^2 - 3*M2^3)/M1^3*y*z + (1/2*(3*w - 
	    3)*M1*M2 + (3*w + 3)*M2)/M1*z^2 + ((-3*w - 1)*M1^2*M2 + 1/2*(w + 
	    1)*M1*M2^3 + 2*M1*M2 + 3*M2^4)/M1^3*x + (-M1^2 + 1/2*(5*w - 5)*M1*M2^2 +
	    (-8*w - 5)*M2^3)/M1^3*y + ((4*w - 3)*M1*M2 + (-w - 1)*M2^2 + 1/2*(3*w + 
	    3)*M2)/M1^2*z + (-M1^2*M2 + (w - 1)*M1*M2^3 + (w - 1)*M1*M2^2 + 1/2*(3*w
	    - 3)*M1*M2 - 3*w*M2^4 + (-2*w - 2)*M2^2)/M1^3,
	z^4 + (1/2*(-9*w - 9)*M1^2*M2^3 + 7*M1*M2^4 + (w - 1)*M2^5)/M1^4*y*z^2 + 
	    (2*w*M1^2 + 1/2*(-5*w - 5)*M1*M2^2 + 2*M1 + M2^3)/M1^2*z^3 + 
	    (3*w*M1^2*M2^3 + 2*M1^2*M2 + 1/2*(-5*w - 5)*M1*M2^5 + (-w - 1)*M1*M2^4 -
	    3*M1*M2^3 + M2^6 + (-w + 1)*M2^4)/M1^4*x*y + (8*M1*M2^3 + (2*w - 
	    2)*M2^4)/M1^5*y^2 + (4*w*M1^3*M2^2 + 1/2*(-w - 1)*M1^2*M2^4 + (-w - 
	    1)*M1^2*M2^3 - 4*M1^2*M2^2 - 5*M1*M2^5 + (-w + 1)*M1*M2^3 + (-w + 
	    1)*M2^6)/M1^4*x*z + (1/2*(-9*w + 9)*M1^3*M2^3 + (-w - 1)*M1^3*M2 + 
	    7*w*M1^2*M2^4 + 1/2*(-9*w - 9)*M1^2*M2^3 - 4*M1^2*M2^2 + (-w - 
	    1)*M1*M2^5 + 7*M1*M2^4 + (w - 1)*M2^5)/M1^5*y*z + (-M1^3 + 1/2*(-5*w + 
	    5)*M1^2*M2^2 + (3*w - 1)*M1^2 + w*M1*M2^3 + (-5*w - 5)*M1*M2^2 + M1 + 
	    2*M2^3)/M1^3*z^2 + ((6*w + 2)*M1^3*M2^2 + 1/2*(-w - 1)*M1^2*M2^4 + (-w -
	    3)*M1^2*M2^3 - 4*M1^2*M2^2 - 5*M1*M2^5 + (-w + 1)*M1*M2^3 + (-w + 
	    1)*M2^6)/M1^5*x + ((-w + 1)*M1^3*M2 + 1/2*(-9*w + 9)*M1^2*M2^3 - 
	    4*w*M1^2*M2^2 + (16*w + 9)*M1*M2^4 + (-w - 5)*M2^5)/M1^5*y + ((-w - 
	    1)*M1^3 + (-6*w + 5)*M1^2*M2^2 + (w - 1)*M1^2 + (4*w + 2)*M1*M2^3 + 
	    1/2*(-5*w - 5)*M1*M2^2 + M2^3)/M1^4*z + (M1^3*M2^2 - w*M1^3 + (-2*w + 
	    2)*M1^2*M2^4 + (-2*w + 2)*M1^2*M2^3 + 1/2*(-5*w + 5)*M1^2*M2^2 + 
	    6*w*M1*M2^5 + 2*w*M1*M2^4 + (5*w + 4)*M1*M2^3 + (-w - 1)*M2^6 - 
	    2*M2^4)/M1^5,
	x*y^2 + 1/2*(-w - 1)*M1/M2*y*z^2 + 1/2*(w + 1)*M1*x*z + (1/2*(-w + 1)*M1 + 
	    1/2*(-w - 1))/M2*y*z + 1/2*(w + 1)*x + 1/2*(-w + 1)/M2*y + 1/2*(-w + 1),
	y^3 + (1/4*(3*w - 3)*M1^3 + 1/2*w*M1^2*M2^2 - 1/2*w*M1^2*M2 + 1/4*(-3*w - 
	    3)*M1^2 + 1/4*(-w - 1)*M1*M2^3 + 1/2*M1*M2)/M2*y*z^2 + (1/4*(w - 1)*M1^4
	    + 1/4*w*M1^3*M2^2 + 1/4*(-w - 1)*M1^3)/M2^2*z^3 + (1/2*(w + 1)*M1^2 + 
	    1/4*w*M1*M2^4)/M2*x*y + (1/2*(-w + 1)*M1^3 + 1/2*w*M1^2*M2 + 1/2*(w + 
	    1)*M1^2 + 1/4*(w + 1)*M1*M2^3 - 1/2*M1*M2)*x*z + (1/4*(-3*w - 3)*M1^3 - 
	    1/2*M1^2*M2^2 + 1/2*M1^2*M2 - 1/2*w*M1^2 + 1/4*(-w + 1)*M1*M2^3 + 
	    1/2*w*M1*M2^2 + 1/4*(-3*w - 3)*M1 + 1/4*(-w - 1)*M2^3 + 1/2*M2)/M2*y*z +
	    (1/4*(-w - 1)*M1^4 - 1/4*M1^3*M2^2 + 1/4*(-w - 1)*M1^3 + 1/2*w*M1^2*M2^2
	    + 1/2*(-w - 1)*M1^2)/M2^2*z^2 + (1/2*(-2*w + 1)*M1^2 + 1/2*w*M1*M2 + 
	    1/2*(w + 1)*M1 + 1/4*(w + 1)*M2^3 - 1/2*M2)*x + (1/4*(-3*w - 1)*M1^2 + 
	    1/4*(-w - 1)*M1*M2^2 + 1/2*(-2*w + 3)*M1*M2 + 1/4*(-3*w + 3)*M1 + 
	    1/4*(-5*w + 1)*M2^3 + 1/2*(3*w + 2)*M2)/M2*y + (-1/2*w*M1^3 + 1/4*(-w - 
	    1)*M1^2*M2^2 + 1/4*(-3*w + 1)*M1^2 + 1/4*w*M1*M2^2 + 1/4*(-w - 
	    1)*M1)/M2^2*z + (1/2*(-w - 1)*M1^2*M2^2 + 1/4*(-w + 1)*M1^2 - 
	    1/4*M1*M2^4 + 1/4*(-3*w + 2)*M1*M2^2 + 1/4*(-w + 1)*M1 + 1/4*(-w + 
	    1)*M2^5)/M2^2,
	x*y*z + (1/2*(-w - 1)*M1 + M2)/M1*y*z^2 + 1/2*(-w - 1)*M1/M2*z^3 + (1/2*(-w 
	    - 1)*M2^2 + 1)/M1*x*y + (1/2*(-w - 1)*M1*M2 - M2^2)/M1*x*z + (1/2*(-w + 
	    1)*M1^2 + w*M1*M2 + 1/2*(-w - 1)*M1 + M2)/M1^2*y*z + (1/2*(-w + 1)*M1 - 
	    w - 1)/M2*z^2 + (1/2*(-w - 1)*M1*M2 - M2^2)/M1^2*x + (1/2*(-w + 1)*M1 + 
	    (2*w + 1)*M2)/M1^2*y + ((-w + 1)*M1 + 1/2*(-w - 1))/(M1*M2)*z + (1/2*(-w
	    + 1)*M1 + w*M2^3)/(M1^2*M2),
	y^2*z + -M2*x*y + w*y^2 + -w,
	x*z^2 + (1/2*(3*w + 3)*M1*M2 - 2*M2^2)/M1^2*y*z^2 + (w + 1)*z^3 + (w + 
	    1)*M2^3/M1^2*x*y + -2*M2/M1^3*y^2 + (1/2*(w + 1)*M1*M2^2 + 2*M1 + 
	    2*M2^3)/M1^2*x*z + (1/2*(3*w - 3)*M1^2*M2 - 2*w*M1*M2^2 + 1/2*(3*w + 
	    3)*M1*M2 - 2*M2^2)/M1^3*y*z + ((w - 1)*M1 + (2*w + 2))/M1*z^2 + (1/2*(w 
	    + 1)*M1*M2^2 + M1 + 2*M2^3)/M1^3*x + (1/2*(3*w - 3)*M1*M2 + (-5*w - 
	    3)*M2^2)/M1^3*y + ((2*w - 2)*M1 + (-w - 1)*M2 + (w + 1))/M1^2*z + 
	    (1/2*(w - 1)*M1*M2^2 + (w - 1)*M1 - 2*w*M2^3 + (-w - 1)*M2)/M1^3,
	x^2 + (w*M1 + 1/2*(w + 1)*M2^2 - 1)/(M1*M2)*x*y + 1/2*(w + 1)*x*z + (1/2*(-w
	    + 1)*M1^2 + w*M1*M2 + 1/2*(w + 1)*M1 - M2)/(M1^2*M2)*y*z + (1/2*(-w + 
	    1)*M1 + 1/2*(w + 1))/M2^2*z^2 + (-w*M1*M2 + 1/2*(w + 1)*M1 + M2)/M1^2*x 
	    + (1/2*(w + 1)*M1^2 - M1*M2 + 1/2*(w - 1)*M1 + (-2*w - 
	    1)*M2)/(M1^2*M2)*y + (1/2*(w + 1)*M1^2 + 1/2*(w + 1))/(M1*M2^2)*z + 
	    (1/2*(w + 1)*M1^2 + 1/2*(w - 1)*M1 - w*M2^3)/(M1^2*M2^2)
    ];
end procedure;

time test_gb_anf(11863249*11863213, 11863253*11863213);

p := 11863279;
M1 := p;
p := PreviousPrime(p);
M2 := p;
for i := 1 to 10 do
    p := PreviousPrime(p);
    M1 *:= p;
    if Random(0, 1) eq 1 then
	M2 *:= p;
    end if;
end for;
time test_gb_anf(M1, M2);

h := 11863279;
l := h div 2;
M1 := h;
M2 := 1;
for i := 1 to 30 do
    p := PreviousPrime(Random(l, h));
    M1 *:= p;
    if Random(0, 1) eq 1 then
	M2 *:= p;
    end if;
end for;
time test_gb_anf(M1, M2);

/// Aug 2016

r5<t1,t2,t3,t4,t5> := PolynomialRing(Rationals(),5);
rr<u> := PolynomialRing(r5);
para := [1, u, t4 + t5 * u];
quad := t3 * (u^2 + t1 * u + t2)^2;
r3<x,y,z> := PolynomialRing(Rationals(),3);
tr := 8*x^4 + 113*x^3*y - 80*x^3*z + 906*x^2*y^2 - 531*x^2*y*z +
328*x^2*z^2 + 3257*x*y^3
- 2818*x*y^2*z + 539*x*y*z^2 - 640*x*z^3 + 4008*y^4 - 5859*y^3*z +
1220*y^2*z^2 + 287*y*z^3 + 512*z^4;
time for i := 1 to 3 do
    id := ideal<r5 | Coefficients(Evaluate(tr,para) - quad)>;
    "Elim test", i;
    time elim := EliminationIdeal(id, {r5.5});
    B := Basis(elim);
    assert #B eq 1;
    L := Factorization(B[1]);
    assert L eq [
	<t5 - 7/8, 1>,
	<t5^2 - 50/161*t5 - 25/23, 1>,
	<t5^2 - 436/289*t5 + 164/289, 1>,
	<t5^6 - 1568087/4981464*t5^5 + 57038977/119555136*t5^4 - 
	    896309167/59777568*t5^3 + 1206130591/39851712*t5^2 - 
	    500148623/29888784*t5 + 872009/29888784, 1>,
	<t5^8 + 930968578/17574047*t5^7 + 207655519/17574047*t5^6 - 
	    9453675842/17574047*t5^5 + 18705217732/17574047*t5^4 - 
	    20230371962/17574047*t5^3 + 14958506767/17574047*t5^2 - 
	    5071510022/17574047*t5 - 255167521/17574047, 1>,
	<t5^8 - 2563955546/249124197*t5^7 + 27204542591/747372591*t5^6 - 
	    10990454150/249124197*t5^5 - 26748918268/747372591*t5^4 + 
	    40467448370/249124197*t5^3 - 141902932753/747372591*t5^2 + 
	    3628499362/35589171*t5 - 16246862833/747372591, 1>
    ];
end for;


r3<x,y,z> := PolynomialRing(Rationals(),3);
r5<t1,t2,t3,t4,t5> := PolynomialRing(Rationals(),5);
rr<u> := PolynomialRing(r5);
para := [1, u, t4 + t5 * u];
quad := t3 * (u^2 + t1 * u + t2)^2;
f4 := 2*x^4 - 4*x^3*y - 6*x^3*z + 12*x^2*y^2 + 24*x^2*y*z + 10*x^2*z^2 +
2*x*y^3 -
     18*x*y^2*z - 28*x*y*z^2 - 6*x*z^3 + y^4 + 8*y^2*z^2 + 6*y*z^3 + 2*z^4;
subs := [x,y,z];
SetSeed(836398180, 257963728);
tr := Evaluate(f4,subs);
id := ideal<r5 | Coefficients(Evaluate(tr,para) - quad)>;
elim := EliminationIdeal(id, {r5.5});
time for i := 1 to 3 do
    id := ideal<r5 | Coefficients(Evaluate(tr,para) - quad)>;
    "Elim test2", i;
    time elim := EliminationIdeal(id, {r5.5});
    B := Basis(elim);
    assert #B eq 1;
    L := Factorization(B[1]);
    assert L eq [
	<t5^4 - t5^2 + 1, 1>,
	<t5^8 + 222/11*t5^7 + 17403/121*t5^6 + 3552/11*t5^5 + 27556/121*t5^4 - 
	    114/11*t5^3 + 1950/121*t5^2 + 1374/121*t5 + 193/121, 1>,
	<t5^8 + 70/3*t5^7 + 502/3*t5^6 + 9430/27*t5^5 + 67171/324*t5^4 -
	    949/27*t5^3 + 4135/108*t5^2 - 65/9*t5 + 223/36, 1>,
	<t5^8 + 626/57*t5^7 + 1825/171*t5^6 - 680/171*t5^5 - 1388/513*t5^4 + 
	    98/9*t5^3 + 1414/171*t5^2 + 58/57*t5 - 11/57, 1>
    ];
end for;

// 

R<x12, x2, x1> := PolynomialRing(IntegerRing(), 3);
I := Ideal(
    [  x2 + 1, x2 + x1, 29599 ]
);
B := Basis(I);
G := GroebnerBasis(B: SUnit := &*[2], Homogenize := false);
assert G eq [ x2 + 1, x1 + 29598, 29599 ];


// [Bug 109]
Q:=RationalField();
P<u, f, e, d, c, b, a>:=PolynomialRing(Q, 7, "grevlex");
I:=ideal<P | 1 - 2*a^2 - 2*b^2 + 2*a*b^2 + a^2*b^2 + 2*a^2*c +
2*a*b*c - 2*a^2*b*c - 2*a*b^2*c - 2*c^2 + a^2*c^2 + 2*b*c^2 - 2*a*b*c^2 +
b^2*c^2, 1 - 2*a^2 + 2*a^2*c - 2*c^2 + c^4 + 4*a*c*d - 4*a*c^2*d - 2*d^2 +
2*c*d^2, a*c - a*b^2*c - a*c^2 - b*c^2 + a*b*c^2 + b^2*c^2 - d + 2*b^2*d
- a*b^2*d + c*d - b*c*d + a*b*c*d - b^2*c*d - a*b*e + a^2*b*e + c*e -
a^2*c*e - b*c*e + a*b*c*e, a^2 - a^2*b^2 - 2*a^2*c - a*b*c + a^2*b*c +
2*a*b^2*c + c^2 - b*c^2 + a*b*c^2 - b^2*c^2 - e + a^2*e - a*b*e + a^2*b*e
+ 2*b^2*e - 2*a*b^2*e + c*e - a^2*c*e - b*c*e + a*b*c*e, -(b*c) + a*b*c +
a*c^2 - a*b*c^2 - a*c^3 + b*c^3 - b*d + 2*a*b*d - c*d + b*c*d - 2*a*b*c*d
+ c^2*d + e - 2*a^2*e + 2*a^2*c*e - c^2*e, -(a*b) + a^2*b + a^2*c -
b*c + a*b*c - 2*a^2*c^2 + c^3 + e - 2*a^2*e + a*b*e - a^2*b*e - c*e +
3*a^2*c*e + b*c*e - a*b*c*e - c^2*e, -(a*c) + b^2*c + a*c^2 - a*b*c^2 +
a*c^3 - b*c^3 + d - b^2*d - a*b^2*d - c*d + b*c*d + a*b*c*d + b^2*c*d -
c^2*d - b*e + a*b*e + a^2*b*e - a^2*c*e - a*b*c*e + c^2*e, -a^2 + a*b^2
+ c - a^2*b*c - b^2*c + a^2*c^2 + a*b*c^2 - c^3 + a^2*e - b*e + a*b*e +
a^2*b*e - a*b^2*e - c*e - a^2*c*e + b*c*e - 2*a*b*c*e + b^2*c*e + c^2*e,
c - b^2*c - 2*c^3 + 2*b*c^3 - 2*a*d + b^2*d + a*b^2*d + 2*a*c*d + b*c*d -
2*a*b*c*d - b^2*c*d + a*c^2*d - b*c^2*d - b*e + a*b*e + a*c*e - a*b*c*e -
a*c^2*e + b*c^2*e, a - a*b^2 - a*c + b^2*c - a*c^2 + a*b*c^2 + a*c^3 -
b*c^3 - a*e - b*e + a*b*e + a*b^2*e + 2*a*c*e + b*c*e - 2*a*b*c*e -
b^2*c*e - a*c^2*e + b*c^2*e, -b + a*b + a^2*b - a^2*c - a*b*c + c^2 +
b*c^2 - c^4 - b*c*d - 2*a*b*c*d + 2*a*c^2*d + b*c^2*d + b*d^2 - c*d^2 -
a*c*e + a*c^2*e + d*e - c*d*e, -(a*b) + a^2*b + c - a^2*c - b*c + a*b*c -
c^3 + b*c^3 + b*c*d - 2*a*b*c*d + 2*a*c^2*d - b*c^2*d + b*d^2 - c*d^2 +
a*c*e - a*c^2*e - d*e + c*d*e, -b + a*b + a*c - a*b*c - a*c^2 + b*c^2 -
b*c*d - c^2*d + b*c^2*d + c^3*d + 2*b*d^2 - 2*b*c*d^2 + c*e - c^3*e -
2*a*d*e + 2*a*c*d*e, 1 - 2*a^2 - 2*b^2 + 2*a*b^2 + a^2*b^2 + 2*a^2*c -
2*a*b^2*c - c^2 + b^2*c^2 + 2*a*b*e - 2*a^2*b*e + 2*b*c*e - 2*a*b*c*e -
e^2 + a^2*e^2, -(a*b) + a^2*b + c - 2*a^2*c - b*c + a*b*c + 2*a^2*c^2 -
c^3 + a^2*e + a*b*e - a^2*b*e - 2*a^2*c*e + b*c*e - a*b*c*e + c^2*e -
e^2 + a^2*e^2, 1 - 3*a^2 + 4*a^2*c - 2*c^2 + 2*a^2*e - 4*a^2*c*e +
2*c^2*e - e^2 + a^2*e^2, 1 - a^2 - 2*b^2 + 2*a*b^2 - c^2 + b^2*c^2 +
2*a*c*d - 2*b^2*c*d - d^2 + b^2*d^2 + 2*b*c*e - 2*a*b*c*e + 2*b*d*e -
2*a*b*d*e - e^2 + a^2*e^2, a*b*c^2 - b*c^3 - a*b*d - b*c*d + 2*a*b*c*d
+ a^2*e - a*c*e - 2*a^2*c*e + b*c*e - a*b*c*e + c^2*e + a*c^2*e + d*e
+ b*d*e - a*b*d*e - c*d*e - e^2 + a^2*e^2, 1 - a^2 - b^2 + 2*a*b*c -
2*c^2 + c^4 + 2*a*c*d - 2*b*c^2*d - d^2 + b^2*d^2 + 2*b*c*e - 2*a*c^2*e
- 2*a*b*d*e + 2*c*d*e - e^2 + a^2*e^2, a*c^2 - b*c^3 - a*b^2*d - c*d +
a*b*c*d + b^2*c*d + a^2*b*e - a*c*e - a^2*c*e - 2*a*b*c*e + b^2*c*e +
c^2*e + a*c^2*e + d*e + b*d*e - b^2*d*e - c*d*e - b*e^2 + a*b*e^2, -c^3
+ b*c^3 - a*d + a*b^2*d + 2*a*c*d - a*b*c*d - b^2*c*d + c*e + a*c*e
- a*b*c*e - b^2*c*e - c^2*e - a*c^2*e + 2*b*c^2*e - a*d*e + b*d*e -
a*b*d*e + b^2*d*e + a*c*d*e - b*c*d*e - b*e^2 + a*b*e^2, -a^2 + a*b^2 +
c - b*c + a*b*c - b^2*c - c^3 + b*c^3 + 2*a*c*d - b^2*c*d - b*c^2*d -
d^2 + b^2*d^2 - a*b*c*e - a*c^2*e + 2*b*c^2*e + b*d*e - 2*a*b*d*e +
c*d*e + a^2*e^2 - c*e^2, -(b*c^2) + a*c^3 + a*b*d - c^2*d - a^2*e + c*e +
a*c*e - 2*a*c^2*e + b*c^2*e - d*e - a*b*d*e + 2*c*d*e + a^2*e^2 - c*e^2,
-b + a*b + 2*a^2*b - a^2*c - 2*a*b*c + c^2 - a^2*e - 2*a^2*b*e + c*e +
a^2*c*e + 2*a*b*c*e - c^2*e + a^2*e^2 + b*e^2 - a*b*e^2 - c*e^2, -a^2 +
a*b^2 + c - b*c + a*b*c + a^2*b*c - b^2*c - a*b*c^2 + a^2*e - a^2*b*e -
a*b^2*e - c*e - a^2*c*e + a*b*c*e + b^2*c*e + c^2*e + a^2*e^2 + b*e^2 -
a*b*e^2 - c*e^2, 1 - 2*a^2 - b^2 + 2*a*b*c - c^2 + a^2*c^2 + 2*a^2*e +
2*a*b*e - 2*a^2*c*e - 2*a*b*c*e - 2*e^2 + a^2*e^2 - 2*a*b*e^2 + b^2*e^2 +
2*c*e^2, -a + b^2 + a*c - a*b*c + a*c^2 - b*c^2 + c*d - c^2*d + b*c^2*d -
c^3*d - b^2*d^2 + b*c*d^2 - b*c*e + c^3*e - b*d*e + a*b*d*e - a*c*d*e +
b*c*d*e + a*e^2 - a*c*e^2, b*c^2 - c^4 - a*b*d - a*c*d + 2*a*c^2*d + a*e -
c*e - a*c*e + c^2*e - b*c^2*e + c^3*e + 2*a*d*e + a*b*d*e - 3*a*c*d*e -
a*e^2 + a*c*e^2, a - a*b^2 - a*c - b*c + a*b*c + b^2*c - c*d + b^2*c*d +
c^2*d - b*c^2*d - b^2*d^2 + b*c*d^2 + c^2*e - b*c^2*e + b*d*e + a*b*d*e -
a*c*d*e - b*c*d*e - a*e^2 + a*c*e^2, -b + a*b + a*c - 2*a*b*c - a*c^2 +
2*b*c^2 + a*e - 2*a*c*e + 2*a*b*c*e + a*c^2*e - 2*b*c^2*e - a*e^2 + b*e^2
- a*b*e^2 + a*c*e^2, a - a*b^2 - a*c - b*c + a*b*c + b^2*c - a*b*c^2 +
b*c^3 - a*e + a*b^2*e + 2*a*c*e + a*b*c*e - b^2*c*e - a*c^2*e - b*c^2*e -
a*e^2 + b*e^2 - a*b*e^2 + a*c*e^2, c^2 - c^4 - a*d - a*b*c*d + a*c^2*d +
b*c^2*d + a*b*e - 2*b*c*e - c^2*e + b*c^2*e + c^3*e + a*d*e + b*d*e +
a*b*d*e - a*c*d*e - 2*b*c*d*e - a*b*e^2 + b*c*e^2, -a + b^2 + 2*a*c -
a*b*c + a*c^2 - b*c^2 - a*c^3 - a*b*e - 2*a*c*e - b*c*e + a*b*c*e +
2*a*c^2*e + b*c^2*e + 2*a*e^2 + a*b*e^2 - b^2*e^2 - 3*a*c*e^2 + b*c*e^2,
1 - b^2 - 2*c^2 + 2*b*c^2 - 2*d^2 + b^2*d^2 + 2*c*d^2 - 2*b*c*d^2 +
c^2*d^2 + 2*b*d*e + 2*c*d*e - 2*b*c*d*e - 2*c^2*d*e - e^2 + c^2*e^2, 1 -
b^2 - 3*c^2 + 2*b*c^2 + c^4 + 2*b*c*e + 2*c^2*e - 2*b*c^2*e - 2*c^3*e -
2*e^2 + b^2*e^2 + 2*c*e^2 - 2*b*c*e^2 + c^2*e^2, -(a*c) + a*b^2*c + a*c^2
- b^2*c^2 + a^2*d - a*b^2*d - c*d + b^2*c*d + a*b*e - a^2*b*e + 2*b*c*e
- 2*a*b*c*e + b*d*e - a*b*d*e - e^2 + a^2*e^2 + f - a^2*f - 2*b^2*f +
2*a*b^2*f, -(a*c^2) + a*c^3 + a*b*d - a^2*b*d + a^2*c*d + b*c*d - a*b*c*d
- c^2*d + a*c*e - a*c^2*e - d*e + c*d*e - a*b*f + a^2*b*f + c*f - a^2*c*f
- b*c*f + a*b*c*f, -(a*c^2) + a*b*c^2 + a*c^3 - b*c^3 - a^2*b*d + a^2*c*d
+ a*b*c*d - c^2*d + a^2*e - 2*a^2*c*e + b*c*e - a*b*c*e + c^2*e + b*d*e
- a*b*d*e - e^2 + a^2*e^2 - a*b*f + a^2*b*f + c*f - a^2*c*f - b*c*f +
a*b*c*f, a*b*c^2 - b*c^3 + a^2*d - a*b^2*d - c*d + b^2*c*d - a^2*c*e -
a*b*c*e + c^2*e + b*c^2*e + b*d*e - a*b*d*e + a^2*e^2 - c*e^2 - a^2*f +
a*b^2*f + c*f - b*c*f + a*b*c*f - b^2*c*f, -(a*c^2) + a*b*c^2 + a^2*d -
a*b*c*d - a^2*b*e + a*c*e + a*b*c*e - b^2*c*e - a*c^2*e + b*c^2*e - d*e -
a*b*d*e + b^2*d*e + c*d*e + a^2*e^2 + b*e^2 - a*b*e^2 - c*e^2 - a^2*f +
a*b^2*f + c*f - b*c*f + a*b*c*f - b^2*c*f, -(a*d) + a*b^2*d + a*c*d -
a*b*c*d - b^2*c*d + b*c^2*d + a*c*e - a*c^2*e + b*d*e - b*c*d*e - a*e^2 +
a*c*e^2 + a*f - a*b^2*f - a*c*f - b*c*f + a*b*c*f + b^2*c*f, c^3 - b*c^3 -
a*c*d + b*c^2*d - c*e + a*b*c*e + b^2*c*e + c^2*e - 2*b*c^2*e + a*d*e +
a*b*d*e - b^2*d*e - a*c*d*e - a*e^2 + b*e^2 - a*b*e^2 + a*c*e^2 + a*f -
a*b^2*f - a*c*f - b*c*f + a*b*c*f + b^2*c*f, -(a*c) + a*c^2 + 2*a^2*d -
c*d - 2*a^2*c*d + c^2*d + a*c*e - a*c^2*e - d*e + c*d*e + f - 2*a^2*f +
2*a^2*c*f - c^2*f, -(a*c) + a*c^3 + a^2*d - a*b*c*d + a*b*e + a*c*e -
a^2*c*e + b*c*e - a*c^2*e - b*c^2*e - d*e - a*b*d*e + b^2*d*e + c*d*e -
e^2 + a^2*e^2 - a*b*e^2 + c*e^2 + f - a^2*f - b^2*f + 2*a*b*c*f - c^2*f,
a*b*c - a*c^3 - a*b*d - a^2*b*d + a^2*c*d + a*b*c*d - a*c*e - a*b*c*e +
2*a*c^2*e + d*e + b*d*e - 2*c*d*e - b*f + a*b*f + a^2*b*f - a^2*c*f -
a*b*c*f + c^2*f, a*b*c - b*c^2 - a^2*b*d + a^2*c*d + a*b*c*d - c^2*d -
a^2*e + c*e - a*b*c*e + b*c^2*e + b*d*e - a*b*d*e + a^2*e^2 - c*e^2 -
b*f + a*b*f + a^2*b*f - a^2*c*f - a*b*c*f + c^2*f, a*d - a*c*d + a*b*c*d -
a*c^2*d - a*b*e + a*c^2*e - b*d*e - b^2*d*e + 2*b*c*d*e + a*e^2 + a*b*e^2
- 2*a*c*e^2 - a*f + b^2*f + a*c*f - a*b*c*f + a*c^2*f - b*c^2*f, c^2 -
c^4 - a*c*d + b*c^2*d - 2*b*c*e - c^2*e + a*c^2*e + b*c^2*e + c^3*e +
a*d*e + a*b*d*e - b^2*d*e - a*c*d*e + a*e^2 - 2*a*c*e^2 + b*c*e^2 -
a*f + b^2*f + a*c*f - a*b*c*f + a*c^2*f - b*c^2*f, -(b*c^2) + c^4 +
b*c*d + a*b*c*d - a*c^2*d - b*c^2*d + c*e - c^2*e + b*c^2*e - c^3*e -
2*a*d*e + b*d*e - a*b*d*e + 3*a*c*d*e - b*c*d*e - b*f + a*b*f + a*c*f
- a*b*c*f - a*c^2*f + b*c^2*f, -(a*b*d) - a*c*d + b*c*d + a*b*c*d +
a*c^2*d - b*c^2*d + a*e - a*c*e + b*d*e - b*c*d*e - a*e^2 + a*c*e^2 -
b*f + a*b*f + a*c*f - a*b*c*f - a*c^2*f + b*c^2*f, -(c*d) + c^2*d -
b*c^2*d + c^3*d + b*c*e - c^3*e - d*e + b*d*e + b^2*d*e + 2*c*d*e -
2*b*c*d*e - c^2*d*e - e^2 + c*e^2 - b*c*e^2 + c^2*e^2 + f - b^2*f -
2*c^2*f + 2*b*c^2*f, a^2 - 2*a^2*c + c^2 - c^4 - a*c*d + 3*a*c^2*d -
c*d^2 - e + a^2*e + c^2*e - 2*a*c*d*e + d^2*e - a*c*f + a*c^2*f + d*f -
c*d*f, a^2*b - a^2*c - a*b*c + c^2 - c^4 - a*b*c*d + a*c^2*d + b*c^2*d -
b*e + a*b*e + a*c^2*e + b*c^2*e - c*d*e - b*c*d*e - a*c*e^2 + d*e^2 -
a*b*c*f + a*c^2*f + b*d*f - c*d*f, -a^2 + c - c^3 + a*c*d + a*c^2*d -
c*d^2 + a^2*e - c*e + c^3*e - 2*a*c*d*e + d^2*e + a*c*f - a*c^2*f - d*f
+ c*d*f, a - a*c + c^2*d - c^3*d - 2*a*d^2 + 2*a*c*d^2 - a*e + a*c*e +
c*d*e - c^2*d*e - c*f + c^3*f + 2*a*d*f - 2*a*c*d*f, a*b - b*c - c^3*d -
a*b*d^2 + a*c*d^2 + b*c*d^2 - a*b*e + b*c*e + c^2*e - a*d*e + b*c*d*e -
b*d^2*e - c^2*e^2 + a*d*e^2 - b*c*f + c^3*f + b*d*f + a*b*d*f - a*c*d*f
- b*c*d*f, a*c - a*b*c - a*c^2 + b*c^2 + c^3*d + a*b*d^2 - a*c*d^2 -
b*c*d^2 - b*e + a*b*e - c^3*e - a*d*e + 2*a*c*d*e - b*c*d*e + b*d^2*e +
c*e^2 - a*d*e^2 - c^2*f + b*c^2*f + b*d*f - a*b*d*f + a*c*d*f - b*c*d*f,
a*b - a^2*b + b*c - a*b*c - b*c^3 + a*b*c*d + b*c^2*d - a*b*d^2 - e +
a^2*e - a*c*e + c^2*e + a*c^2*e + a^2*d*e - c*d*e - 2*a*c*d*e + d^2*e -
b*c*f + a*b*c*f - b*d*f + a*b*d*f + e*f - a^2*e*f, a*b - a^2*c + a*c^2*d
- a*b*d^2 - e + a^2*e - a*b*e + c*e - a*c*e + c^2*e - c^3*e + a^2*d*e
- 2*a*c*d*e + b*c*d*e + d^2*e + a*c*e^2 - d*e^2 - b*c*f + a*c^2*f +
a*b*d*f - c*d*f + e*f - a^2*e*f, -(b*c) + a*b*c - a*b*c^2 + b*c^3 -
b*d + a*b*d + a^2*b*d - a*b*c*d + e - 2*a^2*e + a*c*e + 2*a^2*c*e -
c^2*e - a*c^2*e - a^2*d*e + c*d*e + a*b*f - a^2*b*f + b*c*f - a*b*c*f -
e*f + a^2*e*f, -(a*c) + a*c^2 + d - a^2*d - c*d + 2*a^2*c*d - c^2*d +
a*c*e - a*c^2*e - a^2*d*e + c*d*e + a^2*f - 2*a^2*c*f + c^2*f - e*f +
a^2*e*f, a - a*b^2 - a*c + b^2*c + c^2*d - a*d^2 - b*e + a*b*e - b*c^2*e
+ 2*a*b*d*e - 2*b*c*d*e + b*d^2*e - a*e^2 + c*e^2 + a*c*e^2 - a*d*e^2 -
c*f + b^2*c*f + a*d*f - b^2*d*f + b*e*f - a*b*e*f, -a^2 + a*b^2 + c -
b^2*c - c^3 + a*c*d - b*e + a*b*e - a*b*c*e + 3*b*c^2*e - a*b*d*e -
b*c*d*e + a^2*e^2 - c*e^2 - a*c*e^2 + d*e^2 + a*c*f - b^2*c*f - d*f +
b^2*d*f + b*e*f - a*b*e*f, a*c - b^2*c - a*c^2 - d + a^2*d + b^2*d +
b*e - a*b*e - a^2*b*e + 2*a*b*c*e + b*c^2*e - 2*a*b*d*e + a^2*e^2 -
c*e^2 - a*c*e^2 + d*e^2 - a^2*f + a*b^2*f + c*f - b^2*c*f - b*e*f +
a*b*e*f, -c + b^2*c + c^3 + a*d - b^2*d - a*c*d + b*e - a*b*e + a*b*c*e -
3*b*c^2*e + a*b*d*e + b*c*d*e - a*e^2 + c*e^2 + a*c*e^2 - a*d*e^2 + a*f -
a*b^2*f - a*c*f + b^2*c*f - b*e*f + a*b*e*f, -(a*b*c) + a*c^2 - a*c^3 +
b*d - a^2*b*d - c*d + a^2*c*d + a*b*c*d + a*b*c*e + a*c^2*e - a*b*d*e -
c*d*e - a*c*e^2 + d*e^2 + a^2*b*f - a^2*c*f - a*b*c*f + c^2*f - b*e*f +
a*b*e*f, -c^2 + b*c^2 + c^4 + b*d - a*b*d + a*c*d - b*c*d + a*b*c*d -
a*c^2*d - b*c^2*d - b*c^2*e - c^3*e - a*d*e + 2*a*c*d*e + b*c*d*e +
c*e^2 - a*d*e^2 + a*c*f - a*b*c*f - a*c^2*f + b*c^2*f - b*e*f + a*b*e*f,
a*c - 2*a*c^2 - d + a^2*d + c*d + 2*a*c^2*e - a^2*d*e - c*d*e - a*c*e^2 +
d*e^2 - a^2*f + c*f + a^2*e*f - c*e*f, a*c^2 - b*c^2 - a*c^3 + a*b*d -
c*d + a^2*c*d - a^2*e + c*e + a*c^2*e + b*c^2*e - a^2*d*e - a*b*d*e +
a^2*e^2 - c*e^2 - a*c*e^2 + d*e^2 - a^2*c*f + c^2*f + a^2*e*f - c*e*f,
a*b*c - b*c^2 - b*d + a*b*d + a^2*b*d - a*b*c*d - a^2*e + c*e - a*b*c*e
+ b*c^2*e - a^2*d*e + c*d*e + a^2*e^2 - c*e^2 - a^2*b*f + a*b*c*f +
a^2*e*f + b*e*f - a*b*e*f - c*e*f, a*c - b*c^2 - d + a^2*d + b^2*d -
a*b*e + c*e - a*c*e - a^2*c*e + a*c^2*e - 2*a*b*d*e + a^2*e^2 + a*b*e^2
- a*c*e^2 + b*c*e^2 + d*e^2 - e^3 - a^2*f + a*b*c*f + e*f + a*b*e*f -
b^2*e*f - c*e*f, -(a^2*b) + a*b*c + a*b*c*d - a*b*d^2 + a^2*e + b*e -
a*b*e - c*e - a*c^2*e - b*c^2*e + c^3*e + a^2*d*e - 2*a*c*d*e + b*c*d*e +
d^2*e + a*c*e^2 - d*e^2 + a*b*c*f - b*c^2*f - b*d*f + a*b*d*f - a^2*e*f
+ c*e*f, -(a^2*c) + c^2 - c^4 + a*c^2*d + b*c^2*d - a*b*d^2 + a^2*e -
c*e + c^3*e + a^2*d*e - c*d*e - 2*a*c*d*e + d^2*e + a*c^2*f - b*c^2*f +
a*b*d*f - c*d*f - a^2*e*f + c*e*f, -a^2 + a*b*c + a*c*d + e + a*b*e -
b^2*e - c*e - c^2*e - a*c^2*e + c^3*e - a*b*d*e - b*c*d*e + a^2*e^2 -
a*c*e^2 + 2*b*c*e^2 + d*e^2 - e^3 + a*c*f - b*c^2*f - d*f + b^2*d*f -
a*b*e*f + c*e*f, -(b*c) + a*c^2 + a*b*d - c*d + a^2*c*d + e - 2*a^2*e +
a*c*e - a*c^2*e - a^2*d*e - a*b*d*e + 2*a^2*e^2 - a*c*e^2 + b*c*e^2 +
d*e^2 - e^3 + a*b*f - a^2*c*f - e*f + a^2*e*f - a*b*e*f + c*e*f, b*c -
c^3 - a*b*d + a*c*d - a*c^2*d - a*e + 2*a*c*e - c^2*e + c^3*e + a*b*d*e
+ a*c*d*e - 2*a*c*e^2 - b*c*e^2 + c^2*e^2 - a*d*e^2 + a*e^3 - a*b*f +
a*c^2*f + a*e*f + a*b*e*f - 2*a*c*e*f, a*c - a*c^2 - b*c^2*d + c^3*d +
a*b*d^2 - a*c*d^2 - a*e + a*c*e - a*d*e + c*d*e + a*c*d*e - c^2*d*e -
c^2*f + b*c^2*f - a*b*d*f + a*c*d*f + a*e*f - a*c*e*f, a*b*c - b*c^2 -
b*c^2*d + b*c*d^2 - a*e + b*e - a*b*e + a*c*e + c^3*e + c*d*e - a*c*d*e +
b*c*d*e - c^2*d*e - b*d^2*e - c*e^2 + a*d*e^2 - b*d*f + b*c*d*f + a*e*f
- a*c*e*f, a*c - b*c^2 - c^2*d - a*e - a*b*e + b^2*e + a*c*e + c^3*e +
c*d*e + 2*b*c*d*e - c^2*d*e - a*c*e^2 - b*c*e^2 - b*d*e^2 + a*e^3 -
b^2*d*f + b*c*d*f + a*b*e*f - a*c*e*f, -c + 2*c^3 + 2*a*d - 3*a*c*d -
2*c^3*e - a*d*e + 3*a*c*d*e + c*e^2 - a*d*e^2 + a*f - a*c*f - a*e*f +
a*c*e*f, -c^2 + b*c^2 + c^4 - a*b*d + a*c*d - a*c^2*d + a*e - a*c*e -
b*c^2*e - c^3*e + a*b*d*e + a*c*d*e - a*e^2 + c*e^2 + a*c*e^2 - a*d*e^2 +
a*c*f - a*c^2*f - a*e*f + a*c*e*f, -(a*b) + a*c^2 + a*b*d^2 - a*c*d^2 +
a*e + a*b*e - 2*a*c*e + a*d*e - c*d*e - a*c*d*e - b*c*d*e + 2*c^2*d*e +
b*c*f - c^3*f - a*b*d*f + a*c*d*f - a*e*f + a*c*e*f, -(b*c) + a*c^2 -
c^3*d + b*c*d^2 + a*e - 2*a*c*e + b*c*e + c^2*e - c*d*e - a*c*d*e +
2*c^2*d*e - b*d^2*e - c^2*e^2 + a*d*e^2 + b*d*f - b*c*d*f - a*e*f +
a*c*e*f, -a + a*c^2 + a*d^2 + a*e + b*e - a*c*e - b*c*e - c*d*e -
2*a*c*d*e + c^2*d*e - b*d^2*e + a*e^2 - c*e^2 + c^2*e^2 + a*d*e^2 +
b*d*e^2 - a*e^3 + c*f - c^3*f - a*d*f + b*c*d*f - b*e*f + a*c*e*f, -(b*d)
+ b*c*d - a*b*c*d + b*c^2*d + a*e - a*c*e + a*d*e + a*b*d*e - a*c*d*e -
b*c*d*e - a*e^2 + a*c*e^2 + a*b*c*f - b*c^2*f - a*e*f + b*e*f - a*b*e*f
+ a*c*e*f, -(b^2*d) - a*c*d + b*c*d + a*b*e - a*c*e + a*c^2*e + a*d*e +
a*b*d*e - a*c*d*e + b*c*d*e - a*b*e^2 - a*c*e^2 - b*d*e^2 + a*e^3 + a*c*f
- b*c^2*f - a*e*f - a*b*e*f + b^2*e*f + a*c*e*f, a*b*c - b*c^2 - c^2*d +
a*d^2 - a*b*e + b*c*e + c^3*e - a*b*d*e - a*c*d*e + 2*b*c*d*e - b*d^2*e -
c^2*e^2 + a*d*e^2 + c^2*f - b*c^2*f - a*d*f + b*c*d*f + a*b*e*f - b*c*e*f,
-c^2 + b*c^2 + c*d^2 + e - b^2*e - c*e + b*c*e - b*c*d*e - c^2*d*e - d^2*e
- b*d^2*e + c*d^2*e + c^2*e^2 + d*e^2 + 2*b*d*e^2 - c*d*e^2 - e^3 - d*f +
b^2*d*f + c*d*f - b*c*d*f + c*e*f - b*c*e*f, c - c^3 - a*d + b*c*d - b*e +
a*c*e + a*d*e - a*c*d*e + a*e^2 - 2*c*e^2 - a*c*e^2 + 2*c^2*e^2 + a*d*e^2
- b*d*e^2 - a*e^3 + b*e^3 - a*f + a*c^2*f + a*e*f + b*e*f - a*c*e*f -
b*c*e*f, -c^2 + b*c^2 + a*d - b^2*d - a*c*d - a*c*e + b*c*e + c^2*e +
a*c^2*e - c^3*e + a*b*d*e + b*c*d*e - a*c*e^2 - 2*b*c*e^2 + c^2*e^2 -
a*d*e^2 + a*e^3 + a*c*f - a*b*c*f - a*e*f + b^2*e*f + a*c*e*f - b*c*e*f,
-(b*c) + c^3 + b*d + a*b*d - a*c*d - b*c*d + c^2*e - c^3*e - a*d*e -
a*b*d*e + a*c*d*e + b*c*d*e + b*c*e^2 - c^2*e^2 + a*d*e^2 - b*d*e^2 +
a*b*f - b*c*f - a*b*e*f + b*c*e*f, c^2 - b*c^2 - a*d + b*c*d + a*b*e -
b*c*e - c^2*e + c^3*e + a*d*e - a*c*d*e - a*b*e^2 + 2*b*c*e^2 - c^2*e^2 +
a*d*e^2 - b*d*e^2 + a*b*c*f - b*c^2*f - a*b*e*f + b*c*e*f, -d + b^2*d +
c*d - b*c*d + c^2*d + c*e - b*c*e - c^3*e - c*d*e - 2*b*c*d*e + c^2*d*e +
b*c*e^2 + c^2*e^2 + d*e^2 + b*d*e^2 - c*d*e^2 - e^3 - c^2*f + b*c^2*f +
e*f - b^2*e*f - c*e*f + b*c*e*f, b*d - b*c*d - a*c^2*d - a*e + 2*a*c*e
- a*d*e + 2*a*c*d*e + b*c*d*e - 2*a*c*e^2 - b*d*e^2 + a*e^3 - b*c*f +
a*c^2*f + a*e*f - 2*a*c*e*f + b*c*e*f, a*c - a*b*c - a*d^2 - a*e + b^2*e +
a*c*e - b*c*e + a*b*d*e + c*d*e + a*c*d*e - c^2*d*e + b*d^2*e - a*c*e^2 -
b*c*e^2 + c^2*e^2 - a*d*e^2 - b*d*e^2 + a*e^3 - c^2*f + b*c^2*f + a*d*f -
b^2*d*f - a*c*e*f + b*c*e*f, b*c - c^3 - b*c*d^2 + c^2*d^2 - e + c*e -
b*c*e + c^2*e - c*d*e + c^2*d*e + 2*d^2*e + b*d^2*e - 3*c*d^2*e - d*e^2 +
c*d*e^2 - b*d*f - c*d*f + b*c*d*f + c^2*d*f + e*f - c^2*e*f, c - c^3 -
c*d^2 - b*e - c*e + b*c*e + c^2*e + 2*c^2*d*e + d^2*e + b*d^2*e - c*d^2*e
- c*e^2 + d*e^2 - b*d*e^2 - 2*c*d*e^2 + c*e^3 - d*f + c*d*f - b*c*d*f +
c^2*d*f + b*e*f - c^2*e*f, -(b*d) - c*d + b*c*d + c^2*d + c^3*d + e -
2*c^2*e + c*d*e - b*c*d*e - 2*c^2*d*e + 2*c^2*e^2 + d*e^2 + b*d*e^2 -
c*d*e^2 - e^3 + b*c*f - c^3*f - e*f + c*e*f - b*c*e*f + c^2*e*f, -d +
c*d - b*c*d + c^2*d + b*e - c^2*e - c*d*e + c^2*d*e - c*e^2 + c^2*e^2 +
2*d*e^2 + b*d*e^2 - 3*c*d*e^2 - b*e^3 + c*e^3 + c*f - c^3*f - b*e*f -
c*e*f + b*c*e*f + c^2*e*f, 1 - 2*a^2 + 2*a^2*c - 2*c^2 + c^4 + 2*a*c*d -
2*a*c^2*d - d^2 + a^2*d^2 + 2*a*c*f - 2*a*c^2*f - 2*a^2*d*f + 2*c*d*f -
f^2 + a^2*f^2, 1 - 2*a^2 - c^2 + 2*a*c*d - d^2 + a^2*d^2 + 2*a^2*e -
2*a*c*d*e - e^2 + c^2*e^2 + 2*a*c*f - 2*a^2*d*f - 2*a*c*e*f + 2*d*e*f -
f^2 + a^2*f^2, b - a*b - a^2*b + a*b*c - b*c^2 + b*c*d + a^2*e - c*e +
a*c*e - a*c^2*e + c^3*e - d*e + a^2*d*e - a*c*d*e + 2*a*b*c*f - b*c^2*f -
a*b*d*f - a^2*e*f + c*e*f - a*c*e*f + d*e*f - b*f^2 + a*b*f^2, b - a*b
+ a*b*c - b*c^2 + b*c*d - b*d^2 - a*e - c*e + a*c*e + c^3*e + a*d*e -
a*c*d*e - c^2*d*e + a*d^2*e - b*c^2*f - a*b*d*f + 2*b*c*d*f + a*e*f +
c*e*f - a*c*e*f - a*d*e*f - b*f^2 + a*b*f^2, -a + b^2 + a*c + c*d - a*b*e
- 2*b*c*e - b*d*e + 2*a*e^2 - a*c*e^2 + c^2*e^2 + a*d*e^2 - c*d*e^2 -
c^2*f - a*d*f + a*b*e*f + 2*b*c*e*f + b*d*e*f - 2*a*e^2*f + a*f^2 -
b^2*f^2, 1 - a^2 - b^2 - c^2 + 2*a*b*e + 2*b*c*e - 2*e^2 + a^2*e^2 -
2*a*c*e^2 + c^2*e^2 + 2*a*c*f - 2*a*b*e*f - 2*b*c*e*f + 2*e^2*f - f^2 +
b^2*f^2, 1 - b^2 - c^2 - d^2 + 2*b*c*e + 2*b*d*e - 2*e^2 + c^2*e^2 -
2*c*d*e^2 + d^2*e^2 + 2*c*d*f - 2*b*c*e*f - 2*b*d*e*f + 2*e^2*f - f^2 +
b^2*f^2, -a^2 + c - c^3 + 2*a*c*d - a*c^2*d - d^2 + a^2*d^2 + a^2*e -
c*e + c^3*e - a*c*d*e + a*c^2*f - 2*a^2*d*f + c*d*f - a*c*e*f + d*e*f
+ a^2*f^2 - c*f^2, -(a*b) + c - a^2*c - c^3 + b*c*d + a^2*e + a*b*e +
a*c*e - d*e + a^2*d*e - a*c*d*e - e^2 - a*c*e^2 + c^2*e^2 + 2*a*c^2*f -
a*b*d*f - a^2*e*f - a*c*e*f - b*c*e*f + d*e*f + e^2*f + a*b*f^2 - c*f^2,
-a + 2*a*c + c*d - c^2*d - a*c*d^2 - 2*a*c*e + c^2*d*e + a*d^2*e + a*e^2 -
c*d*e^2 - c^2*f - a*d*f + 2*a*c*d*f + c^2*e*f - a*d*e*f + a*f^2 - a*c*f^2,
-b + a*c - c^2*d + b*d^2 + a*e + c*e - a*c*e - a*d*e - a*d^2*e - a*e^2 +
b*e^2 + a*d*e^2 + c*d*e^2 - c*e^3 + a*c*d*f - a*e*f - c*e*f + c^2*e*f +
a*d*e*f - 2*b*d*e*f + a*e^2*f + b*f^2 - a*c*f^2, a - a*c - c*d + c^2*d
+ c^3*d - a*c*d^2 - a*e + a*c*e - c^2*d*e + a*d^2*e - c^3*f + a*d*f +
c*e*f - a*d*e*f - a*f^2 + a*c*f^2, a*b - a*c + a*c^2 - b*c*d + c^2*d -
a*b*e - a*c*e - a*c*d*e + c^2*d*e + a*e^2 + a*d*e^2 - c*d*e^2 - c^3*f +
a*b*d*f - a*c*d*f + a*c*e*f + b*c*e*f - a*e^2*f - a*b*f^2 + a*c*f^2, -c
+ b*c + d^2 + c*e - c^2*e - b*d*e - c*d*e - b*e^2 + 2*c*e^2 + c*d*e^2 -
d^2*e^2 - c*e^3 + d*e^3 - c*d*f + c^2*e*f - d*e*f + b*d*e*f + c*d*e*f +
b*e^2*f - 2*c*e^2*f + c*f^2 - b*c*f^2, -(a*c) + b*c + a*c^2 + c^2*d -
b*d^2 - a*c*e - b*c*e - c^2*e + a*d*e - a*c*d*e + a*d^2*e + a*e^2 +
c^2*e^2 - c*d*e^2 - c^3*f - a*c*d*f + b*c*d*f + a*c*e*f + c^2*e*f -
a*d*e*f + b*d*e*f - a*e^2*f + a*c*f^2 - b*c*f^2, a - b*c - c*d - a*e +
a*c*e + c^2*e + b*d*e - 2*a*e^2 + b*e^2 - a*d*e^2 + c*d*e^2 + a*e^3 -
c*e^3 + a*d*f + c*e*f - a*c*e*f - c^2*e*f - b*d*e*f + 2*a*e^2*f - b*e^2*f
- a*f^2 + b*c*f^2, a*b - b*c - b*c*d + b*d^2 - a*b*e + b*c*e + c^2*e -
a*d*e + c^2*d*e - a*d^2*e - c^2*e^2 + a*d*e^2 + a*b*d*f - b*c*d*f +
b*c*e*f - c^2*e*f + a*d*e*f - b*d*e*f - a*b*f^2 + b*c*f^2, c - b*c -
c^3 + b*d^2 - c*d^2 + b*c*e + c^2*e - d*e + c*d*e + c^2*d*e - c*d^2*e
- e^2 - c*d*e^2 + d^2*e^2 - b*c*d*f + 2*c^2*d*f - c^2*e*f + d*e*f -
b*d*e*f - c*d*e*f + e^2*f - c*f^2 + b*c*f^2, 1 - 2*c^2 - 2*d^2 + 2*c*d^2
+ c^2*d^2 + 2*c^2*e - 2*c*d^2*e - e^2 + d^2*e^2 + 2*c*d*f - 2*c^2*d*f +
2*d*e*f - 2*c*d*e*f - f^2 + c^2*f^2, 1 - c^2 - d^2 + 2*c*d*e - 3*e^2
+ 2*c*e^2 + d^2*e^2 - 2*d*e^3 + e^4 + 2*d*e*f - 2*c*d*e*f + 2*e^2*f -
2*c*e^2*f - f^2 + c^2*f^2, b - c^2 - b*d^2 + c*d^2 - c*e + c^2*e - d*e +
c*d*e + c*d^2*e - b*e^2 + c*e^2 - c*d*e^2 - d^2*e^2 + d*e^3 - c^2*d*f +
c*e*f + d*e*f + 2*b*d*e*f - 2*c*d*e*f - c*e^2*f - b*f^2 + c^2*f^2, 1 +
(-1 + a)*(a - b)*(-1 + b)*(a - c)*(b - c)*(-1 + c)*(a - d)*(b - d)*(c -
d)*(-1 + d)*(a - e)*(b - e)*(c - e)*(d - e)*(-1 + e)*(a - f)*(b - f)*(c -
f)*(d - f)*(e - f)*(-1 + f)*u>;
time Groebner(I);
C := ChangeOrder(I, "lex");
Groebner(C);
G := Basis(C);
a := Generic(C)!a;
assert G[#G] eq 
a^11 - 78777/4843*a^10 + 873/4843*a^9 + 99237/4843*a^8 - 41810/4843*a^7 - 
        31290/4843*a^6 + 37522/4843*a^5 - 9910/4843*a^4 - 7785/4843*a^3 + 
        5411/4843*a^2 - 811/4843*a - 31/4843;
