Ask Your Question
1

How to convert string to sage expression?

asked 2018-06-20 18:15:49 +0100

Nasser gravatar image

updated 2018-06-20 18:39:27 +0100

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 ToExpressiondescribed in http://reference.wolfram.com/language... or python parse_expr

I found answer here https://ask.sagemath.org/question/411... 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?

edit retag flag offensive close merge delete

Comments

The fact that

SR('integrate(exp(x),x)')

fails but

SR('integral(exp(x),x)')

succeeds is probably something that can be fixed: integrate should be able to do indefinite integrals as well. I think you can file an enhancement ticket for that. That's going to be easy to do.

nbruin gravatar imagenbruin ( 2018-06-20 18:58:30 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2018-06-20 19:02:59 +0100

nbruin gravatar image

If you're happy using the bindings that are available on top-level you can do

sage_eval('exp(x)',locals=globals())

The semantics of sage/python are simply not consistent with assuming that any unbound identifier is a symbolic variable. You could write something yourself, first use python to parse the string, collect all the names occurring and look up if those are bound, and insert bindings for the ones that are not.

edit flag offensive delete link more

Comments

The above I am afraid does not work in all cases. Here is an example

sage: var('x')
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
-1/2*x/(x^2 + 1) + 1/2*arctan(x) + integrate(-1/2*(3*x^10 - 4*x^9 + 5*x^8 - 2*x^7 + 15*x^6 + 6*x^5 + 9*x^4)/(2*x^13 + 7*x^11 - 4*x^10 + 11*x^9 - 11*x^8 + 13*x^7 - 13*x^6 + 11*x^5 - 11*x^4 + 4*x^3 - 7*x^2 - 2*(x^12 + 3*x^10 - 2*x^9 + 3*x^8 - 6*x^7 + 2*x^6 - 6*x^5 + 3*x^4 - 2*x^3 + 3*x^2 + 1)*sqrt(x^2 + 1) - 2), x) + 1/6*log ...
(more)
Nasser gravatar imageNasser ( 2018-06-20 19:14:48 +0100 )edit

I think this in fact does work in response to your original question: it converts a string which contains a valid Sage expression to a Sage expression. There is a separate problem that the command integrate(...) in the definition of expr does not produce such a string.

John Palmieri gravatar imageJohn Palmieri ( 2018-06-20 20:08:24 +0100 )edit

I don't think this is a problem with sage_eval. If I enter the value of expr directly into sage, it hangs too. It seems it's just a nasty integral for maxima.

nbruin gravatar imagenbruin ( 2018-06-20 23:16:28 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2018-06-20 18:15:49 +0100

Seen: 1,596 times

Last updated: Jun 20 '18