Combinations with repeated elements in Maple

376 Views Asked by At

How does one output the combinations (with repeated elements) in Maple? I tried:

with(combinat):

choose([a, b, c], 2)

$$[[a, b], [a, c], [b, c]]$$

These are all combinations without repeated elements. How does one get [a,a], [b,b] and [c,c] in the output list.

1

There are 1 best solutions below

3
On BEST ANSWER

The documentation for choose(n, m) mentions:

If m is given, then only combinations of size m are generated; otherwise, all combinations are generated, including the empty combination, that is, the power set is generated and converted to a sorted list. Note that duplicates in the list n are taken into account.

This suggests this algorithm takes duplicate values correctly into consideration, you just need to mention those duplicates in the list explicitly. So something like this works just fine:

> choose([a, a, b, b, c, c], 2)
[[a, a], [a, b], [a, c], [b, b], [b, c], [c, c]]

While it would be nice to have this supported as an additional parameter, you can just write such function by yourself. Such as for example:

choose_with_repetition := proc (n, m, k:=1) 
local duplicated := [seq(op(n), i = 1 .. k)]:
return combinat:-choose(duplicated, m): 
end proc:

This will return combinations of members of $n$ length $m$ (to use original notation) with maximal of $k$ repetitions, so

> choose_with_repetition([a, b, c], 2)
[[a, b], [a, c], [b, c]]

> choose_with_repetition([a, b, c], 2, 2)
[[a, a], [a, b], [a, c], [b, b], [b, c], [c, c]]