Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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?