<<PolylogTools`

I51[x_,y_]:=G[y,0,0,0,0,x,1];
I51p[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]={oneMinusP->False,oneMinusQ->False, sumP->False,sumQ->False};
L6sym[coeff_,arg_,opts:OptionsPattern[]]:=Module[{oneMinusPTF,oneMinusQTF,sumPTF,sumQTF,coeffs,args},(
oneMinusPTF=OptionValue[oneMinusP]==True;
oneMinusQTF=OptionValue[oneMinusQ]==True;
sumPTF=OptionValue[sumP]==True;
sumQTF=OptionValue[sumQ]==True;

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

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

If[oneMinusQTF,
coeffs=Join[coeffs,-coeffs/.{HoldPattern[a2]->b2,HoldPattern[b2]->a2}];
args=Join[args,args/.{q->(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/(b*d*a*b*(b-a)*(2a + b)) * L6i[t/((1-y)^b y^a)]+
-1/(a*d*a*b*(b-a)*(a + 2b)) * L6i[t/((1-y)^a y^b)]+
-1/(b*d*a*b*(b-a)*(2a + b))*L6i[t/((y-1)^b y^d)]+
-1/(a*d*a*b*(b-a)*(a + 2b))*L6i[t/((y-1)^a y^d)]+

+((-1)/d) Lip[1-(1/p)]+((-1)/(2a)) Lip[(1/(1-p))]+(1/(2b))Lip[(1/p)]+
((-1)/(2d)) Lip[(1/((1-(1/p)) (1-y)))]-((-1)/(2d)) Lip[(((-1+(1/p)) (1-y))/y)]+
(1/(2d)) Lip[((1-(1/p))/(1-y))]-(1/(2d))Lip[((-1+y)/((1-(1/p)) y))]+
((-1)/a) Lip[((1-p)/(1-y))]-((-1)/a) Lip[-((1-y)/((1-p) y))]+
(1/b) Lip[(p/(1-y))]-(1/b)Lip[-((1-y)/(p y))]+
(1/(6(b-a))) Lip[((1-y)/((1-(1/p)) (p^2) (y^2)))]+ (1/(6(b-a)))Lip[(((-1+y) y)/((1-(1/p)) (p^2)))]+
((9a+2b)/(4a^2)) Lip[(1/((1-p) y))]+((9a+2b)/(4a^2)) Lip[(y/(1-p))]+
((2a+9b)/(4b^2)) Lip[(p/y)]+((2a+9b)/(4b^2)) Lip[p y]+
-(1/(12(2a+b))) Lip[((((1-y))^2)/((((1-(1/p)))^2) p y))]-(1/(12(a + 2b))) Lip[-((p y)/((1-(1/p)) (((1-y))^2)))];

li6termsFullQYslot[x_] := ((L6i[u (((1-x))^(-b2)) (x^(-a2))])/(a2 (b2^2) (-a2+b2) (2 a2+b2) d2))+((L6i[u (((1-x))^(-a2)) (x^(-b2))])/((a2^2) b2 (-a2+b2) (a2+2 b2) d2))+((L6i[u (((-1+x))^(-a2)) (x^(-d2))])/((a2^2) b2 (-a2+b2) (a2+2 b2) d2))+((L6i[u (((-1+x))^(-b2)) (x^(-d2))])/(a2 (b2^2) (-a2+b2) (2 a2+b2) d2))+((Liq[1-(1/q)])/d2)+((Liq[(1/(1-q))])/(2 a2))-((Liq[(1/q)])/(2 b2))+((Liq[(1/((1-(1/q)) (1-x)))])/(2 d2))-((Liq[((1-(1/q))/(1-x))])/(2 d2))+((Liq[((1-q)/(1-x))])/a2)-((Liq[(q/(1-x))])/b2)-((Liq[((1-x)/((1-(1/q)) (q^2) (x^2)))])/(6 (-a2+b2)))-(((9 a2+2 b2) Liq[(1/((1-q) x))])/(4 (a2^2)))-(((2 a2+9 b2) Liq[(q/x)])/(4 (b2^2)))-((Liq[(((-1+(1/q)) (1-x))/x)])/(2 d2))-((Liq[-((1-x)/((1-q) x))])/a2)+((Liq[-((1-x)/(q x))])/b2)+((Liq[((((1-x))^2)/((((1-(1/q)))^2) q x))])/(12 (2 a2+b2)))+((Liq[((-1+x)/((1-(1/q)) x))])/(2 d2))-(((9 a2+2 b2) Liq[(x/(1-q))])/(4 (a2^2)))-(((1/2) Liq[1-q]+((Liq[x])/2)+(5/2) Liq[(1-q) x])/a2)-(((2 a2+9 b2) Liq[q x])/(4 (b2^2)))+((((Liq[q])/2)+((Liq[x])/2)+(5/2) Liq[q x])/b2)+((Liq[-((q x)/((1-(1/q)) (((1-x))^2)))])/(12 (a2+2 b2)))-((Liq[(((-1+x) x)/((1-(1/q)) (q^2)))])/(6 (-a2+b2)))

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

(* -1/a2 i51p[-1/a[1-p] + 1/b[p], 1-q] + 1/b2 i51p[-1/a[1-p] + 1/b[p], q] = i51p[1/a[1 - p] + 1/b[p], -1/a2[1 - q] + 1/b2[q]] =
-1/a i51p[1-p, -1/a2 [1-q] + 1/b2[q]] + 1/b i51p[p, -1/a2[1-q] + 1/b2[q]]

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]]&]&)))

fe = (((-1/a2 li6termsFullXslot[1-q]+ 1/b2 li6termsFullXslot[q] )/.{L6i->Liq,Lip->Lipq}) - 
((-1/a li6termsFullQYslot[1-p] + 1/b li6termsFullQYslot[p])/.{L6i->Lip,Liq->Lipq}))//Expand//L6pcan//L6qcan//L6pqcan//Lleadu//Lleadt;

result = fe/.{d->-a-b,d2->-a2-b2}//coeffsimp

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

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

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

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

result - (L6sym[((-1)/( (a^2) b d(a-b) (b-d)a2 )),(t/(g[p,q])),{oneMinusP->True,oneMinusQ->True,sumQ->True}]+L6sym[(1/(a  (b^2)d(a-b)  (a-d)b2  )),(t/(g[(1/p),q])),{oneMinusP->True,oneMinusQ->True,sumQ->True}]+
 L6sym[(1/( b a2 (b2^2) (a2-b2) (a2-d2) d2)),(u/(g[q,p])),{oneMinusP->True,oneMinusQ->True,sumP->True}]+
L6sym[(1/( b a2 (b2^2) (a2-b2) (a2-d2) d2)),(u/(g[(1/q),p])),{oneMinusP->True,oneMinusQ->True,sumP->True}]+

L6sym[(((a b2-a2 b))/(6 b (2 a+b) b2 ( a2- d2))),((p (((1-q))^2))/((((1-p))^2) q)),{oneMinusP->True,oneMinusQ->True,sumP->True,sumQ->True}]+
L6sym[-(1/(12 b (a2-b2))),((p-1)/((p^2) (1-q) q)),{oneMinusP->True,oneMinusQ->True,sumP->True,sumQ->True}]+
L6sym[-(1/(12(a-b) b2)),((q-1)/((1-p) p (q^2))),{oneMinusP->True,oneMinusQ->True,sumP->True,sumQ->True}]+

L6sym[(((a2 b-a b2) (a a2-b b2))/(24 a a2 (a-b) b (a2-b2) b2)),(((1-p) p)/((1-q) q)),{oneMinusP->True,oneMinusQ->True,sumP->True,sumQ->True}]+

L6sym[(((a2 b-a b2) (a a2+b b2))/(4 a  b d a2 b2 d2)),((p (1-q))/((1-p) q)),{oneMinusP->True,oneMinusQ->True,sumP->True,sumQ->True}]+
L6sym[(((a2 b+2 d b2) )/(2 a2 b d b2)),((p-1)/(p (1-q))),{oneMinusP->True,oneMinusQ->True,sumP->True,sumQ->True}]+
L6sym[-(((2 a d2+a2 b))/(2 a  b a2 d2)),((p (q-1))/q),{oneMinusP->True,oneMinusQ->True,sumP->True,sumQ->True}]+

L6sym[-(((a2 b-a b2) (a a2-2 b b2) )/(2 a (b^2) a2 (b2^2))),(p/q),{oneMinusP->True,oneMinusQ->True,sumP->True,sumQ->True}]+
L6sym[(((a2 b+a b2+4 b b2) )/(2 (b^2) (b2^2))),p q,{oneMinusP->True,oneMinusQ->True,sumP->True,sumQ->True}]+

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

Hold[ L6sym[((-1)/( (a^2) b d(a-b) (b-d)a2 )),(t/(g[p,q])),{oneMinusP->True,oneMinusQ->True,sumQ->True}]+L6sym[(1/(a  (b^2)d(a-b)  (a-d)b2  )),(t/(g[(1/p),q])),{oneMinusP->True,oneMinusQ->True,sumQ->True}]+
 L6sym[(1/( b a2 (b2^2) (a2-b2) (a2-d2) d2)),(u/(g[q,p])),{oneMinusP->True,oneMinusQ->True,sumP->True}]+
L6sym[(1/( b a2 (b2^2) (a2-b2) (a2-d2) d2)),(u/(g[(1/q),p])),{oneMinusP->True,oneMinusQ->True,sumP->True}]+

L6sym[(((a b2-a2 b))/(6 b (2 a+b) b2 ( a2- d2))),((p (((1-q))^2))/((((1-p))^2) q)),{oneMinusP->True,oneMinusQ->True,sumP->True,sumQ->True}]+
L6sym[-(1/(12 b (a2-b2))),((p-1)/((p^2) (1-q) q)),{oneMinusP->True,oneMinusQ->True,sumP->True,sumQ->True}]+
L6sym[-(1/(12(a-b) b2)),((q-1)/((1-p) p (q^2))),{oneMinusP->True,oneMinusQ->True,sumP->True,sumQ->True}]+

L6sym[(((a2 b-a b2) (a a2-b b2))/(24 a a2 (a-b) b (a2-b2) b2)),(((1-p) p)/((1-q) q)),{oneMinusP->True,oneMinusQ->True,sumP->True,sumQ->True}]+

L6sym[(((a2 b-a b2) (a a2+b b2))/(4 a  b d a2 b2 d2)),((p (1-q))/((1-p) q)),{oneMinusP->True,oneMinusQ->True,sumP->True,sumQ->True}]+
L6sym[(((a2 b+2 d b2) )/(2 a2 b d b2)),((p-1)/(p (1-q))),{oneMinusP->True,oneMinusQ->True,sumP->True,sumQ->True}]+
L6sym[-(((2 a d2+a2 b))/(2 a  b a2 d2)),((p (q-1))/q),{oneMinusP->True,oneMinusQ->True,sumP->True,sumQ->True}]+

L6sym[-(((a2 b-a b2) (a a2-2 b b2) )/(2 a (b^2) a2 (b2^2))),(p/q),{oneMinusP->True,oneMinusQ->True,sumP->True,sumQ->True}]+
L6sym[(((a2 b+a b2+4 b b2) )/(2 (b^2) (b2^2))),p q,{oneMinusP->True,oneMinusQ->True,sumP->True,sumQ->True}]+

L6sym[-(((a-b) )/(2 a b b2)),q,{oneMinusP->True,oneMinusQ->True,sumP->True,sumQ->True}]+
L6sym[-(((a-b))/(4a b d2)),((-1+q)/q),{oneMinusP->True,oneMinusQ->True,sumP->True,sumQ->True}]+
L6sym[-(((a2-b2) )/(2a2 b b2)),p,{oneMinusP->True,oneMinusQ->True,sumP->True,sumQ->True}]+
L6sym[-(((a2-b2))/(4d a2 b2)),((-1+p)/p),{oneMinusP->True,oneMinusQ->True,sumP->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