polynomial variable rewrite

Say I have a polynomial (over a ring) x^4+x^2+1. I want to change variables t=x^2 so it becomes t^2+t+1, and if possible rename back to x^2+x+1. How do I do that?

edit retag close merge delete

Sort by ยป oldest newest most voted

To express the fact that $t=x^2$ you can define a quotient:

sage: R.<x,t> = PolynomialRing(QQ)
sage: p = x^4+x^2+1
sage: Q = R.quotient(x^2-t)
sage: Q
Quotient of Multivariate Polynomial Ring in x, t over Rational Field by the ideal (x^2 - t)
sage: q = Q(p)
sage: q
tbar^2 + tbar + 1
sage: q.parent()
Quotient of Multivariate Polynomial Ring in x, t over Rational Field by the ideal (x^2 - t)


Tu put the result back in the ring R and letting the undeterminate t go to x, you can do:

sage: q.lift()
t^2 + t + 1
sage: q.lift().subs(t=x)
x^2 + x + 1

more

Is there a shorter way if I don't go through t?

( 2023-01-23 19:53:48 +0100 )edit

Also, I initialized the ring via

F = GF(p)
R.<x> = F['x']
f = R.lagrange_polynomial(fib)


If I try to introduce 't', I get an error that the class doesn't have lagrange_polynomial

( 2023-01-23 19:58:38 +0100 )edit

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

more