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.Mon, 01 Jan 2018 22:30:58 +0100Delayed assignmenthttps://ask.sagemath.org/question/40299/delayed-assignment/Hi!
I'm new in Sage and I'm trying with the following input:
a=2;
x=a;
a=3;
x
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!Tue, 26 Dec 2017 12:06:31 +0100https://ask.sagemath.org/question/40299/delayed-assignment/Answer by Emmanuel Charpentier for <p>Hi!</p>
<p>I'm new in Sage and I'm trying with the following input:</p>
<pre><code>a=2;
x=a;
a=3;
x
</code></pre>
<p>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.</p>
<p>Thank you so much in advance!</p>
https://ask.sagemath.org/question/40299/delayed-assignment/?answer=40303#post-id-40303(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
2
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
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
y
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()
0.9439976532284053
sage: a()
0.7276353240432383
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.
HTH,Tue, 26 Dec 2017 15:40:49 +0100https://ask.sagemath.org/question/40299/delayed-assignment/?answer=40303#post-id-40303Comment by dg.aragones for <p>(Very roughly) : in Python (hence in Sage), a (Python) variable is a label for an object</p>
<pre><code>sage: a=2
</code></pre>
<p>makes the label <code>a</code> point to the object 2</p>
<pre><code>sage: x=a
</code></pre>
<p>makes the label <code>x</code> point to the same object as the label <code>a</code>, _i. e._ the object 2</p>
<pre><code>sage: a=3
</code></pre>
<p>makes the label <code>a</code> point to the object 3. But x still points to the object 2 :</p>
<pre><code>sage: x
2
</code></pre>
<p>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 :</p>
<pre><code>sage: reset() # Roughly : start again...
sage: x
x
</code></pre>
<p>That is how Sage's symbolic variables are printed : by their name, wich, by default, is also given to the (Python) variable</p>
<pre><code>sage: a=x
</code></pre>
<p><code>a</code> is now a Python variable, i. e. a label pointing to the Sage symbolic variable whose name is "x"</p>
<pre><code>sage: z=SR.var("y")
sage: z
y
</code></pre>
<p>Indeed : z is a Python variable, i. e. a label for the Sage symbolic variable whose symbolic name is "y".</p>
<p>Furthermore :</p>
<pre><code>sage: y
...
NameError: name 'y' is not defined
</code></pre>
<p>Indeed, you have never defined a Python variable named <code>y</code>.</p>
<p>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) :</p>
<pre><code>sage: def a(): return random()
sage: a()
0.9439976532284053
sage: a()
0.7276353240432383
</code></pre>
<p>But you have to use the "function call" syntax :</p>
<pre><code>sage: a
<function a at 0x7f555e54d668>
</code></pre>
<p>There might be some Pythonistic way to "hide" a function call in a variable name, bit, if so, I do not know it.</p>
<p>A bit more about Python and symbolic variables might come in handy. When you do :</p>
<pre><code>sage: var("t, u, v")
</code></pre>
<p>you are, in fact, using a shortcut for :</p>
<pre><code>sage:t, u, v = SR.var("t, u, v")
</code></pre>
<p>A cause of further confusion is that, in a "virgin" interpreter, the Python variable <code>x</code> is, by default, a label to the Sage symbolic variable whose symbolic name is "x" (it is the only one in this case...).</p>
<p>This shortcut, supposed to "help beginners", has been grandfathered, but may have trapped quite a lot of not-totally-beginners...</p>
<p>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...</p>
<p>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 <code>solve</code> and <code>desolve</code>, 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.</p>
<p>HTH,</p>
https://ask.sagemath.org/question/40299/delayed-assignment/?comment=40430#post-id-40430Thank you so much for your answer!Mon, 01 Jan 2018 22:30:58 +0100https://ask.sagemath.org/question/40299/delayed-assignment/?comment=40430#post-id-40430