# Collect polynomial in a different variable

I want to collect my polynomial in a different variable. How am I to do that. For example I have :

D=(a1*u^3+a2*u^2+a3*u+a4)x^4+(a5*u^3+a6*u^2+a7*u+a8)x^3+(a9*u^3+a10*u^2+a11*u+a12)x^2+(a13*u^3+a14*u^2+a15*u+a16)x


Now I want my D to be in the form where u is the main variable, so I will have :

D=(a1*x^4+a5*x^3+a9*x^2+a13*x)u^3+(...)u^2+(...)u


Maple do it with collect code. I try to search for a similar code in Sage but no luck.

edit retag close merge delete

Please include code that will work in a fresh Sage session.

We are missing definitions for u, a1, a2, ...

Sort by » oldest newest most voted

You can see your first polynomial as a polynomial with variable x, whose coefficients belong to the polynomial ring in u with coefficients in the polynomial ring with variables ai:

sage: R = PolynomialRing(QQ,'a',17) ; R
Multivariate Polynomial Ring in a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16 over Rational Field
sage: R.inject_variables()
Defining a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16
sage: S = PolynomialRing(R,'u') ; S
Univariate Polynomial Ring in u over Multivariate Polynomial Ring in a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16 over Rational Field
sage: S.inject_variables()
Defining u
sage: T = PolynomialRing(S,'x') ; T
Univariate Polynomial Ring in x over Univariate Polynomial Ring in u over Multivariate Polynomial Ring in a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16 over Rational Field
sage: T.inject_variables()
Defining x

sage: D = (a1*u^3+a2*u^2+a3*u+a4)*x^4+(a5*u^3+a6*u^2+a7*u+a8)*x^3+(a9*u^3+a10*u^2+a11*u+a12)*x^2+(a13*u^3+a14*u^2+a15*u+a16)*x
sage: D
(a1*u^3 + a2*u^2 + a3*u + a4)*x^4 + (a5*u^3 + a6*u^2 + a7*u + a8)*x^3 + (a9*u^3 + a10*u^2 + a11*u + a12)*x^2 + (a13*u^3 + a14*u^2 + a15*u + a16)*x
sage: D.parent()
Univariate Polynomial Ring in x over Univariate Polynomial Ring in u over Multivariate Polynomial Ring in a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16 over Rational Field


Now, your second polynomial is a polynomial with variable u whose coefficients belong to the polynomial ring with variable x and with coefficients in the polynomial ring with variables ai:

sage: S2 = PolynomialRing(R,'x') ; S2
Univariate Polynomial Ring in x over Multivariate Polynomial Ring in a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16 over Rational Field
sage: T2 = PolynomialRing(S2,'u') ; T2
Univariate Polynomial Ring in u over Univariate Polynomial Ring in x over Multivariate Polynomial Ring in a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16 over Rational Field


Unfortunately, Sage seems NOT able to do the conversion between those two polynomial rings correctly (and gives a somewhat unexpected result):

sage: T2(D)
(a1*x^3 + a2*x^2 + a3*x + a4)*u^4 + (a5*x^3 + a6*x^2 + a7*x + a8)*u^3 + (a9*x^3 + a10*x^2 + a11*x + a12)*u^2 + (a13*x^3 + a14*x^2 + a15*x + a16)*u
sage: T2(D).parent()
Univariate Polynomial Ring in u over Univariate Polynomial Ring in x over Multivariate Polynomial Ring in a0, a1, a2, a3, a4, a5 ...
more