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.Sat, 25 Jan 2014 11:49:22 -0600Difficulties evaluating an expression stringhttp://ask.sagemath.org/question/10959/difficulties-evaluating-an-expression-string/For some recent computations, I devolved some initial calculus to the Maxima sub-system, and I've ended up with a list `eqs` of Maxima expressions. I want to transfer this list out of Maxima. This works:
sage: eqs2 = [repr(xx) for xx in eqs]
but gives me strings, which are not useful. And this:
sage: eqs2 = [sage_eval(repr(xx)) for xx in eqs]
returns the error message
NameError: name 'a43' is not defined
which is strange, as it is defined as a variable:
sage: a43
a43
I could create a list of local variables:
vars = {'a21':a21,'a31':a31}
and so on, and use
sage_eval(xx,vars)
but as there are 20 more variables, this would be tedious.
Is there a better way?
Fri, 24 Jan 2014 13:17:42 -0600http://ask.sagemath.org/question/10959/difficulties-evaluating-an-expression-string/Comment by Alasdair for <p>For some recent computations, I devolved some initial calculus to the Maxima sub-system, and I've ended up with a list <code>eqs</code> of Maxima expressions. I want to transfer this list out of Maxima. This works:</p>
<pre><code>sage: eqs2 = [repr(xx) for xx in eqs]
</code></pre>
<p>but gives me strings, which are not useful. And this:</p>
<pre><code>sage: eqs2 = [sage_eval(repr(xx)) for xx in eqs]
</code></pre>
<p>returns the error message</p>
<pre><code>NameError: name 'a43' is not defined
</code></pre>
<p>which is strange, as it is defined as a variable:</p>
<pre><code>sage: a43
a43
</code></pre>
<p>I could create a list of local variables:</p>
<pre><code>vars = {'a21':a21,'a31':a31}
</code></pre>
<p>and so on, and use</p>
<pre><code>sage_eval(xx,vars)
</code></pre>
<p>but as there are 20 more variables, this would be tedious.</p>
<p>Is there a better way?</p>
http://ask.sagemath.org/question/10959/difficulties-evaluating-an-expression-string/?comment=16393#post-id-16393Thanks very much for your answers: I wasn't aware that the `locals` list could be used like this.Sat, 25 Jan 2014 11:49:22 -0600http://ask.sagemath.org/question/10959/difficulties-evaluating-an-expression-string/?comment=16393#post-id-16393Answer by Luca for <p>For some recent computations, I devolved some initial calculus to the Maxima sub-system, and I've ended up with a list <code>eqs</code> of Maxima expressions. I want to transfer this list out of Maxima. This works:</p>
<pre><code>sage: eqs2 = [repr(xx) for xx in eqs]
</code></pre>
<p>but gives me strings, which are not useful. And this:</p>
<pre><code>sage: eqs2 = [sage_eval(repr(xx)) for xx in eqs]
</code></pre>
<p>returns the error message</p>
<pre><code>NameError: name 'a43' is not defined
</code></pre>
<p>which is strange, as it is defined as a variable:</p>
<pre><code>sage: a43
a43
</code></pre>
<p>I could create a list of local variables:</p>
<pre><code>vars = {'a21':a21,'a31':a31}
</code></pre>
<p>and so on, and use</p>
<pre><code>sage_eval(xx,vars)
</code></pre>
<p>but as there are 20 more variables, this would be tedious.</p>
<p>Is there a better way?</p>
http://ask.sagemath.org/question/10959/difficulties-evaluating-an-expression-string/?answer=15965#post-id-15965`sage_eval` does not have access by default to the variables declared in your session. Use the `locals` keword:
sage: eqs2 = [sage_eval(repr(xx), locals=globals()) for xx in eqs]
Depending on what you want to do, it may be more appropriate to use `locals()` instead of `globals()`. See <http://stackoverflow.com/questions/7969949/whats-the-difference-between-globals-locals-and-vars>.
Sat, 25 Jan 2014 07:57:14 -0600http://ask.sagemath.org/question/10959/difficulties-evaluating-an-expression-string/?answer=15965#post-id-15965Answer by ndomes for <p>For some recent computations, I devolved some initial calculus to the Maxima sub-system, and I've ended up with a list <code>eqs</code> of Maxima expressions. I want to transfer this list out of Maxima. This works:</p>
<pre><code>sage: eqs2 = [repr(xx) for xx in eqs]
</code></pre>
<p>but gives me strings, which are not useful. And this:</p>
<pre><code>sage: eqs2 = [sage_eval(repr(xx)) for xx in eqs]
</code></pre>
<p>returns the error message</p>
<pre><code>NameError: name 'a43' is not defined
</code></pre>
<p>which is strange, as it is defined as a variable:</p>
<pre><code>sage: a43
a43
</code></pre>
<p>I could create a list of local variables:</p>
<pre><code>vars = {'a21':a21,'a31':a31}
</code></pre>
<p>and so on, and use</p>
<pre><code>sage_eval(xx,vars)
</code></pre>
<p>but as there are 20 more variables, this would be tedious.</p>
<p>Is there a better way?</p>
http://ask.sagemath.org/question/10959/difficulties-evaluating-an-expression-string/?answer=15948#post-id-15948Let sage_eval evaluate in the namespace of the locals dictionary.
expr = sage_eval(xx,locals=locals())
Or do you need something like this:
var_list =['a'+str(k) for k in (11..20)]
vars = dict([(v,var(v)) for v in var_list])
sage_eval('2*a11 +a14^a13-a12',locals=vars)Sat, 25 Jan 2014 07:55:24 -0600http://ask.sagemath.org/question/10959/difficulties-evaluating-an-expression-string/?answer=15948#post-id-15948