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
    Commented May 15, 2022 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
    Commented May 15, 2022 at 16:56
  • 1
    $\begingroup$ SAGE reference page for Maps between finite sets. $\endgroup$
    – Syed
    Commented May 15, 2022 at 17:22

2 Answers 2

12
$\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
    Commented May 15, 2022 at 17:22
  • $\begingroup$ Last@Values@mapfs[[2]] $\endgroup$
    – Syed
    Commented May 15, 2022 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
    Commented May 15, 2022 at 17:35
  • 1
    $\begingroup$ "b" /. mapfs[[2]] $\endgroup$
    – Syed
    Commented May 15, 2022 at 17:39
  • 3
    $\begingroup$ @Thrash Just map Association over mapfs. $\endgroup$
    – Alan
    Commented May 15, 2022 at 19:25
7
$\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
    Commented May 15, 2022 at 17:19
  • 1
    $\begingroup$ @Thrash Try Evaluate[f /@ aa] = {3, 4}. $\endgroup$
    – Michael E2
    Commented May 15, 2022 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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