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, 13 Nov 2016 16:39:11 -0600Collect polynomial in a different variablehttps://ask.sagemath.org/question/35537/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.Fri, 11 Nov 2016 22:54:11 -0600https://ask.sagemath.org/question/35537/collect-polynomial-in-a-different-variable/Comment by slelievre for <p>I want to collect my polynomial in a different variable. How am I to do that. For example I have :</p>
<pre><code>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
</code></pre>
<p>Now I want my <code>D</code> to be in the form where <code>u</code> is the main variable, so I will have :</p>
<pre><code>D=(a1*x^4+a5*x^3+a9*x^2+a13*x)u^3+(...)u^2+(...)u
</code></pre>
<p>Maple do it with <code>collect</code> code. I try to search for a similar code in Sage but no luck.</p>
https://ask.sagemath.org/question/35537/collect-polynomial-in-a-different-variable/?comment=35543#post-id-35543Please include code that will work in a fresh Sage session.
We are missing definitions for `u`, `a1`, `a2`, ...Sat, 12 Nov 2016 04:21:01 -0600https://ask.sagemath.org/question/35537/collect-polynomial-in-a-different-variable/?comment=35543#post-id-35543Answer by tmonteil for <p>I want to collect my polynomial in a different variable. How am I to do that. For example I have :</p>
<pre><code>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
</code></pre>
<p>Now I want my <code>D</code> to be in the form where <code>u</code> is the main variable, so I will have :</p>
<pre><code>D=(a1*x^4+a5*x^3+a9*x^2+a13*x)u^3+(...)u^2+(...)u
</code></pre>
<p>Maple do it with <code>collect</code> code. I try to search for a similar code in Sage but no luck.</p>
https://ask.sagemath.org/question/35537/collect-polynomial-in-a-different-variable/?answer=35542#post-id-35542You 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, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16 over Rational Field
But you can use an intermediate polynomial ring from which the conversion is correct:
sage: M = PolynomialRing(R,'u,x') ; M
Multivariate Polynomial Ring in u, 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: M(D)
a1*u^3*x^4 + a5*u^3*x^3 + a2*u^2*x^4 + a9*u^3*x^2 + a6*u^2*x^3 + a3*u*x^4 + a13*u^3*x + a10*u^2*x^2 + a7*u*x^3 + a4*x^4 + a14*u^2*x + a11*u*x^2 + a8*x^3 + a15*u*x + a12*x^2 + a16*x
sage: T2(M(D))
(a1*x^4 + a5*x^3 + a9*x^2 + a13*x)*u^3 + (a2*x^4 + a6*x^3 + a10*x^2 + a14*x)*u^2 + (a3*x^4 + a7*x^3 + a11*x^2 + a15*x)*u + a4*x^4 + a8*x^3 + a12*x^2 + a16*x
Sat, 12 Nov 2016 03:27:35 -0600https://ask.sagemath.org/question/35537/collect-polynomial-in-a-different-variable/?answer=35542#post-id-35542Comment by Sha for <div class="snippet"><p>You can see your first polynomial as a polynomial with variable <code>x</code>, whose coefficients belong to the polynomial ring in <code>u</code> with coefficients in the polynomial ring with variables <code>ai</code>:</p>
<pre><code>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
</code></pre>
<p>Now, your second polynomial is a polynomial with variable <code>u</code> whose coefficients belong to the polynomial ring with variable <code>x</code> and with coefficients in the polynomial ring with variables <code>ai</code>:</p>
<pre><code>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
</code></pre>
<p>Unfortunately, Sage seems <strong>NOT</strong> able to do the conversion between those two polynomial rings correctly (and gives a somewhat unexpected result):</p>
<pre><code>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 ...</code></pre><span class="expander"> <a>(more)</a></span></div>https://ask.sagemath.org/question/35537/collect-polynomial-in-a-different-variable/?comment=35563#post-id-35563Thank you for explaining this to me.Sun, 13 Nov 2016 16:39:11 -0600https://ask.sagemath.org/question/35537/collect-polynomial-in-a-different-variable/?comment=35563#post-id-35563