Pensieve header: A free-Lie calculator.

A Lyndon word is a word lexicographically smaller than all of its proper right factors; see http://katlas.math.toronto.edu/drorbn/bbs/show?shot=Chu-071214-182203.jpg

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[⟨w⟩];
⟨w_⟩:=LW[w];
Deg[LW[x_]] := StringLength[x];

{LyndonQ["abba"], LyndonQ["ababb"]}

{AllWords[3, {"1", "2"}], AllLyndonWords[{3}, {"1", "2"}]}

Table[Length[AllLyndonWords[k, {"1", "2"}]], {k,10}]

Table[Length[AllLyndonWords[k, {"1", "2", "3"}]], {k,8}]

TreeForm[LW["12122"] //. w_LW :> LyndonFactorization[w] /. LW[w_] :> w]

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]
]
]
];

b[LW["112"], LW["122"]]

Outer[b, AllLyndonWords[{3}, {"1", "2"}], AllLyndonWords[{3}, {"1", "2"}]] // MatrixForm

Union[Flatten[Outer[(b[#1,#2]+b[#2,#1])&,
AllLyndonWords[{6}, {"1", "2"}], AllLyndonWords[{6}, {"1", "2"}]
]]]

Outer[(b[#1,b[#2,#3]]+b[#2,b[#3,#1]]+b[#3,b[#1,#2]])&,
AllLyndonWords[{5}, {"1", "2"}], AllLyndonWords[{5}, {"1", "2"}], AllLyndonWords[{5}, {"1", "2"}]
] // Flatten // Union

{xw=LW["x"], yw=LW["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_][ψ_] := Module[
{ser,as,ni,nf,t,l},
ser=List@@Series[f, {var,0, d}];
{as, ni, nf}=ser[[{3,4,5}]];
t=Nest[op, ψ, 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]]
]
]

OperatorSeries[E^(-ad), ad->ad[yw], 3][xw]

MakeLieSeries[5, OperatorSeries[E^(-ad), ad->ad[xw], 3][yw]]

{
t1=MakeLieSeries[2, ⟨"y"⟩+OperatorSeries[E^(-ad), ad->ad[⟨"y"⟩], 1][⟨"x"⟩]],
bch=Append[LieSeries[⟨"x"⟩+⟨"y"⟩],0];
t2=OperatorSeries[(1-E^(-ad))/ad, ad->ad[bch], 1][EulerE[bch]],
t1-t2
}

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

BCH[2]

BCH[8]

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]];

MakeLieSeries[3, {LW["x"] -> LW["x"], LW["y"] -> LW["z"]}]

MakeLieSeries[2, LW["x"]]

BCH[3]

BCH[3, LW["y"], LW["z"]]

BCH[3, LW["x"], BCH[3, LW["y"], LW["z"]]]

BCH[3, BCH[3, LW["x"], LW["y"]], LW["z"]]

{n=4,
t1=BCH[n, LW["x"], BCH[n, LW["y"], LW["z"]]],
t2=BCH[n, BCH[n, LW["x"], LW["y"]], LW["z"]],
t1==t2}

BCH[6, LW["y"], LW["z"]]

BCH[6, LW["x"], BCH[6, LW["y"], LW["z"]]]

Timing[{n=6,
Length /@ (t1=BCH[n, LW["x"], BCH[n, LW["y"], LW["z"]]]),
Length /@ (t2=BCH[n, BCH[n, LW["x"], LW["y"]], LW["z"]]),
t1==t2}]

Timing[{n=7,
Timing[Length /@ (t1=BCH[n, LW["x"], BCH[n, LW["y"], LW["z"]]])],
Timing[Length /@ (t2=BCH[n, BCH[n, LW["x"], LW["y"]], LW["z"]])],
t1==t2}]