/// Algebraic Power Series tests - adapted from a test file of Tobias Beck

//import "../../package/Ring/RngPowAlg/power_series.m": ScalePolynomial1, 
//ScalePolynomial2, PlOrd;

// SetVerbose("AlgSeries", 1);
ord := 30;


Q := Rationals();
S<x,y> := PolynomialRing(Q, 2, "glex"); P<z> := PolynomialRing(S);
IV := RSpace(Integers(), 2);

// Change defining polynomial according to an automorphism
function ChangeDefPol(p, lambda)
	P := Parent(p); S := BaseRing(P);
	return hom<P -> P | hom<S -> S | lambda[1]*S.1,lambda[2]*S.2>, P.1>(p);
end function;

// Function to test whether power series is sane
/*
function TestSer(s)
	// try to expand
	good, approx := Expand(s, ord);
	if not good then
		error "series not expandable!";
	end if;
	
	// check if defining polynomial vanishes on it
	defpol := DefiningPolynomial(s);
	_, e := Explode(ExponentLattice(s));
	o := PlOrd(Evaluate(ScalePolynomial2(defpol,e),approx));
	if not ((o eq -1) or (o ge ord)) then
		error "defining polynomial and series do not match!";
	end if;
	
	return true;
end function;
*/

// Initially define a few power series
// -----------------------------------

// Example with quite different branches
p1 := (1-x*y)*z^4+(4*x^3*y^3+3*x^5*y^6-4*x*y+2*x^2*y^2)*z^3+(-9*x^6*y^7+4*x^2*y^2-9*x^7*y^8-5*x^5*y^5-12*x^4*y^4)*z^2+(9*x^9*y^10+13*x^6*y^6+2*x^7*y^7-7*x^9*y^8+8*x^5*y^5+18*x^8*y^9+9*x^7*y^8)*z+x^7*y^7-4*x^8*y^8-3*x^8*y^9-9*x^9*y^10-9*x^10*y^11-3*x^11*y^12+7*x^10*y^9+7*x^11*y^10;

lambda := [1/2, 1]; Gamma := StandardLattice(2); e := 1; init := 1/2*x^2*y^2 + x*y; s11 := AlgebraicPowerSeries(ChangeDefPol(p1,lambda), init, Gamma, e);
lambda := [1/2, 1]; Gamma := StandardLattice(2); e := 1; init := x*y; s12 := AlgebraicPowerSeries(ChangeDefPol(p1,lambda), init, Gamma, e);
lambda := [-4, 1]; Gamma := Lattice(RMatrixSpace(Integers(), 2, 2) ! [1,1, -1,1]); e := 2; init := 16*x^5*y^5; s13 := AlgebraicPowerSeries(ChangeDefPol(p1,lambda), init, Gamma, e);

// Example with one complicated branch
p2 := (1+5*y+10*y^3+10*y^2+5*y^4+y^5)*z^5+(-20*y^3*x-30*y^2*x-5*y^4*x-5*x-20*y*x)*z^4+(10*x^2+30*y^2*x^2+10*y^3*x^2+30*x^2*y)*z^3+(-20*y*x^3-10*x^3-10*y^2*x^3)*z^2+(5*y*x^4+5*x^4)*z-x^5-x^2*y;
lambda := [1, 1]; Gamma := Lattice(RMatrixSpace(Integers(), 2, 2) ! [1,-2, 2,1]); e := 5; init := x^2*y; s21 := AlgebraicPowerSeries(ChangeDefPol(p2,lambda), init, Gamma, e);

// Example from implicit function theorem;
p3 := z^2 + z + x + y;
s31 := ImplicitFunction(p3);

// Test expansions against precomputed approximations
approx11 := 593733/8589934592*x^16*y^13 + 467733875595/4503599627370496*x^15*y^14 - 
3354140482983/36028797018963968*x^14*y^15 - 1244675727/2199023255552*x^13*y^16 - 
1701/1073741824*x^12*y^17 - 489782979/1099511627776*x^15*y^13 + 
8911836926734447/1152921504606846976*x^14*y^14 - 41694498333/35184372088832*x^13*y^15 - 
963819/17179869184*x^12*y^16 + 12005/268435456*x^15*y^12 + 
123600381429/281474976710656*x^14*y^13 - 551569755159/1125899906842624*x^13*y^14 - 
63732177/137438953472*x^12*y^15 - 37387147/68719476736*x^14*y^12 - 
97061084933713/18014398509481984*x^13*y^13 - 2782804563/2199023255552*x^12*y^14 - 
19845/536870912*x^11*y^15 + 343/16777216*x^14*y^11 + 1397204669/2199023255552*x^13*y^12 - 
64440134505/70368744177664*x^12*y^13 - 646245/2147483648*x^11*y^14 - 
541989/1073741824*x^13*y^11 + 9767251689793/1125899906842624*x^12*y^12 - 
77477067/68719476736*x^11*y^13 - 405/33554432*x^10*y^14 + 156383857/137438953472*x^12*y^11 
- 767459025/549755813888*x^11*y^12 - 29457/134217728*x^10*y^13 - 38367/67108864*x^12*y^10 - 
239937593613/35184372088832*x^11*y^11 - 3470931/4294967296*x^10*y^12 + 
6038543/4294967296*x^11*y^10 - 68500437/34359738368*x^10*y^11 - 945/4194304*x^9*y^12 - 
1029/2097152*x^11*y^9 + 21513900755/2199023255552*x^10*y^10 - 5085/67108864*x^9*y^11 + 
569205/268435456*x^10*y^9 - 3009579/1073741824*x^9*y^10 - 27/262144*x^8*y^11 - 
49/131072*x^10*y^8 - 165932327/17179869184*x^9*y^9 - 135/4194304*x^8*y^10 + 
10773/4194304*x^9*y^8 - 249321/67108864*x^8*y^9 + 12146667/1073741824*x^8*y^8 - 
189/131072*x^7*y^9 + 1141/262144*x^8*y^7 - 6537/1048576*x^7*y^8 - 570737/33554432*x^7*y^7 - 
9/8192*x^6*y^8 + 49/8192*x^7*y^6 - 873/65536*x^6*y^7 + 27067/2097152*x^6*y^6 + 
7/512*x^6*y^5 - 69/2048*x^5*y^6 - 1095/32768*x^5*y^5 - 3/128*x^4*y^5 + 31/2048*x^4*y^4 - 
7/64*x^3*y^3 + 1/2*x^2*y^2 + x*y;

approx12 := -593733/8589934592*x^16*y^13 - 1517182290827/4503599627370496*x^15*y^14 - 
20188185879129/36028797018963968*x^14*y^15 + 1244675727/2199023255552*x^13*y^16 + 
1701/1073741824*x^12*y^17 + 451247811/1099511627776*x^15*y^13 + 
1298526549020561/1152921504606846976*x^14*y^14 + 37957373469/35184372088832*x^13*y^15 + 
963819/17179869184*x^12*y^16 - 12005/268435456*x^15*y^12 + 
145925593611/281474976710656*x^14*y^13 + 771821046807/1125899906842624*x^13*y^14 + 
63732177/137438953472*x^12*y^15 + 35781515/68719476736*x^14*y^12 - 
21766835720623/18014398509481984*x^13*y^13 + 2386442835/2199023255552*x^12*y^14 + 
19845/536870912*x^11*y^15 - 343/16777216*x^14*y^11 + 672684355/2199023255552*x^13*y^12 + 
145625082729/70368744177664*x^12*y^13 + 646245/2147483648*x^11*y^14 + 
642341/1073741824*x^13*y^11 - 5709044465985/1125899906842624*x^12*y^12 + 
98710731/68719476736*x^11*y^13 + 405/33554432*x^10*y^14 - 200424049/137438953472*x^12*y^11 
+ 1126072017/549755813888*x^11*y^12 + 29457/134217728*x^10*y^13 + 38367/67108864*x^12*y^10 
- 173402676979/35184372088832*x^11*y^11 + 7894611/4294967296*x^10*y^12 - 
13607951/4294967296*x^11*y^10 + 27605973/34359738368*x^10*y^11 + 945/4194304*x^9*y^12 + 
1029/2097152*x^11*y^9 + 3861638445/2199023255552*x^10*y^10 + 97245/67108864*x^9*y^11 - 
741237/268435456*x^10*y^9 + 748587/1073741824*x^9*y^10 + 27/262144*x^8*y^11 + 
49/131072*x^10*y^8 + 147320103/17179869184*x^9*y^9 + 135/4194304*x^8*y^10 - 
3605/4194304*x^9*y^8 + 273897/67108864*x^8*y^9 + 4859925/1073741824*x^8*y^8 + 
189/131072*x^7*y^9 - 245/262144*x^8*y^7 + 9609/1048576*x^7*y^8 - 342671/33554432*x^7*y^7 + 
9/8192*x^6*y^8 - 49/8192*x^7*y^6 + 489/65536*x^6*y^7 - 35259/2097152*x^6*y^6 - 
7/512*x^6*y^5 - 27/2048*x^5*y^6 + 455/32768*x^5*y^5 + 3/128*x^4*y^5 + 161/2048*x^4*y^4 + 
7/64*x^3*y^3 + x*y;

approx13 := -2035712*x^15*y^13 - 12160*x^14*y^14 - 2301504*x^13*y^15 + 1728*x^11*y^17 - 
1567360*x^13*y^13 - 5376*x^12*y^14 + 20736*x^11*y^15 + 71680*x^13*y^11 - 3584*x^12*y^12 + 
74496*x^11*y^13 + 71136*x^11*y^11 + 768*x^10*y^12 - 288*x^9*y^13 - 3584*x^11*y^9 + 
704*x^10*y^10 - 2688*x^9*y^11 - 3712*x^9*y^9 - 448*x^8*y^8 + 96*x^7*y^9 + 256*x^7*y^7 + 
64*x^6*y^6 + 16*x^5*y^5;

approx21 := -x^2*y^26 + x^5*y^20 + x^2*y^21 - x^5*y^15 - x^2*y^16 + x^5*y^10 + x^2*y^11 - 
x^5*y^5 - x^2*y^6 + x^5 + x^2*y;

approx31 := -263747951750360*x^29 - 7648690600760440*x^28*y - 107081668410646160*x^27*y^2 
- 963735015695815440*x^26*y^3 - 6264277602022800360*x^25*y^4 - 
31321388010114001800*x^24*y^5 - 125285552040456007200*x^23*y^6 - 
411652528132926880800*x^22*y^7 - 1132044452365548922200*x^21*y^8 - 
2641437055519614151800*x^20*y^9 - 5282874111039228303600*x^19*y^10 - 
9124964373613212524400*x^18*y^11 - 13687446560419818786600*x^17*y^12 - 
17898968579010532259400*x^16*y^13 - 20455964090297751153600*x^15*y^14 - 
20455964090297751153600*x^14*y^15 - 17898968579010532259400*x^13*y^16 - 
13687446560419818786600*x^12*y^17 - 9124964373613212524400*x^11*y^18 - 
5282874111039228303600*x^10*y^19 - 2641437055519614151800*x^9*y^20 - 
1132044452365548922200*x^8*y^21 - 411652528132926880800*x^7*y^22 - 
125285552040456007200*x^6*y^23 - 31321388010114001800*x^5*y^24 - 
6264277602022800360*x^4*y^25 - 963735015695815440*x^3*y^26 - 107081668410646160*x^2*y^27 - 
7648690600760440*x*y^28 - 263747951750360*y^29 - 69533550916004*x^28 - 
1946939425648112*x^27*y - 26283682246249512*x^26*y^2 - 227791912800829104*x^25*y^3 - 
1423699455005181900*x^24*y^4 - 6833757384024873120*x^23*y^5 - 
26196069972095346960*x^22*y^6 - 82330505626585376160*x^21*y^7 - 
216117577269786612420*x^20*y^8 - 480261282821748027600*x^19*y^9 - 
912496437361321252440*x^18*y^10 - 1493175988409434776720*x^17*y^11 - 
2115332650246699267020*x^16*y^12 - 2603486338765168328640*x^15*y^13 - 
2789449648676966066400*x^14*y^14 - 2603486338765168328640*x^13*y^15 - 
2115332650246699267020*x^12*y^16 - 1493175988409434776720*x^11*y^17 - 
912496437361321252440*x^10*y^18 - 480261282821748027600*x^9*y^19 - 
216117577269786612420*x^8*y^20 - 82330505626585376160*x^7*y^21 - 
26196069972095346960*x^6*y^22 - 6833757384024873120*x^5*y^23 - 
1423699455005181900*x^4*y^24 - 227791912800829104*x^3*y^25 - 26283682246249512*x^2*y^26 - 
1946939425648112*x*y^27 - 69533550916004*y^28 - 18367353072152*x^27 - 
495918532948104*x^26*y - 6446940928325352*x^25*y^2 - 53724507736044600*x^24*y^3 - 
322347046416267600*x^23*y^4 - 1482796413514830960*x^22*y^5 - 5436920182887713520*x^21*y^6 
- 16310760548663140560*x^20*y^7 - 40776901371657851400*x^19*y^8 - 
86084569562388797400*x^18*y^9 - 154952225212299835320*x^17*y^10 - 
239471620782645200040*x^16*y^11 - 319295494376860266720*x^15*y^12 - 
368417878127146461600*x^14*y^13 - 368417878127146461600*x^13*y^14 - 
319295494376860266720*x^12*y^15 - 239471620782645200040*x^11*y^16 - 
154952225212299835320*x^10*y^17 - 86084569562388797400*x^9*y^18 - 
40776901371657851400*x^8*y^19 - 16310760548663140560*x^7*y^20 - 
5436920182887713520*x^6*y^21 - 1482796413514830960*x^5*y^22 - 322347046416267600*x^4*y^23 
- 53724507736044600*x^3*y^24 - 6446940928325352*x^2*y^25 - 495918532948104*x*y^26 - 
18367353072152*y^27 - 4861946401452*x^26 - 126410606437752*x^25*y - 
1580132580471900*x^24*y^2 - 12641060643775200*x^23*y^3 - 72686098701707400*x^22*y^4 - 
319818834287512560*x^21*y^5 - 1119365920006293960*x^20*y^6 - 3198188342875125600*x^19*y^7 
- 7595697314328423300*x^18*y^8 - 15191394628656846600*x^17*y^9 - 
25825370868716639220*x^16*y^10 - 37564175809042384320*x^15*y^11 - 
46955219761302980400*x^14*y^12 - 50567159742941671200*x^13*y^13 - 
46955219761302980400*x^12*y^14 - 37564175809042384320*x^11*y^15 - 
25825370868716639220*x^10*y^16 - 15191394628656846600*x^9*y^17 - 
7595697314328423300*x^8*y^18 - 3198188342875125600*x^7*y^19 - 1119365920006293960*x^6*y^20 
- 319818834287512560*x^5*y^21 - 72686098701707400*x^4*y^22 - 12641060643775200*x^3*y^23 - 
1580132580471900*x^2*y^24 - 126410606437752*x*y^25 - 4861946401452*y^26 - 
1289904147324*x^25 - 32247603683100*x^24*y - 386971244197200*x^23*y^2 - 
2966779538845200*x^22*y^3 - 16317287463648600*x^21*y^4 - 68532607347324120*x^20*y^5 - 
228442024491080400*x^19*y^6 - 620056923618646800*x^18*y^7 - 1395128078141955300*x^17*y^8 - 
2635241925379248900*x^16*y^9 - 4216387080606798240*x^15*y^10 - 
5749618746281997600*x^14*y^11 - 6707888537328997200*x^13*y^12 - 
6707888537328997200*x^12*y^13 - 5749618746281997600*x^11*y^14 - 
4216387080606798240*x^10*y^15 - 2635241925379248900*x^9*y^16 - 
1395128078141955300*x^8*y^17 - 620056923618646800*x^7*y^18 - 228442024491080400*x^6*y^19 - 
68532607347324120*x^5*y^20 - 16317287463648600*x^4*y^21 - 2966779538845200*x^3*y^22 - 
386971244197200*x^2*y^23 - 32247603683100*x*y^24 - 1289904147324*y^25 - 343059613650*x^24 
- 8233430727600*x^23*y - 94684453367400*x^22*y^2 - 694352658027600*x^21*y^3 - 
3645351454644900*x^20*y^4 - 14581405818579600*x^19*y^5 - 46174451758835400*x^18*y^6 - 
118734304522719600*x^17*y^7 - 252310397110779150*x^16*y^8 - 448551817085829600*x^15*y^9 - 
672827725628744400*x^14*y^10 - 856326196254765600*x^13*y^11 - 927686712609329400*x^12*y^12 
- 856326196254765600*x^11*y^13 - 672827725628744400*x^10*y^14 - 
448551817085829600*x^9*y^15 - 252310397110779150*x^8*y^16 - 118734304522719600*x^7*y^17 - 
46174451758835400*x^6*y^18 - 14581405818579600*x^5*y^19 - 3645351454644900*x^4*y^20 - 
694352658027600*x^3*y^21 - 94684453367400*x^2*y^22 - 8233430727600*x*y^23 - 
343059613650*y^24 - 91482563640*x^23 - 2104098963720*x^22*y - 23145088600920*x^21*y^2 - 
162015620206440*x^20*y^3 - 810078101032200*x^19*y^4 - 3078296783922360*x^18*y^5 - 
9234890351767080*x^17*y^6 - 22427590854291480*x^16*y^7 - 44855181708582960*x^15*y^8 - 
74758636180971600*x^14*y^9 - 104662090653360240*x^13*y^10 - 123691561681243920*x^12*y^11 - 
123691561681243920*x^11*y^12 - 104662090653360240*x^10*y^13 - 74758636180971600*x^9*y^14 - 
44855181708582960*x^8*y^15 - 22427590854291480*x^7*y^16 - 9234890351767080*x^6*y^17 - 
3078296783922360*x^5*y^18 - 810078101032200*x^4*y^19 - 162015620206440*x^3*y^20 - 
23145088600920*x^2*y^21 - 2104098963720*x*y^22 - 91482563640*y^23 - 24466267020*x^22 - 
538257874440*x^21*y - 5651707681620*x^20*y^2 - 37678051210800*x^19*y^3 - 
178970743251300*x^18*y^4 - 644294675704680*x^17*y^5 - 1825501581163260*x^16*y^6 - 
4172575042658880*x^15*y^7 - 7823578204985400*x^14*y^8 - 12170010541088400*x^13*y^9 - 
15821013703414920*x^12*y^10 - 17259287676452640*x^11*y^11 - 15821013703414920*x^10*y^12 - 
12170010541088400*x^9*y^13 - 7823578204985400*x^8*y^14 - 4172575042658880*x^7*y^15 - 
1825501581163260*x^6*y^16 - 644294675704680*x^5*y^17 - 178970743251300*x^4*y^18 - 
37678051210800*x^3*y^19 - 5651707681620*x^2*y^20 - 538257874440*x*y^21 - 24466267020*y^22 
- 6564120420*x^21 - 137846528820*x^20*y - 1378465288200*x^19*y^2 - 8730280158600*x^18*y^3 
- 39286260713700*x^17*y^4 - 133573286426580*x^16*y^5 - 356195430470880*x^15*y^6 - 
763275922437600*x^14*y^7 - 1335732864265800*x^13*y^8 - 1929391915050600*x^12*y^9 - 
2315270298060720*x^11*y^10 - 2315270298060720*x^10*y^11 - 1929391915050600*x^9*y^12 - 
1335732864265800*x^8*y^13 - 763275922437600*x^7*y^14 - 356195430470880*x^6*y^15 - 
133573286426580*x^5*y^16 - 39286260713700*x^4*y^17 - 8730280158600*x^3*y^18 - 
1378465288200*x^2*y^19 - 137846528820*x*y^20 - 6564120420*y^21 - 1767263190*x^20 - 
35345263800*x^19*y - 335780006100*x^18*y^2 - 2014680036600*x^17*y^3 - 
8562390155550*x^16*y^4 - 27399648497760*x^15*y^5 - 68499121244400*x^14*y^6 - 
136998242488800*x^13*y^7 - 222622144044300*x^12*y^8 - 296829525392400*x^11*y^9 - 
326512477931640*x^10*y^10 - 296829525392400*x^9*y^11 - 222622144044300*x^8*y^12 - 
136998242488800*x^7*y^13 - 68499121244400*x^6*y^14 - 27399648497760*x^5*y^15 - 
8562390155550*x^4*y^16 - 2014680036600*x^3*y^17 - 335780006100*x^2*y^18 - 
35345263800*x*y^19 - 1767263190*y^20 - 477638700*x^19 - 9075135300*x^18*y - 
81676217700*x^17*y^2 - 462831900300*x^16*y^3 - 1851327601200*x^15*y^4 - 
5553982803600*x^14*y^5 - 12959293208400*x^13*y^6 - 24067258815600*x^12*y^7 - 
36100888223400*x^11*y^8 - 44123307828600*x^10*y^9 - 44123307828600*x^9*y^10 - 
36100888223400*x^8*y^11 - 24067258815600*x^7*y^12 - 12959293208400*x^6*y^13 - 
5553982803600*x^5*y^14 - 1851327601200*x^4*y^15 - 462831900300*x^3*y^16 - 
81676217700*x^2*y^17 - 9075135300*x*y^18 - 477638700*y^19 - 129644790*x^18 - 
2333606220*x^17*y - 19835652870*x^16*y^2 - 105790148640*x^15*y^3 - 396713057400*x^14*y^4 - 
1110796560720*x^13*y^5 - 2406725881560*x^12*y^6 - 4125815796960*x^11*y^7 - 
5672996720820*x^10*y^8 - 6303329689800*x^9*y^9 - 5672996720820*x^8*y^10 - 
4125815796960*x^7*y^11 - 2406725881560*x^6*y^12 - 1110796560720*x^5*y^13 - 
396713057400*x^4*y^14 - 105790148640*x^3*y^15 - 19835652870*x^2*y^16 - 2333606220*x*y^17 - 
129644790*y^18 - 35357670*x^17 - 601080390*x^16*y - 4808643120*x^15*y^2 - 
24043215600*x^14*y^3 - 84151254600*x^13*y^4 - 218793261960*x^12*y^5 - 
437586523920*x^11*y^6 - 687635966160*x^10*y^7 - 859544957700*x^9*y^8 - 
859544957700*x^8*y^9 - 687635966160*x^7*y^10 - 437586523920*x^6*y^11 - 
218793261960*x^5*y^12 - 84151254600*x^4*y^13 - 24043215600*x^3*y^14 - 4808643120*x^2*y^15 
- 601080390*x*y^16 - 35357670*y^17 - 9694845*x^16 - 155117520*x^15*y - 1163381400*x^14*y^2 
- 5429113200*x^13*y^3 - 17644617900*x^12*y^4 - 42347082960*x^11*y^5 - 77636318760*x^10*y^6 
- 110909026800*x^9*y^7 - 124772655150*x^8*y^8 - 110909026800*x^7*y^9 - 
77636318760*x^6*y^10 - 42347082960*x^5*y^11 - 17644617900*x^4*y^12 - 5429113200*x^3*y^13 - 
1163381400*x^2*y^14 - 155117520*x*y^15 - 9694845*y^16 - 2674440*x^15 - 40116600*x^14*y - 
280816200*x^13*y^2 - 1216870200*x^12*y^3 - 3650610600*x^11*y^4 - 8031343320*x^10*y^5 - 
13385572200*x^9*y^6 - 17210021400*x^8*y^7 - 17210021400*x^7*y^8 - 13385572200*x^6*y^9 - 
8031343320*x^5*y^10 - 3650610600*x^4*y^11 - 1216870200*x^3*y^12 - 280816200*x^2*y^13 - 
40116600*x*y^14 - 2674440*y^15 - 742900*x^14 - 10400600*x^13*y - 67603900*x^12*y^2 - 
270415600*x^11*y^3 - 743642900*x^10*y^4 - 1487285800*x^9*y^5 - 2230928700*x^8*y^6 - 
2549632800*x^7*y^7 - 2230928700*x^6*y^8 - 1487285800*x^5*y^9 - 743642900*x^4*y^10 - 
270415600*x^3*y^11 - 67603900*x^2*y^12 - 10400600*x*y^13 - 742900*y^14 - 208012*x^13 - 
2704156*x^12*y - 16224936*x^11*y^2 - 59491432*x^10*y^3 - 148728580*x^9*y^4 - 
267711444*x^8*y^5 - 356948592*x^7*y^6 - 356948592*x^6*y^7 - 267711444*x^5*y^8 - 
148728580*x^4*y^9 - 59491432*x^3*y^10 - 16224936*x^2*y^11 - 2704156*x*y^12 - 208012*y^13 - 
58786*x^12 - 705432*x^11*y - 3879876*x^10*y^2 - 12932920*x^9*y^3 - 29099070*x^8*y^4 - 
46558512*x^7*y^5 - 54318264*x^6*y^6 - 46558512*x^5*y^7 - 29099070*x^4*y^8 - 
12932920*x^3*y^9 - 3879876*x^2*y^10 - 705432*x*y^11 - 58786*y^12 - 16796*x^11 - 
184756*x^10*y - 923780*x^9*y^2 - 2771340*x^8*y^3 - 5542680*x^7*y^4 - 7759752*x^6*y^5 - 
7759752*x^5*y^6 - 5542680*x^4*y^7 - 2771340*x^3*y^8 - 923780*x^2*y^9 - 184756*x*y^10 - 
16796*y^11 - 4862*x^10 - 48620*x^9*y - 218790*x^8*y^2 - 583440*x^7*y^3 - 1021020*x^6*y^4 - 
1225224*x^5*y^5 - 1021020*x^4*y^6 - 583440*x^3*y^7 - 218790*x^2*y^8 - 48620*x*y^9 - 
4862*y^10 - 1430*x^9 - 12870*x^8*y - 51480*x^7*y^2 - 120120*x^6*y^3 - 180180*x^5*y^4 - 
180180*x^4*y^5 - 120120*x^3*y^6 - 51480*x^2*y^7 - 12870*x*y^8 - 1430*y^9 - 429*x^8 - 
3432*x^7*y - 12012*x^6*y^2 - 24024*x^5*y^3 - 30030*x^4*y^4 - 24024*x^3*y^5 - 12012*x^2*y^6 
- 3432*x*y^7 - 429*y^8 - 132*x^7 - 924*x^6*y - 2772*x^5*y^2 - 4620*x^4*y^3 - 4620*x^3*y^4 
- 2772*x^2*y^5 - 924*x*y^6 - 132*y^7 - 42*x^6 - 252*x^5*y - 630*x^4*y^2 - 840*x^3*y^3 - 
630*x^2*y^4 - 252*x*y^5 - 42*y^6 - 14*x^5 - 70*x^4*y - 140*x^3*y^2 - 140*x^2*y^3 - 
70*x*y^4 - 14*y^5 - 5*x^4 - 20*x^3*y - 30*x^2*y^2 - 20*x*y^3 - 5*y^4 - 2*x^3 - 6*x^2*y - 
6*x*y^2 - 2*y^3 - x^2 - 2*x*y - y^2 - x - y;

_, exp := Expand(s11, ord);
assert approx11 - exp eq 0;

_, exp := Expand(s12, ord);
assert approx12 - exp eq 0;

_, exp := Expand(s13, ord);
assert approx13 - exp eq 0;

_, exp := Expand(s21, ord);
assert approx21 - exp eq 0;

_, exp := Expand(s31, ord);
assert approx31 - exp eq 0;

// Test expansions against minimal polynomial
//TestSer(s11);
//TestSer(s12);
//TestSer(s13);
//TestSer(s21);
//TestSer(s31);

// Test with substituted defining polynomial
// -----------------------------------------
p := z^2 - (y*(x-x*y)); d := (1-y)*z-x;
X := ImplicitFunction((1-y)*z-x);
Y := PolyToSeries(y);
Gamma := Lattice(RMatrixSpace(Integers(), 2, 2) ! [1,-1, 1,1]); e := 2; init := x*y;
Z := AlgebraicPowerSeries(p, init, Gamma, e : subs := [X,Y]);

// test it
assert DefiningPolynomial(Z) - ((y - 1)*z^2 - x*y^2 + x*y) eq 0;
//TestSer(Z);

// test a polynomial
Z2 := EvaluationPowerSeries(Z, [RSpace(Integers(), 2) | [2,0], [0,2]],
        [PolyToSeries(x),PolyToSeries(y)]);
Z3 := AlgebraicPowerSeries(x*z - x^2 - x*y, x+y, StandardLattice(2), 1);
assert not IsPolynomial(X);
assert IsPolynomial(Y);
assert not IsPolynomial(Z);
assert IsPolynomial(Z2);
assert IsPolynomial(Z3);

// Test power series arithmetic
// ----------------------------

// example with interesting exponent lattices
ExponentLattice(s13);
ExponentLattice(s21);
S1 := Add(s13,s21);
L1 := <Lattice(RMatrixSpace(Integers(), 2, 2) ! [-3,1, 1,3]), 10>;
assert L1 eq ExponentLattice(S1);

// test it
_, exp := Expand(S1,ord);
//assert PlOrd(ScalePolynomial1(approx13, 5)+ScalePolynomial1(approx21, 2)-exp) ge ord;
//TestSer(S1);

// zero test
S2 := Add(s13, Mult(s13, PolyToSeries(S ! -1)));
assert IsZero(S2);

// Test substitutions
// ------------------

F<s> := FunctionField(Q);
SS<t> := PolynomialRing(F, 1, "grevlex");
PP<u> := PolynomialRing(SS);
q := u^3 - t + s*t^2;
Gamma := StandardLattice(1); e := 3; init := t;
X := AlgebraicPowerSeries(q, init, Gamma, e);
Y := PolyToSeries(t);

//TestSer(X);
//TestSer(Y);

duals := [IV ! [1, 3], IV ! [2, 1]];
S5 := EvaluationPowerSeries(s21, duals, [X, Y]);
ExponentLattice(S5);

// test it
approxX := -21505/1594323*s^9*t^28 - 935/59049*s^8*t^25 - 374/19683*s^7*t^22 - 
154/6561*s^6*t^19 - 22/729*s^5*t^16 - 10/243*s^4*t^13 - 5/81*s^3*t^10 - 1/9*s^2*t^7 - 
1/3*s*t^4 + t;
approxS5 := (-935/59049*s^8 - 374/19683*s^7 - 44/6561*s^6 - 8/729*s^5 - 7/243*s^4 - 
14/81*s^3 - 35/9*s^2 + 10/3*s + 1)*t^28 + (-374/19683*s^7 - 154/6561*s^6 - 8/729*s^5 - 
5/243*s^4 - 14/81*s^3 + 14/9*s^2 + 10/3*s - 1)*t^25 + (-154/6561*s^6 - 22/729*s^5 - 
5/243*s^4 - 4/81*s^3 + 14/9*s^2 - 7/3*s - 1)*t^22 + (-22/729*s^5 - 10/243*s^4 - 4/81*s^3 - 
2/9*s^2 - 7/3*s + 1)*t^19 + (-10/243*s^4 - 5/81*s^3 - 2/9*s^2 + 4/3*s + 1)*t^16 + 
(-5/81*s^3 - 1/9*s^2 + 4/3*s - 1)*t^13 + (-1/9*s^2 - 1/3*s - 1)*t^10 + (-1/3*s + 1)*t^7 + 
t^4;

_, exp := Expand(X,ord);
assert approxX - exp eq 0;

_, exp := Expand(S5,ord);
assert approxS5 - exp eq 0;

//TestSer(S5);

// expansions consistent?
l := [exp where _, exp := Expand(S5, i) : i in [0..ord]];
l := [l[i+1]-l[i] : i in [1..ord]];
//assert &and[(l[i] eq 0) or (TotalDegree(l[i]) eq i-1) and (PlOrd(l[i]) eq i-1):
	//i in [1..ord]];

// equality test
S6 := EvaluationPowerSeries(s21, duals, [PolyToSeries(s*t^2), PolyToSeries(t)]);
T6 := SimplifyRep(S6);
assert IsEqual(S6,T6);

// Test generator scaling
// ----------------------

// Bad tests! The results depend on the lattice basis for the lattices of the series
// and this is changeable depending on the reduction technique used (i.e., which
// version of lll is used)

//S7 := ScaleGenerators(s21, [1/2, 1/3]);
//S8 := ScaleGenerators(Z, [1/2, 1/3]);
//S9 := ScaleGenerators(S5, [2]);

// test it
//approxS7 := -59049/64*x^2*y^26 + 2187/64*x^5*y^20 + 6561/32*x^2*y^21 - 243/32*x^5*y^15 - 
//  729/16*x^2*y^16 + 27/16*x^5*y^10 + 81/8*x^2*y^11 - 3/8*x^5*y^5 - 9/4*x^2*y^6 + 1/12*x^5 + 1/2*x^2*y;
//approxS8 := 1/2*x*y;
//approxS9 := (-250987151360/59049*s^8 - 100394860544/19683*s^7 - 11811160064/6561*s^6 - 
//2147483648/729*s^5 - 1879048192/243*s^4 - 3758096384/81*s^3 - 9395240960/9*s^2 + 
//2684354560/3*s + 268435456)*t^28 + (-12549357568/19683*s^7 - 5167382528/6561*s^6 - 
//268435456/729*s^5 - 167772160/243*s^4 - 469762048/81*s^3 + 469762048/9*s^2 + 335544320/3*s 
//- 33554432)*t^25 + (-645922816/6561*s^6 - 92274688/729*s^5 - 20971520/243*s^4 - 
//16777216/81*s^3 + 58720256/9*s^2 - 29360128/3*s - 4194304)*t^22 + (-11534336/729*s^5 - 
//5242880/243*s^4 - 2097152/81*s^3 - 1048576/9*s^2 - 3670016/3*s + 524288)*t^19 + 
//(-655360/243*s^4 - 327680/81*s^3 - 131072/9*s^2 + 262144/3*s + 65536)*t^16 + 
//(-40960/81*s^3 - 8192/9*s^2 + 32768/3*s - 8192)*t^13 + (-1024/9*s^2 - 1024/3*s - 1024)
//*t^10 + (-128/3*s + 128)*t^7 + 16*t^4;

//_, exp := Expand(S7, ord);
//assert exp - approxS7 eq 0;

//_, exp := Expand(S8, ord);
//assert exp - approxS8 eq 0;

//_, exp := Expand(S9, ord);
//assert exp - approxS9 eq 0;

//TestSer(S7);
//TestSer(S8);
//TestSer(S9);

// Test coercion
// -------------
Qi := NumberField(R.1^2+1) where R := PolynomialRing(Q);
Si := ChangeRing(S, Qi);
s21i := ChangeRing(s21, Si);
Expand(s21i, ord);
assert Domain(s21i) eq Si;
//TestSer(s21i);

