Ask Your Question
0

How to rewrite multivariate polynomial as polynomial on one variable?

asked 2020-02-12 00:00:22 +0100

JGC gravatar image

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 flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
1

answered 2020-02-12 00:17:16 +0100

rburing gravatar image

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).

edit flag offensive delete link more
1

answered 2020-02-12 03:08:00 +0100

Juanjo gravatar image

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]
edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2020-02-12 00:00:22 +0100

Seen: 795 times

Last updated: Feb 12 '20