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, 19 Jan 2017 11:03:46 -0600problems with sage_eval and evalhttp://ask.sagemath.org/question/36337/problems-with-sage_eval-and-eval/Dear all,
I am trying to construct and evaluate sage code, but what used to work in previous versions does not seem to work in SageMath 7.3 any more. Apparently `eval('opr(16*a^2*x^2)')` does not pre-parse the exponent properly:
var('a x')
term1 = 16*a^2*x^2
print (opr(term1))
print eval('opr(term1)')
print eval('opr(16*a^2*x^2)')
gives:
16*a^2*x^2
16*a^2*x^2
RuntimeError: Use ** for exponentiation, not '^', which means xor
in Python, and has the wrong precedence.
In contrast, if I use `sage_eval`, I get a different error:
term1 = 16*a^2*x^2
print (opr(term1))
print sage_eval('opr(term1)')
print sage_eval('opr(16*a^2*x^2)')
gives:
16*a^2*x^2
NameError: name 'opr' is not defined
What would be the correct way of evaluating a text string in a similar way as if it had been entered directly into a code cell?
Wed, 18 Jan 2017 07:30:26 -0600http://ask.sagemath.org/question/36337/problems-with-sage_eval-and-eval/Comment by stan for <p>Dear all,
I am trying to construct and evaluate sage code, but what used to work in previous versions does not seem to work in SageMath 7.3 any more. Apparently <code>eval('opr(16*a^2*x^2)')</code> does not pre-parse the exponent properly:</p>
<pre><code>var('a x')
term1 = 16*a^2*x^2
print (opr(term1))
print eval('opr(term1)')
print eval('opr(16*a^2*x^2)')
</code></pre>
<p>gives:</p>
<pre><code>16*a^2*x^2
16*a^2*x^2
RuntimeError: Use ** for exponentiation, not '^', which means xor
in Python, and has the wrong precedence.
</code></pre>
<p>In contrast, if I use <code>sage_eval</code>, I get a different error:</p>
<pre><code>term1 = 16*a^2*x^2
print (opr(term1))
print sage_eval('opr(term1)')
print sage_eval('opr(16*a^2*x^2)')
</code></pre>
<p>gives:</p>
<pre><code>16*a^2*x^2
NameError: name 'opr' is not defined
</code></pre>
<p>What would be the correct way of evaluating a text string in a similar way as if it had been entered directly into a code cell?</p>
http://ask.sagemath.org/question/36337/problems-with-sage_eval-and-eval/?comment=36349#post-id-36349Sorry for the missing information. Jupyter with SageMath 7.3 kernel, own machine. `opr` was an operator, and I actually 'reduced' the problem to an incomplete and meaningless one. Thierry's response below solved it, though.Thu, 19 Jan 2017 11:03:46 -0600http://ask.sagemath.org/question/36337/problems-with-sage_eval-and-eval/?comment=36349#post-id-36349Comment by slelievre for <p>Dear all,
I am trying to construct and evaluate sage code, but what used to work in previous versions does not seem to work in SageMath 7.3 any more. Apparently <code>eval('opr(16*a^2*x^2)')</code> does not pre-parse the exponent properly:</p>
<pre><code>var('a x')
term1 = 16*a^2*x^2
print (opr(term1))
print eval('opr(term1)')
print eval('opr(16*a^2*x^2)')
</code></pre>
<p>gives:</p>
<pre><code>16*a^2*x^2
16*a^2*x^2
RuntimeError: Use ** for exponentiation, not '^', which means xor
in Python, and has the wrong precedence.
</code></pre>
<p>In contrast, if I use <code>sage_eval</code>, I get a different error:</p>
<pre><code>term1 = 16*a^2*x^2
print (opr(term1))
print sage_eval('opr(term1)')
print sage_eval('opr(16*a^2*x^2)')
</code></pre>
<p>gives:</p>
<pre><code>16*a^2*x^2
NameError: name 'opr' is not defined
</code></pre>
<p>What would be the correct way of evaluating a text string in a similar way as if it had been entered directly into a code cell?</p>
http://ask.sagemath.org/question/36337/problems-with-sage_eval-and-eval/?comment=36339#post-id-36339Are you working in the Sage REPL, in the Sage Notebook, in the Jupyter notebook? If you are working in the Jupyter notebook, are you using the Python2 kernel, or the SageMath kernel? Are you working on your own machine, or in SageMathCloud? What version of Sage were you using previously where your code was working?Wed, 18 Jan 2017 08:53:19 -0600http://ask.sagemath.org/question/36337/problems-with-sage_eval-and-eval/?comment=36339#post-id-36339Comment by kcrisman for <p>Dear all,
I am trying to construct and evaluate sage code, but what used to work in previous versions does not seem to work in SageMath 7.3 any more. Apparently <code>eval('opr(16*a^2*x^2)')</code> does not pre-parse the exponent properly:</p>
<pre><code>var('a x')
term1 = 16*a^2*x^2
print (opr(term1))
print eval('opr(term1)')
print eval('opr(16*a^2*x^2)')
</code></pre>
<p>gives:</p>
<pre><code>16*a^2*x^2
16*a^2*x^2
RuntimeError: Use ** for exponentiation, not '^', which means xor
in Python, and has the wrong precedence.
</code></pre>
<p>In contrast, if I use <code>sage_eval</code>, I get a different error:</p>
<pre><code>term1 = 16*a^2*x^2
print (opr(term1))
print sage_eval('opr(term1)')
print sage_eval('opr(16*a^2*x^2)')
</code></pre>
<p>gives:</p>
<pre><code>16*a^2*x^2
NameError: name 'opr' is not defined
</code></pre>
<p>What would be the correct way of evaluating a text string in a similar way as if it had been entered directly into a code cell?</p>
http://ask.sagemath.org/question/36337/problems-with-sage_eval-and-eval/?comment=36338#post-id-36338The errors are both correct. Do you want a Python cell or a Sage cell? What is `opr`? Maybe you need to import a Python library first in the Sage case.Wed, 18 Jan 2017 08:44:42 -0600http://ask.sagemath.org/question/36337/problems-with-sage_eval-and-eval/?comment=36338#post-id-36338Answer by tmonteil for <p>Dear all,
I am trying to construct and evaluate sage code, but what used to work in previous versions does not seem to work in SageMath 7.3 any more. Apparently <code>eval('opr(16*a^2*x^2)')</code> does not pre-parse the exponent properly:</p>
<pre><code>var('a x')
term1 = 16*a^2*x^2
print (opr(term1))
print eval('opr(term1)')
print eval('opr(16*a^2*x^2)')
</code></pre>
<p>gives:</p>
<pre><code>16*a^2*x^2
16*a^2*x^2
RuntimeError: Use ** for exponentiation, not '^', which means xor
in Python, and has the wrong precedence.
</code></pre>
<p>In contrast, if I use <code>sage_eval</code>, I get a different error:</p>
<pre><code>term1 = 16*a^2*x^2
print (opr(term1))
print sage_eval('opr(term1)')
print sage_eval('opr(16*a^2*x^2)')
</code></pre>
<p>gives:</p>
<pre><code>16*a^2*x^2
NameError: name 'opr' is not defined
</code></pre>
<p>What would be the correct way of evaluating a text string in a similar way as if it had been entered directly into a code cell?</p>
http://ask.sagemath.org/question/36337/problems-with-sage_eval-and-eval/?answer=36345#post-id-36345Regarding the first error, it is fortunate that Sage does not preparse what is inside a string (if it was the case before 7.3, then it was a huge bug). If you want your code withing a string to be preparsed, just use the `preparse` Sage function, for example:
sage: var('a x')
(a, x)
sage: print eval(preparse('16*a^2*x^2'))
16*a^2*x^2
Regarding your second error, well, `opr` is indeed not defined. Note that you have to pass some context to `sage_eval`:
sage: var('a x')
(a, x)
sage: sage_eval('16*a^2*x^2')
NameError: name 'a' is not defined
sage: sage_eval('16*a^2*x^2', locals={'x':x,'a':a})
16*a^2*x^2Thu, 19 Jan 2017 05:17:52 -0600http://ask.sagemath.org/question/36337/problems-with-sage_eval-and-eval/?answer=36345#post-id-36345Comment by stan for <p>Regarding the first error, it is fortunate that Sage does not preparse what is inside a string (if it was the case before 7.3, then it was a huge bug). If you want your code withing a string to be preparsed, just use the <code>preparse</code> Sage function, for example:</p>
<pre><code>sage: var('a x')
(a, x)
sage: print eval(preparse('16*a^2*x^2'))
16*a^2*x^2
</code></pre>
<p>Regarding your second error, well, <code>opr</code> is indeed not defined. Note that you have to pass some context to <code>sage_eval</code>:</p>
<pre><code>sage: var('a x')
(a, x)
sage: sage_eval('16*a^2*x^2')
NameError: name 'a' is not defined
sage: sage_eval('16*a^2*x^2', locals={'x':x,'a':a})
16*a^2*x^2
</code></pre>
http://ask.sagemath.org/question/36337/problems-with-sage_eval-and-eval/?comment=36348#post-id-36348Oops, sorry, I forgot a few lines when trying to reduce the problem to a minimal example...
The `preparse()` tip solved my problem. I also didn't realise that sage_eval does not use the global namespace. Thanks!!Thu, 19 Jan 2017 11:01:25 -0600http://ask.sagemath.org/question/36337/problems-with-sage_eval-and-eval/?comment=36348#post-id-36348