/*
SetTrace(1458258, true);
SetMark(true);
SetDelCheck(true);
*/

/*
Multivariate localization polynomials test.
AKS Oct 08.
*/

ClearVerbose();

SetEchoInput(true);

VERB := true;
VERB := false;

/*******************************************************************************
				Constants
*******************************************************************************/

Z := IntegerRing();
Q := RationalField();

/*******************************************************************************
				Milnor
*******************************************************************************/

P<x,y,z> := PolynomialRing(Q, 3);
assert [MilnorNumber(x*y*z + x^d + y^d + z^d): d in [1..40]] eq
    [ 0, 1, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 41, 44, 47, 50,
    53, 56, 59, 62, 65, 68, 71, 74, 77, 80, 83, 86, 89, 92, 95, 98, 101,
    104, 107, 110, 113, 116, 119 ];
assert [TjurinaNumber(x*y*z + x^d + y^d + z^d): d in [1..40]] eq
    [ 0, 1, 8, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46, 49, 52,
    55, 58, 61, 64, 67, 70, 73, 76, 79, 82, 85, 88, 91, 94, 97, 100, 103,
    106, 109, 112, 115, 118 ];

P<x,y,z,w> := PolynomialRing(Q, 4);
assert [MilnorNumber(x*y*z*w + x^d + y^d + z^d + w^d): d in [1..40]] eq
    [ 0, 1, 16, 81, 131, 193, 267, 353, 451, 561, 683, 817, 963, 1121,
    1291, 1473, 1667, 1873, 2091, 2321, 2563, 2817, 3083, 3361, 3651,
    3953, 4267, 4593, 4931, 5281, 5643, 6017, 6403, 6801, 7211, 7633,
    8067, 8513, 8971, 9441 ];
assert [TjurinaNumber(x*y*z*w + x^d + y^d + z^d + w^d): d in [1..40]] eq
    [ 0, 1, 15, 81, 107, 165, 235, 317, 411, 517, 635, 765, 907, 1061,
    1227, 1405, 1595, 1797, 2011, 2237, 2475, 2725, 2987, 3261, 3547,
    3845, 4155, 4477, 4811, 5157, 5515, 5885, 6267, 6661, 7067, 7485,
    7915, 8357, 8811, 9277 ];

/*******************************************************************************
				Other
*******************************************************************************/

R<x,y,z> := PolynomialRing(RationalField(), 3);
I := Ideal([x, y, z]);
M := Module(R, 1);
S := sub<M | [[f]: f in Basis(I)]>;
S;
p := Presentation(S);
BettiTable(p);
C:=FreeResolution(p);
Terms(C);
time BettiTable(C);
time BettiTable(p);
BettiNumbers(Localization(S));
L:=Localization(p);
BettiNumbers(L);
assert $1 eq $2;
// Coordinates(L, Basis(L)[1]);

/*******************************************************************************
				Other
*******************************************************************************/

procedure check_coord(I, B)
    Groebner(I);
    IB := Basis(I);
    for i := 1 to #B do
	f := B[i];

	assert Generic(I)!f in I;
//"I:", I; "IB:", IB; "i:", i; "f:", f;
	C, den := Coordinates(I, f);
//"C:", C; res := &+[C[i]*IB[i]: i in [1..#IB]];
//"res:", res; "fact res:", Factorization(res); "den:", den;
	q := ExactQuotient(&+[C[i]*IB[i]: i in [1..#IB]], f);
//"quot:", q;
//"expr:", &+[C[i]*IB[i]: i in [1..#IB]];
//"prod:", den*&+[C[i]*IB[i]: i in [1..#IB]];
//"f:", f;
	assert &+[C[i]*IB[i]: i in [1..#IB]] eq den*f;
	assert den eq q;
    end for;
end procedure;

for K in <Q, GF(2), GF(3), GF(23)> do

    P<x,y,z> := LocalPolynomialRing(K, 3);

"K:", K;
IndentPush();
    for B in 
	[
	    [x*y + z^4, y^2 + z^4, x*y + z^5]
	    , [x*y + y^3 + z^4, y^2*z + x*y^4 + z^5]
	    , [x*y + y^3 + z^4, y^2*z + x*y^4 + z^3, y*z + y^3 + z^3]
	    , [x*y + y^3 + z^4, y^2*z + x*y^4 + z^5, y*z + y^3 + z^6]
	]
    do

"B:", B;
//K; B;

	I := Ideal(B);
"DO Natural B";
	check_coord(I, B);
"DO Natural GB";
	check_coord(I, GroebnerBasis(I));

	if 1 eq 1 then
	    I := IdealWithFixedBasis(B);
	    Groebner(I);
	    C := CoordinateMatrix(I);
	    GB := GroebnerBasis(I);
	    for i := 1 to #GB do
		assert GB[i] eq &+[C[i, j]*B[j]: j in [1 .. #B]];
	    end for;

    "DO Fixed B";
	    check_coord(I, B);
    "DO Fixed GB";
	    //check_coord(I, GroebnerBasis(GroebnerBasis(I)));
	    //check_coord(I, [f: f in GroebnerBasis(I) | Length(f) le 4]);
	    check_coord(I, GroebnerBasis(I));
	    check_coord(I, GroebnerBasis(GroebnerBasis(I)));
	end if;

    end for;
IndentPop();
end for;



P<x, y> := LocalPolynomialRing(Rationals(), 2, "lgrevlex"); H := 1;
B := [5*x^4*y - 7*x^6, 3*H*y^2 + x^5];
f := H*y^3-x^7+x^5*y;
I := IdealWithFixedBasis([Derivative(f, 1), Derivative(f, 2)]);
check_coord(I, [f^2]);
