1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
\cdbalgorithm{permute}{}
Generic algorithm to generate permutations and combinations of
elements in a list. It takes several arguments depending on the type
of permutations which one wishes to generate.
The simplest permutations are generated by stating whether items can
be taken from the original set more than once (multiple-pick or
single-pick), and stating the length of the permutation sets to be
generated. An example with single-pick is
\begin{screen}{1,2}
{a,b,c,d};
@permute!(%){false}{3};
{{a, b, c}, {a, b, d}, {a, c, d}, {b, c, d}};
\end{screen}
while an example with multiple-pick is
\begin{screen}{1,2}
{a,b,c,d};
@permute!(%){true}{2};
{{a, a}, {a, b}, {a, c}, {a, d}, {b, b}, {b, c},
{b, d}, {c, c}, {c, d}, {d, d}};
\end{screen}
The length parameter is in fact fixing the length of the subsets in
the result for which the order of the elements does not
matter. Therefore,
\begin{screen}{1,2}
{a,b,c};
@permute!(%){false}{1,1};
{{a, b}, {a, c}, {b, a}, {b, c}, {c, a}, {c, b}};
\end{screen}
Finally, it is also possible to generate all permutations with a given
maximal length,
\begin{screen}{1,2}
{a,b,c};
@permute!(%){false}{<3};
{{a}, {b}, {c}, {a, b}, {a, c}, {b, c}};
\end{screen}
More complicated permutations can be generated by assigning weights to
the objects in the original list. Here is an example in which~$a,b,c$
have weights~$2,1,0$ respectively, and lists of length~3 are generated
for which the total weight is~4.
\begin{screen}{1,2}
{a,b,c};
@permute!(%){true}{3}{2,1,0}{4};
{{a, a, c}, {a, b, b}};
\end{screen}
Objects can be assigned more than one type of weight by simply
repeating the two arguments associated to weights. For instance, if we
in addition introduce another weight type, under which~$a,b,c$ have
weight~$0,1,0$ respectively, and also impose that this weight should
add up to~$2$, we get
\begin{screen}{1,2}
{a,b,c};
@permute!(%){true}{3}{2,1,0}{4}{0,1,0}{2};
{{a, b, b}};
\end{screen}
This functionality is useful when constructing field polynomials
restricted to certain length dimension or Grassmann number.
\cdbseealgo{range}
|