ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 20 Nov 2018 19:03:54 +0100Converting strings into expressionshttps://ask.sagemath.org/question/41135/converting-strings-into-expressions/Hey everyone,
This is probably pretty straightforward, but I couldn't find any straightforward answers with google searching. I'm looking for a function that works in the same way as python's str() and float() functions, but turns variables (especially strings) into sage's different classes of variables, like expressions.
For example, if I have string variable:
`expression = "x1^2*x2*x3*4"`
and x1, x2, and x3 are already declared as variables, how can I turn that variable into a sage expression so it will work with things like the coefficient() function?
Thanks!Thu, 15 Feb 2018 20:02:49 +0100https://ask.sagemath.org/question/41135/converting-strings-into-expressions/Answer by slelievre for <p>Hey everyone,
This is probably pretty straightforward, but I couldn't find any straightforward answers with google searching. I'm looking for a function that works in the same way as python's str() and float() functions, but turns variables (especially strings) into sage's different classes of variables, like expressions.
For example, if I have string variable:
<code>expression = "x1^2*x2*x3*4"</code>
and x1, x2, and x3 are already declared as variables, how can I turn that variable into a sage expression so it will work with things like the coefficient() function?
Thanks!</p>
https://ask.sagemath.org/question/41135/converting-strings-into-expressions/?answer=41137#post-id-41137To convert a string `expression` to any parent `X` in Sage, you could do `X(expression)`.
Thus, to convert the expression to Sage's symbolic ring, use `SR(expression)`.
Here is how it would work with your example.
sage: x1, x2, x3 = SR.var('x1 x2 x3')
sage: expression = "x1^2*x2*x3*4"
sage: ex = SR(expression)
sage: ex
4*x1^2*x2*x3
If you prefer to work with a polynomial ring:
sage: P.<x1, x2, x3> = QQ[]
sage: f = P(expression)
sage: f
4*x1^2*x2*x3Thu, 15 Feb 2018 23:29:36 +0100https://ask.sagemath.org/question/41135/converting-strings-into-expressions/?answer=41137#post-id-41137Comment by trenzafeeds for <p>To convert a string <code>expression</code> to any parent <code>X</code> in Sage, you could do <code>X(expression)</code>.</p>
<p>Thus, to convert the expression to Sage's symbolic ring, use <code>SR(expression)</code>.</p>
<p>Here is how it would work with your example.</p>
<pre><code>sage: x1, x2, x3 = SR.var('x1 x2 x3')
sage: expression = "x1^2*x2*x3*4"
sage: ex = SR(expression)
sage: ex
4*x1^2*x2*x3
</code></pre>
<p>If you prefer to work with a polynomial ring:</p>
<pre><code>sage: P.<x1, x2, x3> = QQ[]
sage: f = P(expression)
sage: f
4*x1^2*x2*x3
</code></pre>
https://ask.sagemath.org/question/41135/converting-strings-into-expressions/?comment=41203#post-id-41203Thank you! This worked perfect for meTue, 20 Feb 2018 21:40:46 +0100https://ask.sagemath.org/question/41135/converting-strings-into-expressions/?comment=41203#post-id-41203Comment by nimaous for <p>To convert a string <code>expression</code> to any parent <code>X</code> in Sage, you could do <code>X(expression)</code>.</p>
<p>Thus, to convert the expression to Sage's symbolic ring, use <code>SR(expression)</code>.</p>
<p>Here is how it would work with your example.</p>
<pre><code>sage: x1, x2, x3 = SR.var('x1 x2 x3')
sage: expression = "x1^2*x2*x3*4"
sage: ex = SR(expression)
sage: ex
4*x1^2*x2*x3
</code></pre>
<p>If you prefer to work with a polynomial ring:</p>
<pre><code>sage: P.<x1, x2, x3> = QQ[]
sage: f = P(expression)
sage: f
4*x1^2*x2*x3
</code></pre>
https://ask.sagemath.org/question/41135/converting-strings-into-expressions/?comment=44165#post-id-44165is it possible to convert a string of inequality to a sage expression?Fri, 02 Nov 2018 16:19:54 +0100https://ask.sagemath.org/question/41135/converting-strings-into-expressions/?comment=44165#post-id-44165Comment by slelievre for <p>To convert a string <code>expression</code> to any parent <code>X</code> in Sage, you could do <code>X(expression)</code>.</p>
<p>Thus, to convert the expression to Sage's symbolic ring, use <code>SR(expression)</code>.</p>
<p>Here is how it would work with your example.</p>
<pre><code>sage: x1, x2, x3 = SR.var('x1 x2 x3')
sage: expression = "x1^2*x2*x3*4"
sage: ex = SR(expression)
sage: ex
4*x1^2*x2*x3
</code></pre>
<p>If you prefer to work with a polynomial ring:</p>
<pre><code>sage: P.<x1, x2, x3> = QQ[]
sage: f = P(expression)
sage: f
4*x1^2*x2*x3
</code></pre>
https://ask.sagemath.org/question/41135/converting-strings-into-expressions/?comment=44170#post-id-44170@nimaous: good question. I suggest asking a new question for that.Sat, 03 Nov 2018 00:55:16 +0100https://ask.sagemath.org/question/41135/converting-strings-into-expressions/?comment=44170#post-id-44170Comment by loSuarezB for <p>To convert a string <code>expression</code> to any parent <code>X</code> in Sage, you could do <code>X(expression)</code>.</p>
<p>Thus, to convert the expression to Sage's symbolic ring, use <code>SR(expression)</code>.</p>
<p>Here is how it would work with your example.</p>
<pre><code>sage: x1, x2, x3 = SR.var('x1 x2 x3')
sage: expression = "x1^2*x2*x3*4"
sage: ex = SR(expression)
sage: ex
4*x1^2*x2*x3
</code></pre>
<p>If you prefer to work with a polynomial ring:</p>
<pre><code>sage: P.<x1, x2, x3> = QQ[]
sage: f = P(expression)
sage: f
4*x1^2*x2*x3
</code></pre>
https://ask.sagemath.org/question/41135/converting-strings-into-expressions/?comment=44348#post-id-44348Well, but this does no work for the following case.
I want to define an equation from a string, by changing the value of `gammaWStr='9.81'` by a string that is a number.
gammaW - gammaWStr == 0
If I follow the suggested construction like
exprString = 'gammaW - ' + gammaWStr + ' == 0'
eq0 = SR(exprString)
I get the Error Message
TypeError: Malformed expression: gammaW - 9.81 == !!! 0
In other words, and referring to the first question, How about an expression like `expression = "x1^2*x2*x3*4==0"`?Tue, 20 Nov 2018 17:18:50 +0100https://ask.sagemath.org/question/41135/converting-strings-into-expressions/?comment=44348#post-id-44348Comment by slelievre for <p>To convert a string <code>expression</code> to any parent <code>X</code> in Sage, you could do <code>X(expression)</code>.</p>
<p>Thus, to convert the expression to Sage's symbolic ring, use <code>SR(expression)</code>.</p>
<p>Here is how it would work with your example.</p>
<pre><code>sage: x1, x2, x3 = SR.var('x1 x2 x3')
sage: expression = "x1^2*x2*x3*4"
sage: ex = SR(expression)
sage: ex
4*x1^2*x2*x3
</code></pre>
<p>If you prefer to work with a polynomial ring:</p>
<pre><code>sage: P.<x1, x2, x3> = QQ[]
sage: f = P(expression)
sage: f
4*x1^2*x2*x3
</code></pre>
https://ask.sagemath.org/question/41135/converting-strings-into-expressions/?comment=44356#post-id-44356Thanks for reporting. This is now tracked at [Sage Trac ticket 26727: Make SR('x == 0') work](https://trac.sagemath.org/ticket/26727).Tue, 20 Nov 2018 19:03:54 +0100https://ask.sagemath.org/question/41135/converting-strings-into-expressions/?comment=44356#post-id-44356