Function Repository Resource:

TupleIndex

Source Notebook

Compute the index of a given tuple of non-negative integers

Contributed by: Ed Pegg Jr

ResourceFunction["TupleIndex"][tuple]

returns the index of tuple as it would appear in a list of all integer tuples of the same length, sorted by the maximal element.

Details and Options

A tuple with largest element n will be ordered after tuples that have all elements less than n.
In case of ties, tuples with largest element n have Sort ordering.

Examples

Basic Examples (6) 

Get the index of a 3-tuple:

In[1]:=
ResourceFunction["TupleIndex"][{0, 1, 1}]
Out[1]=

Get the index of a 3-tuple with a larger element:

In[2]:=
ResourceFunction["TupleIndex"][{0, 1, 2}]
Out[2]=

Show by example how element sizes and positions affect ordering:

In[3]:=
Map[ResourceFunction[
 "TupleIndex"], {{0, 1, 2}, {0, 2, 0}, {0, 2, 1}, {0, 2, 2}, {1, 0, 2}}]
Out[3]=

Indices of 2-tuples sorted by maximal element:

In[4]:=
ResourceFunction["TupleIndex"] /@ SortBy[Tuples[{0, 1, 2}, {2}], Max]
Out[4]=

Indices of 3-tuples sorted by maximal element:

In[5]:=
ResourceFunction["TupleIndex"] /@ SortBy[Tuples[{0, 1, 2, 3}, {3}], Max]
Out[5]=

Show the tuples as they would be sorted by TupleIndex:

In[6]:=
SortBy[Tuples[Range[0, 2], 3], ResourceFunction["TupleIndex"]]
Out[6]=

Find indices of some larger k-tuples:

In[7]:=
ResourceFunction[
 "TupleIndex"] /@ {{999999, 999999}, {9999, 9999, 9999}, {999, 999, 999, 999}, {58, 251, 174, 151, 123}, {99, 99, 99, 99, 99, 99}, {47,
    11, 16, 51, 29, 21, 41}, {21, 17, 31, 0, 18, 30, 14, 27}, {12, 1, 14, 3, 17, 21, 14, 15, 8}, {13, 15, 14, 1, 3, 13, 15, 3, 7, 0}, {3,
    5, 5, 0, 8, 9, 9, 12, 8, 4, 0}, {9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9}}
Out[7]=

Properties and Relations (2) 

Use Tuples to produce 3-tuples sorted by maximal element:

In[8]:=
orderbymax = SortBy[Tuples[Range[0, 2], {3}], {Max[#], #} &]
Out[8]=

TupleIndex is based on this ordering:

In[9]:=
ResourceFunction["TupleIndex"] /@ orderbymax
Out[9]=

Neat Examples (2) 

Find the index of a large 6-tuple:

In[10]:=
ResourceFunction[
 "TupleIndex"][{46415888336127788, 25278305421600277, 25756471829601431, 6667595205862458, 14964195829129700, 2182024098365373}]
Out[10]=

Find the index of a large 111-tuple:

In[11]:=
ResourceFunction[
 "TupleIndex"][{4, 4, 4, 4, 6, 5, 4, 7, 1, 0, 3, 1, 1, 1, 2, 6, 2, 6, 6, 6, 6, 3, 1, 3, 1, 4, 7, 3, 6, 3, 1, 3, 2, 3, 0, 6, 1, 2, 6, 6, 3,
   7, 4, 1, 1, 5, 6, 2, 1, 1, 2, 1, 4, 5, 1, 6, 3, 5, 0, 2, 3, 2, 6, 0, 7, 2, 5, 6, 3, 2, 0, 3, 2, 2, 3, 1, 3, 7, 1, 5, 2, 0, 5, 7, 6, 4,
   0, 0, 0, 1, 2, 5, 1, 1, 2, 3, 1, 7, 6, 1, 6, 5, 6, 1, 6, 0, 2, 1, 1, 2, 6}]
Out[11]=

Version History

  • 2.0.0 – 20 December 2019
  • 1.0.0 – 02 December 2019

Related Resources

License Information