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

polynomial variable rewrite

add a comment

1

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
```

0

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
```

Please start posting anonymously - your entry will be published after you log in or create a new account.

Asked: ** 2023-01-23 18:24:57 +0100 **

Seen: **255 times**

Last updated: **Jan 27 '23**

Multivariate Polynomials over Rational Function Fields

How do I Pass a tuple as an argument for a multivariate polynomial?

Is there an example of how i could write a polynomial as a product of linear factors

multi-symmetric functions and multi-partitions

polynomials rings over transcendental field extensions

Coercion problem while defining piecewise function

Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.