Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to convert string to sage expression?

In python one can use parse_expr to convert a string which contains valid sympy expression to an sympy expression.

I can't find how to do the same in sage. I tried eval but this works sometimes and not other times. Here is an example

   var('x')
   sage: expr=sin(x)
   sage: expr=str(expr)
   sage: expr=eval(expr)
         sin(x)

But not on this one

sage: expr=-1/2*x/(x^2 + 1)
sage: expr=str(expr)
sage: expr=eval(expr)
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-16-599d1fa18625> in <module>()
----> 1 expr=eval(expr)

<string> in <module>()

/usr/lib/python2.7/site-packages/sage/structure/element.pyx in sage.structure.element.Element.__xor__ (build/cythonized/sage/structure/element.c:8986)()
    951 
    952     def __xor__(self, right):
--> 953         raise RuntimeError("Use ** for exponentiation, not '^', which means xor\n"+\
    954               "in Python, and has the wrong precedence.")
    955 

RuntimeError: Use ** for exponentiation, not '^', which means xor
in Python, and has the wrong precedence.

I do not know before hand the "type" of the expression, other than it is valid sage expression, but in a string.

I am looking for something similar to Mathematica's ToExpression described in http://reference.wolfram.com/language/ref/ToExpression.html or python parse_expr

I found answer here https://ask.sagemath.org/question/41135/converting-strings-into-expressions/ using something called SR which I still do not understand, but it does not work for me.

sage: expr=integrate(x^2*(sqrt(x^2 + 1) - 2)/((x^3 - (x^2 + 1)^(3/2) - 1)*sqrt(x^2 + 1)), x)
sage: expr=str(expr)
sage: expr=SR(expr)

TypeError                                 Traceback (most recent call last)

So, how would one convert string that contains arbitrary sage valid expression to sage expression?

How to convert string to sage expression?

In python one can use parse_expr to convert a string which contains valid sympy expression to an sympy expression.

I can't find how to do the same in sage. I tried eval but this works sometimes and not other times. Here is an example

   var('x')
   sage: expr=sin(x)
   sage: expr=str(expr)
   sage: expr=eval(expr)
         sin(x)

But not on this one

sage: expr=-1/2*x/(x^2 + 1)
sage: expr=str(expr)
sage: expr=eval(expr)
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-16-599d1fa18625> in <module>()
----> 1 expr=eval(expr)

<string> in <module>()

/usr/lib/python2.7/site-packages/sage/structure/element.pyx in sage.structure.element.Element.__xor__ (build/cythonized/sage/structure/element.c:8986)()
    951 
    952     def __xor__(self, right):
--> 953         raise RuntimeError("Use ** for exponentiation, not '^', which means xor\n"+\
    954               "in Python, and has the wrong precedence.")
    955 

RuntimeError: Use ** for exponentiation, not '^', which means xor
in Python, and has the wrong precedence.

I do not know before hand the "type" of the expression, other than it is valid sage expression, but in a string.

I found I could do this

sage: var('x')
sage: expr=-1/2*x/(x^2 + 1)
sage: expr=str(expr)
sage: expr=sage_eval(expr,locals={'x':x})
sage: expr
-1/2*x/(x^2 + 1)

and it works. But this means I need to know before hand that 'x' symbol was there. Which is not possible for me.

I am looking for something similar to Mathematica's ToExpression described in http://reference.wolfram.com/language/ref/ToExpression.html or python parse_expr

I found answer here https://ask.sagemath.org/question/41135/converting-strings-into-expressions/ using something called SR which I still do not understand, but it does not work for me.

sage: expr=integrate(x^2*(sqrt(x^2 + 1) - 2)/((x^3 - (x^2 + 1)^(3/2) - 1)*sqrt(x^2 + 1)), x)
sage: expr=str(expr)
sage: expr=SR(expr)

TypeError                                 Traceback (most recent call last)

So, how would one convert string that contains arbitrary sage valid expression to sage expression?