BeginPackage["multipleZeta`",{"loopTool`"}];

[変数]

hplZC::usage="";

hplZ::usage="";

hplSC::usage="";

hplHC::usage="";

hplH::usage="";

hplSinf::usage="";

hplδ::usage="";

hplN::usage="";

hplI::usage="";

hplSCInt::usage="";

hpl2F1::usage="";

[表示の変換]

hplRepS::usage="";

hplRepZ::usage="";

hplZ2S::usage="";

hplS2Z::usage="";

hplSumToInt::usage="";

hplIntToSum::usage="";

hplEliminateOffsets::usage="";

hplEliminatePositivePower::usage="";

hplZSumToMPL::usage="";

hplZSumToMPL2::usage="";

hplZ2I::usage="";

[数値近似等]

hplZSum::usage="";

hplZNSum::usage="";

hplSSum::usage="";

hplSNSum::usage="";

hplHIntegrate::usage="";

hplHNIntegrate::usage="";

[Shuffle代数]

hplShuffle::usage="";

hplShuffleAll::usage="";

[収束性・極限]

hplConvergenceQ::usage="";

hplZeroQ::usage="";

hplLimit::usage="";

hplSeries::usage="";

[複素共役・微分・積分]

hplIntegrate::usage="";

[級数表示での引数の変換]

hplMultiply::usage="";

hplNegate::usage="";

hplShift::usage="";

hplEliminateNonPositiveIntegers::usage="";

hplExpandProjector::usage="";

[積分表示での引数の変換]

hplArgTransform::usage="";

[PowerLog, Cutoff関連]

hplHPLToLog::usage="";

hplLogToHPL::usage="";

hplLogExtract::usage="";

hplZHNExtract::usage="";

hplZSinfExtract::usage="";

[2F1の展開係数]

hplD2F1::usage="";

[多重和の評価]

hplSum::usage="";

$hplSumState::usage="";

[SCInt関連]

hplSTrf::usage="";

hplS2Trf::usage="";

hplTTrf::usage="";

hplT2Trf::usage="";

hplZTrf::usage="";

hplZ2Trf::usage="";

hplNTrf::usage="";

hplSum::usage="";

hplSCIntToMZV::usage="";

[HPLの簡約化]

hplDepth::usage="";

hplLevel::usage="";

hplOrder::usage="";

[MZV関連]

hplMZVSimplify::usage="";

Begin["`private`"]

Unprotect["multipleZeta`*"];

Unprotect["multipleZeta`private`*"];

[共通の関数]

myExpand=Developer`ReplaceAllUnheld[#,Times->(Distribute[Times[##],Plus,Times]&)]&;

hplZCanonical=hplSCanonical=hplHCanonical=Identity;

checkZ[in_]:=MatchQ[in,hplZC[a_List,_,c_List,d_List]/;Length[a]==Length[c]==Length[d]]

checkH[in_]:=MatchQ[in,hplHC[a_List,_,c_List]/;Length[a]==Length[c]]

cExpand[in_]:=
Module[{F},
Expand[
in/.{
(((x_-1))^(p_.)):>(((-1))^p)((F[1-x])^p),(((1-x_))^(p_.)):>((F[1-x])^p),(((1+x_))^(p_)):>((F[1+x])^p),
(((x_-I))^(p_.)):>(((-I))^p)((F[1+I x])^p),(((I-x_))^(p_.)):>(((I))^p)((F[1+I x])^p),(((I+x_))^(p_.)):>(((I))^p)((F[1-I x])^p),
(((I x_-1))^(p_.)):>(((-1))^p)((F[1-I x])^p),(((-I x_-1))^(p_.)):>(((-1))^p)((F[1+I x])^p)
}
]/.F->Identity
];

[表示の変換]

hplRepS[in_]:=
Module[{data,mm},
data=in/.t:hplZC[a_,b_,c_,d_]/;checkZ[t]:>mm[Sequence@@Transpose[{a,c,d,0a}],b];
data=data//.mm[x___,{a_,b_,c_,d_},{e_,f_,g_,0},y___,z_]:>
If[c===g,
mm[x,{a,b,c,d},{e,f,g,1},y,z]-mm[x,{a+e,b f,c,d},y,z],
mm[x,{a,b,c,d},{e,f,g,1},y,z]-
Total[#[[1]]mm[x,{#[[2]],b f,#[[3]],d},y,z]&/@tc[{a,e},{c,g}]]
];
data=data/.mm[a___,_]/;MemberQ[{a}[[1;;,2]],0]->0;
data/.mm[a___,b_]:>hplSC[{a}[[1;;,1]],b,{a}[[1;;,2]],{a}[[1;;,3]]]/.
a_ projω[b_,c_,d_]:>projω[b,c,a d]//Expand
];

hplRepZ[in_]:=
Module[{data,mm},
data=in/.t:hplSC[a_,b_,c_,d_]/;checkZ[hplZC@@t]:>mm[Sequence@@Transpose[{a,c,d,0a}],b];
data=data//.mm[x___,{a_,b_,c_,d_},{e_,f_,g_,0},y___,z_]:>
If[c===g,
mm[x,{a,b,c,d},{e,f,g,1},y,z]+mm[x,{a+e,b f,c,d},y,z],
mm[x,{a,b,c,d},{e,f,g,1},y,z]+
Total[#[[1]]mm[x,{#[[2]],b f,#[[3]],d},y,z]&/@tc[{a,e},{c,g}]]
];
data=data/.mm[a___,_]/;MemberQ[{a}[[1;;,2]],0]->0;
data=data/.mm[a___,b_]:>hplZC[{a}[[1;;,1]],b,{a}[[1;;,2]],{a}[[1;;,3]]]/.
a_ projω[b_,c_,d_]:>projω[b,c,a d]//Expand
];

hplZ2S[in_]:=
Module[{data},
in/.t:hplZC[a_List,b_,c_List,d_List]/;checkZ[t]:>
With[{dp=Range[1-Length[a],0]},
hplSC[a,b+1-Length[a],MapThread[If[#1[[0]]===projω,projω[#1[[1]],#1[[2]]+#2,#1[[3]]],#1]&,{c,dp}],d-dp]/
(Times@@(c^dp)/.projω[_,_,s_]:>s)
]
];

hplS2Z[in_]:=
Module[{data},
in/.t:hplSC[a_List,b_,c_List,d_List]/;checkZ[hplZC@@t]:>
With[{dp=Range[1-Length[a],0]},hplZC[a,b-1+Length[a],MapThread[If[#1[[0]]===projω,projω[#1[[1]],#1[[2]]-#2,#1[[3]]],#1]&,{c,dp}],d+dp]*
(Times@@(c^dp)/.projω[_,_,s_]:>s)
]
];

Options[hplSumToInt]={VerifyConvergence->True};

hplSumToInt[in_,OptionsPattern[]]:=
hplExpandProjector[hplRepZ[in]]/.
x:hplZC[a:{(_Integer?Positive..)},∞,b_,c_]/;
checkZ[x]&&((hplConvergenceQ[x]=!=False)||OptionValue[VerifyConvergence]===False):>
Module[{i,tmp,tmp2},
tmp=Transpose[{Rest[FoldList[Divide,1,b]],-Differences[Append[c,0]]}];
{tmp,tmp2}=
Join@@Table[
Append[ConstantArray[{0,0},a[[i]]-1],tmp[[i]]],
{i,Length[a]}
]//Transpose;
hplHC[tmp,1,tmp2](-1)^Length[a]
];

Options[hplIntToSum]={VerifyConvergence->True};

hplIntToSum[in_,OptionsPattern[]]:=
in/.x:hplHC[a_List,b_,c_]/;checkH[x]:>
Module[{res,pos,arg1,tmp,arg2,arg3},
Catch[
If[!FreeQ[a,Function]||a==={}||Last[a]==0,Throw[x]];
If[MemberQ[Transpose[{a,c}],{0,Except[0]}],Throw[x]];
pos=Position[a,Except[0],{1},Heads->False];
If[MemberQ[Delete[c,pos],Except[0]],Throw[x]];
arg1=Flatten[pos]-Prepend[Most[Flatten[pos]],0];
tmp=Extract[a,pos]/b;
arg2=1/tmp Prepend[Most[tmp],1];
tmp=Extract[c,pos];
arg3=Reverse[Accumulate[Reverse[tmp]]];
If[OptionValue[VerifyConvergence]=!=False&&
hplConvergenceQ[hplZC[arg1,∞,arg2,0arg1]]===False,Throw[x]];
hplZC[arg1,∞,arg2,arg3](-1)^Length[pos]Times@@(b^tmp)]
];

Options[hplEliminateOffsets]={VerifyConvergence->True};

hplEliminateOffsets[in_,OptionsPattern[]]:=
Module[{tmp,uuu,vvv},
tmp=in/.a_hplZC/;checkZ[a]&&FreeQ[a,projω]:>fk[a,OptionValue[VerifyConvergence]];
vvv=Cases[tmp,hplZC[_,∞,_,_],{0,∞}]//Union;
uuu=hplSumToInt[vvv]/.a_hplHC:>hplEliminatePositivePower[a];
uuu=hplIntToSum[uuu/.hplEliminatePositivePower->Identity];
tmp/.Thread[vvv->uuu]//Expand
];

fk[in_,verify_]:=
Module[{tmp,v,cond,X,xx,Y},
tmp=in;
v=Reap[
tmp=tmp/.hplZC[a__,b_,c__,d:{p_ ..}]/;Negative[p]=!=True&&(p∈Integers)=!=False:>
(Sow[{p}];X[Transpose[{a,d}],c,
Thread[{Array[xx,Length[a]],1,Prepend[Array[xx,Length[a]-1]-1,b]}]]);
][[2]]//Flatten;
cond=#≥0&/@v;
tmp=tmp//.X[{a___,{b_,c:Except[0]},d___},e_,f_]:>
((e[[Length[{a}]+1]])^(-c))X[{a,{b,0},d},e,f/.xx[Length[{a}]+1]->xx[Length[{a}]+1]-c];
tmp=tmp/.{xx[a_]+b_,c_,d_}:>{xx[a],c-b,d-b};
tmp=tmp//.X[a_,b_,{c___,{xx[d_],e_/;(e=!=1&&Refine[e≥1,cond]),f_},g___}]:>
X[a,b,{c,{xx[d],1,f},g}]-X[a,b,{c,{xx[d],1,e-1},g}];
tmp=tmp//.
{X[a_,b_,{c___,{xx[d_],e_,xx[f_]+g_./;Refine[g>-1,cond]},h___}]:>
X[a,b,{c,{xx[d],e,-1+xx[f]},h}]+Y[a,b,{c,{xx[d],xx[f],xx[f]+g},h}],X[a_,b_,{c___,{xx[d_],e_,xx[f_]+g_./;Refine[g<-1,cond]},h___}]:>
X[a,b,{c,{xx[d],e,-1+xx[f]},h}]-Y[a,b,{c,{xx[d],xx[f]+g+1,-1+xx[f]},h}]};
If[!FreeQ[tmp,Y],Abort[]];
tmp=tmp//.X[a_,b_,{c__,{xx[d_],e_,f_},g___}]/;FreeQ[{e,f},xx]:>
X[a[[1;;Length[{c}]]],b[[1;;Length[{c}]]],{c}]*
X[a[[Length[{c}]+1;;]],b[[Length[{c}]+1;;]],{{xx[d],e,f},g}];
tmp=tmp/.X[a_,b_,{{xx[_],1,p_},{_xx,1,_xx-1}...}]:>hplZC[a[[1;;,1]],p,b,a[[1;;,2]]];
If[!FreeQ[tmp,X],Abort[]];
tmp=tmp/.a:hplZC[_,_Integer,__]:>(hplZSum@@a)//Quiet;
If[!FreeQ[tmp,Indeterminate|(a_hplZC/;TrueQ[verify]&&!hplConvergenceQ[a])],Return[in]];
Expand[tmp]
];

Options[hplZSumToMPL]={VerifyConvergence->True};

hplZSumToMPL[in_,OptionsPattern[]]:=hplExpandProjector[in]/.a_hplZC?checkZ:>fff[a,OptionValue[VerifyConvergence]]//Expand;

fff[in_hplZC,conv_]:=
FixedPoint[
#/.p:hplZC[a_,m_/;FreeQ[m,hplN|DirectedInfinity],c_,d_]/;(hplConvergenceQ[p]||!conv):>
Module[{tmp,var,n},
tmp=hplZC[a,hplN,c,d]-
Total[
Table[
(((Times@@c[[1;;i]]))^n)hplZC[a[[1;;i]],hplN,c[[1;;i]],d[[1;;i]]+n]*
hplZC[a[[i+1;;]],n,c[[i+1;;]],d[[i+1;;]]],
{i,Length[a]}
]
]/.hplZC[{},hplN|n,{},{}]->1;
tmp/.v_hplZC/;hplConvergenceQ[v/.hplN->∞/.n->0]:>(v/.hplN->∞)/.n->m
]&,
in
]

hplZSumToMPL2[in_,v_]:=
Module[{tmp},
tmp=in/.p:hplZC[_,_+c_?(MatchQ[#,v]&),_,_]/;checkZ[p]:>
FixedPoint[
#/.q:hplZC[_,c,_,_]/;checkZ[q]:>
hplZSumToMPL[hplZHNExtract[q]]&,
hplShift[p,c,"useOffset"->True]
];
tmp/.{
p:hplZC[_,_Integer,_,_]/;checkZ[p]:>hplZSum@@p,
p:HarmonicNumber[_?NumericQ]:>PowerExpand[FunctionExpand[p]]
}//Expand
];

Options[hplZ2I]={"shuffle"->hplH,"simplifyIntegrand"->False};

hplZ2I//Clear;
hplZ2I[in_,δ_,r_,OptionsPattern[]]:=
Module[{tmp,ret},
ret/;Catch[
If[OptionValue["shuffle"]=!=hplH,Throw[False]];
tmp=in;
tmp=hplZSumToMPL[in/.hplZC[a_,b_,c_,d_]:>hplZC[a,b,Replace[c,1->1-δ,{1}],d],VerifyConvergence->False]/.hplN->∞;
tmp=hplSumToInt[tmp,VerifyConvergence->False];
If[!FreeQ[tmp,hplZC],Throw[False]];
tmp=hplShuffleAll@tmp/.δ->0;
tmp=Block[{Integrate=hplI,Hypergeometric2F1=hpl2F1,$xx=r},
tmp=tmp/.x_hplHC/;hplConvergenceQ[x/.δ->0]&&!FreeQ[Flatten[List@@x/.δ->0],_?(!NumericQ[#]&)]:>(x/.δ->0);
tmp/.x_hplHC:>Most[hplHIntegrate@@x]
];
tmp=tmp/.a:(_hplZC|_hplHC)/;NumericQ[a[[2]]]:>hplMZVSimplify[a];
ret=tmp/.hplI[k_,m__]:>hplI[k,Sequence@@({m}/.{a_,0,1}:>{a,δ,1-δ}/.{a_,0,b_}:>{a,δ,b})];
True
]
];

hplZ2I[in_,δ_,xx_,OptionsPattern[]]:=
Module[{ret,tmp},
ret/;Catch[
If[OptionValue["shuffle"]=!=hplZ||OptionValue["simplifyIntegrand"]=!=False,Throw[False]];
tmp=in;
tmp=hplZSumToMPL[in/.hplZC[a_,b_,c_,d_]:>hplZC[a,b,Replace[c,1->(E^(-δ)),{1}],d],VerifyConvergence->False]/.hplN->∞;
tmp=FixedPoint[
Expand[#/.{
(a:hplZC[_,_,_,{b_ ...}])(c:hplZC[_,_,_,{b_ ...}]):>hplShuffleAll[a c],
(a:((hplZC[_,_,_,{b_ ...}])^(_))):>hplShuffleAll[a]
}]&,tmp
];
tmp=hplSumToInt[tmp,VerifyConvergence->False];
If[!FreeQ[tmp,hplZC],Throw[False]];
tmp=hplShuffleAll[tmp];
tmp=Block[{Integrate=hplI,Hypergeometric2F1=hpl2F1,$xx=xx},
tmp=tmp/.x_hplHC/;hplConvergenceQ[x/.δ->0]&&!FreeQ[Flatten[List@@x/.δ->0],_?(!NumericQ[#]&)]:>(x/.δ->0);
tmp/.x_hplHC:>Most[hplHIntegrate@@x]
];
ret=tmp/.a:(_hplZC|_hplHC)/;NumericQ[a[[2]]]:>hplMZVSimplify[a]//PowerExpand;
True
]
];

hplZ2I[in_,δ_,xx_,OptionsPattern[]]:=
Module[{ret,tmp},
ret/;Catch[
If[OptionValue["shuffle"]=!=hplZ||OptionValue["simplifyIntegrand"]=!=True,Throw[False]];
tmp=in;
tmp=tmp/.a:HoldPattern[Times[(((_hplZC)^(_.)))...]]:>tttt[a,δ,xx];
If[!FreeQ[tmp,tttt],Throw[False]];
tmp=tmp/.a:((_hplZC)^(_.)):>tttt[a,δ,xx];
If[!FreeQ[tmp,tttt],Throw[False]];
ret=Expand[tmp]/.hplI[0,__]->0;
True
]
];

tttt//Clear;
tttt[in_,δ_,xx_]:=
Module[{tmp,var1,var2,var3,var4,repZ,repI,val1,val2,lTotal,var5,
dVar,var6,repl,dval1,dval2,diff,coeff,ppp,yy,zz,p,ret,inf},
ret/;Catch[
tmp=in;
If[!MatchQ[tmp,(((hplZC[_,_,_,{0..}])^(a_.))/;IntegerQ[a]&&a≥0)]&&
!(tmp[[0]]===Times&&And@@(MatchQ[#,(((hplZC[_,_,_,{0..}])^(a_.))/;IntegerQ[a]&&a≥0)]&/@List@@tmp)),
Throw[False]
];
var1=Cases[tmp,_hplZC,{0,∞}];
var2=var1[[1;;,2]]//Union;
var3=Array[yy,Length[var2]];
var4=MapAt[Replace[#,Thread[var2->var3]]&,#,{2}]&/@var1;
repZ=tmp/.Thread[var1->var4];
lTotal=Replace[repZ,Times->Plus,{1},Heads->True]/.((a_hplZC)^(b_)):>b a/.a_hplZC:>hplLevel[a];
var5=Cases[hplShuffleAll[repZ],_hplZC,{0,∞}]//Union;
var5=FixedPoint[Union[Cases[{#,hplIdZSum/@#},_hplZC,{0,∞}]]&,var5];
var5=Union[var5/._yy->yy];
var5=var5/.yy->yy[#]&/@Range[Length[var2]]//Flatten;
var5=GatherBy[var5,hplLevel];
(ppp[hplLevel[#[[1]]]]=#)&/@var5;
ppp[_]={};
dVar=Table[
tmp=IntegerPartitions[i];
Select[Join@@(Times@@@Tuples[ppp/@#]&/@tmp),FreeQ[#,((_hplZC)^(_))]&],
{i,0,lTotal-2}
];
tmp=repZ/.hplZC[a_,b_,c_,d_]:>hplZC[a,b,If[#==1,1-δ,#]&/@c,d];
tmp=hplZSumToMPL[tmp,VerifyConvergence->False]/.hplN->∞;
tmp=FixedPoint[
Expand[#/.{
(a:hplZC[_,_,_,{b_ ...}])(c:hplZC[_,_,_,{b_ ...}]):>hplShuffleAll[a c],
(a:((hplZC[_,_,_,{b_ ...}])^(_))):>hplShuffleAll[a]
}]&,tmp
];
tmp=hplSumToInt[tmp,VerifyConvergence->False]//Expand//hplShuffleAll;
Block[{Integrate=hplI,Hypergeometric2F1=hpl2F1,$xx=xx},
tmp=tmp/.x_hplHC/;hplConvergenceQ[x/.δ->0]&&!FreeQ[Flatten[List@@x/.δ->0],_?(!NumericQ[#]&)]:>(x/.δ->0);
tmp=tmp/.x_hplHC:>Most[hplHIntegrate@@x]
]/.(GenerateConditions->False)->Sequence[];
repI=tmp/.δ->0/.hplI[k_,m__]:>hplI[k,Sequence@@({m}/.{a_,0,1}:>{a,δ,1-δ}/.{a_,0,b_}:>{a,δ,b})];
tmp=MinValue[{p,p^Length[var2]>Max[Length/@dVar]+5&&p>0},p,Integers];
tmp=Tuples[Range[lTotal,tmp+lTotal-1],Length[var2]];
repl=Thread[var3->#]&/@tmp;
val1=Table[repZ/.i/.hplZC->hplZSum,{i,repl}];
val2=Block[{hplMZVSimplify=#&},Table[hplLimit[repI/.i/.hplI->hplIntegrate,δ->0],{i,repl}]];
var6=Cases[val2,_hplHC|((Log[2])^(_)),{0,∞}]//Union;
If[(val2/.Alternatives@@var6->0)=!=val1,Throw[False]];
tmp=If[MatchQ[#,((Log[2])^(_))],Exponent[#,Log[2]],hplLevel[#]]&/@var6;
tmp=dVar[[lTotal-#+1]]&/@tmp;
diff=Array[zz,Length[#]].#&/@tmp;
dval1=Table[diff/.i/.hplZC->hplZSum,{i,repl}]//Transpose;
dval2=Coefficient[val1-val2,#]&/@var6;
tmp=MapThread[Solve[#1==#2]&,{dval1,dval2}];
If[Length[#]=!=1,Throw[False]]&/@tmp;
diff=MapThread[#1/.#2[[1]]&,{diff,tmp}].var6;
val2=Table[hplLimit[diff+repI/.i/.hplI->hplIntegrate/.hplZC->hplZSum,δ->0],{i,repl}];
diff=diff/._zz->0/.a:(_hplZC|_hplHC)/;NumericQ[a[[2]]]:>hplMZVSimplify[a];
diff=diff/.Thread[var3->(var2/.∞->inf)]/.((_xx)^(j_))/;Coefficient[j,inf]>0->0;
If[diff===0,
ret=repI/.Thread[var3->(var2/.∞->inf)]/.((_xx)^(j_))/;Coefficient[j,inf]>0->0;
Throw[True]
];
tmp=If[diff[[0]]===Plus,List@@diff,{diff}];
coeff=tmp/._hplZC->1;
val1=tmp/coeff;
val2=If[#===1,#,tttt[#,δ,xx]]&/@val1;
If[!FreeQ[val2,tttt],Throw[False]];
ret=Expand[Total[coeff val2]]+repI/.Thread[var3->(var2/.∞->inf)]/.((_xx)^(j_))/;Coefficient[j,inf]>0->0;
True
]

];

[数値近似等]

Options[hplZSum]=Append[Options[Sum],Sum->True];

Options[hplZNSum]=Options[NSum];

hplZSum[{},_,{},OptionsPattern[]]=hplSSum[{},_,{},OptionsPattern[]]=1;

hplZNSum[{},_,{},OptionsPattern[]]=hplSNSum[{},_,{},OptionsPattern[]]=1;

hplZSum[a0_,c_,b0_,d0_,mm:OptionsPattern[]]/;checkZ[hplZC[a0,c,b0,d0]]:=
Module[{tmp,arg,add,head},
arg=hplZC[a0,c,b0,d0];
(
tmp=newvarList[arg,Length[arg[[1]]]];
If[Sign[c]===-1,
add={Times@@MapThread[If[#1[[0]]===projω,#1[#2],((#1)^(#2))]&,{arg[[3]],tmp}]/Times@@(((tmp+arg[[4]]))^(arg[[1]])),
Sequence@@Transpose[{tmp,Most[Prepend[tmp,c+1]],0 tmp}],mm},
add={Times@@MapThread[If[#1[[0]]===projω,#1[#2],((#1)^(#2))]&,{arg[[3]],tmp}]/Times@@(((tmp+arg[[4]]))^(arg[[1]])),
Sequence@@Transpose[{tmp,1-0 tmp,Most[Prepend[tmp,c+1]]-1}],mm}
];
add=add/.(Sum->_)->Sequence[];
If[!IntegerQ[c]&&TrueQ[OptionValue[Sum]],add=add//projExpand];
head=If[!TrueQ[OptionValue[Sum]],sum,Sum];
tmp=If[Sign[c]===-1,(((-1))^(Length[a0])),1]head@@add/.LerchPhi->HurwitzLerchPhi/.Zeta[a_,b_]:>HurwitzZeta[a,b];
tmp
)/;
FreeQ[arg,Rule|RuleDelayed]&&c[[0]]=!=Real&&c[[0]]=!=Complex&&c[[0]]=!=Rational
];

hplZNSum[a0_,c_,b0_,d0_,mm:OptionsPattern[]]/;checkZ[hplZC[a0,c,b0,d0]]:=
Module[{tmp,arg,add},
arg=hplZC[a0,c,b0,d0];
(
tmp=newvarList[arg,Length[arg[[1]]]];
If[Sign[c]===-1,
add={Times@@MapThread[If[#1[[0]]===projω,#1[#2],((#1)^(#2))]&,{arg[[3]],tmp}]/Times@@(((tmp+arg[[4]]))^(arg[[1]])),
Sequence@@Transpose[{tmp,Most[Prepend[tmp,c+1]],0 tmp}],mm},
add={Times@@MapThread[If[#1[[0]]===projω,#1[#2],((#1)^(#2))]&,{arg[[3]],tmp}]/Times@@(((tmp+arg[[4]]))^(arg[[1]])),
Sequence@@Transpose[{tmp,1-0 tmp,Most[Prepend[tmp,c+1]]-1}],mm}
];
If[!IntegerQ[c],add=add//projExpand];
tmp=If[Sign[c]===-1,(((-1))^(Length[a0])),1]NSum@@add;
tmp
)/;
FreeQ[arg,Rule|RuleDelayed]&&c[[0]]=!=Real&&c[[0]]=!=Complex&&c[[0]]=!=Rational
];

Options[hplSSum]=Options[Sum];

Options[hplSNSum]=Options[NSum];

hplSSum[a0_,c_,b0_,d0_,mm:OptionsPattern[]]/;checkZ[hplZC[a0,c,b0,d0]]:=
Module[{tmp,arg,add},
arg=hplSC[a0,c,b0,d0];
(
tmp=newvarList[arg,Length[arg[[1]]]];
If[Sign[c]===-1,
add={Times@@MapThread[If[#1[[0]]===projω,#1[#2],((#1)^(#2))]&,{arg[[3]],tmp}]/Times@@(((tmp+arg[[4]]))^(arg[[1]])),
Sequence@@Transpose[{tmp,Most[Prepend[tmp+1,c+1]],0 tmp}],mm},
add={Times@@MapThread[If[#1[[0]]===projω,#1[#2],((#1)^(#2))]&,{arg[[3]],tmp}]/Times@@(((tmp+arg[[4]]))^(arg[[1]])),
Sequence@@Transpose[{tmp,1-0 tmp,Most[Prepend[tmp,c]]}],mm}
];
If[!IntegerQ[c],add=add//projExpand];
tmp=If[Sign[c]===-1,(((-1))^(Length[a0])),1]Sum@@add/.LerchPhi->HurwitzLerchPhi/.Zeta[a_,b_]:>HurwitzZeta[a,b];
tmp
)/;
FreeQ[arg,Rule|RuleDelayed]&&c[[0]]=!=Real&&c[[0]]=!=Complex&&c[[0]]=!=Rational
];

hplSNSum[a0_,c_,b0_,d0_,mm:OptionsPattern[]]/;checkZ[hplZC[a0,c,b0,d0]]:=
Module[{tmp,arg,add},
arg=hplSC[a0,c,b0,d0];
(
tmp=newvarList[arg,Length[arg[[1]]]];
If[Sign[c]===-1,
add={Times@@MapThread[If[#1[[0]]===projω,#1[#2],((#1)^(#2))]&,{arg[[3]],tmp}]/Times@@(((tmp+arg[[4]]))^(arg[[1]])),
Sequence@@Transpose[{tmp,Most[Prepend[tmp+1,c+1]],0 tmp}],mm},
add={Times@@MapThread[If[#1[[0]]===projω,#1[#2],((#1)^(#2))]&,{arg[[3]],tmp}]/Times@@(((tmp+arg[[4]]))^(arg[[1]])),
Sequence@@Transpose[{tmp,1-0 tmp,Most[Prepend[tmp,c]]}],mm}
];
If[!IntegerQ[c],add=add//projExpand];
tmp=If[Sign[c]===-1,(((-1))^(Length[a0])),1]NSum@@add/.LerchPhi->HurwitzLerchPhi/.Zeta[a_,b_]:>HurwitzZeta[a,b];
tmp
)/;
FreeQ[arg,Rule|RuleDelayed]&&c[[0]]=!=Real&&c[[0]]=!=Complex&&c[[0]]=!=Rational
];

Options[hplHIntegrate]=Options[Integrate];

Options[hplHNIntegrate]=Options[NIntegrate];

hplHIntegrate[{},_,{},OptionsPattern[]]=1;

hplHNIntegrate[{},_,{},OptionsPattern[]]=1;

hplHNIntegrate[{a___,b_,0},x_,{c___,d_,e_}]/;
e≠0&&Length[{a}]==Length[{c}]:=hplHNIntegrate[{a,b},x,{c,d+e}]/e;

hplHIntegrate[{a___,b_,0},x_,{c___,d_,e_}]/;
e≠0&&Length[{a}]==Length[{c}]:=hplHIntegrate[{a,b},x,{c,d+e}]/e;

hplHIntegrate[a_List,c_,d0_,mm:OptionsPattern[]]/;Length[a]===Length[d0]:=
Module[{d,tmp,int,p},
p=LengthWhile[Reverse[Transpose[{a,d0}]],#=={0,0}&];
If[Length[a]==p,Return[((Log[c])^p)/p!]];
d=Drop[d0,-p];
tmp=newvarList[{a,c,d},Length[a]-p];
int=Times@@(tmp^d)/Times@@(tmp-Drop[a,-p])((Log[tmp[[-1]]])^p)/p!;
Integrate@@{int,
Sequence@@MapThread[{#1,0,#2}&,{tmp,Most[Prepend[tmp,c]]}],
Sequence@@If[!FreeQ[{mm},GenerateConditions],{mm},{mm,GenerateConditions->False}]}
];

hplHNIntegrate[a_List,c_,d0_,mm:OptionsPattern[]]/;Length[a]===Length[d0]:=
Module[{d,tmp,int,p},
p=LengthWhile[Reverse[Transpose[{a,d0}]],#=={0,0}&];
If[Length[a]==p,Return[((Log[c])^p)/p!]];
d=Drop[d0,-p];
tmp=newvarList[{a,c,d},Length[a]-p];
int=Times@@(tmp^d)/Times@@(tmp-Drop[a,-p])((Log[tmp[[-1]]])^p)/p!;
NIntegrate@@{int,Sequence@@MapThread[{#1,0,#2}&,{tmp,Most[Prepend[tmp,c]]}],mm}
];

[Shuffle代数]

$fff[in:{_List...}]:=$fff[in]=
Module[{lr,vtmp,tmp,pop,res},
lr=MapThread[ConstantArray[#1,Length[#2]]&,{Range[Length[in]],List@@in}];
vtmp=List@@@List@@in;
res=Map[
Function[v,
tmp=vtmp;
({pop,tmp[[#]]}={tmp[[#,1]],tmp[[#,2;;]]};pop)&/@v
],
lr//Flatten//Permutations
];
res
];

tc[{v1_,v2_},{u1_,u2_}]:=
Module[{x,tmp,tmp2},
tmp=Flatten[{Replace[Apart[(((x+u1))^(-v1))(((x+u2))^(-v2)),x],Plus->List,{1},Heads->True]}];
tmp=tmp/.(((a_.+b_ x))^(c_.)):>(((a/b+x))^c)(b^c);
tmp2=Replace[#,a_.(x+b_.)^(c_.)/;FreeQ[{a,b,c},x]:>{a,-c,b}]&/@tmp;
tmp2=If[#[[0]]===List,#,{#,0,0}]&/@tmp2;
If[Union[Length/@tmp2]=!={3}||tmp=!=(#[[1]](x+#[[3]])^(-#[[2]])&/@tmp2),Abort[]];
tmp2
];

$mzs[{a_,c_,e_},{b_,d_,f_}]:=$mzs[{a,c,e},{b,d,f}]=
Module[{tmp,cden,cnum,tx,cofs,res,id1,id2},
tmp=$fff[{Array[$xx,Length[a]],Array[$yy,Length[b]]}];
tmp=tmp/.{$xx[m_]:>{$xx[m]},$yy[n_]:>{$yy[n]}};
tmp=FixedPoint[
Function[v,
Union[
Union@@(ReplaceList[
#,{k___,{$xx[m_]},{$yy[n_]},l___}:>{k,{$xx[m],$yy[n]},l}]&/@v),
v]
],tmp
];
tmp=Map[
Function[v,
{1,Map[
Function[u,
cden=id1@@(u/.{$xx[m_]:>a[[m]],$yy[n_]:>b[[n]]});
cnum=Times@@(u/.{$xx[m_]:>c[[m]],$yy[n_]:>d[[n]]});
cofs=id2@@(u/.{$xx[m_]:>e[[m]],$yy[n_]:>f[[n]]});
{cden,cnum,cofs}
],v
]}],
tmp
];
tmp=
FixedPoint[
Replace[#,
{r_,{k1___,{id1[t1_,t2_],p_,id2[s1_,s2_]},k2___}}:>
If[s1===s2,{r,{k1,{t1+t2,p,s1},k2}},
Sequence@@({r#[[1]],{k1,{#[[2]],p,#[[3]]},k2}}&/@tc[{t1,t2},{s1,s2}])
]
]&/@#&,
tmp
]/.id1|id2->Identity;
{#[[1]],Sequence@@Transpose[#[[2]]]}&/@tmp
];

$mss[{a_,c_,e_},{b_,d_,f_}]:=$mss[{a,c,e},{b,d,f}]=
Module[{tmp,cden,cnum,tx,cofs,res,id1,id2},
tmp=$fff[{Array[$xx,Length[a]],Array[$yy,Length[b]]}];
tmp=tmp/.{$xx[m_]:>{$xx[m]},$yy[n_]:>{$yy[n]}};
tmp=FixedPoint[
Function[v,
Union[
Union@@(ReplaceList[
#,{k___,{$xx[m_]},{$yy[n_]},l___}:>{k,{$xx[m],$yy[n]},l}]&/@v),
v]
],tmp
];
tmp=Map[
Function[v,
{1,Map[
Function[u,
cden=id1@@(u/.{$xx[m_]:>a[[m]],$yy[n_]:>b[[n]]});
cnum=Times@@(u/.{$xx[m_]:>c[[m]],$yy[n_]:>d[[n]]});
cofs=id2@@(u/.{$xx[m_]:>e[[m]],$yy[n_]:>f[[n]]});
{cden,cnum,cofs}
],v
]}],
tmp
];
tmp=
FixedPoint[
Replace[#,
{r_,{k1___,{id1[t1_,t2_],p_,id2[s1_,s2_]},k2___}}:>
If[s1===s2,{-r,{k1,{t1+t2,p,s1},k2}},
Sequence@@({-r#[[1]],{k1,{#[[2]],p,#[[3]]},k2}}&/@tc[{t1,t2},{s1,s2}])
]
]&/@#&,
tmp
]/.id1|id2->Identity;
{#[[1]],Sequence@@Transpose[#[[2]]]}&/@tmp
];

$hps[{a_,c_},{b_,d_}]:=$hps[{a,c},{b,d}]=
Module[{tmp},
tmp=$fff[{Array[$xx,Length[a]],Array[$yy,Length[b]]}];
tmp=tmp/.{$xx[m_]:>{a[[m]],c[[m]]},$yy[m_]:>{b[[m]],d[[m]]}};
Transpose/@tmp
];

hplShuffle[a_hplHC,b_hplHC]/;checkH[a]&&checkH[b]:=
Module[{tmp,res},
tmp={a,b};
(
res=$hps[{tmp[[1,1]],tmp[[1,3]]},{tmp[[2,1]],tmp[[2,3]]}];
hplHC[#[[1]],tmp[[1,2]],#[[2]]]&/@res//Total
)/;SameQ@@tmp[[1;;,2]]
];

hplShuffle[a_hplZC,b_hplZC]:=
Module[{tmp,res},
tmp={a,b};
CheckAbort[
res=$mzs[{tmp[[1,1]],tmp[[1,3]],tmp[[1,4]]},{tmp[[2,1]],tmp[[2,3]],tmp[[2,4]]}];
res=#[[1]]hplZC[#[[2]],tmp[[1,2]],#[[3]],#[[4]]]&/@res//Total;
res/.hplZC[_,_,_?(MemberQ[#,0]&),_]->0/.
w_ projω[x_,y_,z_]:>projω[x,y,z w],
a b
]/;SameQ@@tmp[[1;;,2]]
];

hplShuffle[a_hplSC,b_hplSC]:=
Module[{tmp,res},
tmp={a,b};
CheckAbort[
res=$mss[{tmp[[1,1]],tmp[[1,3]],tmp[[1,4]]},{tmp[[2,1]],tmp[[2,3]],tmp[[2,4]]}];
res=#[[1]]hplSC[#[[2]],tmp[[1,2]],#[[3]],#[[4]]]&/@res//Total;
res/.hplSC[_,_,_?(MemberQ[#,0]&),_]->0/.
w_ projω[x_,y_,z_]:>projω[x,y,z w],
a b
]/;SameQ@@tmp[[1;;,2]]
];

hplShuffleAll[in_]:=
Module[{tmp,hplh,hplz,hpls},
tmp=FixedPoint[
#/.{
s_.(a:hplHC[_,j_:1,_List:{}])( b:hplHC[_,j_:1,_List:{}]):>cExpand[s hplShuffle[a,b]],
s_.(a:hplZC[_,j_,_,_])(b:hplZC[_,j_,_,_]):>cExpand[s hplShuffle[a,b]],
s_.(a:hplSC[_,j_,_,_])(b:hplSC[_,j_,_,_]):>cExpand[s hplShuffle[a,b]],
s_.(((a:(_hplHC|_hplZC|_hplSC)))^(k_Integer?Positive)):>cExpand[s (a^(k-2))hplShuffle[a,a]]
}&,cExpand[in/.a_hplHC?(!checkH[#]&):>hplh@@a/.a_hplZC?(!checkZ[#]&):>hplz@@a/.a_hplSC?(!checkZ[hplZC@@#]&):>hplz@@a]
]/.hplh->hplHC/.hplz->hplZC/.hpls->hplSC
];

[収束性・極限]

hplConvergenceQ[in:hplZC[a_,b_,c_,d_]]/;checkZ[in]:=
Module[{tmp},
Catch[
If[!TrueQ[And@@(NumericQ/@a)&&And@@(NumericQ/@c)&&And@@(NumericQ/@d)],Throw[False]];
If[NumericQ[b]&&NumberQ[Quiet[hplZSum@@in]],Throw[True]];
If[b===∞,
If[MemberQ[a,_?(#<1&)],Throw[False]];
If[MemberQ[c,_?(Abs[#]>1&)],Throw[False]];
tmp=d+Reverse[Range[Length[d]]];
If[MemberQ[tmp,_?(#<1&&IntegerQ[#]&)],Throw[False]];
Throw[!(a[[1]]==c[[1]]==1)]
];
False
]
];

hplZeroQ[in_]/;!FreeQ[in,Indeterminate]:=Indeterminate;

hplZeroQ[hplHC[a_,b_,t_]]/;Length[a]==Length[t]≥1:=
(And@@Thread[a==0]&&And@@Thread[t==0]&&b==1)||
(And@@Thread[a==0]&&And@@Thread[t≥0]&&MemberQ[t,Except[0]]&&b==0)||
(MemberQ[a,Except[0]]&&And@@Thread[t≥0]&&b==0);

Options[hplLimit]=Options[hplLimit0]=Options[Limit]

hplLimit//Clear;
hplLimit[expr_,x_->x0_,OptionsPattern[]]:=
Module[{tmp,tmp2,res},
tmp=expr/.a_hplHC:>hplLogExtract[a,a[[2]]/.x->x0];
tmp=tmp//hplLogExtract//hplHPLToLog//Expand;
tmp=If[tmp[[0]]===Plus,List@@tmp,{tmp}];
res/;Catch[
res=Map[
Function[v,
(*If[FreeQ[v,a_hplHC/;!hplConvergenceQ[a/.x->x0]]&&
Quiet[NumericQ[v/.x->x0/._hplHC->1]],
v/.x->x0,
(*else*)*)
tmp2=hplSeries1[v,{x,x0,0}];
If[tmp2[[0]]===hplSeries1,Throw[False]];
Normal[tmp2]
(*]*)
],
tmp
]//Total;
res=hplIntToSum[res/._hplHC?hplZeroQ->0,VerifyConvergence->False];
res=res/.a:(_hplZC|_hplHC)/;NumericQ[a[[2]]]:>hplMZVSimplify[a];
res=hplSumToInt[res,VerifyConvergence->False]//Expand;
True
]
];

hplSeries//Clear;
hplSeries[expr_,{x_,x0_,n_},OptionsPattern[]]:=
Module[{tmp,tmp2,res},
tmp=expr/.a_hplHC:>hplLogExtract[a,a[[2]]/.x->x0];
tmp=tmp//hplLogExtract//hplHPLToLog//Expand;
tmp=If[tmp[[0]]===Plus,List@@tmp,{tmp}];
res/;Catch[
res=Map[
Function[v,
tmp2=hplSeries1[v (((x-x0))^(-n)),{x,x0,0}];
If[tmp2[[0]]===hplSeries1,Throw[False]];
tmp2(((x-x0))^n)
],
tmp
]//Total;
res=hplIntToSum[res/._hplHC?hplZeroQ->0,VerifyConvergence->False];
res=res/.a:(_hplZC|_hplHC)/;NumericQ[a[[2]]]:>hplMZVSimplify[a];
res=hplSumToInt[res,VerifyConvergence->False]//Expand;
True
]
];

Clear[hplSeries1];
hplSeries1[f_,{x_,x0_,n_}]:=
Module[{tmp,tmp2,coeff,fun,err},
tmp=f//hplLogExtract//hplHPLToLog//Expand;
tmp=If[tmp[[0]]===Plus,List@@tmp,{tmp}];
fun=Function[v,
tmp2=v;
tmp2=If[tmp2[[0]]===Times,List@@tmp2,{tmp2}];
coeff=Cases[tmp2,((Log[c_])^(_.))/;(c/.x->x0)==0];
hplSeries0[Times@@DeleteCases[tmp2,Alternatives@@coeff],{x,x0,n}]Times@@coeff
];
tmp=CheckAbort[Check[fun/@tmp//Total,err],err];
tmp/;tmp=!=err
];

hplSeries0//Clear;
hplSeries0[f_,{x_,x0_,n_}]:=
Module[{order,δ,tmp0,tmp,tmp2,logorder},
tmp=tmp0=f/.x->δ+x0;
(*Print["in: ",tmp];*)
order=0;
While[tmp2=getCoefficient[tmp (δ^(-order)) ,δ];tmp2[[0]]===DirectedInfinity,order--];
tmp=tmp (δ^(-order))-tmp2//Together;
While[++order<n+1,
logorder=0;
(*Print["order: ",order];
Print["tmp: ",tmp];*)
While[tmp2=getCoefficient[tmp ((Log[δ])^(-logorder))/δ,δ];tmp2[[0]]===DirectedInfinity,logorder++];
tmp=tmp ((Log[δ])^(-logorder))/δ-tmp2//Together;
While[--logorder≥0,
(*Print["logorder: ",logorder];
Print["tmp: ",tmp];*)
tmp2=getCoefficient[tmp Log[δ] ,δ];
If[tmp2[[0]]===DirectedInfinity,Abort[]];
tmp=tmp Log[δ]-tmp2//Together;
(*Print["tmp: ",tmp];*)
];
];
tmp=(tmp0 -tmp (δ^n))//FunctionExpand//PowerExpand//Together;
tmp=(Collect[tmp,δ,Expand[Together[#]]&]/.δ->x-x0)+((O[x,x0])^(n+1))
];

getCoefficient[in_,d_]:=
Module[{tmp,F},
Quiet[
tmp=in/.t:hplHC[a_,b_,c_]/;!FreeQ[b,d]:>hplLogExtract[t,b/.d->0]//hplHPLToLog;
tmp=applyLHospital[tmp//Together,d];
tmp=tmp/.hplHC->F;
tmp=Series[tmp,{d,0,0}]/.F->hplHC;
tmp=tmp/._hplHC?hplZeroQ->0;
tmp=tmp/.hplHC[a_,b_,{0..}]/;Last[a]=!=0&&First[a]===b:>ComplexInfinity;
tmp=tmp/.a:(_hplZC|_hplHC)/;NumericQ[a[[2]]]:>hplMZVSimplify[a];
tmp=tmp//FunctionExpand//PowerExpand//Expand;
Limit[tmp,d->0,Analytic->True]
]
];

applyLHospital[in_,d_]:=
Module[{tmp,num,den,F,tmp2},
tmp=in//Together;
While[
True,
If[Limit[Denominator[tmp],d->0]=!=0,Break[]];
tmp2=Series[Numerator[tmp]/.hplHC->F,{d,0,0}]/.F->hplHC//Normal;
tmp2=tmp2/._hplHC?hplZeroQ->0;
tmp2=tmp2/.a:(_hplZC|_hplHC)/;NumericQ[a[[2]]]:>hplMZVSimplify[a]//FunctionExpand//PowerExpand//Expand;
If[tmp2=!=0&&FreeQ[tmp2,hplHC],Return[ComplexInfinity]];
If[!FreeQ[tmp2,hplHC],Abort[]];
tmp=((D[Numerator[tmp],d])/(D[Denominator[tmp],d]));
tmp=tmp/.t:hplHC[a_,b_,c_]/;!FreeQ[b,d]:>hplLogExtract[t,b/.d->0]//hplHPLToLog;
tmp=tmp//Together;
];
tmp//hplHPLToLog
];

[複素共役・微分・積分]

hplZC/:Conjugate[hplZC[a_,b_,c_,d_]]/;(IntegerQ[b]||b===∞)&&And@@(IntegerQ/@a):=hplZC[a,b,Conjugate[c],Conjugate[d]]

hplHC/:Conjugate[hplHC[a_,b_,c_]]/;hplConvergenceQ[hplHC[a,b,c]]&&{a}[[-1]]=!=0:=hplHC[Conjugate[a],Conjugate[b],Conjugate[c]]

hplSinf/:Conjugate[hplSinf]=hplSinf;

Derivative[a:{0..},0,c:{_Integer?NonNegative..},d:{0..}][hplZC][e_,f_,g_,h_]/;
Length[a]==Length[c]==Length[d]==Length[e]==Length[g]==Length[h]:=
Module[{pos,tmp,p},
pos=Position[c,_Integer?Positive,{1},1];
If[
pos=pos[[1,1]];
c[[pos]]==1,
(*then*)
tmp=((g[[pos]])^(-1))ReplacePart[hplZC[e,f,g,h],{1,pos}->e[[pos]]-1]+
-((g[[pos]])^(-1))h[[pos]]hplZC[e,f,g,h];
tmp/.hplZC->Derivative[a,0,ReplacePart[c,pos->0],d][hplZC],
(*else*)
tmp=D[Derivative[a,0,ReplacePart[c,pos->c[[pos]]-1],d]
[hplZC][e,f,ReplacePart[g,pos->p],h],p]/.p->g[[pos]];
Remove[p];
tmp
]/;pos=!={}
];

Derivative[a:{0..},0,c_,d:{_Integer?NonNegative..}][hplZC][e_,f_,g_,h_]/;
Length[a]==Length[c]==Length[d]==Length[e]==Length[g]==Length[h]:=
Module[{pos,tmp,p},
pos=Position[d,_Integer?Positive,{1},1];
If[
pos=pos[[1,1]];
d[[pos]]==1,
(*then*)
tmp=-e[[pos]]ReplacePart[hplZC[e,f,g,h],{1,pos}->e[[pos]]+1];
tmp/.hplZC->Derivative[a,0,c,ReplacePart[d,pos->0]][hplZC],
(*else*)
tmp=D[Derivative[a,0,c,ReplacePart[d,pos->d[[pos]]-1]]
[hplZC][e,f,g,ReplacePart[h,pos->p]],p]/.p->h[[pos]];
Remove[p];
tmp
]/;pos=!={}
];

Derivative[a:{0...},1,t:{0...}][hplHC][b_List,x_,c_List]/;
Length[a]==Length[b]==Length[c]==Length[t]:=
(((x^(First[c]))hplHC[Rest[b],x,Rest[c]])/(x-First[b]))/.hplHC[{},_,{}]->1

Derivative[a:{0...},n_Integer,t:{0...}][hplHC][b_List,x_,c_List]/;
Length[a]==Length[b]==Length[c]==Length[t]&&n>1:=
Module[{p,res},
res=D[Derivative[a,1,t][hplHC][b,p,c],{p,n-1}]/.p->x;
Remove[p];
res
]

felim[in_]:=
Expand[in]/.{
hplHC[{},_,{}]->1,
a_hplHC?hplZeroQ->0,
a_hplHC?(!hplFiniteQ[#]&)->ComplexInfinity,
hplHC[p:{0..},x_?NumericQ,{0..}]:>(((Log[x])^(Length[p]))/(Length[p]!)),
hplHC[p:{k_ ..},x_?NumericQ,{0..}]:>(((Log[1-x/k])^(Length[p]))/(Length[p]!))
}//hplShuffleAll//Expand;

fmatch[in_,x_]:=
Module[{tmp,pat},
If[in==1,Return[True]];
pat=(x^(_.))|(((1-x))^(_.))|(((x-1))^(_.))|(((1+x))^(_.))|(((I-x))^(_.))|(((x-I))^(_.))|(((I+x))^(_.))|((((x^2)-1))^(_.))|(((1-(x^2)))^(_.))|((((x^2)+1))^(_.));
pat=(x^(_.))|(((_+_.x))^(_.))|(((_+_.(x^2)))^(_.));
If[MatchQ[in,pat],Return[True]];
If[in[[0]]===Times&&And@@(MatchQ[#,pat]&/@List@@in),Return[True]];
False
];

Options[hplIntegrate]={Assumptions:>$Assumptions};

(* 基本的な性質 *)

hplIntegrate[x_Plus,s_,t:OptionsPattern[]]:=hplIntegrate[#,s,t]&/@x;

hplIntegrate[coeff_ fx_,x_,t:OptionsPattern[]]/;FreeQ[coeff,x]&&x[[0]]=!=List:=coeff hplIntegrate[fx,x,t];

hplIntegrate[coeff_ ,x_,OptionsPattern[]]/;FreeQ[coeff,x]&&x[[0]]=!=List:=coeff x;

hplIntegrate[fx_,x_,t:OptionsPattern[]]/;FreeQ[fx,hplIntegrate]&&x[[0]]=!=List:=
Module[{tmp,F,G},
tmp=fx/.{
Log[x]|Log[-x]->hplHC[{0},x,{0}],
Log[1+x]|Log[-1-x]->hplHC[{-1},x,{0}],
Log[1-x]|Log[-1+x]->hplHC[{1},x,{0}],
Log[1+I x]|Log[-1-I x]->hplHC[{I},x,{0}],
Log[1-I x]|Log[-1+I x]->hplHC[{-I},x,{0}],
Log[1-(x^2)]|Log[-1+(x^2)]->hplHC[{1},x,{0}]+hplHC[{-1},x,{0}],
Log[1+(x^2)]|Log[-1-(x^2)]->hplHC[{I},x,{0}]+hplHC[{-I},x,{0}],
ArcTan[x]->(I/2)hplHC[{-I},x,{0}]-(I/2)hplHC[{I},x,{0}],
 ArcTanh[x]->(1/2)hplHC[{-1},x,{0}]-(1/2)hplHC[{1},x,{0}]
};
tmp=tmp/.hplHC[a:{_ (x^(p_.)),___},c_,d__]/;d[[-1]]=!=0:>hplHC[a (x^(-p)),c (x^(-p)),d](x^(p Total[d]));
tmp=tmp/.hplHC[a_,c_ x,d__]/;d[[-1]]=!=0:>hplHC[a /c,x,d](c^( Total[d]));
tmp=Apart[tmp (x^F)//cExpand,x]/.F->0//cExpand//hplShuffleAll//cExpand;
tmp=If[tmp[[0]]===Plus,List@@tmp,{tmp}];
tmp=MapAll[Collect[#,x]&,#]&/@tmp//Total;
hplIntegrate[tmp,x,t]/;tmp=!=fx
];

(* 被積分関数が xの冪 だけを含む場合 *)

hplIntegrate[(1/(x_)),x_,OptionsPattern[]]/;x[[0]]=!=List:=hplHC[{0},x,{0}];

hplIntegrate[((x_)^(s_.)),x_,OptionsPattern[]]/;FreeQ[s,x]&&s=!=-1&&x[[0]]=!=List:=((x^(s+1))/(s+1));

(* 被積分関数が (a*x+b)の冪 だけを含む場合 *)

hplIntegrate[(1/(a_. x_+b_)),x_,OptionsPattern[]]/;FreeQ[{a,b},x]&&x[[0]]=!=List:=(1/a)hplHC[{-(b/a)},x,{0}];

hplIntegrate[(((a_. x_+b_))^(p_.)),x_,OptionsPattern[]]/;FreeQ[{a,b},x]&&p=!=-1&&x[[0]]=!=List:=((((a x+b))^(p+1))/(a(p+1)));

(* 被積分関数が hpl だけを含む場合 *)

hplIntegrate[hplHC[d_List,x_,t_List],x_,OptionsPattern[]]/;FreeQ[{d,t},x]&&x[[0]]=!=List:=
x hplHC[d,x,t]-hplIntegrate[x D[hplHC[d,x,t],x],x];

(* 被積分関数が xの冪と(a*x+b)の冪 を含む場合 *)

hplIntegrate[(((x_)^(s_.))/(a_. x_+b_)),x_,OptionsPattern[]]/;FreeQ[{a,b,s},x]&&x[[0]]=!=List:=(1/a)hplHC[{-(b/a)},x,{s}];

hplIntegrate[((x_)^(s_.))(((a_. x_+b_))^(p_Integer?Negative)),x_,OptionsPattern[]]/;FreeQ[{a,b,s},x]&&x[[0]]=!=List:=
((x^s)/(a(p+1)))(((a x+b))^(p+1))-(s/(a(p+1))) hplIntegrate[(x^(s-1))(((a x+b))^(p+1)),x];

(* 被積分関数が hplと(a*x+b)の冪 を含む場合 *)

hplIntegrate[((hplHC[d_List,x_,t_List])/(a_. x_+b_)),x_,OptionsPattern[]]/;FreeQ[{a,b,d,t},x]&&x[[0]]=!=List:=
(1/a)hplHC[Prepend[d,-(b/a)],x,Prepend[t,0]];

hplIntegrate[hplHC[d_List,x_,t_List](((a_. x_+b_))^(p_Integer?Negative)),x_,OptionsPattern[]]/;FreeQ[{a,b,p,d,t},x]&&x[[0]]=!=List:=
((hplHC[d,x,t])/(a(p+1)))(((a x+b))^(p+1))-(1/(a(p+1))) hplIntegrate[D[hplHC[d,x,t],x](((a x+b))^(p+1)),x];

(* 被積分関数が hplとxの冪 を含む場合 *)

hplIntegrate[((hplHC[d_List,x_,t_List])/(x_)),x_,OptionsPattern[]]/;FreeQ[{d,t},x]&&x[[0]]=!=List:=hplHC[Prepend[d,0],x,Prepend[t,0]];

hplIntegrate[((x_)^(s_.))hplHC[d_List,x_,t_List],x_,OptionsPattern[]]/;FreeQ[{d,t},x]&&s=!=-1&&x[[0]]=!=List:=
((x^(s+1))/(s+1))hplHC[d,x,t]-(1/(s+1))hplIntegrate[(x^(s+1))D[hplHC[d,x,t],x],x];

(* 被積分関数が hpl,xの冪,(a*x+b)の冪 を含む場合 *)

hplIntegrate[((((x_)^(s_.))hplHC[d_List,x_,t_List])/(a_. x_+b_)),x_,OptionsPattern[]]/;FreeQ[{a,b,s},x]&&x[[0]]=!=List:=(1/a)hplHC[Prepend[d,-(b/a)],x,Prepend[t,s]];

hplIntegrate[((x_)^(s_.))(((a_. x_+b_))^(p_Integer?Negative))hplHC[d_List,x_,t_List],x_,OptionsPattern[]]/;FreeQ[{a,b,s,p},x]&&x[[0]]=!=List:=
(((x^s)hplHC[d,x,t])/(a(p+1)))(((a x+b))^(p+1))-(s/(a(p+1))) hplIntegrate[(x^(s-1))hplHC[d,x,t](((a x+b))^(p+1)),x]-(1/(a(p+1)))hplIntegrate[(x^s)D[hplHC[d,x,t],x](((a x+b))^(p+1)),x];

(* 多重積分 *)
hplIntegrate[a_,b__,c_,s:OptionsPattern[]]/;FreeQ[{b,c},Rule|RuleDelayed]:=
hplIntegrate[Refine[hplIntegrate[a,c,s],OptionValue[Assumptions]],b,s];

(* 定積分 *)

hplIntegrate[fx_,{x_,y_,z_},OptionsPattern[]]:=
With[{p=hplIntegrate[fx,x]},
Refine[
(cExpand[(p/.x->z)-(p/.x->y)]/.a_hplHC/;Refine[hplZeroQ[a],OptionValue[Assumptions]]->0),
OptionValue[Assumptions]
]/;FreeQ[p,hplIntegrate]
];

(* Mathematicaの仕様に由来するバグを取り除く *)

Derivative[1,_][hplIntegrate]=0&;
Derivative[0,{0,0,1}][hplIntegrate]=#1/.#2[[1]]->#2[[3]]&;
Derivative[0,{0,1,0}][hplIntegrate]=-#1/.#2[[1]]->#2[[2]]&;

[級数表示での引数の変換]

Options[hplMultiply]={"useProjector"->Automatic};

hplMultiply[in_hplZC,j_Integer/;j=!=0,OptionsPattern[]]/;checkZ[in]:=
Module[{useProj,tmp},
useProj=OptionValue["useProjector"];
If[(useProj=!=True)&&(useProj=!=False),useProj=!FreeQ[in,projω]];
tmp=If[j>0,in,zMinus1[in]];
tmp=tmp/.hplZC[c_,a_,d_,v_]:>
With[{l=Abs[j]},
Times@@(l^(c-1))(l^(Length[c]))hplZC[c,Expand[l a],If[#[[0]]===projω,projω[l#[[1]],l#[[2]],((#[[3]])^(1/l))],projω[l,0,((#)^(1/l))]]&/@d,Expand[l v]]
];
If[!useProj,tmp=tmp//hplExpandProjector];
tmp//Expand
];

zMinus1[hplZC[a_,b_,c_,d_]]:=
(-1)^Length[a](Times@@c)^b *hplRepZ[hplSC[Reverse[a],-b,Reverse[c],Reverse[d]+b]];

hplNegate[in:hplZC[a_,___]]:=hplShift[in,1-Length[a]]/.x_hplZC:>zneg[x]//Expand;

zneg[hplZC[a_,b_,c_,d_]]:=(-1)^Length[a](Times@@c)^b *hplS2Z[hplSC[Reverse[a],-b,Reverse[c],Reverse[d]+b]];

Options[hplShift]={"useOffset"->False};

hplShift[in:hplZC[c_,a_,d_,v_],l_,OptionsPattern[]]:=
Module[{tmp,k},
If[TrueQ[OptionValue["useOffset"]]||!IntegerQ[l],Return[hplShiftI3[in,l]]];
tmp=hplZC[c,Expand[a-l],d,v]-bSum[If[d[[1,0]]===projω,d[[1]][k],d[[1]]^k]/(Expand[k+v[[1]]])^(c[[1]])hplZC[Rest[c],k-1,Rest[d],Rest[v]],{k,a,a-l}];
tmp/.hplZC[{},_,{},{}]->1
];

hplShiftI3[hplZC[a_,b_,c_,d_],sh_]:=
Module[{tmp,i},
Sum[
(((Times@@(c[[1;;i]]/.projω[_,_,w_]:>w)))^sh)hplZC[a[[1;;i]],b-sh,c[[1;;i]]/.projω[w_,x_,y_]:>projω[w,x-sh,y],d[[1;;i]]+sh]*
hplZC[a[[i+1;;]],sh,c[[i+1;;]],d[[i+1;;]]],
{i,0,Length[a]}
]/.hplZC[{},_,{},{}]->1
]

$hsum1[m_Integer?NonNegative]:=$hsum1[m]=
Module[{t,n,y,k,s,tmp},
tmp=Sum[(t^n)(((n+s))^m),{n,y,k}];
tmp=List@@Expand[(t^(k+y))s k y tmp]/.(((1-t))^(a_.)):>(((t-1))^a)(((-1))^a);
tmp/.(((t-1))^(a_.))(t^(b_.+c_.k+d_.y))(k^(e_.))(y^(f_.))(s^(g_.))x_.:>{x,-a,b,c-1,d-1,e-1,f-1,g-1}
];

$hsum2[m_Integer?NonNegative]:=$hsum2[m]=
Module[{t,n,y,k,s,tmp},
tmp=Sum[(((n+s))^m),{n,y,k}];
tmp=List@@Expand[s k y tmp];
tmp/.(k^(e_.))(y^(f_.))(s^(g_.))x_.:>{x,Log,0,0,0,e-1,f-1,g-1}
];

elimNPIint//Clear;
elimNPIint[{a___},n_,{b___},k_,{c___},t_,{d___},{e___},s_,{f___}]:=
Module[{f0,f1,a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,c1,c2,c3,c4,c5,c6,dummy,mm,j1,j2,dx},
f0=If[t===1,$hsum2[-n],$hsum1[-n]];
{a1,a2,a3,a4,a5,a6}={{dummy,a},{b,dummy},{dummy,c},{d,dummy},{dummy,e},{f,dummy}};
{b1,b2,b3,b4,b5,b6}={Last[a1],First[a2],Last[a3],First[a4],Last[a5],First[a6]};
{c1,c2,c3,c4,c5,c6}={Most[a1],Rest[a2],Most[a3],Rest[a4],Most[a5],Rest[a6]};
f1=If[#2===Log,1,(((t-1))^(-#2))](-#1(t^(#3)))*
Sum[(((-1))^(j1+j2))Binomial[#6,j1]Binomial[#7,j2](((b6+dx))^j1)(((b5+dx))^j2)(((s+dx))^(#8))*
mm[{c1,b1-#7+j2,b2-#6+j1,c2},{c3,b3 (t^(#5)),b4 (t^(#4)),c4},{c5,b5,b6,c6}],{j1,0,#6},{j2,0,#7}]&@@@f0;
f1=f1/.w_ projω[x_,y_,z_]:>projω[x,y,z w];
f1=f1/.dx->0;
f1=f1/.mm[x_,y_,z_]:>mm[Flatten[x],Flatten[y],Flatten[z]];
f1=f1/.mm[{___,_+dummy},__]->0;
f1=f1/.mm[{w_.+dummy ,x___,dummy},{dummy y_.,z___,_},{dummy,v___,dummy}]:>
mm[{x},k,{z},{v}](y^(1+k))(1+k)^(-w)/. mm[{},k,{},{}]->1/.dummy->0/.mm->hplZC//Total;
f1
];

elimNPIint[{a___},n_,{b___},∞,{c___},t_,{d___},{e___},s_,{f___}]:=
Module[{tmp,var,coe,x,r1,r2},
tmp=elimNPIint[{a},n,{b},x,{c},t,{d},{e},s,{f}];
var=Cases[tmp,_hplZC,{0,∞}]//Union;
coe=Coefficient[tmp,var];
r1=MapThread[
Switch[hplConvergenceQ[#1/.x->∞],
True,Limit[#2,x->∞]#1/.x->∞,
False,Indeterminate
]&,
{var,coe}
]//Total;
r2=Limit[tmp/._hplZC->0,x->∞];
r1+r2
];

hplEliminateNonPositiveIntegers//Clear;
hplEliminateNonPositiveIntegers[in_]:=
Module[{tmp,d},
tmp=in/.hplZC[a_,∞,b_,c_]:>hplZC[a,hplN,b,c];
tmp=FixedPoint[
hplExpandProjector[#,"nonPositiveOnly"->True]/.x:hplZC[{a___,n_Integer?NonPositive,b___},k_,{c___,t_,d___},{e___,s_,f___}]/;
Length[{a}]==Length[{c}]==Length[{e}]:>With[{ret=elimNPIint[{a},n,{b},k,{c},t,{d},{e},s,{f}]},ret/;ret=!=Indeterminate]&,
tmp
]//Quiet;
Expand[tmp]
];

Options[hplExpandProjector]:={"nonPositiveOnly"->False};

hplExpandProjector[in_,OptionsPattern[]]:=
Module[{tmp,J,s,w},
tmp=in;
If[TrueQ[OptionValue["nonPositiveOnly"]],
tmp=tmp/.projω->w;
tmp=tmp/.(h:hplZC|hplSC)[a_,b_,c_,d_]:>h[a,b,MapAt[#/.w->projω&,c,Position[a,_Integer?NonPositive,{1}]],d];
];
tmp=tmp//projExpand;
tmp=tmp/.projω[a_,b_,c_]:>
With[{k=Expand[projExpand[projω[a,b,c][s]]]},
J/@If[k[[0]]===Plus,List@@k,{k}]];
tmp=tmp/.J[a_]:>J[a/.s->0,(a/.s->1)/(a/.s->0)];
tmp=tmp/.(h:hplZC|hplSC)[a_,b_,c_,d_]:>h[a,b,If[#[[0]]===List,#,{J[1,#]}]&/@c,d];
tmp=tmp/.(h:hplZC|hplSC)[a_,b_,c_,d_]:>h[a,b,Tuples@c,d];
tmp=tmp/.(h:hplZC|hplSC)[a_,b_,c:{_List...},d_]:>Total[h[a,b,#,d]&/@c];
tmp=tmp/.(h:hplZC|hplSC)[a_,b_,c_,d_]:>Times@@Cases[c,J[x_,y_]:>x]h[a,b,c/.J[x_,y_]:>y,d];
tmp=tmp/.w->projω;
myExpand[tmp]
];

[積分表示での引数の変換]

argtrf[{},{_,_,_,_},{x_,s_}]:=hplHC[{},x,{}]

argtrf[p:{0..},{a_?(#≠0&),b_?(#≠0&),c_?(#≠0&),d_?(#≠0&)},{x_,s_}]/;a d-b c≠0:=
hplShuffleAll[Expand[
((((2 I π Floor[(1/2)-((Arg[1+(c/d)s])/(2 π))+((Arg[1+(a/b)s])/(2 π))-((Arg[(d/b)])/(2 π))]hplHC[{},x,{}]+Log[(d/(b ))]hplHC[{},x,{}]-hplHC[{((-b)/a)},x,{}]+hplHC[{-(d/c)},x,{}]))^(Length[p]))/(Length[p]!))]
];

argtrf[p:{0..},{a_?(#≠0&),b_?(#≠0&),c_?(#≠0&),0},{x_,s_}]:=
hplShuffleAll[Expand[
((((2 I π Floor[(1/2)-((Arg[s])/(2 π))+((Arg[1+(a/b)s])/(2 π))-((Arg[(c/b)])/(2 π))]hplHC[{},x,{}]+Log[(c/b)]hplHC[{},x,{}]-hplHC[{((-b)/a)},x,{0}]+hplHC[{0},x,{0}]))^(Length[p]))/(Length[p]!))]
];

argtrf[p:{0..},{a_?(#≠0&),b_?(#≠0&),0,d_?(#≠0&)},{x_,s_}]:=
hplShuffleAll[Expand[((((2 I π Floor[(1/2)-((Arg[(d/b)])/(2 π))+((Arg[1+((a s)/b)])/(2 π))]hplHC[{},x,{}]+Log[(d/(b ))]hplHC[{},x,{}]-hplHC[{((-b)/a)},x,{0}]))^(Length[p]))/(Length[p]!))]]

argtrf[p:{0..},{a_?(#≠0&),0,c_?(#≠0&),d_?(#≠0&)},{x_,s_}]:=
hplShuffleAll[Expand[
((((2 I π Floor[(1/2)-((Arg[1+(c/d)s])/(2 π))+((Arg[s])/(2 π))-((Arg[(d/a)])/(2 π))]hplHC[{},x,{0}]+Log[(d/(a ))]hplHC[{},x,{0}]+hplHC[{((-d)/c)},x,{0}]-hplHC[{0},x,{0}]))^(Length[p]))/(Length[p]!))]
];

argtrf[p:{0..},{0,b_?(#≠0&),c_?(#≠0&),d_?(#≠0&)},{x_,s_}]:=
hplShuffleAll[Expand[((((2 I π Floor[(1/2)-((Arg[1+(c/d)s])/(2 π))-((Arg[(d/b)])/(2 π))]hplHC[{},x,{}]+Log[(d/(b ))]hplHC[{},x,{}]+hplHC[{-(d/c)},x,{0}]))^(Length[p]))/(Length[p]!))]]

argtrf[p:{0..},{0,b_?(#≠0&),c_?(#≠0&),0},{x_,s_}]:=
hplShuffleAll[Expand[((((2 I π Floor[(1/2)-((Arg[s])/(2 π))-((Arg[(c/b)])/(2 π))]+Log[(c/b)]hplHC[{},x,{}]+hplHC[{0},x,{0}]))^(Length[p]))/(Length[p]!))]]

argtrf[p:{0..},{a_?(#≠0&),0,0,d_?(#≠0&)},{x_,s_}]:=
hplShuffleAll[Expand[((((2 I π Floor[(1/2)+((Arg[s])/(2 π))-((Arg[(d/a)])/(2 π))]+Log[(d/(a ))]hplHC[{},x,{}]-hplHC[{0},x,{0}]))^(Length[p]))/(Length[p]!))]]

argtrf[{α_,β___},{a_,b_,c_,d_},{x_,s_}]:=
Module[{tmp,r1,r2,t,res,vv},
tmp=argtrf[{β},{a,b,c,d},{t,s}];
res/;Catch[
If[!FreeQ[tmp,argtrf],Throw[False]];
tmp=Which[
a α≠c&&a≠0,
tmp/.hplHC[{p___},t,{0...}]:>
-hplH[{((-b)/a),p},x]+hplH[{((-b)/a),p},s]hplH[{},x]+hplH[{-((b α-d)/(a α-c)),p},x]-hplH[{-((b α-d)/(a α-c)),p},s]hplH[{},x],
a α==c&&a≠0,
tmp/.hplHC[{p___},t,{0...}]:>-hplH[{((-b)/a),p},x]+hplH[{((-b)/a),p},s]hplH[{},x],
a α≠c&&a==0,
tmp/.hplHC[{p___},t,{0...}]:>hplH[{-((b α-d)/(a α-c)),p},x]-hplH[{-((b α-d)/(a α-c)),p},s]hplH[{},x],
True,Throw[False]
];
If[!FreeQ[tmp,t],Throw[False]];
res=hplH[{α,β},((c s+d)/(a s+b))]hplH[{},x]+tmp//Expand;
res=res/.hplH[v_,w_]:>hplHC[v,w,0v];
True
]
];

hplArgTransform//Clear;
hplArgTransform[{α___},{a_,b_,c_,d_},{x_,s_}]:=
Module[{tmp},
tmp=hplLogExtract[hplHC[{α},x,0{α}]];
tmp=tmp/.y_hplHC:>argtrf[y[[1]],{a,b,c,d},{x,s}];
(
tmp=tmp//felim;
tmp=tmp/.hplHC[p:{___,Except[0]},q_?(#<0&),r:{0...}]:>hplHC[-p,-q,r]//Quiet;
tmp//Expand//hplShuffleAll
)
/;FreeQ[tmp,argtrf]
];

hplArgTransform[{α___},n_Integer?Positive,x_]:=
Module[{tmp},
tmp=hplLogExtract[hplHC[{α},(x^n),0{α}]]/.{
hplHC[p:{0..},(x^n),q:{0..}]:>2^Length[p]hplHC[p,x,q],
a:hplHC[_,(x^n),_]:>(hplIntToSum[a,VerifyConvergence->False]/.b_hplZC:>hplMultiply[b,n])
};
(tmp=hplSumToInt[PowerExpand[tmp],VerifyConvergence->False];
tmp/.hplHC[p:{___,((k_.)/x)},1,q_]:>hplHC[p x,x,q]//Expand//hplShuffleAll)/;FreeQ[tmp,hplMultiple]
];

[PowerLog, Cutoff関連]

hplHPLToLog[in_]:=
in/.{
hplHC[p:{0..},x_,q:{0..}]/;Length[p]==Length[q]:>(((Log[x])^(Length[p]))/(Length[p]!)),
hplHC[p:{k_ ..},x_,q:{0..}]/;Length[p]==Length[q]:>(((Log[1-x/k])^(Length[p]))/(Length[p]!))
};

hplLogToHPL[in_,x_]:=in//.{
Log[a_+b_.x]/;Quiet[a>0]:>Log[a]+hplHC[{-a/b},x,{0}],
Log[a_.x]/;Quiet[a>0]:>Log[a]+hplHC[{0},x,{0}]
};

hplLogExtract[in_,0|PatternSequence[]]:=
Expand[
in//.
s:hplHC[{a___,b:Except[0],p:PatternSequence[0..]},x_,q_]/;checkH[s]:>
With[{l=Length[{a,b}]},
s-(hplShuffle[hplHC[{a,b},x,q[[1;;l]]],hplHC[{p},x,q[[l+1;;]]]]-hplHC[{a,b},x,q[[1;;l]]]hplHC[{p},x,q[[l+1;;]]])
]
];

hplLogExtract[in_,d_]:=
Expand[
in//.
s:hplHC[{p:PatternSequence[d..],a:Except[d],b___},x_,q_]/;checkH[s]:>
With[{l=Length[{p}]},
s-(hplShuffle[hplHC[{p},x,q[[1;;l]]],hplHC[{a,b},x,q[[l+1;;]]]]-hplHC[{p},x,q[[1;;l]]]hplHC[{a,b},x,q[[l+1;;]]])
]
];

sinfExpand[n_,m_:1]:=
Module[{tmp,ζ},
tmp=Tally/@IntegerPartitions[n];
tmp=Plus@@Times@@@Map[(1/(#[[2]]!))((((((((-1))^(#[[1]]+1))ζ[#[[1]]])/(#[[1]]))))^(#[[2]]))&,tmp,{2}];
tmp/.ζ[1]->hplSinf+Log[m]/.ζ[x_]:>Zeta[x]
];

hplZHNExtract[in_]:=
Expand[
in//.{

s:hplZC[x:{1..},k_,x_,{0..}]/;checkZ[s]:>
With[{l=Length[x]},
s+(((hplZC[{1},k,{1},{0}])^l)/(l!))-hplShuffleAll[(((hplZC[{1},k,{1},{0}])^l)/(l!))]/.
 hplZC[{},_,{},{}] ->1],
s:hplZC[a:{1,___},k_,b:{1,___},c:{0..}]/;checkZ[s]:>
With[{l=LengthWhile[Transpose[{a,b}],#==={1,1}&]},
s+hplZC[a[[1;;l]],k ,b[[1;;l]],c[[1;;l]]]hplZC[a[[l+1;;]],k ,b[[l+1;;]],c[[l+1;;]]]-
hplShuffle[hplZC[a[[1;;l]],k ,b[[1;;l]],c[[1;;l]]],hplZC[a[[l+1;;]],k ,b[[l+1;;]],c[[l+1;;]]]]/.
 hplZC[{},_,{},{}] ->1]
}/.hplZC[{1},k_,{1},{0}] :>HarmonicNumber[k]
];

[2F1の展開係数]

hgfSR1[a_,b_,c_,z_]:=If[
(b>0≥a||c>a≥b>0||b≥c>a||b>a≥c)/.hgfϵ->0,
((((1/(1-z))))^b) hpl2F1[-a+c,b,c,((-z)/(1-z))],((((1/(1-z))))^a) hpl2F1[-b+c,a,c,((-z)/(1-z))]
];

hgfSR2[a_,b_,c_,z_]:=If[
(b>0≥a||c>a≥b>0||b≥c>a||b>a≥c)/.hgfϵ->0,
((((1/(1-z))))^b) hpl2F1[b,-a+c,c,((-z)/(1-z))],((((1/(1-z))))^a) hpl2F1[a,-b+c,c,((-z)/(1-z))]
];

hgfSR3[a_,b_,c_,z_]:=
((((((1/(1-z))))^a) Gamma[-a+b] Gamma[c] hpl2F1[-b+c,a,1+a-b,(1/(1-z))])/(Gamma[b] Gamma[-a+c]))+((((((1/(1-z))))^b) Gamma[a-b] Gamma[c] hpl2F1[b,-a+c,1-a+b,(1/(1-z))])/(Gamma[a] Gamma[-b+c]));

hiq[a_]:=(a/.hgfϵ->0)∉Integers&&2(a/.hgfϵ->0)∈Integers

iq[a_]:=(a/.hgfϵ->0)∈Integers

hgfRule1=
hpl2F1[a_,b_,c_,z_]:>
Which[
TrueQ[hiq[a]&&hiq[b]&&hiq[c]],{hgfSR1[a,b,c,z],hplT2Trf},
TrueQ[hiq[a]&&iq[b]&&hiq[c]],{hpl2F1[b,a,c,z],hplZTrf},
TrueQ[iq[a]&&hiq[b]&&hiq[c]],{hpl2F1[a,b,c,z],hplZTrf},
TrueQ[iq[a]&&iq[b]&&hiq[c]],{hgfSR2[a,b,c,z],hplTTrf},
TrueQ[hiq[a]&&iq[b]&&iq[c]],{hgfSR3[a,b,c,z],hplSTrf},
TrueQ[iq[a]&&hiq[b]&&iq[c]],{hgfSR3[b,a,c,z],hplSTrf},
TrueQ[iq[a]&&iq[b]&&iq[c]],{hpl2F1[a,b,c,z],hplNTrf},
True,Abort[]
];

hgfsum[n_]:=hpl2F1[a_,b_,c_,z_]:>((Gamma[c])/(Gamma[a]Gamma[b]))((Gamma[a+n]Gamma[b+n])/(Gamma[c+n]Gamma[1+n]))(z^n)

pgRule1=
{PolyGamma[n_?NumericQ,m_?NumericQ]:>FunctionExpand[PolyGamma[n,m]],
PolyGamma[n_Integer?Positive,k_.+a_?hiq]:>-PolyGamma[n,(1/2)+k+a]+(2^(1+n)) PolyGamma[n,2 k+2a],
PolyGamma[0,k_.+a_?hiq]:>-2 Log[2]-PolyGamma[0,(1/2)+k+a]+2 PolyGamma[0,2 k+2a]};

pgRule2=
{PolyGamma[0,k_]:>-EulerGamma+HarmonicNumber[k-1],
PolyGamma[m_/;m≥1,k_]:>(((-1))^(m+1)) Gamma[m+1](Zeta[m+1]-HarmonicNumber[k-1,m+1])};

xm[a_,m_]:=
Module[{t},
t=Cases[a,Gamma[c_.m+x_.]|PolyGamma[_,c_.m+x_]:>x/c,{0,∞}]//Union;
t=Select[t/.hgfϵ->0,#≤0&];
If[t==={},0,Max[-t]+1]//Floor
];

gpgRule1[m_]:=
{
Gamma[s_]:>
Module[{t=xm[Gamma[s],m],i},
Gamma[s+t]/Product[s+i,{i,0,t-1}]
],
PolyGamma[n_,s_]:>
Module[{t=xm[PolyGamma[n,s],m],i},
(((-1))^(1+n))Gamma[n+1]Sum[(((s+i))^(-1-n)),{i,0,t-1}]+PolyGamma[n,s+t]
]
};

gammaCancel[in_,n_]:=
Module[{args,group,transform},
args=Cases[in,Gamma[n+k_.]:>k,{0,∞}]//Union;
group={1-Mod[#[[1]],1],#}&/@GatherBy[args,Mod[#,1]&];
transform=Function[v,Gamma[n+#]->Gamma[n+v[[1]]]FunctionExpand[Gamma[n+#]/Gamma[n+v[[1]]]]&/@v[[2]]]/@group;
transform=Union[Flatten[transform]];
in/.transform//Expand
]

hn2hpl[n_]:=
{
HarmonicNumber[x_Rational+a_. n,r_:1]/;a∈Integers:>
Module[{num,den,tmp,k},
num=Numerator[x];
den=Denominator[x];
tmp=(den^(r-1))Sum[Exp[-2π I i x]hplZC[{r},den a n+num,{Exp[2π I i/den]},{0}],{i,0,den-1}];
tmp+=(den^r)bSum[If[Divisible[k-num,den],(k^(-r)),0],{k,num,0}];
tmp=PowerExpand[FunctionExpand[HarmonicNumber[x,r]]]+tmp;
tmp//Expand
],
HarmonicNumber[a_.n+b_.,c_: 1]/;{a,b}∈Integers:>hplZC[{c},a n+b,{1},{0}]
};

toHPL[in_,n_]:=
Module[{tmp,pg1,pg2,lcm,hpl1,hpl2,hpln},
tmp=Apart[in,n]//ExpandAll;
pg1=Cases[tmp,_PolyGamma,{0,∞}]//Union;
pg2=pg1/.pgRule1/.pgRule2/.hn2hpl[n]//Expand;
lcm=LCM@@Append[Cases[pg2,hplZC[_,_.+k_.n,__]:>k,{0,∞}],1];
pg2=pg2/.a:hplZC[_,_.+k_.n,___]:>hplMultiply[a,lcm/k]//ExpandAll;
pg2=pg2/.a:hplZC[_,k_.+_.n,___]:>hplShift[a,k-1]//Expand;
pg2=Refine[pg2,n∈Integers];
tmp=tmp/.Thread[pg1->pg2]//Expand;
tmp=tmp//hplShuffleAll//myExpand;
tmp=hplSum[tmp,{n,0,∞},Expand->True,Simplify->False]//hplEliminateNonPositiveIntegers;
tmp=tmp/.((p_)^(j_.+k_.hplN))/;k>0:>0/.hplN->∞;
If[FreeQ[tmp,hplN],tmp,Indeterminate]
];

hplD2F1[{a0_,b0_,c_},{p0_,q0_,r_},z0_]:=
Module[{res,tmp,a,b,p,q},
res/;Catch[
If[a0>b0,
a=a0;b=b0;p=p0;q=q0,
a=b0;b=a0;p=q0;q=p0,
Throw[False]
];
res=Which[
Length[z0]===0,$hplD2F1internal[{a,b,c},{p,q,r},$fZ],

Length[z0]===3,$hplD2F1internal[{a,b,c},{p,q,r},{$fS,$fT,$fZ}],
True,Throw[False]
];
If[res[[0]]===$hplD2F1internal,Throw[False]];
res=If[Length[z0]===0,
res/.{$fZ->z0},
res/.{$fS->z0[[1]],$fT->z0[[2]],$fZ->z0[[3]]}
];
True
]
];

$hplD2F1internal//Clear;
$hplD2F1internal[{a_,b_,c_},{p_,q_,r_},z0_]:=
Module[{res,tmp,tmp2,coeff1,coeff2,s,t,w,x,y,n,z,sp,tp,z1},
res/;Catch[
tmp=hpl2F1[a+w hgfϵ,b+x hgfϵ,c+y hgfϵ,z1];
CheckAbort[tmp=tmp/.hgfRule1,Throw[False]];
tmp=tmp[[1]]/.hgfsum[n]/.{(1/(1-z1))->s,((-z1)/(1-z1))->t,z1->z};
tmp2=tmp/.gpgRule1[n]/.n->n+xm[tmp,n];
coeff1=SeriesCoefficient[tmp2,{w,0,p},{x,0,q},{y,0,r},
Assumptions->0≤n&&n∈Integers]p!q!r!/.hgfϵ->1//Expand;
coeff2=SeriesCoefficient[Sum[tmp,{n,0,xm[tmp,n]-1}],{w,0,p},{x,0,q},{y,0,r},
Assumptions->0≤n&&n∈Integers]p!q!r!/.hgfϵ->1//Expand;
coeff1=gammaCancel[coeff1,n]/.x:PolyGamma[_?NumericQ,_?NumericQ]:>FunctionExpand[x];
coeff1=coeff1/.pgRule1/.v_Log:>PowerExpand[v,Assumptions->0<s<1&&-1<t<1&&0<z<1];
coeff2=coeff2/.pgRule1/.v_Log:>PowerExpand[v,Assumptions->0<s<1&&-1<t<1&&0<z<1];
tmp=toHPL[coeff1,n]+coeff2;
tmp=tmp/.x:hplZC[__,_,{t|s,___},_]:>hplMultiply[x,2];
tmp=Apart[tmp,n]//Expand;
tmp=tmp/.(_.s|_.t|_.z|(Sqrt[_.s])|(Sqrt[_.t])|(Sqrt[_.z]))^(x_.n+_.)/;Positive[x]->0/.n->∞;
tmp=hplSumToInt[tmp,VerifyConvergence->False];
tmp=tmp/.hplHC[k_,l_,m:{0..}]/;k[[-1]]=!=0:>With[{fac=(k[[-1]]/.s|t|z->1)/k[[-1]]},hplHC[k fac,l fac,m]];
If[z0[[0]]===List&&Length[z0]==3,
tmp=tmp/.{s->z0[[1]],t->z0[[2]],z->z0[[3]]},
tmp=tmp/.{s->(1/(1-z0)),t-> ((-z0)/(1-z0)),z->z0}
];
tmp=Coefficient[tmp,hplδ,0];
res=tmp//myExpand;
Remove[s,t,w,x,y,n,z];
$hplD2F1internal[{a,b,c},{p,q,r},z0]=res;
True
]
];

[多重和の評価]

Clear[qSum];
qSum[0,___]=0;
qSum[a_Plus,b__]:=qSum[#,b]&/@a;
qSum[a_Times,b__]:=
With[{k=Select[a,FreeQ[#,Alternatives@@{b}[[1;;,1]]]&]},
 k qSum[Select[a,!FreeQ[#,Alternatives@@{b}[[1;;,1]]]&],b]/;k=!=1
];
qSum[a_,b__]:=With[{k=Expand[a]},qSum[k,b]/;k=!=a];
qSum[a_,b__]/;!FreeQ[a,Power]:=
Module[{tmp,tmp2,sum},
tmp=If[a[[0]]===Times,List@@a,{a}]/.((x_)^(y_)):>(x^(Expand[y]))//.((x_)^(y_?NumericQ z_))/;MatchQ[z,Alternatives@@{b}[[1;;,1]]|hplN|cc[_]]:>((((x^y)))^z);
tmp2=Cases[tmp,((x_)^(y_+z_))/;FreeQ[{x,y},Alternatives@@{b}[[1;;,1]]]:>{(x^(y+z)),x,y,z}];
tmp=DeleteCases[tmp,Alternatives@@tmp2[[1;;,1]]];
tmp=(Times@@Power@@@tmp2[[1;;,2;;3]])sum[(Times@@Power@@@tmp2[[1;;,{2,4}]])Times@@tmp,b];
tmp=Refine[tmp,Append[{b}[[1;;,1]],hplN]∈Integers];
(tmp/.sum->qSum)/;tmp=!=sum[a,b]
];
qSum[a_,b__]:=sum[a,b];

Clear[$fApart,$foldApart,$sumApart];
$fApart[in_Plus,var_List]:=$fApart[#,var]&/@in;
$fApart[in_Times,var_List]:=$fApart[in,var]=
Expand[
Select[in,!FreeQ[#,Alternatives@@Most[var]]&]*
Apart[
Select[in,FreeQ[#,Alternatives@@Most[var]]&],
Last[var]
],
Alternatives@@var
];
$fApart[in_,var_List]:=in;
$sumApart[in_,var__List]:=$sumApart[in,var]=
Module[{tmp},
$hplSumState="sumApart"[in,var]/.cc->"cV";
tmp=Fold[$fApart,Expand[in,Alternatives@@{var}[[1;;,1]]],Rest[FoldList[Append,{},{var}[[1;;,1]]//Reverse]]];
qSum[tmp,var]//myExpand
];

Clear[shiftSumRegion,$sumDifference,$sumDifference2];
shiftSumRegion[in:sum[_,a___],b___]:=
Module[{tmp},
tmp=Quiet[
Reap[
Fold[
(
Sow[
pSum[Evaluate[#1/.#2[[1]]->Sequence[]],{#2[[1,1]],#2[[1,2]],#2[[2,2]]-1}]-
pSum[Evaluate[#1/.#2[[1]]->Sequence[]],{#2[[1,1]],#2[[1,3]]+1,#2[[2,3]]}]
];
#1/.#2[[1]]->#2[[2]]
)&,
in,
Thread[{{a},{b}}]
]
]/.sum[x_]:>x//Flatten//Total,
Infinity::indet
];
tmp/.x_hplZC/;IntegerQ[x[[2]]]:>(hplZSum@@x)
];
$sumDifference[a_,b__]:=
Module[{mResult,faclArgument,lIntLimit,tfaclIncludingIthVar,nDiffPosition,tlLimitIncludingIthVar,faclDiffSummand,faclOthers,i},
$hplSumState="sumDifference"[a,b]/.cc->"cV";
faclArgument=FactorList[a];
lIntLimit={b}[[1;;,2;;]]//Flatten;
For[i=1,i<Length[{b}],i++,
tfaclIncludingIthVar=Select[faclArgument,!FreeQ[#,{b}[[i,1]]]&];
If[!FreeQ[tfaclIncludingIthVar,Alternatives@@{b}[[i+1;;,1]]],Break[]];
tlLimitIncludingIthVar=Select[lIntLimit,!FreeQ[#,{b}[[i,1]]]&];
If[!FreeQ[tlLimitIncludingIthVar,Alternatives@@{b}[[i+1;;,1]]],Break[]];
If[MemberQ[Coefficient[{b}[[i+1;;,3]],{b}[[i,1]]],Except[0|1]],Break[]];
If[!FreeQ[{b}[[i+1;;,2]],{b}[[i,1]]],Break[]];
];
nDiffPosition=i;
If[nDiffPosition==Length[{b}],Return[sum[a,b]]];
faclDiffSummand=Select[faclArgument,!FreeQ[#,Alternatives@@{b}[[nDiffPosition+1;;,1]]]&];
faclOthers=Select[faclArgument,FreeQ[#,Alternatives@@{b}[[nDiffPosition+1;;,1]]]&];
mResult=$sumDifference2[Times@@Power@@@faclDiffSummand,Sequence@@({b}[[nDiffPosition+1;;]]),{b}[[nDiffPosition,1]],newvar[{a,b}]];
mResult=mResult/.sum[x_,y___]:>sum[x Times@@Power@@@faclOthers,Sequence@@({b}[[1;;nDiffPosition]]),y];
mResult=mResult/.x_ sum[y_,z___]:>sum[x y,z];
mResult=mResult/.sum->qSum/.sum[x_,y__]:>$sumApart[x//projSimplify,y]//myExpand;
$sumDifference[a,b]=mResult;
mResult
];
$sumDifference2[a_,c___,b_,v_]:=
Module[{term,relvTerm,coeff,denom,i,s,dlcm,projs,ct,shiftVar,shiftRule,tmp,lhs,lhs0,rhs,fac,diff,p,res,cnew,nrp,dummy,dummy2,dummy3},
$hplSumState="sumDifference2"[a,c,b,v]/.cc->"cV";
term=If[a[[0]]===Times,List@@a,{a}];
relvTerm=Times@@Select[term,!FreeQ[#,Alternatives@@{c}[[1;;,1]]]&];
coeff=Times@@Select[term,FreeQ[#,Alternatives@@{c}[[1;;,1]]]&];
denom=Denominator[relvTerm]/. (((_?NumericQ))^(_))|(((_)^(t_))/;!FreeQ[t,Alternatives@@{c}[[1;;,1]]])->1;
denom=If[denom[[0]]===Times,List@@denom,{denom}];
shiftVar=Array[s,Length[{c}]+1,0];
shiftRule=
Prepend[
MapIndexed[x_.#1/;FreeQ[x,#1]:>Evaluate[x #1+x s[#2[[1]]]]&,{c}[[1;;,1]]],
x_.b/;FreeQ[x,b]:>Evaluate[x b+x s[0]]
];
tmp=solveAlways2[(denom/.shiftRule)==denom,Prepend[{c}[[1;;,1]],b],Rest[shiftVar]]//Quiet;
If[tmp==={},Throw["sumDifference2Error"]];
dlcm= LCM@@Denominator[Rest[shiftVar]/.tmp[[1]]/.s[Except[0]]->0];
projs=Cases[a,projω[__][_],{0,∞}]//Union;
ct=dlcm;
While[Complement[Cases[projs/.shiftRule/.tmp[[1]]/.s[0]->ct/.s[_]->0,projω[__][_],{0,∞}],projs]=!={},ct+=dlcm];
Evaluate[shiftVar]=shiftVar /.tmp[[1]]/.s[0]->ct/.s[_]->0//Expand;
If[!TrueQ[Simplify[(denom/.shiftRule)==denom]],Throw["sumDifference2Error"]];
lhs=lhs0=sum[a,{dummy,dummy2,dummy3},c];
rhs=sum[a,{dummy,dummy2,dummy3},Sequence@@({c}-Rest[shiftVar])]/.shiftRule;
rhs=rhs/.x_sum:>shiftSumRegion[x,Sequence@@lhs[[2;;]]];
If[!FreeQ[{lhs,rhs},bSum],Throw["sumDifference2Error"]];
fac=(relvTerm/.shiftRule)/relvTerm/._hplZC->1//Cancel;
diff=rhs-lhs fac;
diff=diff/.x_.sum[y_,z__]:>sum[Expand[x y],z]//.sum[x_,y__]+sum[z_,y__]:>sum[Expand[x+z],y];
res=Sum[projω[s[0],0,1][b-p+s[0]]projω[1,0,(fac^(1/(s[0])))][b-p](lhs0/.b->p),{p,s[0]}]+
Sum[projω[1,0,(fac^(1/(s[0])))][b]projω[s[0],0,1][b-p+s[0]]sum2[projω[1,0,(fac^(-(1/(s[0]))))][v]projω[s[0],0,1][v-p](diff/.b->v-s[0]),{v,p+1,b}],{p,s[0]}]//Expand;
res=res/.x_.sum2[y_,z__]:>sum2[x y,z]/.sum2[x_,y__]:>Distribute[sum2[Expand[x],y]];
res=res/.x_.sum[y_,z__]:>sum[x y,z]/.sum2[sum[x_,y__],z__]:>sum[x,z,y]/.sum2[0,__]->0;
res=res/.sum[x_,y___]:>Distribute[sum[Expand[x],y]]//Expand;
res=res/.sum->pSum/.pSum->sum/.{dummy,dummy2,dummy3}->Sequence[];
res=res/.x_hplZC/;IntegerQ[x[[2]]]:>$hplZSum2@@x;
res=res/.sum[x_,y__]:>qSum[x,{b,b,b},y]/.sum[x_,{b,b,b},y__]:>sum[x,y]//elimST;
$sumDifference2[a,c,b,v]=res;
res
];

Clear[$sumRefine];
$sumRefine0[in_,v___]:=
Module[{tfaclArgument,tfaclProj,tfaclPoly,tfaclPower,tOther},
$hplSumState="sumRefine"[in,v]/.cc->"cV";
tfaclArgument=FactorList[in];
tfaclProj=Cases[tfaclArgument,p:{projω[a___][b_?(!FreeQ[{v}[[1;;,1]],#]&)],_}:>{p,a,b}];
tfaclPoly=Cases[tfaclArgument,p:{a_.+b_.c_,d_}:>
{p,a/.hplδ|_cc->0,a-(a/.hplδ|_cc->0),b,c,d}/;!FreeQ[{v}[[1;;,1]],c]];
tfaclPower=Cases[tfaclArgument,p:{a_^(b_.c_),d_}:>{p,a,b,c,d}/;!FreeQ[{v}[[1;;,1]],c]];
tOther=Times@@Power@@@DeleteCases[tfaclArgument,
Alternatives@@Union[tfaclProj[[1;;,1]],tfaclPoly[[1;;,1]],tfaclPower[[1;;,1]]]];
tfaclProj=Join[tfaclProj[[1;;,2;;]],{1,0,((#[[2]])^(#[[3]]#[[5]])),#[[4]]}&/@tfaclPower];
tfaclPoly=tfaclPoly[[1;;,2;;]];
qSum[
Times@@((((#[[1]]+#[[2]]+#[[3]]#[[4]]))^(#[[5]]))&/@tfaclPoly)*
Times@@(projω[#[[1]],#[[2]],#[[3]]][#[[4]]]&/@tfaclProj)tOther,
v
]
];
$sumRefine[in_,v___]:=
Module[{tfaclArgument,tfaclProj,tfaclPoly,tfaclPower,tOther,
tLCM,tIntRegion,tIndex,tmp,tDiff,tNewRegion},
$hplSumState="sumRefine"[in,v]/.cc->"cV";
tIndex={v}[[1;;,1]];
tfaclArgument=FactorList[in];
tfaclProj=Cases[tfaclArgument,p:{projω[a___][b_?(!FreeQ[{v}[[1;;,1]],#]&)],_}:>{p,a,b}];
tfaclPoly=Cases[tfaclArgument,p:{a_.+b_.c_,d_}:>
{p,a/.hplδ|_cc->0,a-(a/.hplδ|_cc->0),b,c,d}/;!FreeQ[{v}[[1;;,1]],c]];
tfaclPower=Cases[tfaclArgument,p:{a_^(b_.c_),d_}:>{p,a,b,c,d}/;!FreeQ[{v}[[1;;,1]],c]];
tOther=Times@@Power@@@DeleteCases[tfaclArgument,
Alternatives@@Union[tfaclProj[[1;;,1]],tfaclPoly[[1;;,1]],tfaclPower[[1;;,1]]]];
tfaclProj=Join[tfaclProj[[1;;,2;;]],{1,0,((#[[2]])^(#[[3]]#[[5]])),#[[4]]}&/@tfaclPower];
tfaclPoly=tfaclPoly[[1;;,2;;]];
tLCM=LCM[Sequence@@tfaclPoly[[1;;,3]],1];
tfaclProj=Join[
tfaclProj/.{a_,b_,c_,d_}/;!FreeQ[{v}[[1;;,1]],d]:>{a tLCM,b tLCM,c^(1/tLCM),d},
{tLCM,0,1,#}&/@tIndex
];
tIntRegion={v}/.Thread[tIndex->tIndex/tLCM]/.{a_ b_?NumericQ,c_,d_}:>{a,c/b,d/b};
If[!stest[Sequence@@tIntRegion],Throw["sumRefineError"]];
tmp=sum[
Expand[Times@@((((#[[1]]tLCM+#[[2]]tLCM+#[[3]]#[[4]]))^(#[[5]]))&/@tfaclPoly)]*
Times@@(projω[#[[1]],#[[2]],#[[3]]][#[[4]]]&/@tfaclProj)tOther,
Sequence@@Expand[tIntRegion]
];
tDiff=-tLCM tfaclPoly[[1;;,1]]/tfaclPoly[[1;;,3]];
If[!IntegerQ[#],Throw["sumRefineError"]]&/@tDiff;
tmp=tmp/.Thread[tfaclPoly[[1;;,4]]->tfaclPoly[[1;;,4]]+tDiff]//myExpand;
tmp=tmp//.sum[a_,b___,{c_+d_Integer,e_,f_},g___]:>sum[a,b,{c,e-d,f-d},g];
tNewRegion=List@@tmp[[2;;]]-(List@@tmp[[2;;]]/._cc|Alternatives@@tIndex->0);
tNewRegion[[1;;,2]]++;
tmp=Times@@((tLCM^(-tfaclPoly[[1;;,5]])))shiftSumRegion[tmp,Sequence@@tNewRegion];
tmp=tmp/.sum->pSum/.pSum->sum/.sum->qSum/.sum->$sumApart//myExpand;
$sumRefine[in,v]=tmp;
tmp
];

Clear[seriesx,$nestedSum];
seriesx[in_,x_]:=
Module[{tmp,tmp2,coeff},
Fold[
Function[{u,v},
tmp=If[u[[0]]===Plus,List@@u,{u}];
tmp=(
tmp2=#/.a_hplZC:>(a/.∞->hplN)/.v->0//Quiet;
If[!FreeQ[tmp2,DirectedInfinity|Indeterminate]||tmp2===0//Quiet,
If[#[[0]]===Times,
coeff=Select[#,FreeQ[#,v]&];
tmp2=Select[#,!FreeQ[#,v]&],
(*else*)
coeff=1;
tmp2=#
];
tmp2=coeff Series[tmp2,{v,0,0}]//Normal//myExpand,
(*else*)
tmp2=#/.v->0
];
tmp2
)&/@tmp//Total;
tmp//myExpand
],
in//myExpand,x
]
];
$nestedSum[in_,var__]:=$nestedSum[in,var]=
Module[{tmp,bef,aft},
tmp=sum[in,var]//.sum[a_,b_,c__]:>sum[sum[a,c],b]//.sum[a_ b_,{c_,d_,e_}]/;FreeQ[a,c]:>a sum[b,{c,d,e}];
FixedPoint[
Function[v,
bef=Cases[v,a_sum/;FreeQ[List@@a,sum],{0,∞}]//Union;
aft=hplShuffleAll[bef]/.sum->qSum//.{
sum[(((a_+b_.))^(c_.)),{a_,1,z_}]:>hplSC[{-c},z,{1},{b}],
sum[(((a_+b_.))^(c_.))projω[d__][a_],{a_,1,z_}]:>hplSC[{-c},z,{projω[d]},{b}],
sum[projω[d__][a_],{a_,1,z_}]:>hplSC[{0},z,{projω[d]},{0}],
sum[(((a_+b_.))^(c_.))hplSC[{p__},a_,{q__},{r__}],{a_,1,z_}]:>hplSC[{-c,p},z,{1,q},{b,r}],
sum[(((a_+b_.))^(c_.))projω[d__][a_]hplSC[{p__},a_,{q__},{r__}],{a_,1,z_}]:>hplSC[{-c,p},z,{projω[d],q},{b,r}],
sum[projω[d__][a_]hplSC[{p__},a_,{q__},{r__}],{a_,1,z_}]:>hplSC[{0,p},z,{projω[d],q},{0,r}]
};
v/.Thread[bef->aft]
],
tmp
]//hplRepZ
];

Clear[proj]
proj[1,x_]:=1
proj[a_,x_]:=projω[a,0,1][x]

elimST[in_]:=
Module[{tmp},
tmp=in/.x_sum/;!FreeQ[Denominator[x[[1]]],cc[∞]]->0;
tmp=tmp/.sum[__,{_,_+_. cc[∞],_.cc[∞]}]->0;
tmp=myExpand[tmp];
tmp=If[tmp[[0]]===Plus,List@@tmp,{tmp}];
tmp=If[!FreeQ[Denominator[#],cc[∞]],0,#]&/@tmp//Total
];

(* indexのshiftの際、表面項を無視できるか *)
Clear[irrevelantSurfaceTermQ];
irrevelantSurfaceTermQ[sum[in_,v___]]:=
Module[{tmp,var,tfaclArgument,tfaclProj,tfaclPoly,tfaclPower,tOther,coeff,const},
Catch[
(* 変数が含まれている和は不可 *)
If[!FreeQ[in,cc|hplN],Throw[False]];
(* 下端が定数のものに限る *)
If[MemberQ[{v}[[1;;,2]],Except[_Integer]],Throw[False]];
(* 上端が変数または∞のものに限る *)
var=Append[{v}[[1;;,1]],hplN];
(* 因数分解 *)
If[!MatchQ[#,a_.+b_]/;IntegerQ[a]&&MemberQ[var,b],Throw[False]]&/@{v}[[1;;,3]];
tfaclArgument=FactorList[in/.hplδ->0];
tfaclProj=Cases[tfaclArgument,p:{projω[a___][b_?(!FreeQ[{v}[[1;;,1]],#]&)],_}:>{p,a,b}];
tfaclPoly=Cases[tfaclArgument,p:{a_ ?(PolynomialQ[#,var]&&!FreeQ[#,Alternatives@@var]&),d_}:>{p,a,d}];
tfaclPower=Cases[tfaclArgument,p:{a_^(b_.c_),d_}:>{p,a,b,c,d}/;!FreeQ[{v}[[1;;,1]],c]];
tOther=Times@@Power@@@DeleteCases[tfaclArgument,
Alternatives@@Union[tfaclProj[[1;;,1]],tfaclPoly[[1;;,1]],tfaclPower[[1;;,1]]]];
tfaclProj=Join[tfaclProj[[1;;,2;;]],{1,0,((#[[2]])^(#[[3]]#[[5]])),#[[4]]}&/@tfaclPower];
tfaclPoly=tfaclPoly[[1;;,2;;]];
(* 変な項が含まれている場合 *)
If[!FreeQ[tOther,Alternatives@@var],Throw[False]];
(* 分子は(((絶対値1の定数))^index)に限る *)
If[MemberQ[tfaclProj,{_,_,_?(Abs[#]=!=1&),_}],Throw[False]];
If[MemberQ[tfaclPoly,{___,_?Positive}],Throw[False]];
(* 分母は(((indexの一次式))^const)の積に限る *)
Function[p,
coeff=Coefficient[p,var];
const=p/.Thread[var->0];
If[coeff.var+const=!=p,Throw[False]];
]/@tfaclPoly[[1;;,1]];
(* 発散する和は不可 *)
If[-Total[tfaclPoly[[1;;,2]]]≤Length[{v}],Throw[False]];
(* 分母の係数行列の階数が低いものは不可 *)
If[MatrixRank[Coefficient[#[[1]],Most[var]]&/@tfaclPoly]<Length[{v}],Throw[False]];
True
]
];

Clear[$multipleSum];
$multipleSum[in_,x:({_Symbol,_,_}...),vvv_Symbol]:=$multipleSum[in,x,vvv]=
Module[{convCond,varAll,varSumIndex,varConst,mSummand,lSumRegion,tmp,hplδ2},
varAll=Union[Cases[{in,x},a_Symbol/;!NumericQ[a],{0,∞}]];
varSumIndex={x}[[1;;,1]];
varConst=Complement[varAll,varSumIndex,{hplδ,hplN,vvv}];
mSummand=in/.Thread[varConst->(cc/@varConst)];
lSumRegion={x}/.Thread[varConst->(cc/@varConst)];
If[irrevelantSurfaceTermQ[sum[in,x]],lSumRegion=lSumRegion/.hplN->cc[∞]];
tmp=If[vvv=!=None,sum[mSummand,{vvv,vvv,vvv},Sequence@@lSumRegion],sum[mSummand,Sequence@@lSumRegion]];
tmp=tmp/.sum->$sumApart//projSimplify;
tmp=tmp/.sum->qSum//.sum->$sumDifference//elimST;
tmp=tmp/.{vvv,vvv,vvv}->Sequence[]/.sum[p_]:>p/.sum[0,__]->0/.sum->qSum;
tmp=tmp/.j:vvv|hplN:>cc[j];
tmp=tmp//.sum->$sumRefine//myExpand//elimST;
tmp=myExpand[tmp]//.a_ sum[b_,c__]:>sum[a b,c];
tmp=tmp/.sum[a_,b__]:>sum[seriesx[a,{hplδ}],b]/.sum->qSum/.sum->$sumApart;
tmp=tmp/.sum->$nestedSum//myExpand;
tmp=seriesx[tmp/.s_hplZC:>(s/.hplδ->hplδ2),{hplδ}]/.hplδ2->hplδ;
tmp=tmp//.((a_)^(b_.+c_.d_cc)):>(a^b)projω[1,0,(a^c)][d]//projSimplify;
tmp/.∞->hplN/.cc[a_]:>a
];

stest[x__]:=
Module[{var,chk,coeff},
var=Variables[{x}];
chk={x}[[1;;,2;;]]//Flatten;
coeff=Coefficient[#,var]&/@chk;
If[MemberQ[coeff//Flatten,Except[_Integer]],Return[False]];
MapThread[If[!IntegerQ[#1.var-#2],Return[False]]&,{coeff,chk}];
True
];

Options[hplSum]:={Simplify->True,Expand->False};
hplSum//Clear;
hplSum[in_,x:({_Symbol,_,_}..),vvv_Symbol:None,OptionsPattern[]]:=
Module[{res,ftest,tmp,var,SurfaceTermIsIrrelevant},
SurfaceTermIsIrrelevant=True;
res=Catch[
If[!stest[x]||MemberQ[{x}[[1;;,1]],vvv],Throw["hplSumError"]];
tmp=in;
tmp=qSum[tmp,x]/.DirectedInfinity[a_]:>a hplN/.a_HarmonicNumber:>FunctionExpand[a];
tmp=tmp/.a:hplZC[_,_?(!FreeQ[#,_Rational|_Complex]&),__]:>hplZSumToMPL[a];
tmp=tmp/.hplZC[a__]:>hplZSum[a,Sum->False];
tmp=tmp/.{
PolyGamma[n_Integer?Positive,a_]:>
With[{s=newvar[a]},
If[stest[{s,0,a-1}],(((-1))^n)n!(sum[(1/(s^(n+1))),{s,1,a-1}]-Zeta[n+1]),n!(((-1))^(n+1))sum[(1/(((s+a))^(n+1))),{s,0,hplN}]]
],
PolyGamma[0,a_]:>With[{s=newvar[a]},sum[(1/(s+1))-(1/(s+a)),{s,0,hplN}]-EulerGamma]
};
tmp=tmp//.sum[a_,b___,{s_,c_},d___]:>sum[a,b,{s,1,c},d];
tmp=tmp//.sum[a_,b__]:>sum[Expand[a],b];
tmp=tmp/.((a_sum)^(b_Integer?Positive)):>
Module[{p,s},
p=List@@a[[2;;,1]];
s=Partition[newvarList[a,Length[p]b],Length[p]];
Times@@(a/.Thread[p->#]&/@s)
];
tmp=tmp//.a_ b_sum:>With[{s=newvarList[a b,Length[b]-1]},
sum[a(b[[1]]/.Thread[List@@b[[2;;,1]]->s]),Sequence@@(b[[2;;]]/.Thread[List@@b[[2;;,1]]->s])]];
tmp=tmp/.sum:>(sum[Expand[#1],##2]&)/.sum:>(Distribute[sum[##]]&);
tmp=tmp//.sum[sum[a_,b__],c__]/;Intersection[List@@{b}[[1;;,1]],List@@{c}[[1;;,1]]]==={}:>sum[a,c,b];
var=Cases[tmp,sum[_,a__]:>{a}[[1;;,1]],{0,∞}]//Flatten//Union;
If[FreeQ[in,hplδ],tmp=tmp/.sum[a_,b__]:>sum[a/.Thread[var->var+Array[Prime[1000000#]&,Length[var]]hplδ],b]];
tmp=tmp/.((a_)^(b_)):>(a^(b/.hplδ->0))/.a:projω[__][_]:>(a/.hplδ->0);
If[!FreeQ[tmp,a_Sum/;!FreeQ[List@@a,sum]],Throw["hplSumError"]];
tmp=Factor//@tmp/.sum->qSum/.sum->$sumRefine0;
tmp=tmp/.sum[a__]:>$multipleSum[a,vvv]/.a_hplZC:>(a/.projω[1,0,v_]:>v)/.projω[1,0,v_][c_]:>(v^c);
tmp=tmp//myExpand;
Sow[Cases[tmp,_hplZC,{0,∞}]//Union];
tmp=tmp/.aa:hplZC[a_,p_Integer b_,c:{_projω..},d_]/;
And@@Divisible[c[[1;;,1]],p]&&And@@Divisible[c[[1;;,2]],p]:>
(p^(-Total[a]))hplZC[a,b,c/.projω[xx_,yy_,zz_]:>projω[xx/p,yy/p,(zz^p)]/.projω[1,0,v_]:>v,d/p];
Series[tmp,{hplδ,0,0}]//Normal//Expand
];
If[TrueQ[OptionValue[Expand]],
If[TrueQ[OptionValue[Simplify]],res//hplMZVSimplify,res]//hplExpandProjector,
If[TrueQ[OptionValue[Simplify]],res//hplMZVSimplify,res]
]
];
hplSum[in_,vvv:(_Symbol|0):0]:=in;

[SCInt関連]

hplSTrf[in_,s_,t_,z_,m_]:=If[m,((4 (1-z))/(((1+z))^3)),1]*
Developer`ReplaceAllUnheld[in,{(((1+(Sqrt[s])))^(p_.)):>(2^p) (((1-z))^(-p)),(((1-(Sqrt[s])))^(p_.)):>(((-1))^(-p)) (2^p) (((1-z))^(-p)) (z^p),(((-1+(Sqrt[s])))^(p_.)):>(2^p) (((1-z))^(-p)) (z^p),(((-1-(Sqrt[s])))^(p_.)):>(((-(1/2)))^(-p)) (((1-z))^(-p)),(((1-s))^(p_.)):>(((-1))^(-p)) (4^p) (((1-z))^(-2 p)) (z^p),(((-1+s))^(p_.)):>(4^p) (((1-z))^(-2 p)) (z^p),Log[s]:>-2 Log[1-z]+2 Log[1+z],(s^(p_.)):>(((1-z))^(-2 p)) (((1+z))^(2 p)),(((1-z))^(p_.)):>(((1-z))^(2 p)) (((1+z))^(-2 p)),(((-1+z))^(p_.)):>(((-1))^p) (((1-z))^(2 p)) (((1+z))^(-2 p)),Log[1-z]:>2 Log[1-z]-2 Log[1+z],Log[-1+z]:>I π+2 Log[1-z]-2 Log[1+z],Log[z]:>2 Log[2]+Log[z]-2 Log[1+z],Log[-z]:>-I π+2 Log[2]+Log[z]-2 Log[1+z],(z^(p_.)):>(4^p) (z^p) (((1+z))^(-2 p))}]

hplS2Trf[in_,s_,t_,z_,m_]:=If[m,((8 (1-z) z (1+z))/((((1-I z))^3) (((1+I z))^3))),1]*
Developer`ReplaceAllUnheld[in,{(((1+(Sqrt[s])))^(p_.)):>(2^p) (((1-z))^(-p)) (((1+z))^(-p)),(((1-(Sqrt[s])))^(p_.)):>(((-(1/2)))^(-p)) (((1-z))^(-p)) (z^(2 p)) (((1+z))^(-p)),(((-1+(Sqrt[s])))^(p_.)):>(2^p) (((1-z))^(-p)) (z^(2 p)) (((1+z))^(-p)),(((-1-(Sqrt[s])))^(p_.)):>(((-(1/2)))^(-p)) (((1-z))^(-p)) (((1+z))^(-p)),(((1-s))^(p_.)):>(((-(1/4)))^(-p)) (((1-z))^(-2 p)) (z^(2 p)) (((1+z))^(-2 p)),(((-1+s))^(p_.)):>(4^p) (((1-z))^(-2 p)) (z^(2 p)) (((1+z))^(-2 p)),Log[s]:>-2 Log[1-z]+2 Log[1-I z]+2 Log[1+I z]-2 Log[1+z],(s^(p_.)):>(((1-z))^(-2 p)) (((1-I z))^(2 p)) (((1+I z))^(2 p)) (((1+z))^(-2 p)),(((1-z))^(p_.)):>(((1-z))^(2 p)) (((1-I z))^(-2 p)) (((1+I z))^(-2 p)) (((1+z))^(2 p)),(((-1+z))^(p_.)):>(((-1))^p) (((1-z))^(2 p)) (((1-I z))^(-2 p)) (((1+I z))^(-2 p)) (((1+z))^(2 p)),Log[1-z]:>2 Log[1-z]-2 Log[1-I z]-2 Log[1+I z]+2 Log[1+z],Log[-1+z]:>I π+2 Log[1-z]-2 Log[1-I z]-2 Log[1+I z]+2 Log[1+z],Log[z]:>2 Log[2]-2 Log[1-I z]-2 Log[1+I z]+2 Log[z],Log[-z]:>-I π+2 Log[2]-2 Log[1-I z]-2 Log[1+I z]+2 Log[z],(z^(p_.)):>(4^p) (((1-I z))^(-2 p)) (((1+I z))^(-2 p)) (z^(2 p))}]

hplTTrf[in_,s_,t_,z_,m_]:=If[m,-(((1-z) (1+z))/(4 (z^2))),1]*
Developer`ReplaceAllUnheld[in,{(((1+(Sqrt[t])))^(p_.)):>(2^p) (((1-z))^(-p)),(((1-(Sqrt[t])))^(p_.)):>(((-1))^(-p)) (2^p) (((1-z))^(-p)) (z^p),(((-1+(Sqrt[t])))^(p_.)):>(2^p) (((1-z))^(-p)) (z^p),(((-1-(Sqrt[t])))^(p_.)):>(((-(1/2)))^(-p)) (((1-z))^(-p)),(((1-t))^(p_.)):>(((-1))^(-p)) (4^p) (((1-z))^(-2 p)) (z^p),(((-1+t))^(p_.)):>(4^p) (((1-z))^(-2 p)) (z^p),Log[s]:>-I π+2 Log[2]-2 Log[1-z]+Log[z],(t^(p_.)):>(((1-z))^(-2 p)) (((1+z))^(2 p)),(s^(p_.)):>(((-1))^(-p)) (4^p) (((1-z))^(-2 p)) (z^p),(((1-z))^(p_.)):>(((-(1/4)))^p) (((1-z))^(2 p)) (z^(-p)),(((-1+z))^(p_.)):>(4^(-p)) (((1-z))^(2 p)) (z^(-p)),Log[1-z]:>I π-2 Log[2]+2 Log[1-z]-Log[z],Log[-1+z]:>-2 Log[2]+2 Log[1-z]-Log[z],Log[z]:>-2 Log[2]-Log[z]+2 Log[1+z],Log[-z]:>I π-2 Log[2]-Log[z]+2 Log[1+z],(z^(p_.)):>(4^(-p)) (z^(-p)) (((1+z))^(2 p))}]

hplT2Trf[in_,s_,t_,z_,m_]:=If[m,-(((1-z) (1-I z) (1+I z) (1+z))/(2 (z^3))),1]*
Developer`ReplaceAllUnheld[in,{(((1+(Sqrt[t])))^(p_.)):>(2^p) (((1-z))^(-p)) (((1+z))^(-p)),(((1-(Sqrt[t])))^(p_.)):>(((-(1/2)))^(-p)) (((1-z))^(-p)) (z^(2 p)) (((1+z))^(-p)),(((-1+(Sqrt[t])))^(p_.)):>(2^p) (((1-z))^(-p)) (z^(2 p)) (((1+z))^(-p)),(((-1-(Sqrt[t])))^(p_.)):>(((-(1/2)))^(-p)) (((1-z))^(-p)) (((1+z))^(-p)),(((1-t))^(p_.)):>(((-(1/4)))^(-p)) (((1-z))^(-2 p)) (z^(2 p)) (((1+z))^(-2 p)),(((-1+t))^(p_.)):>(4^p) (((1-z))^(-2 p)) (z^(2 p)) (((1+z))^(-2 p)),Log[s]:>-I π+2 Log[2]-2 Log[1-z]+2 Log[z]-2 Log[1+z],(t^(p_.)):>(((1-z))^(-2 p)) (((1-I z))^(2 p)) (((1+I z))^(2 p)) (((1+z))^(-2 p)),(s^(p_.)):>(((-(1/4)))^(-p)) (((1-z))^(-2 p)) (z^(2 p)) (((1+z))^(-2 p)),(((1-z))^(p_.)):>(((-(1/4)))^p) (((1-z))^(2 p)) (z^(-2 p)) (((1+z))^(2 p)),(((-1+z))^(p_.)):>(4^(-p)) (((1-z))^(2 p)) (z^(-2 p)) (((1+z))^(2 p)),Log[1-z]:>I π-2 Log[2]+2 Log[1-z]-2 Log[z]+2 Log[1+z],Log[-1+z]:>-2 Log[2]+2 Log[1-z]-2 Log[z]+2 Log[1+z],Log[z]:>-2 Log[2]+2 Log[1-I z]+2 Log[1+I z]-2 Log[z],Log[-z]:>I π-2 Log[2]+2 Log[1-I z]+2 Log[1+I z]-2 Log[z],(z^(p_.)):>(4^(-p)) (((1-I z))^(2 p)) (((1+I z))^(2 p)) (z^(-2 p))}]

hplZTrf[in_,s_,t_,z_,m_]:=If[m,2 z,1]*
Developer`ReplaceAllUnheld[in,{(((1+(Sqrt[z])))^(p_.)):>(((1+z))^p),(((1-(Sqrt[z])))^(p_.)):>(((1-z))^p),(((-1+(Sqrt[z])))^(p_.)):>(((-1))^p) (((1-z))^p),(((-1-(Sqrt[z])))^(p_.)):>(((-1))^(-p)) (((1+z))^p),(((1-z))^(p_.)):>(((1-z))^p) (((1+z))^p),(((-1+z))^(p_.)):>(((-1))^p) (((1-z))^p) (((1+z))^p),Log[1-z]:>Log[1-z]+Log[1+z],Log[-1+z]:>I π+Log[1-z]+Log[1+z],Log[z]:>2 Log[z],Log[-z]:>-I π+2 Log[z],(z^(p_.)):>(z^(2 p))}]

hplZ2Trf[in_,s_,t_,z_,m_]:=If[m,-((8 (1-z) z (1+z))/((((1-I z))^3) (((1+I z))^3))),1]*
Developer`ReplaceAllUnheld[in,{(((1+(Sqrt[z])))^(p_.)):>(2^p) (((1-I z))^(-p)) (((1+I z))^(-p)),(((1-(Sqrt[z])))^(p_.)):>(2^p) (((1-I z))^(-p)) (((1+I z))^(-p)) (z^(2 p)),(((-1+(Sqrt[z])))^(p_.)):>(((-(1/2)))^(-p)) (((1-I z))^(-p)) (((1+I z))^(-p)) (z^(2 p)),(((-1-(Sqrt[z])))^(p_.)):>(((-2))^p) (((1-I z))^(-p)) (((1+I z))^(-p)),(((1-z))^(p_.)):>(4^p) (((1-I z))^(-2 p)) (((1+I z))^(-2 p)) (z^(2 p)),(((-1+z))^(p_.)):>(((-(1/4)))^(-p)) (((1-I z))^(-2 p)) (((1+I z))^(-2 p)) (z^(2 p)),Log[1-z]:>2 Log[2]-2 Log[1-I z]-2 Log[1+I z]+2 Log[z],Log[-1+z]:>-I π+2 Log[2]-2 Log[1-I z]-2 Log[1+I z]+2 Log[z],Log[z]:>2 Log[1-z]-2 Log[1-I z]-2 Log[1+I z]+2 Log[1+z],Log[-z]:>I π+2 Log[1-z]-2 Log[1-I z]-2 Log[1+I z]+2 Log[1+z],(z^(p_.)):>(((1-z))^(2 p)) (((1-I z))^(-2 p)) (((1+I z))^(-2 p)) (((1+z))^(2 p))}]

hplNTrf[in_,s_,t_,z_,m_]:=in;

fExp[coeff_ x_,z_]/;FreeQ[coeff,z]:=fExp[x,z];

fExp[x_ ((hpl2F1[___,z_])^(_.)),z_]:=fExp[x,z];

fExp[x_,z_]/;FreeQ[x,z]={0,0,0};

fExp[in_ ,z_]/;fmatch[in,z]:=
Module[{tmp},
Catch[
Cases[in,((((1-z)|(z-1)))^(a_.))(((1+z))^(b_.))(z^(c_.)):>Throw[{a,b,c}],{0}];
Cases[in,((((1-z)|(z-1)))^(a_.))(((1+z))^(b_.)):>Throw[{a,b,0}],{0}];
Cases[in,((((1-z)|(z-1)))^(a_.))(z^(c_.)):>Throw[{a,0,c}],{0}];
Cases[in,((((1-z)|(z-1)))^(a_.)):>Throw[{a,0,0}],{0}];
Cases[in,(((1+z))^(b_.))(z^(c_.)):>Throw[{0,b,c}],{0}];
Cases[in,(((1+z))^(b_.)):>Throw[{0,b,0}],{0}];
Cases[in,(z^(c_.)):>Throw[{0,0,c}],{0}];
Cases[in,1:>Throw[{0,0,0}],{0}];
Abort[]
]
];

fHGF[coeff_ x_,z_]/;FreeQ[coeff,z]:=fHGF[x,z];

fHGF[x_ y_,z_]/;fmatch[x,z]:=fHGF[y,z];

fHGF[x_,z_]/;fmatch[x,z]:={};

fHGF[a:(((hpl2F1[___,y_])^(x_.))/;x∈Integers&&x>0),y_]:={a};

fHGF[a:HoldPattern[Times[(((hpl2F1[___,y_])^(_?(#∈Integers&&#>0&)))|hpl2F1[___,y_])..]],y_]:=
Flatten[{Replace[a,Times->List,{1},Heads->True]}];

trfavail[exp_,trf0_]:=
Module[{trf},
If[MemberQ[trf0,hplTTrf|hplT2Trf]&&MemberQ[trf0,hplZTrf|hplNTrf],Return[{}]];
If[MemberQ[trf0,hplTTrf|hplT2Trf]&&MemberQ[trf0,hplSTrf],Return[{}]];
If[MemberQ[trf0,hplSTrf]&&MemberQ[trf0,hplZTrf|hplNTrf],Return[{}]];
If[exp[[2]]==1/2,Return[{}]];
trf=Which[
MemberQ[trf0,hplT2Trf],If[(Times@@trf0/.hplT2Trf->-hplTTrf/.(hplTTrf^(_))->hplTTrf)===hplTTrf,hplTTrf,hplT2Trf],
MemberQ[trf0,hplZTrf],hplZTrf,
trf0==={},All,
True,trf0[[1]]
];
Switch[trf,
hplSTrf,If[exp[[3]]==1/2,{hplS2Trf},{hplSTrf}],
hplTTrf,If[exp[[1]]+exp[[3]]∈Integers,{hplTTrf,hplT2Trf},{hplT2Trf}],
hplT2Trf,If[exp[[1]]+exp[[3]]∉Integers,{hplTTrf,hplT2Trf},{hplT2Trf}],
hplZTrf,If[exp[[2]]==1,If[exp[[1]]==1/2,{},{hplZTrf}],If[exp[[1]]==1/2,{hplZ2Trf},{hplZTrf,hplZ2Trf}]],
hplNTrf,Switch[exp,
{1/2,1,_},{},
{0|1,1,1/2},{hplZTrf},
{0|1,1,0|1},{hplNTrf,hplZTrf},
{1/2,0,_},{hplZ2Trf},
{1|0,0,1/2},{hplZTrf,hplZ2Trf},
{1|0,0,1|0},{hplNTrf,hplZTrf,hplZ2Trf}
],
All,Switch[exp,
{1/2,1,_},{},
{0|1,1,1/2},{hplZTrf},
{0|1,1,0|1},{hplNTrf,hplZTrf},
{1/2,0,1/2},{hplTTrf,hplT2Trf,hplS2Trf,hplZ2Trf},
{1/2,0,0|1},{hplSTrf,hplT2Trf,hplS2Trf,hplZ2Trf},
{1|0,0,1/2},{hplZTrf,hplT2Trf,hplS2Trf,hplZ2Trf},
{1|0,0,1|0},{hplNTrf,hplTTrf,hplSTrf,hplZTrf,hplT2Trf,hplS2Trf,hplZ2Trf}
]
]
];

selectTransformation[hplSCInt[in_,v:({_Symbol,_?NumericQ,_}...),p:({_Symbol..}...)]]:=
Module[{tmp,pos,exp,exp2,trf,hgf,chk,res},
pos=Rule@@@{v}[[1;;,1;;2]];
Catch[
res=Function[{term},
Function[{var},
exp=fExp[term,#]&/@var;
If[!FreeQ[exp,fExp],Throw[None]];
exp2=exp/.pos/.{a_Integer/;a≠0->1,a_Rational/;2a∈Integers->1/2};
If[DeleteCases[Flatten[exp2],0|1|1/2]=!={},Throw[None]];
hgf=fHGF[term,#]&/@var;
If[!FreeQ[hgf,fHGF],Throw[None]];
CheckAbort[
trf=hgf/.pos/.((k_hpl2F1)^(x_.)):>Sequence@@ConstantArray[(k/.hgfRule1)[[2]],x]//Quiet,
Throw[None]
];
chk=Times@@MapThread[(((((#1-1))^(#2[[1]]))(((#1+1))^(#2[[2]]))((#1)^(#2[[3]])))&),{var,exp}]Times@@Flatten[hgf]//Cancel;
If[!FreeQ[term/chk/.(((-1+y_.))^(k_.))/;MemberQ[var,y]:>(((1-y))^k),Alternatives@@var],Throw[None]];
tmp=Intersection@@trfavail@@@Transpose[{exp2,trf}];
If[tmp==={},Throw[None]];
tmp
]/@{p}
]/@Flatten[{Replace[in,Plus->List,{1},Heads->True]}];
res=Intersection@@@Transpose[res];
If[MemberQ[res,{}],Throw[None]];
SortBy[#,#/.{hplNTrf->1,hplZTrf->2,hplTTrf->3,hplSTrf->4,hplZ2Trf->5,hplT2Trf->6,hplS2Trf->7}&][[1]]&/@res
]
];

hplSCIntToMZV//Clear;
hplSCIntToMZV[data:hplSCInt[integ_,ser:({_Symbol,_?NumericQ,_Integer?NonNegative}...),symb:({_Symbol...}...)],δ_Symbol,OptionsPattern[]]:=
Module[{f,res,var1,avar,bvar,cvar,serCoeff,hgf1,hgf2,trfs,trff,s,t,z,w,dummy,dom1,dom2,symb2},
f=Catch[
$ttt=TimeUsed[];
var1=Variables[integ]/.(hpl2F1|Gamma|Power)[a___]:>Variables[{a}]//Flatten//Union;
var1=Cases[var1,_Symbol,{0,∞}];
If[var1=!=Variables[{ser,symb}],Throw[False]];
avar=Flatten[{symb}];
If[Length[avar]=!=Length[Union[avar]],Throw[False]];
trfs=selectTransformation[data];
If[trfs===None,Throw[False]];
MapThread[Function[{x,y},(trff[#]=y)&/@x],{{symb},trfs}];
serCoeff=If[{ser}==={},integ,
If[integ[[0]]===Plus,SeriesCoefficient[#,ser]&/@integ,SeriesCoefficient[integ,ser]]];
serCoeff=serCoeff/.v:(_PolyGamma|_Log)/;And@@(NumericQ/@List@@v):>PowerExpand[FunctionExpand[v]];
serCoeff=serCoeff/.x_hpl2F1:>Hold[x]/.x:Derivative[__][hpl2F1][__]:>Hold[x]//Together;
serCoeff=Fold[hplLogToHPL[trff[#2][#1,dummy,dummy,#2,True],#2]&,serCoeff,avar]//Together//ReleaseHold;
hgf1=Cases[serCoeff,_hpl2F1|Derivative[__][hpl2F1][___],{0,∞}]//Union;
hgf2=hgf1/.{
hpl2F1[a_,b_,c_,z_]:>
hplLogToHPL[trff[z][hplD2F1[{a,b,c},{0,0,0},{s,t,z}(*,∞*)],s,t,z,False],z],
Derivative[p_,q_,r_,0][hpl2F1][a_,b_,c_,z_]:>
hplLogToHPL[trff[z][hplD2F1[{a,b,c},{p,q,r},{s,t,z}(*,∞*)],s,t,z,False],z]
};
hgf2=hgf2/.a_hplZC:>hplSumToInt[a,VerifyConvergence->False]/.
hplHC[p:{___,a_},1,q:{0...}]:>hplHC[p/a,1/a,q];
hgf2=hgf2/.a_hplZC:>hplSumToInt[a,VerifyConvergence->False]/.
hplHC[p_,t_?NumericQ a_,q:{0...}]:>hplHC[p/t,a,q];
serCoeff=serCoeff/.Dispatch[Thread[hgf1->hgf2]]//Expand//hplShuffleAll;
dom1=Cases[serCoeff,_hplHC,{0,∞}]//Union;
dom2=dom1/.{
hplHC[a_,(1+z_)/(1-z_),{0...}]:>hplArgTransform[a,{-1,1,1,1},{z,-1+δ}],
hplHC[a_,((1-I z_) (1+I z_))/((1-z_) (1+z_)),{0...}]:>
(hplArgTransform[a,{-1,1,1,1},{(z^2),I-I δ}]/.hplHC[b_,(z^2),{0...}]:>hplArgTransform[b,2,z]),
hplHC[a_,((z_)^2),{0...}]:>hplArgTransform[a,2,z],
hplHC[a_,((1-z_) (1+z_))/((1-I z_) (1+I z_)),{0...}]:>
(hplArgTransform[a,{1,1,-1,1},{(z^2),1-δ}]/.hplHC[b_,(z^2),{0...}]:>hplArgTransform[b,2,z]),
hplHC[a_,((1-z_)^2(1+z_)^2)/((1-I z_)^2 (1+I z_)^2),{0...}]:>
(hplArgTransform[a,{1,1,-1,1},{(z^4),1-δ}]/.hplHC[b_,(z^4),{0...}]:>hplArgTransform[b,4,z])
};
dom2=hplLimit[#,δ->0]&/@dom2;
If[!FreeQ[dom2,hplLimit|δ],Throw[False]];
dom2=dom2/.a_hplZC:>hplSumToInt[a,VerifyConvergence->False]/.
hplHC[p:{___,a_},1,q:{0...}]:>hplHC[p/a,1/a,q];
dom2=dom2//.a_hplZC:>hplSumToInt[a,VerifyConvergence->False]/.
hplHC[p_,t_?NumericQ a_,q:{0...}]:>hplHC[p/t,a,q];
dom2=dom2//PowerExpand;
dom2=dom2/.a:(_hplZC|_hplHC)/;NumericQ[a[[2]]]:>hplMZVSimplify[a]//Expand;
dom2=Fold[hplLogToHPL,dom2,avar]//hplShuffleAll;
serCoeff=serCoeff/.Dispatch[Thread[dom1->dom2]]//Expand//hplShuffleAll;
symb2=MapThread[
Function[{u,v},
Switch[u,
hplTTrf,{-1+δ+I δ,Sequence@@v,1-δ+I δ},hplT2Trf,{I+δ-I δ,Sequence@@v,1-δ+I δ},
hplSTrf|hplS2Trf,{δ+I δ,Sequence@@v,1-δ+I δ},hplZTrf|hplNTrf,{δ+I δ,Sequence@@v,1-δ+I δ},
hplZ2Trf,{1-δ+I δ,Sequence@@v,δ+I δ}
]
],{trfs,{symb}}
];
serCoeff=Fold[Function[{x,y},If[x[[0]]===Plus,Apart[#,y]&/@x,Apart[x,y]]],serCoeff,avar];
serCoeff=If[serCoeff[[0]]===Plus,hplInt[#,Sequence@@symb2]&/@serCoeff,hplInt[serCoeff,Sequence@@symb2]];
serCoeff=serCoeff//.{
k_.hplInt[ii_,{a_,p__,b_},m___]:>
hplInt[Expand[k Fold[hplIntegrate[#1,{#2[[1]],a,#2[[2]]}]&,ii,Transpose[{{p},Append[Rest[{p}],b]}]]],m],
k_.hplInt[ii_]:>ii
};
serCoeff=hplLimit[serCoeff,δ->0]//Expand;
serCoeff=serCoeff/.hplHC[p_,-1,q:{0...}]:>hplHC[-p,1,q]/.a_hplHC/;!FreeQ[a,δ]:>(dummy@@a);
serCoeff=serCoeff/.a_hplZC:>hplSumToInt[a,VerifyConvergence->False]/.{
hplHC[p:{___,-a_},1,q:{0...}]:>hplHC[p/a,1/a,q],
hplHC[p:{___,a_},1,q:{0...}]:>hplHC[p/a,1/a,q]
};
res=hplIntToSum[serCoeff]/.hplZC[x__]:>With[{r= $hplZC[x]},Uncompress[r]/;r[[0]]===String]/.
dummy->hplHC//hplSumToInt//Expand;
res=Expand[PowerExpand[FunctionExpand[#]]]&/@If[res[[0]]===Plus,List@@res,{res}]//Total;
True
];
res/;f
];

[HPLの簡約化]

hplDepth[(hplZ|hplZC)[a_,___]]:=Length[Flatten[{a}]];

hplDepth[(hplH|hplHC)[a_,___]]:=Count[Flatten[{a}],Except[0]]

hplLevel[(hplZ|hplZC)[a_,___]]:=Total[Flatten[{a}]];

hplLevel[(hplH|hplHC)[a_,___]]:=Length[Flatten[{a}]];

Clear[hplOrder];
hplOrder[a:(_hplZ|_hplZC)]:={
hplDepth[a],
hplLevel[a],
{0,a}
};

hplOrder[a:(_hplH|_hplHC)]:={
hplDepth[a],
hplLevel[a],
Module[{tmp},
tmp=Cases[hplIntToSum[a,VerifyConvegence->False],_hplZC,{0,∞}]//Union;
If[tmp=!={},{0,tmp[[1]]},{1,a}]
]
};

hplAllSeedZ[level_Integer/;level>=1,t_List:{1,-1},r_:∞,d_Integer:1]/;d≥1:=
Module[{part,i,tmp},
part=Union@@Permutations/@IntegerPartitions[level d]/d;
tmp=Function[v,{v,#}&/@Tuples[t,Length[v]]]/@part;
hplZC[#1,r,#2,0#1]&@@@Union@@tmp//Union
];

hplIdZSum[in_hplZ|in_hplZC]:=
Module[{data,tmp},
data=in;
If[Union[data[[4]],{0}]=!={0},Return[{}]];
Table[
tmp={
hplZC[data[[1,1;;i]],data[[2]],data[[3,1;;i]],data[[4,1;;i]]],
hplZC[data[[1,i+1;;]],data[[2]],data[[3,i+1;;]],data[[4,i+1;;]]]
};
(Times@@tmp)-(hplShuffle@@tmp),
{i,Length[data[[1]]]-1}
]//Union
];

hplIdZSum[in:{(_hplZ|_hplZC)...}]:=Union@@hplIdZSum/@in;

hplConvergence2Q[in_hplZ|in_hplZC]:=
Module[{data,tmp},
data=hplZCanonical[hplZ@@in];
tmp=hplConvergenceQ[data]||
(data[[1,1]]==data[[3,1]]==1&&data[[4,1]]≥0&&
hplConvergenceQ[hplZC[Rest[data[[1]]],data[[2]],Rest[data[[3]]],Rest[data[[4]]]]]);
tmp/;data[[0]]===hplZC&&Simplify[And@@Thread[data[[1]]≥0]]
];

hplIdZInt[in_hplZ|in_hplZC]:=
Module[{tmp},
Block[{hplConvergenceQ=True&},
tmp=hplSumToInt[in];
tmp=Cases[tmp,_hplHC,{0,∞}]//Union;
tmp=hplIdHInt[tmp]//hplIntToSum;
tmp=Select[tmp,FreeQ[#,hplHC]&];
];
Select[tmp,!Or@@(TrueQ[!hplConvergence2Q[#]]&/@Union[Cases[#,_hplZC,{0,∞}]])&]
];
hplIdZInt[in:{(_hplZ|_hplZC)...}]:=Union@@hplIdZInt/@in;

hplAllSeedH//Clear;
hplAllSeedH[level_Integer/;level>=1,t_List:{-1,1,0},p_List:{0},c_:1]:=
Function[v,hplHC[v,c,#]&/@Tuples[p,level]]/@Tuples[t,level]//Flatten;

hplIdHInt[in_hplH|in_hplHC]:=
Module[{data,tmp,n},
data=hplH@@in//hplHCanonical;
If[data[[0]]===hplH,Return[{}]];
Table[
tmp={
hplHC[data[[1,1;;i]],data[[2]],data[[3,1;;i]]],
hplHC[data[[1,i+1;;]],data[[2]],data[[3,i+1;;]]]
};
(Times@@tmp)-(hplShuffle@@tmp),
{i,Length[data[[1]]]-1}
]//Union
];

hplIdHInt[in:{_hplH|_hplHC...}]:=Union@@hplIdHInt/@in;

hplIdHSum[in_hplH|in_hplHC]:=
Module[{tmp},
tmp=Block[{hplConvergenceQ=True&},tmp=hplIntToSum[in]];
tmp=Cases[tmp,_hplZC,{0,∞}]//Union;
If[Length[tmp]≠1,Return[{}],tmp=tmp[[1]]];
tmp=hplIdZSum[tmp];
tmp=Select[tmp,!Or@@(TrueQ[!hplConvergence2Q[#]]&/@Union[Cases[#,_hplZC,{0,∞}]])&];
tmp=Block[{hplConvergenceQ=True&},tmp//hplSumToInt];
Select[tmp,FreeQ[#,hplZC]&]
];
hplIdHSum[in:{(_hplH|_hplHC)...}]:=Union@@hplIdHSum/@in;

hplIdPLZ//Clear;
hplIdPLZ[n_Integer/;n>1]:=
Module[{tmp,dom},
{
hplZC[ConstantArray[1,n],∞,Join[{-1,-1},ConstantArray[1,n-2]],ConstantArray[0,n]]+FunctionExpand[PolyLog[n,1/2]],
hplZC[{n},∞,{1},{0}]-Zeta[n],
hplZC[{n},∞,{-1},{0}]+(1-(2^(1-n)))Zeta[n]
}
];

hplIdPLZ[1]:=
{
hplZC[{1},∞,{1},{0}]-hplSinf,
hplZC[{1},∞,{-1},{0}]+Log[2]
};

hplIdPLZI//Clear;
hplIdPLZI[n_Integer/;n>1]:=
Module[{tmp,dom},
{
hplZC[ConstantArray[1,n],∞,Join[{-1,-1},ConstantArray[1,n-2]],ConstantArray[0,n]]+FunctionExpand[PolyLog[n,1/2]],
hplMultiply[hplZC[ConstantArray[1,n],∞,Join[{-1,-1},ConstantArray[1,n-2]],ConstantArray[0,n]],2]+FunctionExpand[PolyLog[n,1/2]],
hplZC[ConstantArray[1,n],∞,Join[{-I,I},ConstantArray[1,n-2]],ConstantArray[0,n]]+FunctionExpand[PolyLog[n,1/2+I/2]],
hplZC[{n},∞,{1},{0}]-Zeta[n],
hplZC[{n},∞,{-1},{0}]+(1-(2^(1-n)))Zeta[n],
hplZC[{n},∞,{I},{0}]+hplZC[{n},∞,{-I},{0}]-(2^(1-n))hplZC[{n},∞,{-1},{0}],
hplZC[{n},∞,{I},{0}]-PolyLog[n,I]
}/.PolyLog[a_?OddQ,I]:>FunctionExpand[PolyLog[a,I]]
];

hplIdPLZI[1]:=
{
hplZC[{1},∞,{1},{0}]-hplSinf,
hplZC[{1},∞,{-1},{0}]+Log[2],
hplZC[{1},∞,{I},{0}]-I π/4+Log[2]/2,
hplZC[{1},∞,{-I},{0}]+I π/4+Log[2]/2
};

hplIdPLH[n_Integer/;n>1]:=hplSumToInt[hplIdPLZ[n]];

hplIdPLHI[n_Integer/;n>1]:=hplSumToInt[hplIdPLZI[n]];

hplIdPLH[1]={
hplHC[{1},1,{0}]-hplIinf,
hplHC[{-1},1,{0}]-Log[2],
hplHC[{0},1,{0}]
};

hplIdPLHI[1]={
hplHC[{1},1,{0}]-hplIinf,
hplHC[{-1},1,{0}]-Log[2],
hplHC[{-I},1,{0}]+I π/4-Log[2]/2,
hplHC[{I},1,{0}]-I π/4-Log[2]/2,
hplHC[{0},1,{0}]
};

Clear[hplIdHTrf];
hplIdHTrf[(hplHC|hplH)[a_,1,b:{0..}]]/;
Union[a,{-1,0,-I,I,1}]==={-1,0,-I,I,1}&&a[[-1]]≠0&&a[[1]]≠1&&Length[b]==Length[a]:=
Module[{tmp,F,p},
tmp=F@@Reverse[a]/.{-1->p[-1],0->-p[1]+p[-1],-I->-p[I]+p[-1],I->-p[-I]+p[-1],1->-p[0]+p[-1]}//Distribute;
tmp=tmp//.F[v___,-c_,u___]:>-F[v,c,u]/.F[x__]:>hplHC[{x}/.p->Identity,1,b];
{hplHC[a,1,b]-tmp}
];

hplIdHTrf[_hplHC|_hplH]:={}

hplIdHTrf[in:{_hplH|_hplHC...}]:=Union@@hplIdHTrf/@in;

Clear[hplIdZTrf];
hplIdZTrf[a:(_hplZC|_hplZ)]:=
Module[{tmp},
tmp=hplSumToInt[a];
tmp=Cases[tmp,_hplHC,{0,∞}]//Union;
tmp=hplIdHTrf/@tmp//Flatten//Union;
tmp=tmp//hplIntToSum//Expand;
Select[tmp,FreeQ[#,hplHC]&&#=!=0&]
];

hplIdZTrf[in:{_hplZ|_hplZC...}]:=Union@@hplIdZTrf/@in;

[MZV関連]

If[$hplZC[{1},∞,{1},{0}][[0]]=!=String,
<<"multipleZeta_mzv.dat";
Share[$hplZC];
];

hplMZVSimplify//ClearAll;
Attributes[hplMZVSimplify]={Listable};
hplMZVSimplify[in_]:=
Module[{tmp,uu,vv,lcm,lim,tmp2,tmp3,hpln},
uu=Cases[in,hplHC[v:{_?NumericQ..},b_:1,{0..}|PatternSequence[]]/;v[[1]]≠b,{0,∞}]//Union;
vv=hplHPLToLog[hplLogExtract[uu]]/.hplHC[p:{___,Except[0]},-1,q:{0...}]:>hplHC[-p,1,q];
vv=hplIntToSum[vv/.a_hplHC?hplZeroQ->0]/.hplZC[x__]:>With[{r= $hplZC[x]},Uncompress[r]/;r[[0]]===String]//Expand;
tmp=in/.Thread[uu->vv];
tmp=tmp/.p:hplZC[_,∞|(a_.hplN+b_./;{a,b}∈Integers),__]:>hplEliminateNonPositiveIntegers[p];
tmp=tmp/.a_hplZC/;IntegerQ[a[[2]]]:>hplZSum@@a//myExpand;
tmp=If[tmp[[0]]===Plus,List@@tmp,{tmp}];
tmp2=Select[tmp,FreeQ[#/._hplZC->1,hplN]&];
tmp3=Select[tmp,!FreeQ[#/._hplZC->1,hplN]&];
tmp2=(
tmp2=#/.hplZC[x__]:>With[{r= $hplZC[x]},Uncompress[r]/;r[[0]]===String];
tmp2=#/.hplZC[a_,p_.hplN+q_.,c_,d_]/;p>0:>
With[{r= $hplZC[a,∞,c,d]},(Uncompress[r]/.hplSinf->hplSinf+Log[p])/;r[[0]]===String];
If[FreeQ[tmp2,hplN],tmp2,#]
)&/@tmp2;
tmp=Total[tmp2]+Total[tmp3]//hplExpandProjector//hplZHNExtract//myExpand;
lcm=LCM[Sequence@@Cases[tmp,hplZC[_,a_.hplN+_.,__]/;IntegerQ[a]&&a>0:>a,{0,∞}],1];
tmp=tmp/.v:hplZC[_,a_.hplN+_.,__]/;IntegerQ[a]&&a>0:>hplMultiply[v,lcm/a];
tmp=Refine[tmp//hplShuffleAll,hplN∈Integers]//myExpand;
tmp=If[tmp[[0]]===Plus,List@@tmp,{tmp}];
tmp=Catch[
lim=Limit[Series[#/._hplZC|HarmonicNumber[_.hplN]->1,{hplN,∞,0}],hplN->∞,Assumptions->hplN∈Integers];
If[!FreeQ[lim,hplN]||Refine[Abs[lim]<∞]===False,Throw[#]];
tmp2=#/.hplZC[a_,p_.hplN+q_.,c_,d_]/;p>0:>
(hplZC[a,∞,c,d]/.hplZC[x__]:>With[{r= $hplZC[x]},Uncompress[r]/;r[[0]]===String]/.hplSinf->hplSinf+Log[p]);
If[!FreeQ[tmp2,a_hplZC/;a[[2]]===∞&&hplConvergenceQ[a]===False],Throw[#]];
tmp2=Refine[tmp2,hplN∈Integers]/.a_hplZC:>(a/.hplN->∞);
tmp3=Series[tmp2,{hplN,∞,0},Assumptions->hplN∈Integers]//Normal;
tmp3=tmp3/.Log[1/hplN]->-Log[hplN]/.Log[hplN]->hplSinf-EulerGamma/.hplN->∞;
If[FreeQ[tmp3,Interval]&&tmp3=!=Indeterminate&&FreeQ[tmp3/.a_hplZC:>(a/.∞->hpln),DirectedInfinity],tmp3,tmp2]
]&/@tmp//Total//Quiet;
tmp=tmp/.t:hplZC[{_Integer?Positive..},_?(FreeQ[#,hplN]&),{_?(FreeQ[#,hplN]&)..},a_List/;MemberQ[a,∞|-∞]]/;checkZ[t]->0;
tmp=tmp//myExpand;
tmp=If[tmp[[0]]===Plus,List@@tmp,{tmp}];
tmp2=Select[tmp,NumericQ]//Total//Simplify//Expand//PowerExpand//Expand;
tmp3=PowerExpand[FunctionExpand[#]]&/@Select[tmp,!NumericQ[#]&]//Total;
tmp2+tmp3//Normal//Expand
];

Protect["multipleZeta`*"];

Protect["multipleZeta`private`*"];

Unprotect["multipleZeta`$*"];

Unprotect["multipleZeta`private`$*"];

End[];

EndPackage[];