5
$\begingroup$

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?

$\endgroup$
3
  • $\begingroup$ In terms of Function or ordered pairs or Rule? $\endgroup$
    – Michael E2
    May 15 at 16:44
  • 2
    $\begingroup$ 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. $\endgroup$
    – Bob Hanlon
    May 15 at 16:56
  • 1
    $\begingroup$ SAGE reference page for Maps between finite sets. $\endgroup$
    – Syed
    May 15 at 17:22

2 Answers 2

11
$\begingroup$
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

$\endgroup$
6
  • $\begingroup$ How can the elements in mapfs be made executable, for example mapfs[[2]]["b"] should return 4? $\endgroup$
    – Thrash
    May 15 at 17:22
  • $\begingroup$ Last@Values@mapfs[[2]] $\endgroup$
    – Syed
    May 15 at 17:25
  • $\begingroup$ 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"? $\endgroup$
    – Thrash
    May 15 at 17:35
  • 1
    $\begingroup$ "b" /. mapfs[[2]] $\endgroup$
    – Syed
    May 15 at 17:39
  • 3
    $\begingroup$ @Thrash Just map Association over mapfs. $\endgroup$
    – Alan
    May 15 at 19:25
6
$\begingroup$

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  *)
$\endgroup$
2
  • $\begingroup$ 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? $\endgroup$
    – Thrash
    May 15 at 17:19
  • 1
    $\begingroup$ @Thrash Try Evaluate[f /@ aa] = {3, 4}. $\endgroup$
    – Michael E2
    May 15 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.