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.Thu, 26 Sep 2013 07:55:06 -0500Is there a way to update an expression with new variable values?http://ask.sagemath.org/question/10087/is-there-a-way-to-update-an-expression-with-new-variable-values/Is there a way to update an expression if the symbolic variables that it contains have been overwritten? For example,
sage: var('beta')
beta
sage: eq = x == beta
sage: beta = 1
sage: eq
x == beta
Is there a way for it to change to `x == 1`? I wrote the following function to do this, but I wanted to know if something similar existed already.
def update(expr):
return expr.subs(dict(zip(expr.variables(), map(lambda v:eval(str(v)), expr.variables()))))Fri, 03 May 2013 13:02:11 -0500http://ask.sagemath.org/question/10087/is-there-a-way-to-update-an-expression-with-new-variable-values/Answer by niles for <p>Is there a way to update an expression if the symbolic variables that it contains have been overwritten? For example,</p>
<pre><code>sage: var('beta')
beta
sage: eq = x == beta
sage: beta = 1
sage: eq
x == beta
</code></pre>
<p>Is there a way for it to change to <code>x == 1</code>? I wrote the following function to do this, but I wanted to know if something similar existed already.</p>
<pre><code>def update(expr):
return expr.subs(dict(zip(expr.variables(), map(lambda v:eval(str(v)), expr.variables()))))
</code></pre>
http://ask.sagemath.org/question/10087/is-there-a-way-to-update-an-expression-with-new-variable-values/?answer=14905#post-id-14905The issue is that there is a difference between variables and "symbolic variables". A variable is a string of letters used to store some value. Every programming language has them. A "symbolic variable" is a Sage object that can be manipulated in certain ways. Just like other Sage objects, symbolic variables can be referenced by variable names. When you type `var('beta')`, Sage defines a new symbolic variable whose name is beta, and *also* defines a new standard variable, also named beta, and makes the standard variable reference the symbolic variable.
But here's the issue: You can later change this standard variable to refer to some other Sage object. And that's what you're doing when you write `beta = 1`.
When you defined `eq`, you were using the earlier object (that is, the symbolic variable) stored in the standard variable `beta`. Storing something new with the name `beta` doesn't change this.
Here's another example where I first use the variable `B` to store the value 1, then use it to define an equation, and then store a new value in that variable. Doing so doesn't change the equation.
sage: B = 1
sage: eq = x == B
sage: eq
x == 1
sage: B = 2
sage: eq
x == 1
Incidentally, this is why the "dictionary approach" in the previous answer doesn't work: The dictionary being used in that case is equivalent to `{1:1}`, and it's key doesn't match any of the symbolic variables in the equation.
Thu, 09 May 2013 04:29:07 -0500http://ask.sagemath.org/question/10087/is-there-a-way-to-update-an-expression-with-new-variable-values/?answer=14905#post-id-14905Comment by fidbc for <p>The issue is that there is a difference between variables and "symbolic variables". A variable is a string of letters used to store some value. Every programming language has them. A "symbolic variable" is a Sage object that can be manipulated in certain ways. Just like other Sage objects, symbolic variables can be referenced by variable names. When you type <code>var('beta')</code>, Sage defines a new symbolic variable whose name is beta, and <em>also</em> defines a new standard variable, also named beta, and makes the standard variable reference the symbolic variable.</p>
<p>But here's the issue: You can later change this standard variable to refer to some other Sage object. And that's what you're doing when you write <code>beta = 1</code>.</p>
<p>When you defined <code>eq</code>, you were using the earlier object (that is, the symbolic variable) stored in the standard variable <code>beta</code>. Storing something new with the name <code>beta</code> doesn't change this.</p>
<p>Here's another example where I first use the variable <code>B</code> to store the value 1, then use it to define an equation, and then store a new value in that variable. Doing so doesn't change the equation.</p>
<pre><code>sage: B = 1
sage: eq = x == B
sage: eq
x == 1
sage: B = 2
sage: eq
x == 1
</code></pre>
<p>Incidentally, this is why the "dictionary approach" in the previous answer doesn't work: The dictionary being used in that case is equivalent to <code>{1:1}</code>, and it's key doesn't match any of the symbolic variables in the equation.</p>
http://ask.sagemath.org/question/10087/is-there-a-way-to-update-an-expression-with-new-variable-values/?comment=17724#post-id-17724very insightful, thanks for the detailed explanation!Thu, 09 May 2013 13:52:33 -0500http://ask.sagemath.org/question/10087/is-there-a-way-to-update-an-expression-with-new-variable-values/?comment=17724#post-id-17724Answer by fidbc for <p>Is there a way to update an expression if the symbolic variables that it contains have been overwritten? For example,</p>
<pre><code>sage: var('beta')
beta
sage: eq = x == beta
sage: beta = 1
sage: eq
x == beta
</code></pre>
<p>Is there a way for it to change to <code>x == 1</code>? I wrote the following function to do this, but I wanted to know if something similar existed already.</p>
<pre><code>def update(expr):
return expr.subs(dict(zip(expr.variables(), map(lambda v:eval(str(v)), expr.variables()))))
</code></pre>
http://ask.sagemath.org/question/10087/is-there-a-way-to-update-an-expression-with-new-variable-values/?answer=14880#post-id-14880How about this:
sage: eq.subs(beta=beta)
x == 1
Something strange is that the "dictionary approach" does not work:
sage: eq.subs({beta:beta})
x == betaFri, 03 May 2013 15:25:47 -0500http://ask.sagemath.org/question/10087/is-there-a-way-to-update-an-expression-with-new-variable-values/?answer=14880#post-id-14880Comment by MvG for <p>How about this:</p>
<pre><code>sage: eq.subs(beta=beta)
x == 1
</code></pre>
<p>Something strange is that the "dictionary approach" does not work:</p>
<pre><code>sage: eq.subs({beta:beta})
x == beta
</code></pre>
http://ask.sagemath.org/question/10087/is-there-a-way-to-update-an-expression-with-new-variable-values/?comment=17686#post-id-17686Not so strange: the Python variable `beta` at that point does not contain the symbolic variable `beta` any more, but instead contains the value 1. Where key names in the first approach are interpreted as strings naming symbolic variables, in the second approach they are treated as Python expressions. You could have saved the old meaning of `beta` by writing `beta_var = beta` before the `beta = 1`. Then you could use `{beta_var: beta}` to update the expression.Wed, 15 May 2013 07:06:46 -0500http://ask.sagemath.org/question/10087/is-there-a-way-to-update-an-expression-with-new-variable-values/?comment=17686#post-id-17686Comment by nbruin for <p>How about this:</p>
<pre><code>sage: eq.subs(beta=beta)
x == 1
</code></pre>
<p>Something strange is that the "dictionary approach" does not work:</p>
<pre><code>sage: eq.subs({beta:beta})
x == beta
</code></pre>
http://ask.sagemath.org/question/10087/is-there-a-way-to-update-an-expression-with-new-variable-values/?comment=16982#post-id-16982the dictionary approach does work if you spell it
sage: eq.subs({SR('beta'): beta})
Here `SR('beta')` creates the symbolic variable named `beta` independent of what
the python name `beta` is bound to.
Thu, 26 Sep 2013 07:55:06 -0500http://ask.sagemath.org/question/10087/is-there-a-way-to-update-an-expression-with-new-variable-values/?comment=16982#post-id-16982