-1

I have a list of six elements

{1,2,3,4,5,6}

I want to describe the list of all 3-tuples (with repetition) in functional form such that

f[1]=<1,1,1>
f[2]=<1,1,2>
...
f[720]=<6,6,6>

I am interested in a method in Mathematica.

Thank you in advance.

phadej
  • 11,947
  • 41
  • 78
user16153
  • 1
  • 1

3 Answers3

0

Try http://rosettacode.org/wiki/Combinations_with_repetitions for examples in many languages.

Here is their Mathematica example:

DeleteDuplicates[Tuples[{"iced", "jam", "plain"}, 2],Sort[#1] == Sort[#2] &]
->{{"iced", "iced"}, {"iced", "jam"}, {"iced", "plain"}, {"jam", "jam"}, {"jam", "plain"}, {"plain", "plain"}}

Combi[x_, y_] := Binomial[(x + y) - 1, y]
Combi[3, 2]
-> 6
Combi[10, 3]
->220

There is a second example which can handle larger input sizes also which may be more what you want:

CombinWithRep[S_List, k_] := Module[{occupation, assignment},
  occupation = 
   Flatten[Permutations /@ 
     IntegerPartitions[k, {Length[S]}, Range[0, k]], 1];
  assignment = 
   Flatten[Table[ConstantArray[z, {#[[z]]}], {z, Length[#]}]] & /@ 
    occupation;
  Thread[S[[#]]] & /@ assignment
  ]

In[2]:= CombinWithRep[{"iced", "jam", "plain"}, 2]

Out[2]= {{"iced", "iced"}, {"jam", "jam"}, {"plain", 
  "plain"}, {"iced", "jam"}, {"iced", "plain"}, {"jam", "plain"}}
Wayne Tanner
  • 1,346
  • 11
  • 18
  • Thank you for the answer, but the code there outputs all the combinations. I want to feed the position i to a function and the function to output the i-th combination in the set of all possible combinations (with repetition). – user16153 Feb 03 '15 at 13:58
  • Based on that requirement, see http://stackoverflow.com/questions/18613690/calculate-nth-multiset-combination-with-repetition-based-only-on-index – Wayne Tanner Feb 03 '15 at 14:05
  • Thanks for the suggestion, this is almost what I am interested in, but: 1) in the code there the order doesn't matter, whereas for me it matters, 2) there is no good suggestion for doing it in mathematica. – user16153 Feb 03 '15 at 14:42
0

Unless someone is friendly and buys me a license for mathematica, I cannot provide a mathematica solution. But the solution I do provide in F# should be easy enough to port.

First off, if you have 3 out of 6 with repetitions, the number of combinations is 6 * 6 * 6 = 216. Not 720 as the question implies.

let func x i =
    let lookup = 
        [|
            let ax = Array.ofList x
            for first in 0..(ax.Length - 1) do
                for second in 0..(ax.Length - 1) do
                    for third in 0..(ax.Length - 1) do
                        yield [ax.[first]; ax.[second]; ax.[third]]
        |]
    match i >= 0 && i < lookup.Length with
    | true -> lookup.[i]
    | false -> failwith "You are cheating!"

let x = [1;2;3;4;5;6]

let f = func x

printfn "%A" (f 0)
printfn "%A" (f 1)
printfn "%A" (f 2)
BitTickler
  • 10,905
  • 5
  • 32
  • 53
0
set = {"e1", "e2", "e3", "e4", "e5", "e6"}
tab = Table[{i,j,k}, {i,1,6},{j,1,6}, {k,1,6}];
{set[[#[[1]]]],set[[#[[2]]]],set[[#[[3]]]]}&/@(Nest[ArrayFlatten[#,1]&,tab,2])

The output is

{{"e1","e1","e1"},{"e1","e1","e2"},{"e1","e1","e3"},{"e1","e1","e4"}, {"e1", "e1","e5"}, {"e1","e1","e6"}, {"e1","e2","e1"}, {"e1","e2","e2"}....
Xeelee
  • 1
  • 1