ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 12 Feb 2020 03:08:00 +0100How to rewrite multivariate polynomial as polynomial on one variable?https://ask.sagemath.org/question/49878/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 +...Wed, 12 Feb 2020 00:00:22 +0100https://ask.sagemath.org/question/49878/how-to-rewrite-multivariate-polynomial-as-polynomial-on-one-variable/Answer by Juanjo for <p>Suppose i have declared many varibles and a polynomial using them</p>
<pre><code>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
</code></pre>
<p>How can i simplify the expression is such a way that it is written as a polynomial over x? I mean something like</p>
<pre><code>(...)*x^3 + (...)*x^2 + (...)*x +...
</code></pre>
https://ask.sagemath.org/question/49878/how-to-rewrite-multivariate-polynomial-as-polynomial-on-one-variable/?answer=49880#post-id-49880If 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]
Wed, 12 Feb 2020 03:08:00 +0100https://ask.sagemath.org/question/49878/how-to-rewrite-multivariate-polynomial-as-polynomial-on-one-variable/?answer=49880#post-id-49880Answer by rburing for <p>Suppose i have declared many varibles and a polynomial using them</p>
<pre><code>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
</code></pre>
<p>How can i simplify the expression is such a way that it is written as a polynomial over x? I mean something like</p>
<pre><code>(...)*x^3 + (...)*x^2 + (...)*x +...
</code></pre>
https://ask.sagemath.org/question/49878/how-to-rewrite-multivariate-polynomial-as-polynomial-on-one-variable/?answer=49879#post-id-49879For 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)`.Wed, 12 Feb 2020 00:17:16 +0100https://ask.sagemath.org/question/49878/how-to-rewrite-multivariate-polynomial-as-polynomial-on-one-variable/?answer=49879#post-id-49879