# How to rewrite multivariate polynomial as polynomial on one variable?

Suppose i have declared many varibles and a polynomial using them

x, y, z = var("x y z")
poly =  x^3*y*z + x^2*y^2 + 3*x^3 + 2*x^2 + x*y + x*z + 1


How can i simplify the expression is such a way that it is written as a polynomial over x? I mean something like

(...)*x^3 + (...)*x^2 + (...)*x +...

edit retag close merge delete

Sort by » oldest newest most voted

For this purpose it is more convenient to work in a polynomial ring than in the symbolic ring:

sage: x, y, z = var("x y z")
sage: poly =  x^3*y*z + x^2*y^2 + 3*x^3 + 2*x^2 + x*y + x*z + 1
sage: A = PolynomialRing(QQ, names='y,z')
sage: B = PolynomialRing(A, names='x')
sage: B(poly)
(y*z + 3)*x^3 + (y^2 + 2)*x^2 + (y + z)*x + 1


Or, avoiding the symbolic ring altogether:

sage: A.<y,z> = PolynomialRing(QQ)
sage: B.<x> = PolynomialRing(A)
sage: poly =  x^3*y*z + x^2*y^2 + 3*x^3 + 2*x^2 + x*y + x*z + 1
sage: poly
(y*z + 3)*x^3 + (y^2 + 2)*x^2 + (y + z)*x + 1


You can go back from a polynomial ring element f to the symbolic ring by SR(f).

more

If you want to work only in the symbolic ring, you can use the collect method:

sage: x, y, z = var("x y z")
sage: poly =  x^3*y*z + x^2*y^2 + 3*x^3 + 2*x^2 + x*y + x*z + 1
sage: poly.collect(x)
(y*z + 3)*x^3 + (y^2 + 2)*x^2 + x*(y + z) + 1


It is also possible to extract the coefficients of each power of $x$:

sage: poly.coefficients(x)
[[1, 0], [y + z, 1], [y^2 + 2, 2], [y*z + 3, 3]]


or just

sage: poly.coefficients(x,sparse=False)
[1, y + z, y^2 + 2, y*z + 3]

more