I would like to define a new function myTuples
, that behaves just like Tuples
, except that it treats, as with Subsets
, re-ordering as equivalent.
As an example output:
n = 3;
myTuples[{1,2,3},n]
(* Output: {{1,1,1}, {1,1,2}, {1,1,3}, {1,2,2}, {1,2,3}, {1,3,3}, {2,2,2}, {2,2,3},{2,3,3},{3,3,3}} *)
It would be possible to achieve this result via the following method:
- first, apply
Tuples[{1,2,3},n]
- then, apply
Sort
on each element - then, apply
DeleteDuplicates
For small $n$, this is ok. However, for larger $n$, the initial list one gets from Tuples
becomes very large, very quickly (it scales like $3^n$). In parallel, this is going to produce large amounts of duplicates which later are removed, which looks like a waste.
Is there a quicker way to do this?
Alternatively, I was thinking of starting with Subsets[{1,2,3},n]
instead, and then find a way to add whatever is missing, but this also looks not very straightforward.
f[{1, 2, 3}, 3]
from this answer does it: mathematica.stackexchange.com/a/42284/4999 $\endgroup$