Ask Your Question
1

Unexpected result in the evaluation of a function

asked 2018-12-30 15:50:53 +0100

matthyuz gravatar image

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)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2018-12-30 18:20:00 +0100

rburing gravatar image

updated 2018-12-30 18:23:23 +0100

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.

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)
1
edit flag offensive delete link more

Comments

1

rburing 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.

matthyuz gravatar imagematthyuz ( 2018-12-30 18:53:04 +0100 )edit

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 2018-12-30 15:50:53 +0100

Seen: 257 times

Last updated: Dec 30 '18