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?
The 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.Are 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?
Sorry 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.