Function Repository Resource:

StuffleProductExpand

Source Notebook

Expand products of symbolic word objects into their canonical stuffle sum

Contributed by: Jayanta Phadikar

ResourceFunction["StuffleProductExpand"][expr]

expands products of symbolic word objects (e.g. w[{}],Inactive[List][] etc.) in expr into their canonical stuffle sum.

Details

A stuffle sum of two words is the sum of all words formed by interlacing them, allowing equal-position letters to merge (via the alphabet product).
The stuffle sum is also known as "sticky shuffle sum", "quasi shuffle sum" or "harmonic shuffle sum".
ResourceFunction["StuffleProductExpand"] algebraically converts products of symbolic word objects into the sum of all stuffles of their index words.
Distributes over Plus, pulls out scalar factors, and acts independently on each multiplicative factor.
Stuffle counts grow combinatorially; limits on depth/terms prevent blow-ups.
Words must appear under a symbolic head (e.g. w[{}]). Bare lists such as {1,2} are not treated as words (to avoid list arithmetic e.g. {1,2} * {2,3}{2,6}).
Handles products of any number of word objects; w[a]w[b]w[c]… expands to the full multi-stuffle (associative and multilinear).
Interprets w[u]^n (with integer n 0) as the n-fold stuffle power of u i.e. the stuffle of u with itself n times.

Examples

Basic Examples (1) 

Stuffle product of two words with an inert head:

In[1]:=
ResourceFunction["StuffleProductExpand"][w[{1, 2}] w[{3, 4}]]
Out[1]=

Scope (5) 

Expand the 3-way stuffle of words:

In[2]:=
ResourceFunction["StuffleProductExpand"][h[{1}] h[{2}] h[{3}]]
Out[2]=

Expand a 2 × 3 stuffle:

In[3]:=
ResourceFunction["StuffleProductExpand"][h[{1, 2}] h[{2, 3, 4}]]
Out[3]=

Treat the empty word as the identity:

In[4]:=
ResourceFunction["StuffleProductExpand"][h[{}] h[{a, b}]]
Out[4]=

StuffleProductExpand distributes an expression linearly before expanding:

In[5]:=
ResourceFunction[
 "StuffleProductExpand"][(h[{a1}] + 2 h[{a2}] - 3 h[{a3}]) h[{a4, a5}]]
Out[5]=

Stuffle independently by head and multiply the results:

In[6]:=
ResourceFunction["StuffleProductExpand"][
 w1[{1, 2}] w1[{3}] w2[{3}] w2[{4}]]
Out[6]=

Properties and Relations (6) 

Stuffle product expansion is commutative:

In[7]:=
ResourceFunction["StuffleProductExpand"][h[{a, b}] h[{c}]] === ResourceFunction["StuffleProductExpand"][h[{c}] h[{a, b}]]
Out[7]=

Stuffle product expansion is associative:

In[8]:=
ResourceFunction["StuffleProductExpand"][(h[{a}] h[{b}]) h[{c}]] === ResourceFunction["StuffleProductExpand"][h[{a}] (h[{b}] h[{c}])]
Out[8]=

If the inputs have lengths m and n, every output term has length m+n-k where 0kMin[m,n]:

In[9]:=
res = ResourceFunction["StuffleProductExpand"][h[{a, b}] h[{c, d, e}]];
In[10]:=
Sort@Union@(Length /@ Cases[res, h[w_List] :> w, \[Infinity]])
Out[10]=

Relate term count to Delannoy number and Multinomial:

In[11]:=
delannoy[m_, n_] := Sum[Binomial[m, k] Binomial[n, k] 2^k, {k, 0, Min[m, n]}];
In[12]:=
List @@ ResourceFunction["StuffleProductExpand"][
   w[{a, b}] w[{c, d, e, f, g}]] // Length
Out[12]=
In[13]:=
% == delannoy[2, 5]
Out[13]=

The number of terms with exactly k merges (output length m+n-k), for k=0, 1, …, Min[m,n] is Multinomial[Max[m,n]-k, Min[m,n]-k, k]:

In[14]:=
%% == Sum[Multinomial[5 - k, 2 - k, k], {k, 0, 2}]
Out[14]=

Expanding an already stuffled sum changes nothing (indempotence):

In[15]:=
res = ResourceFunction["StuffleProductExpand"][h[{p}] h[{q, r}]]
Out[15]=
In[16]:=
res === ResourceFunction["StuffleProductExpand"][res]
Out[16]=

Reverse both inputs to reverse every shuffled word (reversal symmetry):

In[17]:=
Sort[Reverse /@ Cases[ResourceFunction["StuffleProductExpand"][h[{a, b}] h[{c, d}]],
    h[w_List] :> w]]
Out[17]=
In[18]:=
Sort[Cases[
  ResourceFunction["StuffleProductExpand"][
   h[Reverse@{a, b}] h[Reverse@{c, d}]], h[w_List] :> w]]
Out[18]=
In[19]:=
% === %%
Out[19]=

Possible Issues (2) 

Bare lists multiply element-wise:

In[20]:=
ResourceFunction["StuffleProductExpand"][{2, 6} {10, 12}]
Out[20]=

Wrap as h[{}] or use Inactive[List] to treat them as words before expanding:

In[21]:=
ResourceFunction["StuffleProductExpand"][h[{2, 6}] h[{10, 12}]]
Out[21]=
In[22]:=
ResourceFunction["StuffleProductExpand"][
 Inactive[List][{2, 6}] Inactive[List][{10, 12}]]
Out[22]=

Publisher

Jayanta Kumar Phadikar

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.0.0 – 17 December 2025

Related Resources

License Information