(* ::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.                                                         *)
(************************************************************************)



$SeriesShowDegree=3; $SeriesCompareDegree = 3;


LyndonQ[AW[w_String]] := And @@ (
OrderedQ[{w, #}]& /@ Table[StringDrop[w,i], {i,1,StringLength[w]-1}]
);
AllWords[0, _List] = {AW[""]};
AllWords[n_ /; n>0, ab_List] := AllWords[n, ab] = AW /@ Flatten[Outer[
StringJoin[#1, #2]&,
First /@ AllWords[n-1, ab],
ab
]];
AllLyndonWords[n_Integer, ab_List] := LW @@@ Select[AllWords[n, ab], LyndonQ];
AllLyndonWords[{n_}, ab_List] := Join@@Table[AllLyndonWords[k,ab], {k,n}];
LyndonFactorization[LW[w_String] /; StringLength[w] == 1] := LW[w];
LyndonFactorization[LW[w_String] /; StringLength[w] > 1] := Module[
{rf},
rf=First[Sort[Table[StringDrop[w,i], {i,1,StringLength[w]-1}]]];
LW /@ {StringDrop[w, -StringLength[rf]], rf}
];
LW[s_Symbol] := LW[ToString[s]];
LW[LW[w_]]:=LW[w];
LW /: LW[x_] <= LW[y_] := OrderedQ[{x,y}];
LW /: x_LW >=y_LW := y<=x;
LW /: x_LW > y_LW := !(x<=y);
LW /: x_LW<y_LW := !(y<=x);
Format[LW[w_], StandardForm] := Defer[\[LeftAngleBracket]w\[RightAngleBracket]];
\[LeftAngleBracket]w__\[RightAngleBracket]:=LW[w];
LW[is__Integer] := LW[StringJoin@@(StringTake["1234567890abcdefghijklmnopqrstuvwxyz", {#}]& /@ {is})];
Deg[LW[x_]] := StringLength[x];


b[0,_] =0; b[_,0]=0;
b[c_*(x_AW|x_LW), y_] := Expand[c b[x,y]];
b[x_, c_*(y_AW|y_LW)] := Expand[c b[x,y]];
b[x_Plus, y_] := b[#,y]& /@ x;
b[x_, y_Plus] := b[x,#]& /@ y;
b[w_LW, z_LW] := LWAdjoint[w][z];
ad[x_][y_]:=b[x,y];


LWAdjoint[w_] := LWAdjoint[w] = Module[{u},
u=Unique[LWAct];
u[z_] := u[z] = Which[
w===z, 0,
z<w, Expand[-b[z,w]],
Deg[w]==1, LW[First[w]<>First[z]],
True, Module[{x,y},
{x,y}=LyndonFactorization[w];
If[y>=z,
LW[First[w]<>First[z]],
b[x,LWAdjoint[y][z]]+b[LWAdjoint[x][z],y]
]
]
];
u
];


LieSeries[ser_Symbol][{dd_Integer}] := LS@@Table[ser[d], {d, dd}];
LieSeries[ser_Symbol][e___] := ser[e];
Format[s_LieSeries, StandardForm] := s[{$SeriesShowDegree}];
ShowLieSeries[d_Integer][s_LieSeries] := s[{d}];
MakeLieSeries[s_LieSeries] := s;
MakeLieSeries[expr_] := MakeLieSeries[expr] = MakeLieSeries[Unique[MakeLieSeries], expr];
MakeLieSeries[ser_Symbol, expr_] := (
ser[] = Hold[MakeLieSeries[ser, expr]];
ser[d_Integer] := ser[d] = Expand[expr /. w_LW /; Deg[w] != d -> 0];
LieSeries[ser]
);
s1_LieSeries \[Congruent] s2_LieSeries := And @@ ((s1[#]==s2[#])& /@ Range[$SeriesCompareDegree]);


AddLieSeries[ss___LieSeries] := AddLieSeries[ss] = Module[{ser},
ser=Unique[AddLieSeries];
ser[] = Hold[AddLieSeries[ss]];
ser[d_Integer] := ser[d] = Plus @@ ((#[d])& /@ {ss});
LieSeries[ser]
];
ScaleLieSeries[c_, s_LieSeries] := ScaleLieSeries[c, s] = Module[{ser},
ser=Unique[ScaleLieSeries];
ser[] = Hold[ScaleLieSeries[ss]];
ser[d_Integer] := ser[d] = Expand[c*s[d]];
LieSeries[ser]
];
b[s_LieSeries, y_] := b[s, MakeLieSeries[y]];
b[x_, s_LieSeries] := b[MakeLieSeries[x], s];


b[s1_LieSeries, s2_LieSeries] := b[s1,s2] = Module[{ser},
ser=Unique[b];
ser[] = Hold[b[s1,s2]];
ser[d_Integer] := ser[d] = Sum[
b[s1[k], s2[d-k]],
{k,1,d-1}
];
LieSeries[ser]
];
b[s_LieSeries, y_] := b[s, MakeLieSeries[y]];
b[x_, s_LieSeries] := b[MakeLieSeries[x], s];


LieSeries /: EulerE[s_LieSeries] := Module[{ser},
ser=Unique[EulerE];
ser[] = Hold[EulerE[s]];
ser[d_Integer] := ser[d] = Expand[d*s[d]];
LieSeries[ser]
];


adPower[0, x_LieSeries][\[Psi]_LieSeries] := adPower[0,x][\[Psi]] = Module[{ser},
ser=Unique[adPower];
ser[] = Hold[adPower[0,x][\[Psi]]];
ser[d_Integer] := ser[d] = \[Psi][d];
LieSeries[ser]
];
adPower[n_Integer, x_LieSeries][\[Psi]_LieSeries] := adPower[n,x][\[Psi]] = Module[{ser},
ser=Unique[adPower];
ser[] = Hold[adPower[n,x][\[Psi]]];
ser[d_Integer] := ser[d] =  b[x,adPower[n-1, x][\[Psi]]][d];
LieSeries[ser]
];
adSeries[f_, x_LieSeries][\[Psi]_LieSeries] := adSeries[f, x][\[Psi]] = Module[{ser},
ser=Unique[adSeries];
ser[] = Hold[adSeries[f,x][\[Psi]]];
ser[d_Integer] := ser[d] = Module[{c},
Expand[Sum[
c=SeriesCoefficient[f,{ad, 0, k}];
If[c==0, 0, c*adPower[k, x][\[Psi]][d]],
{k,0,d-1}
]]
];
LieSeries[ser]
];
adSeries[f_, x_][\[Psi]_] := adSeries[f, MakeLieSeries[x]][MakeLieSeries[\[Psi]]];
Ad[x_] := adSeries[E^(-ad), x];


LieDerivation[der_][es___] := der[es];
LieDerivation[rules_List] := LieDerivation[rules] = LieDerivation[Unique[LieDerivation], rules];
LieDerivation[der_Symbol, rules_List] := (
der[]=Hold[LieDerivation[der, rules]];
(der[w_LW] /; Deg[w] == 1) := (der[w] = MakeLieSeries[w /. Append[rules, _LW -> 0]]);
der[w_LW] := der[w] = Module[{x,y},
{x,y}=LyndonFactorization[w];
AddLieSeries[b[der[x],y], b[x,der[y]]]
];
der[s_LieSeries] := der[s] = Module[{ser},
ser=Unique[LieDerivationOnLieSeries];
ser[] = Hold[der[s]];
ser[d_] := ser[d] = Sum[
der[s[k]][d],
{k,1, d}
];
LieSeries[ser]
];
der[as_ASeries] := der[as] = Module[{ser},
ser=Unique[LieDerivationOnASeries];
ser[] = Hold[der[as]];
ser[d_] := ser[d] = Sum[
Expand[as[k] /. AW[w_] :> Sum[
NonCommutativeMultiply[
AW[StringTake[w, j-1]],
\[Iota][der[LW[StringTake[w,{j}]]][d-k+1]],
AW[StringDrop[w,j]]
],
{j,k}
]],
{k,1, d}
];
ASeries[ser]
];
der[cws_CWSeries] := der[cws] = Module[{ser},
ser=Unique[LieDerivationOnCWSeries];
ser[] = Hold[der[cws]];
ser[d_] := ser[d] = Sum[
Expand[cws[k] /. CW[w_] :> Sum[
tr[NonCommutativeMultiply[
AW[StringTake[w, j-1]],
\[Iota][der[LW[StringTake[w,{j}]]][d-k+1]],
AW[StringDrop[w,j]]
]],
{j,k}
]],
{k,1, d}
];
CWSeries[ser]
];
der[expr_][d_] := Expand[expr /. {w_LW :> der[w][d], s_LieSeries :> der[s][d]}];
LieDerivation[der]
);


DerivationPower[0, der_LieDerivation][\[Psi]_LieSeries] := DerivationPower[0,der][\[Psi]] = Module[{ser},
ser=Unique[DerivationPower];
ser[] = Hold[DerivationPower[0,der][\[Psi]]];
ser[d_Integer] := ser[d] = \[Psi][d];
LieSeries[ser]
];
DerivationPower[n_Integer, der_LieDerivation][\[Psi]_LieSeries] := DerivationPower[n,x][\[Psi]] = Module[{ser},
ser=Unique[DerivationPower];
ser[] = Hold[DerivationPower[n,der][\[Psi]]];
ser[d_Integer] := ser[d] =  der[DerivationPower[n-1,der][\[Psi]]][d];
LieSeries[ser]
];
DerivationSeries[__][0] = 0;
DerivationSeries[f_, ld_LieDerivation][\[Psi]_LieSeries] := DerivationSeries[f, ld][\[Psi]] = Module[{ser},
ser=Unique[DerivationSeries];
ser[] = Hold[DerivationSeries[f,ld][\[Psi]]];
ser[d_Integer] := ser[d] = Module[{c},
Expand[Sum[
c=SeriesCoefficient[f,{der, 0, k}];
If[c==0, 0, c*DerivationPower[k, ld][\[Psi]][d]],
{k,0,d}
]]
];
LieSeries[ser]
];
DerivationExp[ld_LieDerivation] := DerivationSeries[E^der, ld];


LieMorphism[mor_][es___] := mor[es];
LieMorphism[rules_List] := LieMorphism[rules] = LieMorphism[Unique[LieMorphism], rules];
LieMorphism[mor_Symbol, rules_List] := (
mor[]=Hold[LieMorphism[mor, rules]];
(mor[w_LW] /; Deg[w] == 1) := (mor[w] = MakeLieSeries[w /. rules]);
mor[w_LW] := (mor[w] = b @@ (mor /@ LyndonFactorization[w]));
mor[AW[""]] = MakeASeries[AW[""]];
(mor[AW[w_]] /; StringLength[w] == 1) := (mor[w] = \[Iota][MakeLieSeries[LW[w] /. rules]]);
mor[AW[w_]] := mor[w] = Module[{w1,w2},
w1=StringTake[w, Floor[StringLength[w]/2]];
w2=StringDrop[w, Floor[StringLength[w]/2]];
(mor[AW[w1]])**(mor[AW[w2]])
];
mor[CW[w_]] := tr[mor[AW[w]]];
mor[s_LieSeries] := mor[s] = Module[{ser},
ser=Unique[LieMorphismOnLieSeries];
ser[] = Hold[mor[s]];
ser[d_] := ser[d] = Sum[
mor[s[k]][d],
{k,1, d}
];
LieSeries[ser]
];
mor[cws_CWSeries] := mor[cws] = Module[{ser},
ser=Unique[LieMorphismOnCWSeries];
ser[] = Hold[mor[s]];
ser[d_] := ser[d] = Sum[
mor[cws[k]][d],
{k,1, d}
];
CWSeries[ser]
];
mor[expr_][d_] := Expand[expr /. (w_LW|w_AW|w_CW) :> mor[w][d]];
LieMorphism[mor]
);


StableApply[mor_LieMorphism, (type:(LieSeries|ASeries|CWSeries))[s_]] := (
StableApply[mor, type[s]] = Module[{ser},
ser=Unique[StableApply];
ser[] = Hold[StableApply[mor, type[s]]];
ser[d_] := ser[d] = Nest[mor, type[s], d][d];
type[ser]
]
);


BCHBase = Module[{bch},
bch=Unique["BCHBase"];
bch[] = Hold[BCHBase];
bch[1] = \[LeftAngleBracket]"x"\[RightAngleBracket]+\[LeftAngleBracket]"y"\[RightAngleBracket];
bch[d_Integer] := bch[d] = Expand[Plus[
adSeries[E^(-ad), MakeLieSeries[\[LeftAngleBracket]"y"\[RightAngleBracket]]][MakeLieSeries[\[LeftAngleBracket]"x"\[RightAngleBracket]]][d],
-adSeries[(1-E^(-ad))/ad-1, LieSeries[bch]][EulerE[LieSeries[bch]]][d]
]/d];
LieSeries[bch]
];
BCH[x_, y_] := LieMorphism[{LW["x"] -> x, LW["y"] -> y}][BCHBase];


Unprotect[NonCommutativeMultiply];
x_**0=0; 0**y_=0;
(c_*x_AW) ** y_ := Expand[c(x**y)];
x_ ** (c_*y_AW) := Expand[c(x**y)];
x_Plus ** y_ := (#**y)& /@ x;
x_**y_Plus := (x**#)& /@ y;
Deg[AW[w_]] := StringLength[w];
AW[AW[w_]]:=AW[w];
AW[w1_String] ** AW[w2_String] := AW[w1<>w2];
b[w_AW, z_AW] := w**z-z**w;


ASeries[ser_Symbol][{dd_Integer}] := AS@@Table[ser[d], {d, 0, dd}];
ASeries[as_Symbol][es___] := as[es];
Format[s_ASeries, StandardForm] := s[{$SeriesShowDegree}];
MakeASeries[as_CWSeries] := as;
MakeASeries[expr_] := MakeASeries[expr] = MakeCWSeries[Unique[MakeASeries], expr];
MakeASeries[ser_Symbol, expr_] := (
ser[] = Hold[MakeASeries[ser, expr]];
ser[d_Integer] := ser[d] = Expand[expr /. w_AW /; Deg[w] != d -> 0];
ASeries[ser]
);
(s1_ASeries ** s2_ASeries) := (s1**s2) = Module[{ser},
ser=Unique[NonCommutativeMultiply];
ser[] = Hold[s1**s2];
ser[d_Integer] := ser[d] = Sum[
s1[k]**s2[d-k],
{k,0,d}
];
ASeries[ser]
];


\[Iota][w_LW] /; Deg[w]==1 := AW@@w;
\[Iota][w_LW] := \[Iota][w] = b @@ (\[Iota] /@ LyndonFactorization[w]);
\[Iota][expr_] := Expand[expr /. w_LW :> \[Iota][w]];
\[Iota][ls_LieSeries] := \[Iota][ls] = Module[{as},
as=Unique[\[Iota]];
as[] = Hold[\[Iota][ls]];
as[d_] := as[d] = \[Iota][ls[d]];
ASeries[as]
];


\[Sigma][y_LW, w_LW] /; Deg[y]==1 := \[Sigma][y,w] = Which[
y===w, AW[""],
Deg[w]===1, 0,
True, Module[{w1,w2},
{w1,w2}=LyndonFactorization[w];
\[Iota][w1]**\[Sigma][y,w2]-\[Iota][w2]**\[Sigma][y,w1]
]
];
\[Sigma][y_, ls_LieSeries] := \[Sigma][y, ls] = Module[{as},
as=Unique[\[Sigma]];
as[] = Hold[\[Sigma][y,ls]];
as[d_] := as[d] = \[Sigma][LW[y], ls[d+1]];
ASeries[as]
];
\[Sigma][y_, expr_] := Expand[expr /. w_LW :> \[Sigma][LW[y], w]];


Deg[CW[w_]] := StringLength[w];
CWSeries[cws_Symbol][es___] := cws[es];
CWSeries[ser_Symbol][{dd_Integer}] := CWS@@Table[ser[d], {d, dd}];
Format[s_CWSeries, StandardForm] := s[{$SeriesShowDegree}];
MakeCWSeries[cws_CWSeries] := cws;
MakeCWSeries[expr_] := MakeCWSeries[expr] = MakeCWSeries[Unique[MakeCWSeries], expr];
MakeCWSeries[ser_Symbol, expr_] := (
ser[] = Hold[MakeCWSeries[ser, expr]];
ser[d_Integer] := ser[d] = Expand[expr /. w_CW /; Deg[w] != d -> 0];
CWSeries[ser]
);
s1_CWSeries \[Congruent] s2_CWSeries := And @@ ((s1[#]==s2[#])& /@ Range[$SeriesCompareDegree]);
AddCWSeries[ss___CWSeries] := AddCWSeries[ss] = Module[{ser},
ser=Unique[AddCWSeries];
ser[] = Hold[AddCWSeries[ss]];
ser[d_Integer] := ser[d] = Plus @@ ((#[d])& /@ {ss});
CWSeries[ser]
];
IntegrateCWSeries[cws_CWSeries, {s_, s0_, s1_}] := IntegrateCWSeries[cws, {s, s0, s1}] = Module[{ser},
ser=Unique[IntegrateCWSeries];
ser[]=Hold[IntegrateCWSeries[cws, {s, s0, s1}]];
ser[d_Integer] := ser[d] = Expand[Integrate[cws[d], {s,s0,s1}]];
CWSeries[ser]
];


tr[w_AW] := tr[w] = CW[RotateToMinimal@@w];
tr[expr_] := expr /. aw_AW :> tr[aw];
tr[as_ASeries] := tr[as] = Module[{cws},
cws=Unique[tr];
cws[] = Hold[tr[as]];
cws[d_] := cws[d] = tr[as[d]];
CWSeries[cws]
];


div[y_LW, w_LW] /; Deg[y]==1 := div[y,w] = tr[(AW@@y)**\[Sigma][y,w]];
div[y_, ls_LieSeries] := div[y, ls] = Module[{cws},
cws=Unique[div];
cws[] = Hold[div[y,ls]];
cws[d_] := cws[d] = div[LW[y], ls[d]];
CWSeries[cws]
];
div[y_, expr_] := Expand[expr /. w_LW :> div[LW[y], w]];


J[-1, ___] = MakeCWSeries[0];
J[n_, y_LW, \[Mu]_LieSeries, s_] := J[n,y,\[Mu],s] = Module[
{s\[Mu], \[Mu]s},
s\[Mu] = ScaleLieSeries[s, \[Mu]];
\[Mu]s=StableApply[LieMorphism[{y -> Ad[ScaleLieSeries[-1,s\[Mu]]][LW[z]]}], \[Mu]];
\[Mu]s = \[Mu]s // LieMorphism[{LW[z] -> y}];
IntegrateCWSeries[
AddCWSeries[
J[n-1, y, \[Mu], s] // LieDerivation[{y -> b[\[Mu]s, y]}],
div[y, \[Mu]s ]
],
{s,0,s}
]
];
J[y_LW, \[Mu]_LieSeries] := J[y,\[Mu]] = Module[{cws,s},
cws=Unique[J];
cws[]=Hold[J[y,\[Mu]]];
cws[d_Integer] := cws[d] = J[d-1, y, \[Mu], s][d] /. s->1;
CWSeries[cws]
];



