Ask Your Question

Delayed assignment

asked 2017-12-26 05:06:31 -0500

dg.aragones gravatar image

updated 2018-01-01 15:31:29 -0500


I'm new in Sage and I'm trying with the following input:


Then, the output isn't updated. Is there any way to set a delayed assignment? I'm talking about something similar to the ':=' form of assignment in Mathematica. Note that with a delayed assignment isn't required to define 'x' as a function.

Thank you so much in advance!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-12-26 08:40:49 -0500

Emmanuel Charpentier gravatar image

updated 2017-12-26 09:36:40 -0500

(Very roughly) : in Python (hence in Sage), a (Python) variable is a label for an object

sage: a=2

makes the label a point to the object 2

sage: x=a

makes the label x point to the same object as the label a, _i. e._ the object 2

sage: a=3

makes the label a point to the object 3. But x still points to the object 2 :

sage: x

Do not confuse Python variables (i. e. labels to objects) with Sage's symbolic variables, which are objects recognized as (mathematical) variable in symbolic expressions :

sage: reset() # Roughly : start again...
sage: x

That is how Sage's symbolic variables are printed : by their name, wich, by default, is also given to the (Python) variable

sage: a=x

a is now a Python variable, i. e. a label pointing to the Sage symbolic variable whose name is "x"

sage: z=SR.var("y")
sage: z

Indeed : z is a Python variable, i. e. a label for the Sage symbolic variable whose symbolic name is "y".

Furthermore :

sage: y
NameError: name 'y' is not defined

Indeed, you have never defined a Python variable named y.

If you want a "delayed assignment", _i. e._ a symbol for something evaluated each time it is used, you have to define either a symbolic function (this must have an argument and can be done but using only symbolic functions) or a Python function (which can be defined with no argument and use any function) :

sage: def a(): return random()
sage: a()
sage: a()

But you have to use the "function call" syntax :

sage: a
<function a at 0x7f555e54d668>

There might be some Pythonistic way to "hide" a function call in a variable name, bit, if so, I do not know it.

A bit more about Python and symbolic variables might come in handy. When you do :

sage: var("t, u, v")

you are, in fact, using a shortcut for :

sage:t, u, v = SR.var("t, u, v")

A cause of further confusion is that, in a "virgin" interpreter, the Python variable x is, by default, a label to the Sage symbolic variable whose symbolic name is "x" (it is the only one in this case...).

This shortcut, supposed to "help beginners", has been grandfathered, but may have trapped quite a lot of not-totally-beginners...

The distinction between Python variables (i. e. computer objects, part of programs) and symbolic variables (i. e. one of the many types of objects programs act, _i. e._ data) is characteristic (some would say pathognomonic ,-) of Sage (and SymPy). This helps programming. Really...

The only problem is that some other systems, such as Maxima or Mathematica, do not do this distinction ; when interacting with such system, they are apt to return to you objects containing variables that have never been declared, and to which no Python variable points to (Maxima's solve and desolve, which Sage uses by default, are chronic offenders...). The Sage user has to to search the names of symbiloc variables present in the returned object(s), and declare those not already known.


edit flag offensive delete link more


Thank you so much for your answer!

dg.aragones gravatar imagedg.aragones ( 2018-01-01 15:30:58 -0500 )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


Asked: 2017-12-26 05:06:31 -0500

Seen: 65 times

Last updated: Jan 01