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.Wed, 20 Jun 2018 16:16:28 -0500How to convert string to sage expression?http://ask.sagemath.org/question/42692/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?Wed, 20 Jun 2018 11:15:49 -0500http://ask.sagemath.org/question/42692/how-to-convert-string-to-sage-expression/Comment by nbruin for <p>In python one can use <code>parse_expr</code> to convert a string which contains valid sympy expression to an sympy expression.</p>
<p>I can't find how to do the same in sage. I tried <code>eval</code> but this works sometimes and not other times. Here is an example</p>
<pre><code> var('x')
sage: expr=sin(x)
sage: expr=str(expr)
sage: expr=eval(expr)
sin(x)
</code></pre>
<p>But not on this one</p>
<pre><code>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.
</code></pre>
<p>I do not know before hand the "type" of the expression, other than it is valid sage expression, but in a string. </p>
<p>I found I could do this</p>
<pre><code>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)
</code></pre>
<p>and it works. But this means I need to know before hand that 'x' symbol was there. Which is not possible for me.</p>
<p>I am looking for something similar to Mathematica's <code>ToExpression</code>described in
<a href="http://reference.wolfram.com/language/ref/ToExpression.html">http://reference.wolfram.com/language...</a> or python <code>parse_expr</code></p>
<p>I found answer here <a href="https://ask.sagemath.org/question/41135/converting-strings-into-expressions/">https://ask.sagemath.org/question/411...</a> using something called <code>SR</code> which I still do not understand, but it does not work for me.</p>
<pre><code>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)
</code></pre>
<p>So, how would one convert string that contains arbitrary sage valid expression to sage expression?</p>
http://ask.sagemath.org/question/42692/how-to-convert-string-to-sage-expression/?comment=42693#post-id-42693The 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.Wed, 20 Jun 2018 11:58:30 -0500http://ask.sagemath.org/question/42692/how-to-convert-string-to-sage-expression/?comment=42693#post-id-42693Answer by nbruin for <p>In python one can use <code>parse_expr</code> to convert a string which contains valid sympy expression to an sympy expression.</p>
<p>I can't find how to do the same in sage. I tried <code>eval</code> but this works sometimes and not other times. Here is an example</p>
<pre><code> var('x')
sage: expr=sin(x)
sage: expr=str(expr)
sage: expr=eval(expr)
sin(x)
</code></pre>
<p>But not on this one</p>
<pre><code>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.
</code></pre>
<p>I do not know before hand the "type" of the expression, other than it is valid sage expression, but in a string. </p>
<p>I found I could do this</p>
<pre><code>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)
</code></pre>
<p>and it works. But this means I need to know before hand that 'x' symbol was there. Which is not possible for me.</p>
<p>I am looking for something similar to Mathematica's <code>ToExpression</code>described in
<a href="http://reference.wolfram.com/language/ref/ToExpression.html">http://reference.wolfram.com/language...</a> or python <code>parse_expr</code></p>
<p>I found answer here <a href="https://ask.sagemath.org/question/41135/converting-strings-into-expressions/">https://ask.sagemath.org/question/411...</a> using something called <code>SR</code> which I still do not understand, but it does not work for me.</p>
<pre><code>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)
</code></pre>
<p>So, how would one convert string that contains arbitrary sage valid expression to sage expression?</p>
http://ask.sagemath.org/question/42692/how-to-convert-string-to-sage-expression/?answer=42694#post-id-42694If 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.Wed, 20 Jun 2018 12:02:59 -0500http://ask.sagemath.org/question/42692/how-to-convert-string-to-sage-expression/?answer=42694#post-id-42694Comment by nbruin for <p>If you're happy using the bindings that are available on top-level you can do</p>
<pre><code>sage_eval('exp(x)',locals=globals())
</code></pre>
<p>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.</p>
http://ask.sagemath.org/question/42692/how-to-convert-string-to-sage-expression/?comment=42699#post-id-42699I 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.Wed, 20 Jun 2018 16:16:28 -0500http://ask.sagemath.org/question/42692/how-to-convert-string-to-sage-expression/?comment=42699#post-id-42699Comment by John Palmieri for <p>If you're happy using the bindings that are available on top-level you can do</p>
<pre><code>sage_eval('exp(x)',locals=globals())
</code></pre>
<p>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.</p>
http://ask.sagemath.org/question/42692/how-to-convert-string-to-sage-expression/?comment=42697#post-id-42697I 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.Wed, 20 Jun 2018 13:08:24 -0500http://ask.sagemath.org/question/42692/how-to-convert-string-to-sage-expression/?comment=42697#post-id-42697Comment by Nasser for <p>If you're happy using the bindings that are available on top-level you can do</p>
<pre><code>sage_eval('exp(x)',locals=globals())
</code></pre>
<p>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.</p>
http://ask.sagemath.org/question/42692/how-to-convert-string-to-sage-expression/?comment=42695#post-id-42695The 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(x^2 + x + 1) + 1/6*log(x - 1)
sage: expr=str(expr)
sage: expr=sage_eval(expr,locals=globals())
now it hangs.Wed, 20 Jun 2018 12:14:48 -0500http://ask.sagemath.org/question/42692/how-to-convert-string-to-sage-expression/?comment=42695#post-id-42695