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



LyndonQ[w_String] := And @@ (
OrderedQ[{w, #}]& /@ Table[StringDrop[w,i], {i,1,StringLength[w]-1}]
);
AllWords[0, _List] = {""};
AllWords[n_ /; n>0, ab_List] := AllWords[n, ab] = Flatten[Outer[
StringJoin[#1, #2]&,
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 /: 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];
Deg[LW[x_]] := StringLength[x];


b[0,_] =0; b[_,0]=0;
b[c_*x_LW, y_] := Expand[c b[x,y]];
b[x_, c_*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] := 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[w,z]=b[x,b[y,z]]+b[b[x,z],y]
]
]
];


ad[x_][y_]:=b[x,y];
MakeLieSeries[d_, l_List] := MakeLieSeries[d, #]& /@ l;
MakeLieSeries[d_, s_LieSeries] /; Length[s]<=d := s;
MakeLieSeries[d_, s_LieSeries] /; Length[s]>d := Take[s,d];
MakeLieSeries[d_, a_ -> b_] := (a -> MakeLieSeries[d, b]);
MakeLieSeries[d_, expr_] := LieSeries @@ Table[
expr /. w_LW /; Deg[w] != k -> 0,
{k,d}
];
LieSeries /: s1_LieSeries + s2_LieSeries := Module[
{l=Min[Length /@ {s1, s2}]},
LieSeries@@(Take[List@@s1, l]+Take[List@@s2,l])
];
LieSeries /: c_ * s_LieSeries := Expand[c*#]& /@ s;
b[s1_LieSeries, s2_LieSeries] := Module[
{d,k,m1,m2},
m1=1+LengthWhile[s1, #==0&];
m2=1+LengthWhile[s2, #==0&];
LieSeries @@ Table[
Sum[b[s1[[k]], s2[[d-k]]], {k,m1,d-m2}],
{d, Min[m1+Length[s2], m2+Length[s1]]}
]
];
LieSeries /: EulerE[s_LieSeries] := LieSeries @@ Expand[Range[Length[s]]*(List@@s)];
OperatorSeries[f_, var_ -> op_, d_][\[Psi]_] := Module[
{ser,as,ni,nf,t,l},
ser=List@@Series[f, {var,0, d}];
{as, ni, nf}=ser[[{3,4,5}]];
t=Nest[op, \[Psi], ni];
If[Head[t]===LieSeries,
l=Length[t];
Expand[as.NestList[MakeLieSeries[l, op[#]]&,t,nf-ni-1]],
Expand[as.NestList[op,t,nf-ni-1]]
]
]


BCH[1]= LieSeries[\[LeftAngleBracket]"x"\[RightAngleBracket]+\[LeftAngleBracket]"y"\[RightAngleBracket]];
BCH[n_]:= BCH[n] = Module[
{bch,t1,t2},
bch=Append[BCH[n-1],0];
t1=MakeLieSeries[n, \[LeftAngleBracket]"y"\[RightAngleBracket]+OperatorSeries[E^(-ad), ad->ad[\[LeftAngleBracket]"y"\[RightAngleBracket]], n-1][\[LeftAngleBracket]"x"\[RightAngleBracket]]];
t2=OperatorSeries[(1-E^(-ad))/ad, ad->ad[bch],n-1][EulerE[bch]];
bch+(t1-t2)/n
]


ApplyMorphism[mor_, w_LW] /; Deg[w] == 1 := w /. mor;
ApplyMorphism[mor_, w_LW] := b @@ (ApplyMorphism[mor , #]& /@ LyndonFactorization[w]);
ApplyMorphism[mor_, s_LieSeries] := Module[
{l=Length[s]},
MakeLieSeries[l, Sum[
ApplyMorphism[
MakeLieSeries[l-k+1, mor],
s[[k]]
],
{k,l}
]]
];
ApplyMorphism[mor_, expr_] := Expand[expr /. w_LW :> ApplyMorphism[mor, w]];
BCH[n_, x_, y_] := ApplyMorphism[{LW["x"] -> x, LW["y"] -> y}, BCH[n]];



