ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 21 Sep 2014 04:24:23 -0500Converting polynomials between ringshttps://ask.sagemath.org/question/24207/converting-polynomials-between-rings/ I have two polynomials, each one explicitly created as members of different multivariate polynomial rings. So for example I might have
R1.<a,b,c,t> = PolynomialRing(QQ)
L = (a*t^2+b*t+c).subs(a=2,b=3,c=4)
R2.<A,B,C,D,t> = PolynomialRing(QQ)
p = (A*t+B)^2+(C*t+D)^2
Now I want to compare the coefficients of the two polynomials, which means converting L to be a polynomial in the ring R2.
However, I'm not sure (or more to the point, can't remember) how to do this. Is there some simple, canonical way to do this? Thanks!Fri, 19 Sep 2014 07:44:21 -0500https://ask.sagemath.org/question/24207/converting-polynomials-between-rings/Answer by slelievre for <p>I have two polynomials, each one explicitly created as members of different multivariate polynomial rings. So for example I might have</p>
<pre><code>R1.<a,b,c,t> = PolynomialRing(QQ)
L = (a*t^2+b*t+c).subs(a=2,b=3,c=4)
R2.<A,B,C,D,t> = PolynomialRing(QQ)
p = (A*t+B)^2+(C*t+D)^2
</code></pre>
<p>Now I want to compare the coefficients of the two polynomials, which means converting L to be a polynomial in the ring R2.</p>
<p>However, I'm not sure (or more to the point, can't remember) how to do this. Is there some simple, canonical way to do this? Thanks!</p>
https://ask.sagemath.org/question/24207/converting-polynomials-between-rings/?answer=24210#post-id-24210In your example, having defined
sage: R1.<a,b,c,t> = PolynomialRing(QQ)
sage: L = (a*t^2+b*t+c).subs(a=2,b=3,c=4)
we get
sage: L
2*t^2 + 3*t + 4
(did you mean `L = a*t^2+b*t+c` without substituting values for `a`, `b`, `c`?)
Having then defined
sage: R2.<A,B,C,D,t> = PolynomialRing(QQ)
sage: p = (A*t+B)^2+(C*t+D)^2
you can convert polynomials from one ring to the other
sage: R2(L)
2*D^2 + 3*D + 4
Here, the order of the variables matters, more than their names:
`t`, the fourth variable in `R1`, is mapped to `D`, the fourth variable in `R2`.
If you want `a`, `b`,`c`, `t` to be mapped to `A`, `B`, `C`, `t`, you might
want to include an extra variable `d` in `R1` and not use it.
Or you could compare string representations of your polynomials, applying
string replacements as necessary. Or you could use `p.monomials()` and `p.coefficients()`.
You could also define a ring homomorphism from `R1` to `R2` mapping the variables to the variables of your choice, see [the reference manual](http://www.sagemath.org/doc/reference/rings/sage/rings/morphism.html).Fri, 19 Sep 2014 09:39:45 -0500https://ask.sagemath.org/question/24207/converting-polynomials-between-rings/?answer=24210#post-id-24210Comment by slelievre for <p>In your example, having defined</p>
<pre><code>sage: R1.<a,b,c,t> = PolynomialRing(QQ)
sage: L = (a*t^2+b*t+c).subs(a=2,b=3,c=4)
</code></pre>
<p>we get</p>
<pre><code>sage: L
2*t^2 + 3*t + 4
</code></pre>
<p>(did you mean <code>L = a*t^2+b*t+c</code> without substituting values for <code>a</code>, <code>b</code>, <code>c</code>?)</p>
<p>Having then defined</p>
<pre><code>sage: R2.<A,B,C,D,t> = PolynomialRing(QQ)
sage: p = (A*t+B)^2+(C*t+D)^2
</code></pre>
<p>you can convert polynomials from one ring to the other</p>
<pre><code>sage: R2(L)
2*D^2 + 3*D + 4
</code></pre>
<p>Here, the order of the variables matters, more than their names:
<code>t</code>, the fourth variable in <code>R1</code>, is mapped to <code>D</code>, the fourth variable in <code>R2</code>.</p>
<p>If you want <code>a</code>, <code>b</code>,<code>c</code>, <code>t</code> to be mapped to <code>A</code>, <code>B</code>, <code>C</code>, <code>t</code>, you might
want to include an extra variable <code>d</code> in <code>R1</code> and not use it.</p>
<p>Or you could compare string representations of your polynomials, applying
string replacements as necessary. Or you could use <code>p.monomials()</code> and <code>p.coefficients()</code>.</p>
<p>You could also define a ring homomorphism from <code>R1</code> to <code>R2</code> mapping the variables to the variables of your choice, see <a href="http://www.sagemath.org/doc/reference/rings/sage/rings/morphism.html">the reference manual</a>.</p>
https://ask.sagemath.org/question/24207/converting-polynomials-between-rings/?comment=24225#post-id-24225The variables for a polynomial ring `R` are just `R.0`, `R.1`, etc, (and `a`, `b`, `t`, `x`, `y`, or other, are just display names), so it's easy to map them to the variables `S.0`, `S.1` of another polynomial ring `S`.Sun, 21 Sep 2014 04:24:23 -0500https://ask.sagemath.org/question/24207/converting-polynomials-between-rings/?comment=24225#post-id-24225Comment by Alasdair for <p>In your example, having defined</p>
<pre><code>sage: R1.<a,b,c,t> = PolynomialRing(QQ)
sage: L = (a*t^2+b*t+c).subs(a=2,b=3,c=4)
</code></pre>
<p>we get</p>
<pre><code>sage: L
2*t^2 + 3*t + 4
</code></pre>
<p>(did you mean <code>L = a*t^2+b*t+c</code> without substituting values for <code>a</code>, <code>b</code>, <code>c</code>?)</p>
<p>Having then defined</p>
<pre><code>sage: R2.<A,B,C,D,t> = PolynomialRing(QQ)
sage: p = (A*t+B)^2+(C*t+D)^2
</code></pre>
<p>you can convert polynomials from one ring to the other</p>
<pre><code>sage: R2(L)
2*D^2 + 3*D + 4
</code></pre>
<p>Here, the order of the variables matters, more than their names:
<code>t</code>, the fourth variable in <code>R1</code>, is mapped to <code>D</code>, the fourth variable in <code>R2</code>.</p>
<p>If you want <code>a</code>, <code>b</code>,<code>c</code>, <code>t</code> to be mapped to <code>A</code>, <code>B</code>, <code>C</code>, <code>t</code>, you might
want to include an extra variable <code>d</code> in <code>R1</code> and not use it.</p>
<p>Or you could compare string representations of your polynomials, applying
string replacements as necessary. Or you could use <code>p.monomials()</code> and <code>p.coefficients()</code>.</p>
<p>You could also define a ring homomorphism from <code>R1</code> to <code>R2</code> mapping the variables to the variables of your choice, see <a href="http://www.sagemath.org/doc/reference/rings/sage/rings/morphism.html">the reference manual</a>.</p>
https://ask.sagemath.org/question/24207/converting-polynomials-between-rings/?comment=24223#post-id-24223Thank you very much. It seems as long as I have the same number of variables in each ring, then when I move from one to the other, the variables will be mapped in the order I've defined them. This makes perfect sense, not that I see how it works!Sun, 21 Sep 2014 04:11:12 -0500https://ask.sagemath.org/question/24207/converting-polynomials-between-rings/?comment=24223#post-id-24223Answer by Luca for <p>I have two polynomials, each one explicitly created as members of different multivariate polynomial rings. So for example I might have</p>
<pre><code>R1.<a,b,c,t> = PolynomialRing(QQ)
L = (a*t^2+b*t+c).subs(a=2,b=3,c=4)
R2.<A,B,C,D,t> = PolynomialRing(QQ)
p = (A*t+B)^2+(C*t+D)^2
</code></pre>
<p>Now I want to compare the coefficients of the two polynomials, which means converting L to be a polynomial in the ring R2.</p>
<p>However, I'm not sure (or more to the point, can't remember) how to do this. Is there some simple, canonical way to do this? Thanks!</p>
https://ask.sagemath.org/question/24207/converting-polynomials-between-rings/?answer=24209#post-id-24209Because of the order you've defined the objects, `L(t=t)` will be an element of `R2`. Slightly more robustly, in your example you can do any of the following.
sage: L(t=R2.gen(4)).parent()
Multivariate Polynomial Ring in A, B, C, D, t over Rational Field
sage: L(t=R2.4).parent()
Multivariate Polynomial Ring in A, B, C, D, t over Rational Field
Fri, 19 Sep 2014 09:25:39 -0500https://ask.sagemath.org/question/24207/converting-polynomials-between-rings/?answer=24209#post-id-24209Comment by Alasdair for <p>Because of the order you've defined the objects, <code>L(t=t)</code> will be an element of <code>R2</code>. Slightly more robustly, in your example you can do any of the following.</p>
<pre><code>sage: L(t=R2.gen(4)).parent()
Multivariate Polynomial Ring in A, B, C, D, t over Rational Field
sage: L(t=R2.4).parent()
Multivariate Polynomial Ring in A, B, C, D, t over Rational Field
</code></pre>
https://ask.sagemath.org/question/24207/converting-polynomials-between-rings/?comment=24224#post-id-24224Thank you - this should work, and I'll check it later.Sun, 21 Sep 2014 04:11:35 -0500https://ask.sagemath.org/question/24207/converting-polynomials-between-rings/?comment=24224#post-id-24224