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.Sat, 02 Jul 2022 08:13:15 +0200Creating a polynomial from a string with symbolic constantshttps://ask.sagemath.org/question/63059/creating-a-polynomial-from-a-string-with-symbolic-constants/Below is some code that does the following. First, define `R` to be the univariate polynomial ring `Q_2[x]`, and let `f(x)` be the polynomial `x^2 - 2` in this ring. Then let `K` be the totally ramified quadratic extension defined by this polynomial, and call its generator a. Let `S` be the polynomial ring `K[y]`. Then I would like to define a polynomial `y + a` in `S`. This works find if I write `f1 = S(y+a)`, but it fails if I try `f2 = S('y + a')`. However, I need to be able to define my polynomial from a string as in the `f2` case. Can anyone help?
R.<x> = Qp(2,100)[]
f = R(x^2 - 2)
K.<a> = Qp(2,100).ext(f)
S.<y> = K[]
# The next line works
f1 = S(y + a)
# The next one throws an error
f2 = S('y + a')Wed, 29 Jun 2022 18:32:42 +0200https://ask.sagemath.org/question/63059/creating-a-polynomial-from-a-string-with-symbolic-constants/Comment by rburing for <p>Below is some code that does the following. First, define <code>R</code> to be the univariate polynomial ring <code>Q_2[x]</code>, and let <code>f(x)</code> be the polynomial <code>x^2 - 2</code> in this ring. Then let <code>K</code> be the totally ramified quadratic extension defined by this polynomial, and call its generator a. Let <code>S</code> be the polynomial ring <code>K[y]</code>. Then I would like to define a polynomial <code>y + a</code> in <code>S</code>. This works find if I write <code>f1 = S(y+a)</code>, but it fails if I try <code>f2 = S('y + a')</code>. However, I need to be able to define my polynomial from a string as in the <code>f2</code> case. Can anyone help? </p>
<pre><code>R.<x> = Qp(2,100)[]
f = R(x^2 - 2)
K.<a> = Qp(2,100).ext(f)
S.<y> = K[]
# The next line works
f1 = S(y + a)
# The next one throws an error
f2 = S('y + a')
</code></pre>
https://ask.sagemath.org/question/63059/creating-a-polynomial-from-a-string-with-symbolic-constants/?comment=63089#post-id-63089Why do you need to define a polynomial from a string? Sounds like it could be an [XY problem](https://en.wikipedia.org/wiki/XY_problem).Sat, 02 Jul 2022 08:13:15 +0200https://ask.sagemath.org/question/63059/creating-a-polynomial-from-a-string-with-symbolic-constants/?comment=63089#post-id-63089Answer by slelievre for <p>Below is some code that does the following. First, define <code>R</code> to be the univariate polynomial ring <code>Q_2[x]</code>, and let <code>f(x)</code> be the polynomial <code>x^2 - 2</code> in this ring. Then let <code>K</code> be the totally ramified quadratic extension defined by this polynomial, and call its generator a. Let <code>S</code> be the polynomial ring <code>K[y]</code>. Then I would like to define a polynomial <code>y + a</code> in <code>S</code>. This works find if I write <code>f1 = S(y+a)</code>, but it fails if I try <code>f2 = S('y + a')</code>. However, I need to be able to define my polynomial from a string as in the <code>f2</code> case. Can anyone help? </p>
<pre><code>R.<x> = Qp(2,100)[]
f = R(x^2 - 2)
K.<a> = Qp(2,100).ext(f)
S.<y> = K[]
# The next line works
f1 = S(y + a)
# The next one throws an error
f2 = S('y + a')
</code></pre>
https://ask.sagemath.org/question/63059/creating-a-polynomial-from-a-string-with-symbolic-constants/?answer=63061#post-id-63061There might be room for improvement there.
Here are two possibilities to work around this limitation.
sage: f2 = S(eval('y + a'))
sage: f2 = eval('S(y + a)')
Wed, 29 Jun 2022 21:23:42 +0200https://ask.sagemath.org/question/63059/creating-a-polynomial-from-a-string-with-symbolic-constants/?answer=63061#post-id-63061