 tablereverse - Maple Help

tablereverse

reverse table Calling Sequence tablereverse(t, c, opts) Parameters

 t - table c - (optional) combiner function opts - zero or or more options as specified below Options

 • distinctentries = truefalse
 Specifies that the entries in the input table t are known to be distinct, i.e. numelems(t)=numelems([entries(t)]). This enables more efficient construction of the output table. The default is false.
 • nolist = truefalse
 Specifying 'nolist' or 'nolist'=true causes each entry in the output table s to be a simple collection of indices of t, without wrapping each index in a list. This is useful for tables where every index is a single object. When 'nolist' is used on a table with expression sequence indices or entries, the pairings may be difficult to deduce in the result.
 • sort = truefalse
 Specifying 'sort' or 'sort'=true indicates that each entry of the output table s should be sorted. This is only useful when a non-default combiner function c has been specified which produces ordered containers (for example a list or Array). The default value is false, which means that if the container type is ordered, the order chosen is not specified. Summary

 • Returns a table which is the result of reversing the table t while combining keys mapping to the same value with combiner function f. Description

 • The tablereverse function takes a table t and returns a table s which is a reversal of t, in the sense that the indices of s are the entries of t and the entries of s are a collection of the indices of t.
 • By default, an entry s[u] in the output table s consists of a set of lists. Each element v in s[u] is a list whose contents correspond to an entry of the input table t pointing at u, so the property t[op(v)]=u holds.
 • The optional argument c allows the specification of a combiner function c which accepts an expression sequence of inputs representing keys in t which map to the same value and returns some customized value.
 • The default combiner function is simply the set constructor {} which collects all its arguments together in a set. Examples

Here we reverse a table mapping various foods to food categories.

 > $T≔\mathrm{table}\left(\left["apple"="fruit","orange"="fruit","zucchini"="vegetable","milk"="dairy"\right]\right)$
 ${T}{≔}{table}{}\left(\left[{"apple"}{=}{"fruit"}{,}{"orange"}{=}{"fruit"}{,}{"zucchini"}{=}{"vegetable"}{,}{"milk"}{=}{"dairy"}\right]\right)$ (1)
 > $\mathrm{tablereverse}\left(T\right)$
 ${table}{}\left(\left[{"fruit"}{=}\left\{\left[{"apple"}\right]{,}\left[{"orange"}\right]\right\}{,}{"dairy"}{=}\left\{\left[{"milk"}\right]\right\}{,}{"vegetable"}{=}\left\{\left[{"zucchini"}\right]\right\}\right]\right)$ (2)

Notice that in the above output all the foods in the entries of the output table appear in lists. This is to preserve the keys of the original table T in case any of its keys were expression sequences. Because this is not the case in our example, we can use nolist to simplify the output.

 > $\mathrm{tablereverse}\left(T,\mathrm{nolist}\right)$
 ${table}{}\left(\left[{"fruit"}{=}\left\{{"apple"}{,}{"orange"}\right\}{,}{"dairy"}{=}\left\{{"milk"}\right\}{,}{"vegetable"}{=}\left\{{"zucchini"}\right\}\right]\right)$ (3)

Instead of the default combiner function, use a list constructor and sort the resulting entries.

 > $\mathrm{tablereverse}\left(T,\mathrm{\left[\right]},\mathrm{nolist},\mathrm{sort}\right)$
 ${table}{}\left(\left[{"fruit"}{=}\left[{"apple"}{,}{"orange"}\right]{,}{"dairy"}{=}\left[{"milk"}\right]{,}{"vegetable"}{=}\left[{"zucchini"}\right]\right]\right)$ (4)

This table shows what ABO blood type a person has given their genetic profile.

 > $\mathrm{BloodPhenotypes}≔\mathrm{table}\left(\mathrm{symmetric},\left\{\left("A"$2\right)="A",\left("B"$2\right)="B",\left("O"\$2\right)="O",\left("A","B"\right)="AB",\left("A","O"\right)="A",\left("B","O"\right)="B"\right\}\right)$
 ${\mathrm{BloodPhenotypes}}{≔}{table}{}\left({\mathrm{symmetric}}{,}\left[\left({"B"}{,}{"B"}\right){=}{"B"}{,}\left({"A"}{,}{"A"}\right){=}{"A"}{,}\left({"B"}{,}{"O"}\right){=}{"B"}{,}\left({"A"}{,}{"O"}\right){=}{"A"}{,}\left({"O"}{,}{"O"}\right){=}{"O"}{,}\left({"A"}{,}{"B"}\right){=}{"AB"}\right]\right)$ (5)
 > $\mathrm{BloodGenotypes}≔\mathrm{tablereverse}\left(\mathrm{BloodPhenotypes}\right)$
 ${\mathrm{BloodGenotypes}}{≔}{table}{}\left(\left[{"A"}{=}\left\{\left[{"A"}{,}{"A"}\right]{,}\left[{"A"}{,}{"O"}\right]{,}\left[{"O"}{,}{"A"}\right]\right\}{,}{"O"}{=}\left\{\left[{"O"}{,}{"O"}\right]\right\}{,}{"AB"}{=}\left\{\left[{"A"}{,}{"B"}\right]{,}\left[{"B"}{,}{"A"}\right]\right\}{,}{"B"}{=}\left\{\left[{"B"}{,}{"B"}\right]{,}\left[{"B"}{,}{"O"}\right]{,}\left[{"O"}{,}{"B"}\right]\right\}\right]\right)$ (6)

We can see by this result that there are multiple genetic combinations that result in type A, but only one which results in type O.

 > ${\mathrm{BloodGenotypes}}_{"A"}$
 $\left\{\left[{"A"}{,}{"A"}\right]{,}\left[{"A"}{,}{"O"}\right]{,}\left[{"O"}{,}{"A"}\right]\right\}$ (7)
 > ${\mathrm{BloodGenotypes}}_{"O"}$
 $\left\{\left[{"O"}{,}{"O"}\right]\right\}$ (8)

This table maps countries to their capital cities. Because the entries here are distinct, that is, no city appears as the capital of two countries, we can use the distinctentries option when calling tablereverse.

 > $\mathrm{Capitals}≔\mathrm{table}\left(\left\{"Cambodia"="Phnom Penh","Cape Verde"="Praia","Croatia"="Zagreb","Jordan"="Amman","Mali"="Bamako","Sierra Leone"="Freetown","Uruguay"="Montevideo"\right\}\right)$
 ${\mathrm{Capitals}}{≔}{table}{}\left(\left[{"Uruguay"}{=}{"Montevideo"}{,}{"Sierra Leone"}{=}{"Freetown"}{,}{"Mali"}{=}{"Bamako"}{,}{"Croatia"}{=}{"Zagreb"}{,}{"Cambodia"}{=}{"Phnom Penh"}{,}{"Cape Verde"}{=}{"Praia"}{,}{"Jordan"}{=}{"Amman"}\right]\right)$ (9)
 > $\mathrm{tablereverse}\left(\mathrm{Capitals},\mathrm{distinctentries},\mathrm{nolist}\right)$
 ${table}{}\left(\left[{"Montevideo"}{=}\left\{{"Uruguay"}\right\}{,}{"Bamako"}{=}\left\{{"Mali"}\right\}{,}{"Freetown"}{=}\left\{{"Sierra Leone"}\right\}{,}{"Amman"}{=}\left\{{"Jordan"}\right\}{,}{"Phnom Penh"}{=}\left\{{"Cambodia"}\right\}{,}{"Praia"}{=}\left\{{"Cape Verde"}\right\}{,}{"Zagreb"}{=}\left\{{"Croatia"}\right\}\right]\right)$ (10)

The fact that entries are distinct in the original table means that with the default combiner function, every entry in the output will be a singleton set. We can avoid this using the identity function as combiner.

 > $\mathrm{tablereverse}\left(\mathrm{Capitals},t→t,\mathrm{distinctentries},\mathrm{nolist}\right)$
 ${table}{}\left(\left[{"Montevideo"}{=}{"Uruguay"}{,}{"Bamako"}{=}{"Mali"}{,}{"Freetown"}{=}{"Sierra Leone"}{,}{"Amman"}{=}{"Jordan"}{,}{"Phnom Penh"}{=}{"Cambodia"}{,}{"Praia"}{=}{"Cape Verde"}{,}{"Zagreb"}{=}{"Croatia"}\right]\right)$ (11) Compatibility

 • The tablereverse command was introduced in Maple 2019.