(* ::Package:: *)

(************************************************************************)
(* This file was generated automatically by the Mathematica front end.  *)
(* It contains Initialization cells from a Notebook file, which         *)
(* typically will have the same name as this file except ending in      *)
(* ".nb" instead of ".m".                                               *)
(*                                                                      *)
(* This file is intended to be loaded into the Mathematica kernel using *)
(* the package loading commands Get or Needs.  Doing so is equivalent   *)
(* to using the Evaluate Initialization Cells menu command in the front *)
(* end.                                                                 *)
(*                                                                      *)
(* DO NOT EDIT THIS FILE.  This entire file is regenerated              *)
(* automatically each time the parent Notebook file is saved in the     *)
(* Mathematica front end.  Any changes you make to this file will be    *)
(* overwritten.                                                         *)
(************************************************************************)



Print["In the U(T)\[TensorProduct]U(H) conventions and with renormalized arrows at 1co level. Internal use symbols: ", {rr,pp}]


SetDirectory["C:\\drorbn\\AcademicPensieve\\Projects\\OneCo-1604\\AoBranch"]


ToOld=ToNew=Identity;


Simp[\[CurlyEpsilon]_] := Simplify[\[CurlyEpsilon]];
CF[\[CurlyEpsilon]_]:=\[CurlyEpsilon]/.{\[Lambda]_\[Beta]|\[Lambda]_a|\[Lambda]_\[Delta]\[Beta]|\[Lambda]_c|\[Lambda]_ao|\[Lambda]_ca:>MapAt[Simp,\[Lambda],1],
aao[f_,i_,j_,k_,l_]:>aao[Simp@f,Sort[{i,k}][[1]],Sort[{j,l}][[1]],Sort[{i,k}][[2]],Sort[{j,l}][[2]]]};
AutoCollecting[\[Lambda]_] := (\[Lambda] /: \[Lambda][0,___] = 0;
\[Lambda] /: \[Lambda][f_, r___] + \[Lambda][g_, r___] := \[Lambda][Simp[f+g], r];
\[Lambda] /: g_*\[Lambda][f_, r___] := \[Lambda][Simp[g f], r]);
AutoCollecting /@ {\[Beta], a, \[Delta]\[Beta], c,ao,ca,aao};
UU /: UU[x_]+UU[y_] := UU[x+y];
UU /: a_*UU[x_] := UU[Expand[a x]];
UU /: D[u_UU, vs__] := CF[u/._\[Beta]|\[Lambda]_a|\[Lambda]_\[Delta]\[Beta]|\[Lambda]_c|\[Lambda]_ao|\[Lambda]_ca|\[Lambda]_aao :> MapAt[D[#,vs]&, \[Lambda], 1]];
hb[f_,i_,j_,k_]:=a[Subscript[b, j]f,i,k]-a[Subscript[b, i]f,j,k] (* = f\[CenterDot][Subscript[a, ik],Subscript[a, jk]] *);
UU /: Coefficient[u_UU, \[Lambda]_[js___]] := Total[Cases[u, \[Lambda][f_,js]:>f, \[Infinity]]];
K\[Delta] /: Subscript[K\[Delta], is__]:=KroneckerDelta[1,Length[Union[{is}]]];


UUBasis[T_List, H_List, f_] := Module[
{ff,n=0,h,t,h1,h2,i,j,k,l},
ff:=
\!\(\*SubscriptBox[\(f\), \(++n\)]\)@@Table[Subscript[b, t], {t,T}];
CF /@ UU /@ Flatten@{
\[Beta][ff],
Table[a[ff, t,h], {t,T}, {h,H}],
\[Delta]\[Beta][ff],
Table[c[ff,h],{h,H}],
Table[ao[ff,t,h], {t,T}, {h,H}],
Table[ca[ff, h1,t,h2], {t,T}, {h1,H},{h2,H}],
Table[aao[ff, T[[i]],H[[j]],T[[k]],H[[l]]], {k,Length@T},{i,k}, {l,Length@H},{j,l}]
} /. Subscript[1, _][___] -> 1
];
UUBasis[S_List,f_] := UUBasis[S,S,f];
UUBasis[n_Integer,m_Integer,f_] := UUBasis[Range@n,Range@m,f];
UUBasis[n_Integer, f_] := UUBasis[Range@n, f];
h\[Infinity]Basis[S_List,f_] := Module[{n=0},
Select[UUBasis[S,S\[Union]{h\[Infinity]},f], Count[#,h\[Infinity],{2}]==1&] /. Subscript[f, _][bs___]:> 
\!\(\*SubscriptBox[\(f\), \(++n\)]\)[bs]
];


UU/:UU[L_]**UU[R_]:=CF@UU[Expand[Distribute[pp[L,R]] /. {
pp[0,_] -> 0, pp[_,0] -> 0,
pp[\[Beta][f_], \[Lambda]_\[Beta]|\[Lambda]_a|\[Lambda]_\[Delta]\[Beta]|\[Lambda]_c|\[Lambda]_ao|\[Lambda]_ca|\[Lambda]_aao] :> MapAt[f #&, \[Lambda], 1],
pp[_\[Delta]\[Beta]|_c|_ao|_ca|_aao,_\[Delta]\[Beta]|_c|_ao|ca|_aao] -> 0,
pp[ao[f_,i_,j_],a[g_,k_,l_]] :> (* Not done! *)aao[f g,i,j,k,l]-Subscript[K\[Delta], j\[InvisibleComma]k]\[Delta]hb[f g,i,j,l],
pp[\[Delta]\[Beta][f_],\[Beta][g_]] :> \[Delta]\[Beta][f g],
pp[\[Delta]\[Beta][f_],a[g_,i_,j_]] :> ao[f g,i,j]
}]];


t\[Sigma][x_List,y_List][\[CurlyEpsilon]_] := (rr=Replace[Thread[x->y]];
CF[\[CurlyEpsilon] /. Subscript[b, i_]:>Subscript[b, rr@i] /. {
a[f_,i_,j_] :> a[f,rr@i,j],
ao[f_,i_,j_] :> ao[f,rr@i,j],
ca[f_,i_,j_,k_] :> ca[f,i,rr@j,k],
aao[f_,i_,j_,k_,l_]:>aao[f,rr@i,j,rr@k,l]
}]);
t\[Sigma][x_,y_][\[CurlyEpsilon]_] := t\[Sigma][{x},{y}][\[CurlyEpsilon]];
h\[Sigma][x_List,y_List][\[CurlyEpsilon]_] := (rr=Replace[Thread[x->y]];
CF[\[CurlyEpsilon] /. {
a[f_,i_,j_] :> a[f,i,rr@j],
c[f_,i_] :> c[f,rr@i],
ao[f_,i_,j_] :> ao[f,i,rr@j],
ca[f_,i_,j_,k_] :> ca[f,rr@i,j,rr@k],
aao[f_,i_,j_,k_,l_]:>aao[f,i,rr@j,k,rr@l]
}]);
h\[Sigma][x_,y_][\[CurlyEpsilon]_] := h\[Sigma][{x},{y}][\[CurlyEpsilon]];
d\[Sigma][x_,y_][\[CurlyEpsilon]_] := \[CurlyEpsilon] // t\[Sigma][x,y] // h\[Sigma][x,y];


UU[\[CurlyEpsilon]_] // tm[x_,y_,z_] := (rr=Replace[x|y->z];
CF[UU[Expand[\[CurlyEpsilon] /. {
a[f_,x,j_] :> a[f,z,j]+ao[\!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(y\)]]f\),z,j],
a[f_,y,j_] :> a[f,z,j],
ao[f_,x|y,j_] :> ao[f,z,j],
ca[f_,i_,x|y,k_]:>ca[f,i,z,k],
aao[f_,i_,j_,k_,l_] :> aao[f,rr@i, j, rr@k, l]
} /. Subscript[b, x|y]->Subscript[b, z]]]]);


UU[\[CurlyEpsilon]_] // hm[x_,y_,z_] := (rr=Replace[x|y->z];
CF[UU[Expand[\[CurlyEpsilon] /. {
a[f_,i_,x|y] :> a[f,i,z],
c[f_,x|y]:>c[f,z],
ao[f_,i_,x|y] :> ao[f,i,z],
ca[f_,y,j_, x] :> ca[f,z,j,z] + ao[f,j,z],
ca[f_,i_,j_,k_] :> ca[f,rr@i,j,rr@k],
aao[f_,i_,y,k_,x] :> aao[f,k,z,i,z],
aao[f_,i_,j_,k_,l_] :> aao[f,i,rr@j, k, rr@l]
}]]]);


UU[\[CurlyEpsilon]_] // hts[y_,x_] := CF[UU[Expand[\[CurlyEpsilon] /. {
a[f_,i_,j_] :> a[f,i,j] - Subscript[K\[Delta], j\[InvisibleComma]y]ao[\!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(x\)]]f\),i,y] + Subscript[K\[Delta], i\[InvisibleComma]x] Subscript[K\[Delta], j\[InvisibleComma]y](\[Beta][-Subscript[b, x]f]+\[Delta]\[Beta][Subscript[b, x] \!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(x\)]]f\)]+c[f,y]),
ao[f_,x,y] :> ao[f,x,y] -\[Delta]\[Beta][f Subscript[b, x]],
ca[f_,i_,j_,k_] :> ca[f,i,j,k]+Subscript[K\[Delta], i,y] Subscript[K\[Delta], j,x]ao[f,x,k] + Subscript[K\[Delta], j,x] Subscript[K\[Delta], k,y]c[-f Subscript[b, x],i],
aao[f_,i_,j_,k_,l_] :> aao[f,i,j,k,l]+Subscript[K\[Delta], i\[InvisibleComma]x] Subscript[K\[Delta], j\[InvisibleComma]y]ao[-Subscript[b, x]f,k,l] + Subscript[K\[Delta], i\[InvisibleComma]x] Subscript[K\[Delta], l\[InvisibleComma]y]ao[-Subscript[b, x]f,k,j] + Subscript[K\[Delta], k\[InvisibleComma]x] Subscript[K\[Delta], j\[InvisibleComma]y]ao[-Subscript[b, x]f,i,l] + Subscript[K\[Delta], k\[InvisibleComma]x] Subscript[K\[Delta], l\[InvisibleComma]y]ao[-Subscript[b, x]f,i,j]+2Subscript[K\[Delta], x\[InvisibleComma]i\[InvisibleComma]k] Subscript[K\[Delta], y\[InvisibleComma]j\[InvisibleComma]l]\[Delta]\[Beta][Subscript[b, x] Subscript[b, x]f]
}]]];


dm[x_,y_,z_][\[CurlyEpsilon]_] := \[CurlyEpsilon] // hts[x,y] // tm[x,y,z] // hm[x,y,z]


tb[x_][UU[L_],UU[R_]] := CF[UU[Expand[Distribute[pp[L,R]] /. {
pp[0,_] -> 0, pp[_,0] -> 0,
pp[_\[Beta]|_\[Delta]\[Beta]|_c|_ao|_ca|_aao, _\[Beta]|_\[Delta]\[Beta]|_c|_ao|_ca|_aao] -> 0,
pp[u_\[Beta]|u_\[Delta]\[Beta]|u_c|u_ao|u_ca|u_aao, v_a] :> -pp[v,u]
} /. {
pp[a[f_,x,j_], u_] :> (u /. {
\[Beta][g_] :> ao[f \!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(x\)]]g\),x,j],
a[g_,k_,l_] :> aao[f \!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(x\)]]g\),x,j,k,l] +ca[Subscript[b, k]f \!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(x\)]]g\),l,x,j]+Subscript[K\[Delta], x\[InvisibleComma]k](ca[-f g-Subscript[b, x]g \!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(x\)]]f\),j,x,l]+ca[f g,l,x,j]+aao[-g \!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(x\)]]f\),x,j,x,l]),
_ -> 0
}),
pp[a[f_,j_,k_], a[g_,x,l_]] (*/; j=!=x*) :> -aao[g \!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(x\)]]f\),x,l,j,k]-ca[Subscript[b, j]g \!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(x\)]]f\),k,x,l],
pp[_,_] -> 0
}]]];


hb[y_][UU[L_],UU[R_]] := CF[UU[Expand[Distribute[pp[L,R]] /. {
pp[0,_] -> 0, pp[_,0] -> 0,
pp[_\[Beta]|_\[Delta]\[Beta],_] -> 0,
pp[_, _\[Beta]|_\[Delta]\[Beta]] -> 0,
pp[_c|_ao|_ca|_aao, _c|_ao|_ca|_aao] -> 0,
pp[u_c|u_ao|u_ca|u_aao, v_a] :> -pp[v,u]
} /. {
pp[a[f_,i_,y], u_] :> (u /. {
a[g_,j_,k_] :> Subscript[K\[Delta], y\[InvisibleComma]k]hb[f g, i,j,k],
c[g_, j_] :>Subscript[K\[Delta], y\[InvisibleComma]j]ao[f g,i,y],
ao[g_,j_,k_] :> Subscript[K\[Delta], y\[InvisibleComma]k]ao[-f g Subscript[b, i],j,y],
ca[g_,j_,k_,l_]:>Subscript[K\[Delta], y\[InvisibleComma]j](ca[f g Subscript[b, k],l,i,y]+aao[f g,i,l,k,y])+Subscript[K\[Delta], y\[InvisibleComma]l](ca[-f g Subscript[b, i],j,k,y]+ca[f g Subscript[b, k],j,i,y])+Subscript[K\[Delta], y\[InvisibleComma]j\[InvisibleComma]l]ao[f g Subscript[b, k],i,y],
aao[g_,j_,k_,l_,m_] :> Subscript[K\[Delta], y\[InvisibleComma]k](ca[-f g Subscript[b, j] Subscript[b, l],m,i,y]+aao[-f g Subscript[b, i],j,m,l,y]) + Subscript[K\[Delta], y\[InvisibleComma]m](ca[-f g Subscript[b, j] Subscript[b, l],k,i,y]+aao[-f g Subscript[b, i],j,k,l,y])+Subscript[K\[Delta], y\[InvisibleComma]k\[InvisibleComma]m]ao[-f g Subscript[b, j] Subscript[b, l],i,y]
}),
_pp -> 0
}]]];


thb[x_,y_][UU[L_],UU[R_]] := CF[UU[Expand[Distribute[pp[L,R]] /. {
pp[0,_] -> 0, pp[_,0] -> 0,
pp[_\[Beta]|_\[Delta]\[Beta]|_c|_ao|_ca|_aao, _\[Beta]|_\[Delta]\[Beta]|_c|_ao|_ca|_aao] -> 0,
pp[_a,_\[Beta]|_\[Delta]\[Beta]] -> 0,
pp[\[Beta][f_], a[g_,i_,j_]] :> Subscript[K\[Delta], y\[InvisibleComma]j]ao[g \!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(x\)]]f\),i,y],
pp[a[f_,i_,j_], a[g_,k_,l_]] :> Subscript[K\[Delta], y\[InvisibleComma]l](
aao[g \!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(x\)]]f\),k,l,i,j]+ca[g Subscript[b, i]\!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(x\)]]f\),j,k,y]+Subscript[K\[Delta], x\[InvisibleComma]i](a[-g f Subscript[b, k],x,j]+a[g f Subscript[b, x],k,j]+aao[-g \!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(x\)]]f\),x,j,k,y]+aao[g \!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(x\)]]f\),k,j,x,y]+ao[-g Subscript[b, x] \!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(x\)]]f\),k,j]+ca[-g f,y,k,j]+ca[g f,j,k,y])),
pp[a[f_,i_,j_], c[g_,k_]] :>Subscript[K\[Delta], i\[InvisibleComma]x] Subscript[K\[Delta], k\[InvisibleComma]y]ao[-f g,x,j],
pp[a[f_,i_,j_], ao[g_,k_,l_]] :> Subscript[K\[Delta], x\[InvisibleComma]i] Subscript[K\[Delta], y\[InvisibleComma]l]ao[f g Subscript[b, x],k,j],
pp[a[f_,i_,j_], ca[g_,k_,l_,m_]] :> Subscript[K\[Delta], x\[InvisibleComma]i](
Subscript[K\[Delta], y\[InvisibleComma]k](aao[-f g,l,j,x,m]+ca[-f g Subscript[b, l],m,x,j])+Subscript[K\[Delta], y\[InvisibleComma]m](ca[-f g Subscript[b, l],k,x,j]+ca[f g Subscript[b, x],k,l,j])-Subscript[K\[Delta], y,k,m]ao[f g Subscript[b, l],x,j]),
pp[a[f_,i_,j_], aao[g_,k_,l_,m_,n_]] :> Subscript[K\[Delta], x\[InvisibleComma]i](
Subscript[K\[Delta], y\[InvisibleComma]l](aao[f g Subscript[b, x],k,j,m,n]+ca[f g Subscript[b, k] Subscript[b, m],n,x,j]) + Subscript[K\[Delta], y\[InvisibleComma]n](aao[f g Subscript[b, x],k,j,m,l]+ca[f g Subscript[b, k] Subscript[b, m],l,x,j])+Subscript[K\[Delta], y,l,n]ao[f g Subscript[b, k] Subscript[b, m],x,j]),
pp[_\[Delta]\[Beta]|_c,_a] -> 0,
pp[ao[f_,i_,j_], a[g_,k_,l_]] :> Subscript[K\[Delta], x\[InvisibleComma]i] Subscript[K\[Delta], y\[InvisibleComma]l](-ao[Subscript[b, k]f g,i,j]+ao[Subscript[b, i]f g,k,j]),
pp[ca[f_,m_,i_,j_], a[g_,k_,l_]] :> Subscript[K\[Delta], x\[InvisibleComma]i] Subscript[K\[Delta], y\[InvisibleComma]l](ca[-f g Subscript[b, k],m,x,j]+ca[f g Subscript[b, x],m,k,j]),
pp[aao[f_,x,j_,x,n_], a[g_,k_,y]] :>aao[-2 f g Subscript[b, k],x,j,x,n]+aao[2 f g Subscript[b, x],k,j,x,n],
pp[aao[f_,i_,j_,m_,n_], a[g_,k_,l_]] :> Subscript[K\[Delta], x\[InvisibleComma]i] Subscript[K\[Delta], y\[InvisibleComma]l](aao[-f g Subscript[b, k],m,j,x,n]+aao[f g Subscript[b, x],k,j,m,n]) + Subscript[K\[Delta], x\[InvisibleComma]m] Subscript[K\[Delta], y\[InvisibleComma]l](aao[-f g Subscript[b, k],i,j,x,n]+aao[f g Subscript[b, x],i,j,k,n])}]]];
htb[x_,y_][L_UU,R_UU] := -thb[y,x][R,L];


db[x_][u_UU,v_UU] := Module[{t,h}, Plus[
htb[x,x][u//t\[Sigma][x,t],v//h\[Sigma][x,h]]//tm[t,x,x]//hm[x,h,x],
tb[x][u,v//h\[Sigma][x,h]] // hm[x,h,x],
hb[x][u,v//t\[Sigma][x,t]] // tm[t,x,x],
thb[x,x][u//h\[Sigma][x,h],v//t\[Sigma][x,t]]//tm[t,x,x]//hm[x,h,x] ]];


bb[S_List] := Module[{w,bar,t,n=0,i,k},
w=#2//d\[Sigma][S,bar/@S];
Sum[t=db[S[[k]]][#1,w // d\[Sigma][bar[S[[k]]], S[[k]]]];
Do[t=t//dm[bar[S[[i]]], S[[i]], S[[i]]], {i,1,k-1}];
Do[t=t//dm[S[[i]], bar[S[[i]]], S[[i]]], {i,k+1,Length@S}];
t, {k,Length@S}] ]&
bb[S___] := bb[{S}]


ct::usage="ct[h,t][L,R] contracts the head h in L with the tail t in R. ct[s][L,R] takes h=t=s, and ct[][L,R] takes s=0. When ambiguous, L is placeed below R.";


ct[s_] := ct[s,s]; ct[] = ct[0,0];
ct[h_,t_][UU[L_],UU[R_]] := CF[UU[Distribute[pp[L,R]] /. {
pp[_\[Beta]|_\[Delta]\[Beta],_] -> 0,
pp[a[f_,i_,h], \[Beta][g_]] :> \[Beta][f Subscript[b, i]((\!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(t\)]]g\)) /. Subscript[b, t]->0)],
pp[a[f_,i_,h], a[g_,t,j_]] :> a[f(g/.Subscript[b, t]->0), i,j],
pp[a[f_,i_,h], a[g_,j_,k_]] :> a[f Subscript[b, i]((\!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(t\)]]g\)) /. Subscript[b, t]->0), j,k],
pp[a[f_,i_,h], c[g_,j_]] :> c[f Subscript[b, i]((\!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(t\)]]g\)) /. Subscript[b, t]->0), j],
pp[a[f_,i_,h], ao[g_,t,j_]] :> ao[f(g/.Subscript[b, t]->0), i,j],
pp[a[f_,i_,h], ao[g_,j_,k_]] :> ao[f Subscript[b, i]((\!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(t\)]]g\)) /. Subscript[b, t]->0), j,k],
pp[a[f_,i_,h], ca[g_,k_,t,j_]] :> ca[f(g/.Subscript[b, t]->0), k,i,j],
pp[a[f_,i_,h], ca[g_,l_,j_,k_]] :> ca[f Subscript[b, i]((\!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(t\)]]g\)) /. Subscript[b, t]->0), l,j,k],
pp[a[f_,i_,h], aao[g_,t,j_,t,k_]]  -> 0,
pp[a[f_,i_,h], aao[g_,t,j_,k_,l_]] :> aao[f(g/.Subscript[b, t]->0), i,j,k,l],
pp[a[f_,i_,h], aao[g_,j_,k_,t,l_]] :> aao[f(g/.Subscript[b, t]->0), j,k,i,l],
pp[a[f_,i_,h], aao[g_,j_,k_,l_,m_]] :> aao[f Subscript[b, i]((\!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(t\)]]g\)) /. Subscript[b, t]->0), j,k,l,m],
pp[a[__], _] -> 0,
pp[c[f_,h], \[Beta][g_]] :> \[Delta]\[Beta][f ((\!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(t\)]]g\)) /. Subscript[b, t]->0)],
pp[_c,_\[Beta]]->0,
pp[c[f_,h], a[g_,t,j_]] :> c[f(g/.Subscript[b, t]->0), j],
pp[c[f_,h], a[g_,j_,k_]] :> ao[f((\!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(t\)]]g\)) /. Subscript[b, t]->0), j,k]+c[f Subscript[b, j] ((\!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(t\)]]g\)) /. Subscript[b, t]->0),k],
pp[_c,_a]->0,
pp[_c|_ao|_ca|_aao, _c|_ao|_ca|_aao] -> 0,
pp[ao[f_,i_,h], \[Beta][g_]] :> 0,
pp[ao[f_,i_,h], a[g_,t,j_]] :> ao[f(g/.Subscript[b, t]->0),i,j],
pp[ao[f_,i_,h], a[g_,j_,k_]] :> 0,
pp[_ao,_] -> 0,
pp[ca[_,h,_,h], _] -> 0,
pp[ca[f_,h,i_,j_], \[Beta][g_]] :> ao[f((\!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(t\)]]g\)) /. Subscript[b, t]->0), i,j]+c[f Subscript[b, i] ((\!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(t\)]]g\)) /. Subscript[b, t]->0),j],
pp[ca[f_,i_,j_,h], \[Beta][g_]] :> c[f Subscript[b, j]((\!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(t\)]]g\)) /. Subscript[b, t]->0), i],
pp[ca[f_,h,i_,j_], a[g_,t,k_]] :> ca[f (g/.Subscript[b, t]->0), k,i,j],
pp[ca[f_,h,i_,j_], a[g_,k_,l_]] :> aao[f ((\!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(t\)]]g\)) /. Subscript[b, t]->0),i,j,k,l]+ca[f Subscript[b, i]((\!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(t\)]]g\)) /. Subscript[b, t]->0),j,k,l]+ca[f Subscript[b, k]((\!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(t\)]]g\)) /. Subscript[b, t]->0),l,i,j],
pp[ca[f_,i_,j_,h], a[g_,t,k_]] :> ca[f (g/.Subscript[b, t]->0),i,j,k],
pp[ca[f_,i_,j_,h], a[g_,k_,l_]] :> ca[f Subscript[b, j]((\!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(t\)]]g\)) /. Subscript[b, t]->0), i,k,l],
pp[_ca,_] -> 0,
pp[aao[_,_,h,_,h], _] -> 0,
pp[aao[f_,i_,h,j_,k_], \[Beta][g_]] :> c[-f Subscript[b, i] Subscript[b, j] ((\!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(t\)]]g\)) /. Subscript[b, t]->0),k],
pp[aao[f_,i_,h,j_,k_], a[g_,t,l_]] :> aao[f (g/.Subscript[b, t]->0),i,l,j,k],
pp[aao[f_,i_,h,j_,k_], a[g_,l_,m_]] :> ca[-f Subscript[b, i] Subscript[b, j] ((\!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(t\)]]g\)) /. Subscript[b, t]->0),k,l,m],
pp[aao[f_,i_,j_,k_,h], \[Beta][g_]] :> c[-f Subscript[b, i] Subscript[b, k] ((\!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(t\)]]g\)) /. Subscript[b, t]->0),j],
pp[aao[f_,i_,j_,k_,h], a[g_,t,l_]] :> aao[f (g/.Subscript[b, t]->0),i,j,k,l],
pp[aao[f_,i_,j_,k_,h], a[g_,l_,m_]] :>  ca[-f Subscript[b, i] Subscript[b, k] ((\!\(
\*SubscriptBox[\(\[PartialD]\), 
SubscriptBox[\(b\), \(t\)]]g\)) /. Subscript[b, t]->0),j,l,m],
pp[_aao, _] -> 0 }]];


SetDirectory["C:\\drorbn\\AcademicPensieve\\Projects\\OneCo-1605"];

ConditionalExport[fname_String, rest___] := Module[{temp, exists},
temp="ConditionalExportTemporary"<>"."<>FileExtension[fname];
exists=FileExistsQ[fname];
Export[temp, rest];
If[exists && FileByteCount[fname]===FileByteCount[temp],
DeleteFile[temp],
(* else *) Print["Exporting "<>fname<>"..."];
If[exists, DeleteFile[fname]];
RenameFile[temp, fname]
];
fname
]

Button["Export",
SetOptions[$FrontEndSession,PrintingStyleEnvironment->"Working"];
TagProperties[_] := {};
TagProperties["ct-def"] = {PageWidth ->8/0.65};
Options[CellExport] = {
PageWidth -> 4/0.65, CellFilter -> Identity, ExportDirectory -> "Snips", ExportBaseFilename -> Automatic, ExportFormat -> ".pdf", ExportOptions -> {}, Split -> False
};
CellExport[tag_String, opts___Rule] := CellExport[
NotebookGet[EvaluationNotebook[]],
tag, opts
];
CellExport[nb_Notebook, tag_String] := CellExport[nb, tag, TagProperties[tag]];
CellExport[nb_Notebook, tag_String, OptionsPattern[]] := Module[
{cells, cell, filename, format},
filename = FileNameJoin[{
OptionValue[ExportDirectory] /. Automatic -> Directory[],
OptionValue[ExportBaseFilename] /. Automatic -> tag
}];
format=OptionValue[ExportFormat];
cells=OptionValue[CellFilter][Cases[
nb, c_Cell /; FreeQ[List@@c, Cell] && !FreeQ[c, CellTags->tag],
Infinity
]];
If[!OptionValue[Split],
If[Length[cells]>=1,
If[Length[cells]==1,
cells=Append[First[cells], PageWidth ->1.2 72OptionValue[PageWidth]],
cells=Cell[CellGroup[cells], PageWidth -> 72OptionValue[PageWidth]]
];
ConditionalExport[
filename<>format,cells,
ImageResolution -> 300,
OptionValue[ExportOptions]
]
],
k=0;
Table[
++k;
ConditionalExport[
filename<>"-"<>ToString[k]<>format,
Append[cell, PageWidth -> 72OptionValue[PageWidth]],
ImageResolution -> 300,
OptionValue[ExportOptions]
],
{cell, cells}
]
]
];
nb=NotebookGet[EvaluationNotebook[]];
tags=Cases[nb, (CellTags->tag_) :> tag, Infinity ] // Union;
CellExport /@ tags;
Print["Done."]
]



