Function Repository Resource:

OrderedTupleFromIndex

Source Notebook

Get the ordered tuple with the given index and length

Contributed by: Ed Pegg Jr

ResourceFunction["OrderedTupleFromIndex"][index,len]

returns an ordered tuple of length len with given index.

Details and Options

Ordered tuples are considered to be lists {a1,a2,,ak} such that the ai are non-negative and non-decreasing.
For any ordered tuple, a unique index n can be defined via .
For example, subset {1, 2, 2} has index .

Examples

Basic Examples (3) 

The following ordered 3-tuple sequence can be extended to infinity:

In[1]:=
Table[ResourceFunction["OrderedTupleFromIndex"][index, 3], {index, 1, 10}]
Out[1]=

The function can return ordered tuples of a large index:

In[2]:=
Table[ResourceFunction["OrderedTupleFromIndex"][index, 3], {index, 182101, 182110}]
Out[2]=

The function can return ordered tuples of a given index for various lengths:

In[3]:=
Table[ResourceFunction["OrderedTupleFromIndex"][2019, len], {len, 2, 9}]
Out[3]=

Scope (3) 

Here are some ordered 2-tuples with their indices to show their structure:

In[4]:=
doubles = Table[{ResourceFunction["OrderedTupleFromIndex"][index, 2], index}, {index, 1, 15}];
Graphics[{Text[Style[Column[{StringJoin[ToString /@ #[[1]]], #[[2]]},
       Alignment -> Center], 20], #[[1]]] & /@ doubles, Blue, Line[First /@ doubles]}]
Out[5]=

The ordered 2-tuple {4,4} has an index of :

In[6]:=
Binomial[4 + 0, 1] + Binomial[4 + 1, 2] + 1
Out[6]=

Use this to recover the tuple:

In[7]:=
ResourceFunction["OrderedTupleFromIndex"][15, 2]
Out[7]=

The structure of ordered 3-tuples in 3D:

In[8]:=
triples = Table[{ResourceFunction["OrderedTupleFromIndex"][index, 3], index}, {index, 1, 20}];
Graphics3D[{Text[Style[Column[{StringJoin[ToString /@ #[[1]]], #[[2]]},
       Alignment -> Center], 20], #[[1]]] & /@ triples, Blue, Line[First /@ triples]}]
Out[9]=

Properties and Relations (3) 

Use Tuples to produce ordered 3-tuples:

In[10]:=
SortBy[Union[Sort /@ Tuples[Range[0, 2], {3}]], Reverse]
Out[10]=

The same sequence of ordered 3-tuples can be obtained from ordered Subsets:

In[11]:=
# - {0, 1, 2} & /@ SortBy[Subsets[Range[0, 4], {3}], Reverse]
Out[11]=

OrderedTupleFromIndex returns the same ordered tuples in the same sequence:

In[12]:=
Table[ResourceFunction["OrderedTupleFromIndex"][index, 3], {index, 1, 10}]
Out[12]=

Version History

  • 1.0.0 – 06 December 2019

Related Resources

License Information