<<"Mtheory/GammaProduct.m";
<<"Mtheory/ActVarBase.m";

Setup for V2=(DF)3eDP2

vv2[0]=<<"Mtheory/(DF)3eDP2data/(DF)3eDP2base0";(* leg=0, 194 (194-145=49) *)
vv2[2]=<<"Mtheory/(DF)3eDP2data/(DF)3eDP2base2";(* leg=2, 1809 (1809-1287=522) *)
vv2[4]=<<"Mtheory/(DF)3eDP2data/(DF)3eDP2base4";(* leg=4, 3774 (3774-2724=1050)*)
vv2[6]=<<"Mtheory/(DF)3eDP2data/(DF)3eDP2base6";(* leg=6, 3813 (3813-2909=904) *)
vv2[8]=<<"Mtheory/(DF)3eDP2data/(DF)3eDP2base8";(* leg=8, 2345 (2345-1887=458) *)
vv2[10]=<<"Mtheory/(DF)3eDP2data/(DF)3eDP2base10";(* leg=10, 981 (981-850=131) *)
Print[Length[vv2[0][[1]]]," ",Length[vv2[2][[1]]]," ",Length[vv2[4][[1]]]," ",Length[vv2[6][[1]]]," ",Length[vv2[8][[1]]]," ",Length[vv2[10][[1]]]];
bv2[0]=<<"Mtheory/(DF)3eDP2data/(DF)3eDP2bid0";(* leg=0, 145 *)
bv2[2]=<<"Mtheory/(DF)3eDP2data/(DF)3eDP2bid2";(* leg=2, 1287 *)
bv2[4]=<<"Mtheory/(DF)3eDP2data/(DF)3eDP2bid4";(* leg=4, 2724 *)
bv2[6]=<<"Mtheory/(DF)3eDP2data/(DF)3eDP2bid6";(* leg=6, 2909 *)
bv2[8]=<<"Mtheory/(DF)3eDP2data/(DF)3eDP2bid8";(* leg=8, 1887 *)
bv2[10]=<<"Mtheory/(DF)3eDP2data/(DF)3eDP2bid10";(* leg=10, 850 *)
Print[Length[bv2[0]]," ",Length[bv2[2]]," ",Length[bv2[4]]," ",Length[bv2[6]]," ",Length[bv2[8]]," ",Length[bv2[10]]];
ev2[0]=<<"Mtheory/(DF)3eDP2data/(DF)3eDP2eomid0";(* leg=0, 116 *)
ev2[2]=<<"Mtheory/(DF)3eDP2data/(DF)3eDP2eomid2";(* leg=2, 1196 *)
ev2[4]=<<"Mtheory/(DF)3eDP2data/(DF)3eDP2eomid4";(* leg=4, 2800 *)
ev2[6]=<<"Mtheory/(DF)3eDP2data/(DF)3eDP2eomid6";(* leg=6, 3005 *)
ev2[8]=<<"Mtheory/(DF)3eDP2data/(DF)3eDP2eomid8";(* leg=8, 1965 *)
ev2[10]=<<"Mtheory/(DF)3eDP2data/(DF)3eDP2eomid10";(* leg=10, 855 *)
Print[Length[ev2[0]]," ",Length[ev2[2]]," ",Length[ev2[4]]," ",Length[ev2[6]]," ",Length[ev2[8]]," ",Length[ev2[10]]];

Setup for V4=(DF)4eP

Tns={T0,T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11};(* define (((DF))^4)ϵψγ *)
$Assumptions=Table[Tns[[2u+2]]∈Arrays[Table[11,{i,1,21+2u+1}],Reals,{Antisymmetric[{2,3,4,5}],Antisymmetric[{7,8,9,10}],Antisymmetric[{12,13,14,15}],Antisymmetric[{17,18,19,20}],Antisymmetric[Table[21+i,{i,1,2u+1}]],{Join[{6,7,8,9,10,1,2,3,4,5},Table[i,{i,11,22+2u}]],1},{Join[{1,2,3,4,5},{11,12,13,14,15,6,7,8,9,10},Table[i,{i,16,22+2u}]],1},{Join[{1,2,3,4,5,6,7,8,9,10},{16,17,18,19,20,11,12,13,14,15},Table[i,{i,21,22+2u}]],1}}],{u,0,5}];(* 2u+1 = rank of gamma matrix *)
rotl={{1,{}}};

vv4[1]=<<"Mtheory/(DF)4ePdata/(DF)4ePbase1";(* leg=1, 1839 (1839-1194=645) *)
vv4[3]=<<"Mtheory/(DF)4ePdata/(DF)4ePbase3";(* leg=3, 7067 (7067-4684=2383) *)
vv4[5]=<<"Mtheory/(DF)4ePdata/(DF)4ePbase5";(* leg=5, 11208 (11208-7594=3614)*)
vv4[7]=<<"Mtheory/(DF)4ePdata/(DF)4ePbase7";(* leg=7, 9890 (9890-7124=2766) *)
vv4[9]=<<"Mtheory/(DF)4ePdata/(DF)4ePbase9";(* leg=9, 5807 (5807-4471=1336) *)
vv4[11]=<<"Mtheory/(DF)4ePdata/(DF)4ePbase11";(* leg=11, 2318 (2318-1922=396) *)
Print[Length[vv4[1][[1]]]," ",Length[vv4[3][[1]]]," ",Length[vv4[5][[1]]]," ",Length[vv4[7][[1]]]," ",Length[vv4[9][[1]]]," ",Length[vv4[11][[1]]]];

Transform VBP of XDP2 to DFXP

VBPDP2toDFP /: VBPDP2toDFP[rank0_,basel0_,bia0_,var0_,V0_,Vvar0_]:=
Module[{leg,lenvr,indl,psi,gm,gmdf,bips,ind,gmdf2,varall,cf,indf,vartc,fn,legvr,vcl,vclall,rep,repall,mat},

leg=rank0[[-1]];
lenvr=Total[rank0]-leg+3;(* Length of variation XDFP, no gamma *)
indl=Table[TakeList[basel0[leg][[1,u]],rank0],{u,1,Length[basel0[leg][[1]]]}];
psi={a1,a2};(* index of (Subscript[ψ, 2]) *)
gm=Take[{g1,g2,g3,g4,g5,g6,g7,g8,g9,g10,g11},leg];(* index of γ *)
gmdf=Expand[-1/18 DF[{{a1},{a2,i1,i2,i3}}]GammaGamma[gm,{i1,i2,i3}]
                          +1/144 DF[{{a1},{i1,i2,i3,i4}}]GammaGamma[gm,{a2,i1,i2,i3,i4}]];(* γDF *)
gmdf=(1/2)gmdf-((1/2)gmdf /. {a1->a2,a2->a1});

bips=Flatten[Union[bia0[leg] /. V0[_]->0] /. {VBP[n_]->{n},Times[_,VBP[n_]]->{n}}];(* extract n of VBP[n] *)
bips=Replace[bips,0->Nothing,1];(* delete 0 *)

Do[If[MemberQ[bips,u],
       ind=indl[[u]];
       gmdf2=gmdf /. Table[gm[[i]]->ind[[-1,i]],{i,1,leg}];
       varall=1+Expand[(1/3)F[Join[Drop[ind,-3],{{ind[[-3,1]]}}]]*(gmdf2 /.{psi[[1]]->ind[[-2,1]],psi[[2]]->ind[[-2,2]]})]
                       +Expand[(1/3)F[Join[Drop[ind,-3],{{ind[[-2,1]]}}]]*(gmdf2 /. {psi[[1]]->ind[[-2,2]],psi[[2]]->ind[[-3,1]]})]
                       +Expand[(1/3)F[Join[Drop[ind,-3],{{ind[[-2,2]]}}]]*(gmdf2 /. {psi[[1]]->ind[[-3,1]],psi[[2]]->ind[[-2,1]]})];
(* ...+DF[{{a2},{i1,g1,g2,g3}}] F[{a1,a2,g1,g2,g3}] γ[{a1,i1}]+... *)
       Do[cf=varall[[v]] /. {DF[_]->1,F[_]->1,γ[_]->1};
            indf=Flatten[varall[[v]]/cf /. {DF[x1_]F[x2_]γ[x3_]->{x1,x2,x3},DF[x1_]F[x2_]->{x1,x2}}];
            fn=Union[indf];
            legvr=Length[indf]-lenvr;
            vcl[v]=cf ClassifyInd[var0[legvr],indf,fn,Tns[[1+legvr]],rotl] /. {Vvar0[x_]->Vvar0[legvr,x]};(* classify *)
     ,{v,2,Length[varall]}];
       vclall[u]={VBP[leg,u]->Expand[Sum[vcl[v],{v,2,Length[varall]}]]};,
       vclall[u]=Nothing; 
];
,{u,1,Length[indl]}];

Flatten[Table[vclall[u],{u,1,Length[indl]}]]
];

leg=10;
biv2tov4=VBPDP2toDFP[{1,4,1,4,1,4,1,2,leg},vv2,bv2,vv4,V2,V4];(* VBP of V2[0] to V4 *)

biv2tov4>>("Mtheory/Var(DF)4ePdata/V2VBP_"<>ToString[leg]<>"toV4");

Transform EOM of XDP2 to DFXP

EOMDP2toDFP /: EOMDP2toDFP[rank0_,psDF0_,basel0_,eom0_,var0_,V0_,Vvar0_,VEOM0_]:=
Module[{leg,indl,leg2,gm,gmdfp,eomps,ind,sec,id,var,varall,ps,sgn,gm0,inda1,inda2,gm1,cf,indf,fn,legvr,vcl,vclall},

leg=rank0[[-1]];
indl=Table[TakeList[basel0[leg][[1,u]],rank0],{u,1,Length[basel0[leg][[1]]]}];
Do[leg2=leg-i;
     If[leg2>=0&&EvenQ[i],
          gm[leg2]=Take[{g1,g2,g3,g4,g5,g6,g7,g8,g9,g10,g11},leg2];(* index of γ *)
          gmdfp[leg2]=Expand[-1/36 DF[{{a1},{a2,i1,i2,i3}}]Antisym[GammaGamma[gm[leg2],{i1,i2,i3}],{i1,i2,i3}]Psi[{{a2}}]
                                                +1/288 DF[{{a1},{i1,i2,i3,i4}}]Antisym[GammaGamma[gm[leg2],{a2,i1,i2,i3,i4}],{i1,i2,i3,i4}]Psi[{{a2}}]
                                                +1/36 DF[{{a2},{a1,i1,i2,i3}}]Antisym[GammaGamma[gm[leg2],{i1,i2,i3}],{i1,i2,i3}]Psi[{{a2}}]
                                                -1/288 DF[{{a2},{i1,i2,i3,i4}}]Antisym[GammaGamma[gm[leg2],{a1,i1,i2,i3,i4}],{i1,i2,i3,i4}]Psi[{{a2}}]];(* γ[leg2]DFψ[a1] *)
   ];
     If[leg2>=0&&OddQ[i],
          gm[leg2]=Take[{g1,g2,g3,g4,g5,g6,g7,g8,g9,g10,g11},leg2];(* index of γ *)
          gmdfp[leg2]=Expand[-1/144 DF[{{a1},{i1,i2,i3,i4}}]Antisym[GammaGamma[gm[leg2],{a2,i1,i2,i3,i4,i5}],{i1,i2,i3,i4}]Psi[{{i5}}]
                                                +1/18 DF[{{a1},{a2,i1,i2,i3}}]Antisym[GammaGamma[gm[leg2],{i1,i2,i3,i4}],{i1,i2,i3}]Psi[{{i4}}]
                                                +1/36 DF[{{a1},{i1,i2,i3,i4}}]Antisym[GammaGamma[gm[leg2],{a2,i1,i2,i3}],{i1,i2,i3}]Psi[{{i4}}]
                                                +1/144 DF[{{a1},{i1,i2,i3,i4}}]Antisym[GammaGamma[gm[leg2],{i1,i2,i3,i4}],{i1,i2,i3,i4}]Psi[{{a2}}]
                                                -1/6 DF[{{a1},{a2,i1,i2,i3}}]Antisym[GammaGamma[gm[leg2],{i1,i2}],{i1,i2}]Psi[{{i3}}]];
          gmdfp[leg2]=Expand[(1/2)gmdfp[leg2]-((1/2)gmdfp[leg2] /. {a1->a2,a2->a1})];(* γ[leg2]γDFψ[a1,a2] *)
   ];
,{i,0,5}];

eomps=Flatten[Union[eom0[leg] /. V0[_,_]->0] /. {VEOM0[leg,n_]->{n},Times[_,VEOM0[leg,n_]]->{n}}];(* extract n of VEOM0[leg,n] *)
eomps=Replace[eomps,0->Nothing,1];(* delete 0 *)

Do[If[MemberQ[eomps,u],
       ind=indl[[u]];
       sec={Intersection[ind[[-2]],ind[[-1]]],Intersection[ind[[-3]],ind[[-1]]],Intersection[ind[[-3]],ind[[-2]]]};(* {ψ2γ,Dγ,Dψ2} *)
       id=Map[Length,sec];
       Clear[var,varall];
       Switch[id,(* check EOM of Dψ *)
            {0,0,0},If[Apply[Or,Table[SubsetQ[ind[[psDF0[[i,2]]]],ind[[psDF0[[i,1]]]]],{i,1,Length[psDF0]}]],(* check EOM of DF *)
                                     varall=0;
                             ];,
            {1,0,0},varall=0;,
            {0,1,0},
                    ps=Position[ind[[-1]],sec[[2,1]]][[1,1]];
                    sgn=(((-1))^(leg-ps));(* sgn from gamma *)
                    gm0=ind[[-1]] /. {sec[[2,1]]->Nothing};
                    {inda1,inda2}=ind[[-2]];
                    varall=1-Expand[F[Drop[ind,-3]]*
                                  (gmdfp[Length[gm0]] /. Join[{a1->inda1,a2->inda2},Table[gm[Length[gm0]][[i]]->gm0[[i]],{i,1,Length[gm0]}]])];
(* ...+DF[{{f6},{f9,i1,f5,i3}}] F[{{f1},{f2,f3,f4,f5},{f6},{f1,f2,f7,f8},{f9},{f3,f4,f7,f8}}] Psi[{{i3}}] γ[{i1}]+... *),
            {0,0,1},
                    gm0=ind[[-1]];
                    inda1=Complement[ind[[-2]],ind[[-3]]][[1]];
                    ps=Position[ind[[-2]],inda1][[1,1]];
                    sgn=(((-1))^(ps+1));(* sgn from psi2 *)
                    varall=1-Expand[sgn F[Drop[ind,-3]]*
                                (gmdfp[Length[gm0]] /. Join[{a1->inda1},Table[gm[Length[gm0]][[i]]->gm0[[i]],{i,1,Length[gm0]}]])];,
            {2,0,0},varall=0;,
            {1,1,0},
                    ps={Position[ind[[-1]],sec[[2,1]]][[1,1]],Position[ind[[-1]],sec[[1,1]]][[1,1]]};
                    sgn=Signature[{ps[[1]],ps[[2]]}](((-1))^(ps[[1]]+ps[[2]]+1));(* sgn from gamma *)
                    gm0=ind[[-1]] /. {sec[[2,1]]->Nothing,sec[[1,1]]->Nothing};
                    inda1=Complement[ind[[-2]],sec[[1]]][[1]];
                    ps=Position[ind[[-2]],inda1][[1,1]];
                    sgn=sgn(((-1))^ps);(* sgn from psi2 *)
                    Do[cf=sgn(((-1))^(leg-i));
                         gm1=Delete[gm0,i];
                         inda2=gm0[[i]];
                         var[i]=Expand[cf F[Drop[ind,-3]]*
                                   (gmdfp[Length[gm1]] /. Join[{a1->inda1,a2->inda2},Table[gm[Length[gm1]][[i]]->gm1[[i]],{i,1,Length[gm1]}]])];
                  ,{i,1,leg-2}];
                    varall=1+Sum[var[i],{i,1,leg-2}]-Expand[F[Drop[ind,-3]]*
                                (gmdfp[Length[gm0]] /. Join[{a1->inda1},Table[gm[Length[gm0]][[i]]->gm0[[i]],{i,1,Length[gm0]}]])];,
            {1,0,1},
                    ps=Position[ind[[-1]],sec[[1,1]]][[1,1]];
                    sgn=(((-1))^(leg-ps));(* sgn from gamma *)
                    gm0=ind[[-1]] /. {sec[[1,1]]->Nothing};
                    ps=Position[ind[[-2]],sec[[1,1]]][[1,1]];
                    sgn=sgn(((-1))^(ps+1));(* sgn from psi2 *)
                    Do[cf=-sgn(((-1))^(leg-i));
                         gm1=Delete[gm0,i];
                         inda1=gm0[[i]];
                         var[i]=Expand[cf F[Drop[ind,-3]]*
                                   (gmdfp[Length[gm1]] /. Join[{a1->inda1},Table[gm[Length[gm1]][[i]]->gm1[[i]],{i,1,Length[gm1]}]])];
                  ,{i,1,leg-1}];
                    varall=1+Sum[var[i],{i,1,leg-1}];,
           {2,1,0},
                    ps={Position[ind[[-1]],ind[[-3,1]]][[1,1]],Position[ind[[-1]],ind[[-2,1]]][[1,1]],Position[ind[[-1]],ind[[-2,2]]][[1,1]]};
                    sgn=Signature[{ps[[1]],ps[[2]],ps[[3]]}](((-1))^(leg+ps[[1]]+ps[[2]]+ps[[3]]+1));(* sgn from gamma *)
                    gm0=ind[[-1]] /. {sec[[2,1]]->Nothing,sec[[1,1]]->Nothing,sec[[1,2]]->Nothing};
                    Do[cf=-2sgn(((-1))^(leg-i));
                         gm1=Delete[gm0,i];
                         inda1=gm0[[i]];
                         var[i]=Expand[cf F[Drop[ind,-3]]*
                                   (gmdfp[Length[gm1]] /. Join[{a1->inda1},Table[gm[Length[gm1]][[i]]->gm1[[i]],{i,1,Length[gm1]}]])];
                  ,{i,1,leg-3}];
                    Do[cf=-2sgn(((-1))^(i+j));
                         gm1=Delete[gm0,{{i},{j}}];
                         {inda1,inda2}={gm0[[i]],gm0[[j]]};
                         var[i,j]=Expand[cf F[Drop[ind,-3]]*
                                   (gmdfp[Length[gm1]] /. Join[{a1->inda1,a2->inda2},Table[gm[Length[gm1]][[i]]->gm1[[i]],{i,1,Length[gm1]}]])];
                  ,{i,1,leg-4},{j,i+1,leg-3}];
                    varall=1+Sum[var[i],{i,1,leg-3}]+Sum[var[i,j],{i,1,leg-4},{j,i+1,leg-3}];
      ];
       varall=Expand[varall];
       Do[cf=varall[[v]] /. {DF[_]->1,F[_]->1,Psi[_]->1,γ[_]->1};
            indf=Flatten[varall[[v]]/cf /. {DF[x1_]F[x2_]Psi[x3_]γ[x4_]->{x1,x2,x3,x4},DF[x1_]F[x2_]Psi[x3_]->{x1,x2,x3}}];
            fn=Union[indf];
            legvr=Length[indf]-21;
            vcl[v]=cf ClassifyInd[var0[legvr],indf,fn,Tns[[1+legvr]],rotl] /. {Vvar0[x_]->Vvar0[legvr,x]};(* classify *)
     ,{v,2,Length[varall]}];
       vclall[u]={VEOM0[leg,u]->Sum[vcl[v],{v,2,Length[varall]}]};
 ];
      If[FreeQ[eomps,u],vclall[u]={}];
,{u,1,Length[indl]}];

Flatten[Table[vclall[u],{u,1,Length[indl]}]]
];

leg=10;
eomv2tov4=EOMDP2toDFP[{1,4,1,4,1,4,1,2,leg},{{1,2},{3,4},{5,6}},vv2,ev2,vv4,V2,V4,V2EOM]

eomv2tov4>>("Mtheory/Var(DF)4ePdata/V2EOM_"<>ToString[leg]<>"toV4");