<<PolylogTools`

I51[x_,y_]:=G[y,0,0,0,0,x,1];
I51m[x_,y_]:=(I51[x,y]-I51[x,1/y])/2;
L6[x_]:=-G[0,0,0,0,0,1/x,1];

SetAttributes[L6sym,HoldAll];
Options[L6sym]={oneMinusOneOverP->False,oneMinusOneOverQ->False, sumP->False,sumQ->False};
L6sym[coeff_,arg_,opts:OptionsPattern[]]:=Module[{oneMinusOneOverPTF,oneMinusOneOverQTF,sumPTF,sumQTF,coeffs,args},(
oneMinusOneOverPTF=OptionValue[oneMinusOneOverP]==True;
oneMinusOneOverQTF=OptionValue[oneMinusOneOverQ]==True;
sumPTF=OptionValue[sumP]==True;
sumQTF=OptionValue[sumQ]==True;

coeffs={Hold[coeff]};
args={Hold[arg]};

If[oneMinusOneOverPTF,
coeffs=Join[coeffs,coeffs/.{HoldPattern[a]->d,HoldPattern[d]->b,HoldPattern[b]->a}, coeffs/.{HoldPattern[a]->b,HoldPattern[b]->d,HoldPattern[d]->a}];
args=Join[args,args/.{p->1/(1-p)},args/.{p->1-1/p}];];

If[oneMinusOneOverQTF,
coeffs=Join[coeffs,coeffs/.{HoldPattern[a2]->d2,HoldPattern[d2]->b2,HoldPattern[b2]->a2}, coeffs/.{HoldPattern[a2]->b2,HoldPattern[b2]->d2,HoldPattern[d2]->a2}];
args=Join[args,args/.{q->1/(1-q)},args/.{q->1-1/q}];];

coeffs = Factor[coeffs];

If[sumPTF && sumQTF,
Return[Plus@@Release[MapThread[#1 Lipq[#2]&,{coeffs,args}]]];];

If[sumPTF,
Return[Plus@@Release[MapThread[#1 Lip[#2]&,{coeffs,args}]]];
];

If[sumQTF,
Return[Plus@@Release[MapThread[#1 Liq[#2]&,{coeffs,args}]]];
];

Return[Plus@@Release[MapThread[#1 L6i[#2]&,{coeffs,args}]]];
)];

L6iinv[arg_]:=If[Sort[Factor@Expand[{arg,1/arg}]][[1]]===Factor[arg],L6i[Factor[arg]],-L6i[Factor[1/arg]]];
L6can[expr_]:=expr/.L6i[arg_]:>L6iinv[arg]

L6piinv[arg_]:=If[Sort[Factor@Expand[{arg,1/arg}]][[1]]===Factor[arg],Lip[Factor[arg]],-Lip[Factor[1/arg]]];
L6pcan[expr_]:=expr/.Lip[arg_]:>L6piinv[arg]

L6qiinv[arg_]:=If[Sort[Factor@Expand[{arg,1/arg}]][[1]]===Factor[arg],Liq[Factor[arg]],-Liq[Factor[1/arg]]];
L6qcan[expr_]:=expr/.Liq[arg_]:>L6qiinv[arg]

L6pqiinv[arg_]:=If[Sort[Factor@Expand[{arg,1/arg}]][[1]]===Factor[arg],Lipq[Factor[arg]],-Lipq[Factor[1/arg]]];
L6pqcan[expr_]:=expr/.Lipq[arg_]:>L6pqiinv[arg]

leadingsimpt[expr_] := expr//.{(-1+(1/q))^exp_:>(1-q)^exp/q^exp,(1-(1/q))^exp_:>(-1)^exp(1-q)^exp/(q)^exp,(1/q)^exp_:>q^-exp,(-1/q)^exp_:>(-1)^exp q^-exp,(-1+(1/(1-q)))^exp_:>q^exp/(1-q)^exp,(1-(1/(1-q)))^exp_:>(((-1)^exp(q)^exp)/((1-q)^exp)),(-(1/(1-q)))^exp_:>(-1)^exp(1-q)^-exp,((1/(1-q)))^exp_:>(1-q)^-exp,(-1+q)^exp_:>(-1)^exp(1-q)^exp, (-1)^-exp_:>(-1)^exp,(-q)^exp_:>(-1)^exp q^exp};Lleadt[expr_] := expr/.Liq[arg_]:>Liq[leadingsimpt[arg/.{d->-a-b}]];

leadingsimpu[expr_] := expr//.{(-1+(1/p))^exp_:>(1-p)^exp/p^exp,(1-(1/p))^exp_:>(-1)^exp(1-p)^exp/(p)^exp,(1/p)^exp_:>p^-exp,(-1/p)^exp_:>(-1)^exp p^-exp,(-1+(1/(1-p)))^exp_:>p^exp/(1-p)^exp,(1-(1/(1-p)))^exp_:>(((-1)^exp(p)^exp)/((1-p)^exp)),(-(1/(1-p)))^exp_:>(-1)^exp(1-p)^-exp,((1/(1-p)))^exp_:>(1-p)^-exp,(-1+p)^exp_:>(-1)^exp(1-p)^exp, (-1)^-exp_:>(-1)^exp,(-p)^exp_:>(-1)^exp p^exp};
Lleadu[expr_] := expr/.Lip[arg_]:>Lip[leadingsimpu[arg/.{d2->-a2-b2}]];

sumrules={
Sum[f_Plus,it:{x_Symbol,__}]:>(Sum[#,it]&)/@f,
Sum[c_ f_,it:{x_Symbol,__}]:>c Sum[f,it]/;FreeQ[c,x],
Sum[c_,it:{x_Symbol,__}]:>c Sum[1,it]/;FreeQ[c,x]
};

(* -------------- *)

li6termsFullXslot[y_] := -(1/(2 a (b^2) d (b-a) (a+2 b)))L6i[(t/((((1-y))^b) (y^a)))]+(1/(2 a (b^2) d (b-a) (a+2 b))) L6i[(t/((((y-1))^b) (y^d)))]+
-(1/(2 (a^2) b d (b-a) (2 a+b)))L6i[(t/((((1-y))^a) (y^b)))]+(1/(2 (a^2) b d (b-a) (2 a+b))) L6i[(t/((((y-1))^a) (y^d)))]+
+((1 )/(2 a b (d^2) (2 a+b) (a+2 b)))L6i[(t/((((1-y))^d) (((-y))^a)))]-(1/(2 a b (d^2) (2 a+b) (a+2 b)))L6i[(t/((((1-y))^d) (((-y))^b)))]+

+(-1+((b^2)/(a d))) L6i[y]+
+(1/(4 d))(Lip[((1-(1/p))/(1-y))]-Lip[((1-(1/p))/(1-(1/y)))])+(1/(4 d))(Lip[(1-y) (1-(1/p))]-Lip[(1-(1/y)) (1-(1/p))])+
-(1/(4 a))(Lip[((1-p)/(1-y))]-Lip[((1-p)/(1-(1/y)))])-(1/(4 a))(Lip[(1-p) (1-y)]-Lip[(1-p) (1-(1/y))])+
+(1/(4 b))(Lip[(p/(1-y))]-Lip[(p/(1-(1/y)))])+(1/(4 b))(Lip[p (1-y)]-Lip[p (1-(1/y))])+

+(-(1/(12 (2 a+b)))) (Lip[(((((1-p))^2) (1-y))/(p (y^2)))]-Lip[-(((((1-p))^2) y (1-y))/p)])+
+(1/(12 (2 b+a))) (Lip[(((p^2) (1-y))/((1-p) (y^2)))]-Lip[-(((p^2) y (1-y))/(1-p))])+
-(1/(12 (b-a)))(Lip[(((1-p) p)/((1-y) y))]-Lip[-(((1-p) p (y^2))/(1-y))]) +

+((+(2 b+3 a))/(2 (d^2))) (Lip[(1-(1/p)) y]-Lip[((1-(1/p))/y)])+
+((+(3 b+a) )/(2 (b^2)))(Lip[(p/y)]-Lip[p y])+
((+(-2 a+b) )/(2 (a^2)))(Lip[(1-p) y]-Lip[((1-p)/y)]);

li6termsFullQYslot[x_] := ((L6i[(u/((((1-x))^b2) (x^a2)))])/(2 a2 (b2^2) d2 (b2-a2) (a2+2 b2)))-((L6i[(u/((((x-1))^b2) (x^d2)))])/(2 a2 (b2^2) d2 (b2-a2) (a2+2 b2)))+((L6i[(u/((((1-x))^a2) (x^b2)))])/(2 (a2^2) b2 d2 (b2-a2) (2 a2+b2)))-((L6i[(u/((((x-1))^a2) (x^d2)))])/(2 (a2^2) b2 d2 (b2-a2) (2 a2+b2)))-(1/(2 a2 b2 (d2^2) (2 a2+b2) (a2+2 b2))) L6i[(u/((((1-x))^d2) (((-x))^a2)))]+((L6i[(u/((((1-x))^d2) (((-x))^b2)))])/(2 a2 b2 (d2^2) (2 a2+b2) (a2+2 b2)))+

+(((a2^2)/(2 b2 d2))-((b2^2)/(2 a2 d2))) L6i[x]+

-(1/(4 d2)) (Liq[((1-(1/q))/(1-x))]-Liq[((1-(1/q))/(1-(1/x)))])-((Liq[(1-x) (1-(1/q))]-Liq[(1-(1/x)) (1-(1/q))])/(4 d2))+
+((Liq[((1-q)/(1-x))]-Liq[((1-q)/(1-(1/x)))])/(4 a2))+((Liq[(1-q) (1-x)]-Liq[(1-q) (1-(1/x))])/(4 a2))+
-(1/(4 b2)) (Liq[(q/(1-x))]-Liq[(q/(1-(1/x)))])-((Liq[q (1-x)]-Liq[q (1-(1/x))])/(4 b2))+

-(-(1/(12 (2 a2+b2)))) (Liq[(((((1-q))^2) (1-x))/(q (x^2)))]-Liq[-(((((1-q))^2) x (1-x))/q)])+-(1/(12 (2 b2+a2))) (Liq[(((q^2) (1-x))/((1-q) (x^2)))]-Liq[-(((q^2) x (1-x))/(1-q))])+
+((Liq[(((1-q) q)/((1-x) x))]-Liq[-(((1-q) q (x^2))/(1-x))])/(12 (b2-a2)))+
-((+(2 b2+3 a2))/(2 (d2^2))) (Liq[(1-(1/q)) x]-Liq[((1-(1/q))/x)])+
-((+(3 b2+a2))/(2 (b2^2))) (Liq[(q/x)]-Liq[q x])+
-((+(-2 a2+b2) (Liq[(1-q) x]-Liq[((1-q)/x)]))/(2 (a2^2))) +
+((5 Liq[-((-1+q)/x)])/(2 a2))+((5 Liq[(1/(q x))])/(2 b2))+((5 Liq[(q/((-1+q) x))])/(2 d2)) + ((Liq[1-q])/(2 a2))+((Liq[(1/q)])/(2 b2))-((Liq[((-1+q)/q)])/(2 d2));

(* I51m[1/a [1/(1-p)] + 1/b [p] + 1/d [1-1/p], 1/a2 [1/(1-q)] + 1/b2 [q] + 1/d2[1 - 1/q]]

(*

(* (1/a2 I51mi[1/a [1/(1-p)] + 1/b [p] + 1/d [1-1/p], 1/(1-q)] + 1/b2 I51mi[1/a [1/(1-p)] + 1/b [p] + 1/d [1-1/p], q] + 1/d2 I51mi[1/a [1/(1-p)] + 1/b [p] + 1/d [1-1/p], 1-1/q])
-(1/a I51mi[1/(1-p),  1/a2 [1/(1-q)] + 1/b2 [q] + 1/d2[1 - 1/q]] + 1/b I51mi[p,  1/a2 [1/(1-q)] + 1/b2 [q] + 1/d2[1 - 1/q]] + 1/d I51mi[1-1/p,  1/a2 [1/(1-q)] + 1/b2 [q] + 1/d2[1 - 1/q]]) = 0

(* I51mi[1/a [1/(1-p)] + 1/b [p] + 1/d [1-1/p],y] = li6termsFullXslot[y]
I51mi[x,   1/a2 [1/(1-q)] + 1/b2 [q] + 1/d2[1 - 1/q]] = li6termsFullQYslot[x]

(* (1/a2 I51mi[1/a [1/(1-p)] + 1/b [p] + 1/d [1-1/p], 1/(1-q)] + 1/b2 I51mi[1/a [1/(1-p)] + 1/b [p] + 1/d [1-1/p], q] + 1/d2 I51mi[1/a [1/(1-p)] + 1/b [p] + 1/d [1-1/p], 1-1/q])
-(1/a I51mi[1/(1-p),  1/a2 [1/(1-q)] + 1/b2 [q] + 1/d2[1 - 1/q]] + 1/b I51mi[p,  1/a2 [1/(1-q)] + 1/b2 [q] + 1/d2[1 - 1/q]] + 1/d I51mi[1-1/p,  1/a2 [1/(1-q)] + 1/b2 [q] + 1/d2[1 - 1/q]]) = 0

fe = ((1/a2 li6termsFullXslot[1/(1-q)] + 1/b2 li6termsFullXslot[q] + 1/d2 li6termsFullXslot[1-1/q])/.{L6i->Liq,Lip->Lipq}) - 
((1/a li6termsFullQYslot[1/(1-p)] + 1/b li6termsFullQYslot[p] + 1/d li6termsFullQYslot[1-1/p])/.{L6i->Lip,Liq->Lipq});

coeffsimp[expr_] := Plus@@(#[[1]]#[[2]]&/@(({Factor[Plus@@#[[All,1]]],#[[1,2]]})&/@(expr//Cases[#,a_ b_Liq|a_ b_Lipq| a_ b_Lip:>{a,b}]&//GatherBy[#, #[[2]]&]&)))

fesimp = fe//Lleadu//Lleadt//L6can//L6pcan//L6qcan//L6pqcan//Expand;

result= fesimp//coeffsimp

fesimp - result/.{d->-a-b,d2->-a2-b2}//Simplify

(* ------- *)

g[p,y_] := (1-y)^b y^a;
g[1/(1-p), y_] := (y-1)^a y^d;
g[1-1/p, y_] := (1-y)^d( -y)^b;

g[q,y_] := (1-y)^b2 y^a2;
g[1/(1-q), y_] := (y-1)^a2 y^d2;
g[1-1/q, y_] := (1-y)^d2( -y)^b2;

(result - (+L6sym[((-1)/(2 a^2 b d a2 (a-b) (a- d) )),(t/(g[p,q])), {oneMinusOneOverP->True, oneMinusOneOverQ->True,sumQ->True}]+
+L6sym[((-1)/(2 a b^2 d a2 (a-b) (b-d) )),(t/(g[p,1-q])), {oneMinusOneOverP->True, oneMinusOneOverQ->True,sumQ->True}]+

L6sym[((-1)/(2d  a2 b2 (a2- d2) (b2 - d2) (d2^2))),(u/(g[q,p])), {oneMinusOneOverQ->True,oneMinusOneOverP->True, sumP->True}]+
+L6sym[(1/(2d  a2 b2 (a2- d2) (b2 - d2) (d2^2))),(u/(g[q,1-p])), {oneMinusOneOverQ->True,oneMinusOneOverP->True, sumP->True}]+

+L6sym[(((b^3)+2 (d^3))/(4 a (b^2) b2 d))-(((b2^3)+2 (d2^3))/(4 a2 b (b2^2) d2)),  p/q, {oneMinusOneOverP->True,oneMinusOneOverQ->True, sumP->True,sumQ->True}]   + 
+L6sym[(((a2 b-a b2) (a a2-b b2) )/(12 a  b (a-b) a2  b2(a2-b2))),  (((1-p) p)/((1-q) q)), {oneMinusOneOverP->True,oneMinusOneOverQ->True, sumP->True,sumQ->True}]+

+L6sym[((2 (a^3)+ b^3)/(4a  (b^2) d b2))+((2 (a2^3)+(b2^3))/(4 a2 b (b2^2) d2))- (3/(2 b b2)) ,  p q, {oneMinusOneOverP->True,oneMinusOneOverQ->True, sumP->True,sumQ->True}]+

+L6sym[-((((a^2)+a b+(b^2)) )/(6 a b d b2)), q, {oneMinusOneOverQ->True,oneMinusOneOverP->True,sumP->True, sumQ->True}]+

+L6sym[-(((a2-b2) ((a2^2)+a2 b2+(b2^2)))/(2 a2 b b2 d2)),  p, {oneMinusOneOverP->True, sumP->True}]+
+L6sym[((((a^2)+a b+(b^2)))/(a d b2)),  q, {oneMinusOneOverQ->True, sumQ->True}]
)/.{d->-a-b,d2->-a2-b2}//Lleadu//Lleadt//L6can//L6pcan//L6qcan//L6pqcan//Expand//coeffsimp)/.{(-1)^(-a2)->(-1)^(a2),(-1)^(-b2)->(-1)^(b2),(-1)^(-a2-b2)->(-1)^(a2+b2),(-1)^(-a)->(-1)^(a),(-1)^(-b)->(-1)^(b),(-1)^(-a-b)->(-1)^(a+b)};
Length[%]
%%

Hold[L6sym[((-1)/(2 a^2 b d a2 (a-b) (a- d) )),(t/(g[p,q])), {oneMinusOneOverP->True, oneMinusOneOverQ->True,sumQ->True}]+
+L6sym[((-1)/(2 a b^2 d a2 (a-b) (b-d) )),(t/(g[p,1-q])), {oneMinusOneOverP->True, oneMinusOneOverQ->True,sumQ->True}]+

L6sym[((-1)/(2d  a2 b2 (a2- d2) (b2 - d2) (d2^2))),(u/(g[q,p])), {oneMinusOneOverQ->True,oneMinusOneOverP->True, sumP->True}]+
+L6sym[(1/(2d  a2 b2 (a2- d2) (b2 - d2) (d2^2))),(u/(g[q,1-p])), {oneMinusOneOverQ->True,oneMinusOneOverP->True, sumP->True}]+

+L6sym[(((b^3)+2 (d^3))/(4 a (b^2) b2 d))-(((b2^3)+2 (d2^3))/(4 a2 b (b2^2) d2)),  p/q, {oneMinusOneOverP->True,oneMinusOneOverQ->True, sumP->True,sumQ->True}]   + 
+L6sym[(((a2 b-a b2) (a a2-b b2) )/(12 a  b (a-b) a2  b2(a2-b2))),  (((1-p) p)/((1-q) q)), {oneMinusOneOverP->True,oneMinusOneOverQ->True, sumP->True,sumQ->True}]+

+L6sym[((2 (a^3)+ b^3)/(4a  (b^2) d b2))+((2 (a2^3)+(b2^3))/(4 a2 b (b2^2) d2))- (3/(2 b b2)) ,  p q, {oneMinusOneOverP->True,oneMinusOneOverQ->True, sumP->True,sumQ->True}]+

+L6sym[-((((a^2)+a b+(b^2)) )/(6 a b d b2)), q, {oneMinusOneOverQ->True,oneMinusOneOverP->True,sumP->True, sumQ->True}]+

+L6sym[-(((a2-b2) ((a2^2)+a2 b2+(b2^2)))/(2 a2 b b2 d2)),  p, {oneMinusOneOverP->True, sumP->True}]+
+L6sym[((((a^2)+a b+(b^2)))/(a d b2)),  q, {oneMinusOneOverQ->True, sumQ->True}]
]/.{d->c,a2->d,b2->e,d2->f}

(* ------- *)

case1 = result/.{a->1,a2->1,b->2,b2->2,d->-3,d2->-3}

LiqX[arg_] := Sum[L6i[arg]/.{q->qx[i]},{i, 1, 3}];
LipX[arg_] := Sum[L6i[arg]/.{p->px[i]},{i, 1, 3}];
LipqX[arg_] := Sum[L6i[arg]/.{p->px[i],q->qx[j]},{i, 1, 3}, {j, 1, 3}];

px[1]=1/(1-x+x^2);
px[2]=px[1]/.x->1-1/x//Factor;
px[3]=px[2]/.x->1-1/x//Factor;

qx[1]=1/(1-y+y^2);
qx[2]=qx[1]/.y->1-1/y//Factor;
qx[3]=qx[2]/.y->1-1/y//Factor;

ux = qx[3](1-qx[3])^2//Factor;

tx = px[3](1-px[3])^2//Factor;

case1/.{u->ux,t->tx}/.{Lip->LipX,Liq->LiqX,Lipq->LipqX}//L6can//Expand