# 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!

edit retag close merge delete

Sort by » oldest newest most voted

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.

more

Thank 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!

The 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.

Because 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

more