Pensieve header: A free-Lie calculator.

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;
AW[w1_String] ** AW[w2_String] := AW[w1<>w2];

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[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[⟨w⟩];
⟨w__⟩:=LW[w];
LW[is__Integer] := LW[StringJoin@@(StringTake["1234567890abcdefghijklmnopqrstuvwxyz", {#}]& /@ {is})];
Deg[LW[x_]] := StringLength[x];

{LyndonQ[AW@"abba"], LyndonQ[AW@"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,10}]

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

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_AW, z_AW] := w**z-z**w;
b[w_LW, z_LW] := LWBracket[w,z];

LWBracket[w_LW, z_LW] := Which[
(* If[Deg[w]+Deg[z]>4, Dialog[]]; *)
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]],
LWBracket[w,z]=b[x,LWBracket[y,z]]+b[LWBracket[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

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}
];
MakeLieSeries[w_LW] := Append[LieSeries@@Table[0, {Deg[w]-1}], w];
LieSeries[]=0;
LieSeries /: Expand[s_LieSeries] := Expand /@ s;
LieSeries /: Plus[ss__LieSeries] /; Length[{ss}]>1 := Module[
{l=Min[Length /@ {ss}]},
LieSeries @@ Total[Take[List @@ #, l]& /@ {ss}]
];
LieSeries /: 0*s_LieSeries := 0;
LieSeries /: c_?NumberQ * s_LieSeries := Expand[c*#]& /@ s;
LieSeries /: s_LieSeries + c_.*w_LW /; NumberQ[c] := Module[{d},
d=Deg[w];
If[Length[s]<d, 0,
ReplacePart[s, d -> s[[d]]+Expand[c*w]]
]
];
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]]}
]
];
b[w_LW, s_LieSeries] := Join[
LieSeries@@Table[0, {Deg[w]}],
ad[w] /@ s
];
b[s_LieSeries, w_LW] := Expand[-b[w,s]];
LieSeries /: EulerE[s_LieSeries] := LieSeries @@ Expand[Range[Length[s]]*(List@@s)];
adSeries[f_, x_, d_][ψ_] := Module[
{ser,as,ni,nf,t,l,m},
ser=List@@Series[f, {ad,0, d}];
{as, ni, nf}=ser[[{3,4,5}]];
t=Nest[ad[x], ψ, ni];
If[Head[t]===LieSeries,
l=Length[t];
Expand[First[as]*t+Sum[
m=1+LengthWhile[t, #==0&];
t=ad[MakeLieSeries[l-m,x]][t];
as[[k+1]]*t,
{k, 1, nf-ni-1}
]],
Expand[as.NestList[ad[x],t,nf-ni-1]]
]
];
Ad[s_LieSeries] := adSeries[E^(-ad), s, Length[s]-1];

xw=⟨x⟩; yw=⟨y⟩;
adSeries[E^(-ad), yw, 3][xw]

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

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

BCH[2]

BCH[8]

LieMorphism[rules_List] := LieMorphism[rules] = LieMorphism[Unique[LieMorphism], rules];
LieMorphism[mor_Symbol, rules_List] := (
mor[]=rules;
(mor[w_LW] /; Deg[w] == 1) := (mor[w] = w /. rules);
mor[w_LW] := (mor[w] = b @@ (mor /@ LyndonFactorization[w]));
mor[s_LieSeries] := Module[
{l=Length[s]},
MakeLieSeries[l, Sum[
LieMorphism[MakeLieSeries[l-k+1, rules]][s[[k]]],
{k,l}
]]
];
mor[expr_] := Expand[expr /. {s_LieSeries :> mor[s], w_LW :> mor[w]}];
mor
);
BCH[n_, x_, y_] := LieMorphism[{LW["x"] -> x, LW["y"] -> y}][BCH[n]];
BCH[s1_LieSeries, s2_LieSeries] := BCH[Min[Length /@ {s1,s2}], s1,s2];

{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}

Timing[{n=10,
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=10,
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}]

ι[w_LW] /; Deg[w]==1 := AW@@w;
ι[w_LW] := ι[w] = b @@ (ι /@ LyndonFactorization[w]);
ι[s_LieSeries] := Prepend[ι/@(ASeries @@ s), 0];
ι[expr_] := Expand[expr /. w_LW :> ι[w]];

t1=ι[BCH[3]]

ASeries /: Expand[s_ASeries] := Expand /@ s;
ASeries /: Plus[ss__ASeries] := Module[
{l=Min[Length /@ {ss}]},
ASeries @@ Total[Take[List @@ #, l]& /@ {ss}]
];
ASeries /: c_ * s_ASeries := Expand[c*#]& /@ s;
s1_ASeries**s2_ASeries := Module[
{d,k,m1,m2},
m1=LengthWhile[s1, #==0&];
m2=LengthWhile[s2, #==0&];
ASeries @@ Table[
Sum[s1[[k+1]]**s2[[d-k+1]], {k,m1,d-m2}],
{d, 0, Min[m1+Length[s2]-1, m2+Length[s1]-1]}
]
];
ASeries /: EulerE[s_ASeries] := ASeries @@ Expand[Range[{0,1+Length[s]}]*(List@@s)];

ASeries[AW[""],0,0,0]+t1+t1**t1/2+t1**t1**t1/6

ASeries[AW[""],AW["x"],AW["xx"]/2,AW["xxx"]/6]**ASeries[AW[""],AW["y"],AW["yy"]/2,AW["yyy"]/6]

σ[y_LW, w_LW] /; Deg[y]==1 := σ[y,w] = Which[
y===w, AW[""],
Deg[w]===1, 0,
True, Module[{w1,w2},
{w1,w2}=LyndonFactorization[w];
ι[w1]**σ[y,w2]-ι[w2]**σ[y,w1]
]
];
σ[y_, expr_] := Expand[expr /. w_LW :> σ[LW[y], w]] /. LieSeries -> ASeries;

(#->σ[1, #])& /@ AllLyndonWords[{5}, {"1","2"}]

xw=⟨"x"⟩; yw=⟨"y"⟩;
{σ[xw, BCH[5,xw,yw]], σ[yw, BCH[5,xw,yw]]}/. AW[s_] :> s

σ[xw, BCH[8,xw,yw]] /. AW[s_] :> s