Skip to main content

performance tuning - Combinatorics: Placed errors


Consider a vector of length n where each element can take one of the values U, X, Y or Z. Then define w as the the number of X, Y and Z's in the vector. How can I efficiently write a Mathematica function ErrorOps[n,w] that returns all possible strings for a particular choice of w and n?


My key concern is efficiency, since I have made three attempts that are working, but doing so too slowly.


Input:



n=3, w=2 

Output:


ErrorOps[3, 2] = {{U, X, X}, {X, U, X}, {X, X, U}, {U, X, Y}, {X, U, Y}, {X, Y, U},
{U, X, Z}, {X, U, Z}, {X, Z, U}, {U, Y, X}, {Y, U, X}, {Y, X, U}, {U, Y, Y},
{Y, U, Y}, {Y, Y, U}, {U, Y, Z}, {Y, U, Z}, {Y, Z, U}, {U, Z, X}, {Z, U, X},
{Z, X, U}, {U, Z, Y}, {Z, U, Y}, {Z, Y, U}, {U, Z, Z}, {Z, U, Z}, {Z, Z, U}}

The number of such vectors is thus Binomial[n, w] 3^w.


A typical function call will have n = 10 in my case, and w < 11.




Answer



I propose:


errorOps[n_, w_] :=
Module[{masks, tup},
masks = Permutations[Join @@ ConstantArray @@@ {{1, w}, {0, n - w}}];
tup = ArrayPad[{"X", "Y", "Z"} ~Tuples~ {w}, {0, {1, 0}}, "U"];
Join @@ Map[tup[[All, #]] &, 1 + masks (Accumulate /@ masks)]
]

errorOps[10, 7] // Length // AbsoluteTiming



{0.0406705, 262440}

An alternate formulation that is faster in some cases but slower in others:


errorOps2[n_, w_] :=
Module[{mask, tup},
mask = Join @@ ConstantArray @@@ {{0, n - w}, {1, w}};
tup = GatherBy[{"X", "Y", "Z"} ~Tuples~ {w}, Sort][[All, 1]];
tup = ArrayPad[tup, {0, {1, 0}}, "U"];

Join @@ Permutations /@ tup[[ All, 1 + Accumulate @ mask ]]
]

errorOps2[10, 7] // Length // AbsoluteTiming


{0.0335602, 262440}

Comments