ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 30 Dec 2018 18:53:04 +0100Unexpected result in the evaluation of a functionhttps://ask.sagemath.org/question/44811/unexpected-result-in-the-evaluation-of-a-function/I hope my question isn't stupid.
Why does the following code result in 1 and not 3?
var('a b')
f(a, b) = gcd(a, b)
f(3, 6)Sun, 30 Dec 2018 15:50:53 +0100https://ask.sagemath.org/question/44811/unexpected-result-in-the-evaluation-of-a-function/Answer by rburing for <p>I hope my question isn't stupid.
Why does the following code result in 1 and not 3?</p>
<pre><code>var('a b')
f(a, b) = gcd(a, b)
f(3, 6)
</code></pre>
https://ask.sagemath.org/question/44811/unexpected-result-in-the-evaluation-of-a-function/?answer=44812#post-id-44812Your question is very much valid.
This notation to define a function (which is specific to Sage, and which creates a kind of function object) sadly does not seem to behave well except in the simplest cases. This is a bug.
I recommend not using this function notation. Instead, for one-line functions try (as in Python):
f = lambda a, b: gcd(a, b)
and for more complicated ones:
def f(a, b):
# possibly more statements here
return gcd(a, b)
By the way, the result you got probably stems from the following:
sage: var('a b')
(a, b)
sage: gcd(a, b)
1Sun, 30 Dec 2018 18:20:00 +0100https://ask.sagemath.org/question/44811/unexpected-result-in-the-evaluation-of-a-function/?answer=44812#post-id-44812Comment by matthyuz for <p>Your question is very much valid.
This notation to define a function (which is specific to Sage, and which creates a kind of function object) sadly does not seem to behave well except in the simplest cases. This is a bug.</p>
<p>I recommend not using this function notation. Instead, for one-line functions try (as in Python):</p>
<pre><code>f = lambda a, b: gcd(a, b)
</code></pre>
<p>and for more complicated ones:</p>
<pre><code>def f(a, b):
# possibly more statements here
return gcd(a, b)
</code></pre>
<p>By the way, the result you got probably stems from the following:</p>
<pre><code>sage: var('a b')
(a, b)
sage: gcd(a, b)
1
</code></pre>
https://ask.sagemath.org/question/44811/unexpected-result-in-the-evaluation-of-a-function/?comment=44813#post-id-44813rburing thank you very much for your reply.
Are there any intentions to repair that error? The truth is that it is very frustrating for someone who is just taking their first steps in Sage like me.Sun, 30 Dec 2018 18:53:04 +0100https://ask.sagemath.org/question/44811/unexpected-result-in-the-evaluation-of-a-function/?comment=44813#post-id-44813