(* ::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 \"T before H\" conventions. Internal use symbols: ", {rr,pp}]


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


Simp[\[Gamma]_] := Expand[\[Gamma]];
CF[\[Gamma]_] := \[Gamma] /. (\[Lambda]_\[Beta]|\[Lambda]_a) :> MapAt[Simp, \[Lambda], 1];
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};
UU /: UU[x_]+UU[y_] := UU[x+y];
UU /: a_*UU[x_] := UU[Expand[a x]];
UU /: D[u_UU, vs__] := CF[u /. (\[Lambda]_\[Beta]|\[Lambda]_a) :> MapAt[D[#,vs]&, \[Lambda], 1]];
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},
ff:=
\!\(\*SubscriptBox[\(f\), \(++n\)]\)@@Table[Subscript[b, t], {t,T}];
CF /@ UU /@ Flatten@{
\[Beta][ff],
Table[a[ff, t,h], {t,T}, {h,H}]
} /. 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];


UU[\[Gamma]_] // tm[x_,y_,z_] :=CF[UU[
Expand[\[Gamma] /. a[f_,x|y,j_] :> a[f,z,j] /. Subscript[b, x|y]->Subscript[b, z]]]];


UU[\[Gamma]_] // hm[x_,y_,z_] := CF[UU[Expand[\[Gamma] /. a[f_,i_,x|y] :> a[f,i,z]]]];


UU[\[Gamma]_]//hts[y_,x_]:=CF[UU[Expand[\[Gamma]/.
a[f_,i_,j_] :> a[f,i,j] - Subscript[K\[Delta], i\[InvisibleComma]x] Subscript[K\[Delta], j\[InvisibleComma]y]\[Beta][f Subscript[b, x]]]]];


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


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


tb[x_][UU[L_],UU[R_]] := UU[0];


hb[y_][UU[L_],UU[R_]] := CF[UU[Expand[Distribute[pp[L,R]] /. {
pp[0,_] -> 0, pp[_,0] -> 0,
pp[_\[Beta],_] -> 0, pp[_, _\[Beta]] -> 0
} /. {
pp[a[f_,i_,y], u_] :> (u /. a[g_,j_,k_] :> Subscript[K\[Delta], y\[InvisibleComma]k](a[Subscript[b, j]f g,i,y]-a[Subscript[b, i]f g,j,k])),
_pp -> 0
}]]];


thb[x_,y_][UU[L_],UU[R_]] := CF[UU[Expand[Distribute[pp[L,R]] /. {
pp[0,_] -> 0, pp[_,0] -> 0, pp[_\[Beta], _] -> 0, pp[_,_\[Beta]] -> 0,
pp[a[f_,i_,j_], a[g_,k_,l_]] :> Subscript[K\[Delta], y\[InvisibleComma]l] Subscript[K\[Delta], x\[InvisibleComma]i](-a[Subscript[b, k]f g,i,j]+a[Subscript[b, i]f g,k,j])
}]]];
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. It is assumed that L has one h and R has one t.";


ct[s_] := ct[s,s]; ct[] = ct[0,0];
ct[h_,t_][UU[L_],UU[R_]] := UU[Distribute[pp[L,R]] /. {
pp[_\[Beta],_] -> 0,
pp[a[f_,i_,h], \[Beta][g_]] :> \[Beta][f Subscript[b, i]g/Subscript[b, t]],
pp[a[f_,i_,h], a[g_,t,j_]] :> a[f g, i,j],
pp[a[f_,i_,h], a[g_,j_,k_]] :> a[f Subscript[b, i]g/Subscript[b, t], j,k],
pp[a[__], _] -> 0}] // CF;


dect::usage="dect[h,t][uu] returns a pair {L,R} such that ct[h,t][L,R]=uu. Similarly for dect[s] and dect[]. uu is assumed to be atomic.";
dect[s_] := dect[s,s];
dect[] = dect[0,0];
dect[h_,t_][\[Beta][f_]] :=  {};
dect[h_,t_][\[Delta]\[Beta][f_]] :=  TBD;


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

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 \[Rule]6/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."]
]



