Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Here are two other ways to proceed, depending on context the one or the other one may be more attractive.

First, note that if we have a polynomial, element of a polynomial ring, we have full access to the monomials, and their coefficients. I will illustrate this using a polynomial ring $R=\Bbb Q[t]$ of only one variable, $t$. And the result will be a polynomial in an other ring, $S=\Bbb Q[x]$, as wanted. So far:

R.<t> = PolynomialRing(QQ)    # or just simply R.<t> = QQ[]
S.<x> = PolynomialRing(QQ)    # or just simply S.<x> = QQ[]
f = 2023*t^8 - t^6 + 77*t^2 + 8937

Then we have an iterator associated to pol, thus access to the coefficients and to the powers. Using it...

sage: for coef in f: print(coef)
8937
0
77
0
0
0
-1
0
2023
sage: f.coefficients(sparse=False)
[8937, 0, 77, 0, 0, 0, -1, 0, 2023]
sage: # but be aware of the default...
sage: f.coefficients()
[8937, 77, -1, 2023]
sage: cfs = f.coefficients(sparse=False)

And we can loop, or use list comprehension.

sage: g = sum([cfs[k]*x^(k/2) for k in range(len(cfs)) if k%2 == 0])
sage: g
2023*x^4 - x^3 + 77*x + 8937

An other idea is to use an ideal and to eliminate, in this case however the parent ring contains both unknowns.

R.<t,x> = Polynomialing(QQ)
f = 2023*t^8 - t^6 + 77*t^2 + 8937
J = R.ideal([f, t^2 - x])

And now:

sage: J.elimination_ideal(t)
Ideal (2023*x^4 - x^3 + 77*x + 8937) of Multivariate Polynomial Ring in t, x over Rational Field
sage: J.elimination_ideal(t).gens()
[2023*x^4 - x^3 + 77*x + 8937]
sage: J.elimination_ideal(t).gens()[0]
2023*x^4 - x^3 + 77*x + 8937