Loading [MathJax]/jax/output/HTML-CSS/jax.js
Ask Your Question
2

How to get the collection of all functions from X to Y in SageMath

asked 6 years ago

sagelearner gravatar image

Let X=1,2,,n and Y=1,2,,m. Is there any way to get the set of all functions from X to Y in SageMath?

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
3

answered 6 years ago

FrédéricC gravatar image

Like this:

M = FiniteSetMaps(["a", "b"], [3, 4, 5]); M
Preview: (hide)
link

Comments

Exactly the right tool. To further illustrate:

For n=2, m=3:

sage: M = FiniteSetMaps([1 .. 2], [1 .. 3]); M
Maps from {1, 2} to {1, 2, 3}
sage: for f in M: print(f)
map: 1 -> 1, 2 -> 1
map: 1 -> 1, 2 -> 2
map: 1 -> 1, 2 -> 3
map: 1 -> 2, 2 -> 1
map: 1 -> 2, 2 -> 2
map: 1 -> 2, 2 -> 3
map: 1 -> 3, 2 -> 1
map: 1 -> 3, 2 -> 2
map: 1 -> 3, 2 -> 3

For n=3, m=2

sage: M = FiniteSetMaps([1 .. 3], [1 .. 2]); M
Maps from {1, 2, 3} to {1, 2}
sage: for f in M: print(f)
map: 1 -> 1, 2 -> 1, 3 -> 1
map: 1 -> 1, 2 -> 1, 3 -> 2
map: 1 -> 1, 2 -> 2, 3 -> 1
map: 1 -> 1, 2 -> 2, 3 -> 2
map: 1 -> 2, 2 -> 1, 3 -> 1
map: 1 -> 2, 2 -> 1, 3 -> 2 ...
(more)
slelievre gravatar imageslelievre ( 6 years ago )
1

answered 6 years ago

Emmanuel Charpentier gravatar image

updated 6 years ago

Disclaimer : I know zilch about combinatirics. This problem might have elegant (and already programmed) solutions among the combinatorics tools of Sage. However, the problem seems simple enough to attempt a "naïve" solution.

A function from X to Y is the set of tuples [(xi,yj),...,i=1,,n,j[0..n]], meaning that xi has yj as image, the notation (xi,y0) denoting the case where xi has no image. Let Sn,m the set of all functions from X to Y when X has n elements and Y has m elements.

1): the set S1,m is simply [(x1,yj),j=0,...,m].

2): knowing Sn,m, one can simply build Sn+1,m by adding (i. e. concatenating) to each of the functions in Sn,m a member of [(xn+1,yj),j=0,...,m]. In other words, S_{n+1,m}$ is the cartesian product of Sn,m and [(xn+1,yj),j=0,...,m].

3): 1) and 2) are sufficient to build Sn,m by recurrence on n. It results immediately that the cardinal is (m+1)n, including the degenerate case where no element of X has an image in Y.

The implementation, whose details depend of the planned use of the functions, is, as usual, left to the reader as an exercise ;-).

Preview: (hide)
link

Comments

Thanks for the theory!

Dox gravatar imageDox ( 6 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 6 years ago

Seen: 769 times

Last updated: Dec 04 '18