# Maps between finite sets

With SageMath, one can do FiniteSetMaps(["a", "b"], [3, 4, 5]) to get all maps from {a,b} to {3,4,5}. Is something similar available in Mathematica?

• In terms of Function or ordered pairs or Rule? May 15 at 16:44
• In general you should show what the expected output should look like. Note also that on a Mathematica forum you should not expect readers to know Sage or require them to search for Sage commands. May 15 at 16:56
• SAGE reference page for Maps between finite sets.
– Syed
May 15 at 17:22

alist = {3, 4, 5};
blist = {"a", "b"};
mapfs = Tuples@Outer[Rule, blist, alist]


$$\begin{array}{l} \{\text{a}\to 3,\text{b}\to 3\} \\ \{\text{a}\to 3,\text{b}\to 4\} \\ \{\text{a}\to 3,\text{b}\to 5\} \\ \{\text{a}\to 4,\text{b}\to 3\} \\ \{\text{a}\to 4,\text{b}\to 4\} \\ \{\text{a}\to 4,\text{b}\to 5\} \\ \{\text{a}\to 5,\text{b}\to 3\} \\ \{\text{a}\to 5,\text{b}\to 4\} \\ \{\text{a}\to 5,\text{b}\to 5\} \\ \end{array}$$

EDIT

Following comment by @Alan (Thanks)

mapasc = Map[Association, mapfs]
mapasc[[2]]["b"]


4

• How can the elements in mapfs be made executable, for example mapfs[[2]]["b"]  should return 4? May 15 at 17:22
• Last@Values@mapfs[[2]]
– Syed
May 15 at 17:25
• For that, I have to know in which position b is. How can a function in mapfs be made executable by its respective argument like "a" or "b"? May 15 at 17:35
• "b" /. mapfs[[2]]
– Syed
May 15 at 17:39
• @Thrash Just map Association over mapfs.
– Alan
May 15 at 19:25

There's no built-in function but the following constructs the mappings:

aa = {"a", "b"};
bb = {3, 4, 5};
Thread[aa -> #] & /@ Tuples[bb, Length@aa]


If you want "functional" expressions, then one way to get them is

funcs = ReplaceAll@Thread[aa -> #] & /@ Tuples[bb, Length@aa]

funcs[[2]]["b"]
(*  4  *)

• Thank you! I tried it first in a more direct way via f/@aa = {3,4} for example, but I get Set::write: Tag Map in f /@ {a, b} is Protected.. Why can't be several values assigned at once? May 15 at 17:19
• @Thrash Try Evaluate[f /@ aa] = {3, 4}. May 15 at 17:20