A Free Lie Calculator

Pensieve header: A free-Lie calculator.

The “Lyndon” Kernel

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]
]
]
];
ad[x_][y_]:=b[x,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

{x=LW["x"], y=LW["y"], ad[x][y], ad[y][x]}

LieSeries

LieSeries[name_, expr_]:= (
name@d_Integer := name@d = expr /. w_LW /; Deg[w] ≠ d :> 0;
name
);
LieSeries[expr_] := LieSeries[Unique[], expr]

LieSeries[bch5, ⟨"x"⟩+⟨"y"⟩+((⟨"xy"⟩)/2)+((⟨"xxy"⟩)/12)+((⟨"xyy"⟩)/12)+((⟨"xxyy"⟩)/24)-((⟨"xxxxy"⟩)/720)+((⟨"xxxyy"⟩)/180)+((⟨"xxyxy"⟩)/360)+((⟨"xxyyy"⟩)/180)+((⟨"xyxyy"⟩)/120)-((⟨"xyyyy"⟩)/720)]

bch5@4

OperatorSeries[E^(-ad), ad->ad[y]][x]@3