ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 10 Nov 2017 04:41:23 -0600define function on two setshttp://ask.sagemath.org/question/39472/define-function-on-two-sets/ Hi.
I have two sets: X, Y. I want to define a function
F : X ->Y
and I have the values of f(x). I searched the net but found nothing. thanks.Fri, 10 Nov 2017 02:53:26 -0600http://ask.sagemath.org/question/39472/define-function-on-two-sets/Comment by tmonteil for <p>Hi.
I have two sets: X, Y. I want to define a function</p>
<p>F : X ->Y</p>
<p>and I have the values of f(x). I searched the net but found nothing. thanks.</p>
http://ask.sagemath.org/question/39472/define-function-on-two-sets/?comment=39476#post-id-39476Could you please provide the sets and the function you want to deal with in Sage ?Fri, 10 Nov 2017 04:41:23 -0600http://ask.sagemath.org/question/39472/define-function-on-two-sets/?comment=39476#post-id-39476Comment by B r u n o for <p>Hi.
I have two sets: X, Y. I want to define a function</p>
<p>F : X ->Y</p>
<p>and I have the values of f(x). I searched the net but found nothing. thanks.</p>
http://ask.sagemath.org/question/39472/define-function-on-two-sets/?comment=39475#post-id-39475Your question is not very clear to me. I suppose that $X$ and $Y$ are finite sets. If you know a map from $X$ to $Y$ and you want to define it in SageMath, you may define a dictionary with keys $X$ and values $Y$ : define `d = dict()` and then `d[x] = y` for all pairs $(x,y)$ such that $y = f(x)$. If on the other hand you have $f$ defined in *some* way, such as a dictionary or something similar, and you want it truly as a function, you may write (I use the dictionary, known globally):
def f(x):
if x in d:
return d[x]
raise ValueError("{} is not in the domain of f".format(x))Fri, 10 Nov 2017 03:57:53 -0600http://ask.sagemath.org/question/39472/define-function-on-two-sets/?comment=39475#post-id-39475