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.Sat, 18 Jan 2014 06:35:58 +0100Multivariate polynomials again: specifying variableshttps://ask.sagemath.org/question/10936/multivariate-polynomials-again-specifying-variables/I'm back looking at multivariate polynomials, with which I was last experimenting about six months ago. Suppose I have a polynomial in four variables x,y,z,w. What I need to do is:
1. Rewrite the polynomial in terms of two of the variables (say x, y), to obtain something like Ax^2y+Bxy+Cxy^3+Dx^2y^3, where each of the coefficients A, B, C and D is a polynomial in z and w.
2. Obtain those polynomials in z and w as a list.
I don't know in advance what powers of x and y will be in the expression, nor do I know how many terms there will be.
I would have thought this would be fairly straightforward, but I can't find an easy way to do it. In Maxima number one can be done with "collectterms", but the Sage method "collect" seems only to work on one variable at a time.Thu, 16 Jan 2014 18:13:53 +0100https://ask.sagemath.org/question/10936/multivariate-polynomials-again-specifying-variables/Answer by Luca for <p>I'm back looking at multivariate polynomials, with which I was last experimenting about six months ago. Suppose I have a polynomial in four variables x,y,z,w. What I need to do is:</p>
<ol>
<li>Rewrite the polynomial in terms of two of the variables (say x, y), to obtain something like Ax^2y+Bxy+Cxy^3+Dx^2y^3, where each of the coefficients A, B, C and D is a polynomial in z and w.</li>
<li>Obtain those polynomials in z and w as a list.</li>
</ol>
<p>I don't know in advance what powers of x and y will be in the expression, nor do I know how many terms there will be.</p>
<p>I would have thought this would be fairly straightforward, but I can't find an easy way to do it. In Maxima number one can be done with "collectterms", but the Sage method "collect" seems only to work on one variable at a time.</p>
https://ask.sagemath.org/question/10936/multivariate-polynomials-again-specifying-variables/?answer=15930#post-id-15930Symbolics have a method `.poly`:
sage: var('x,y')
(x, y)
sage: p = x*y + x^2*y^2
sage: p.poly(x)
x^2*y^2 + x*y
sage: p.poly(x).coefficients()
[[y, 1], [y^2, 2]]
Multivariate polynomial rings have a method `.polnomial()`
sage: A.<x,y> = QQ[]
sage: p = x*y + x^2*y^2
sage: p.polynomial(x)
y^2*x^2 + y*x
sage: p.polynomial(x).list()
[0, y, y^2]
Both methods collect only with respect to one variable, so you may need to iterate.
Thu, 16 Jan 2014 20:35:56 +0100https://ask.sagemath.org/question/10936/multivariate-polynomials-again-specifying-variables/?answer=15930#post-id-15930Comment by Luca for <p>Symbolics have a method <code>.poly</code>:</p>
<pre><code>sage: var('x,y')
(x, y)
sage: p = x*y + x^2*y^2
sage: p.poly(x)
x^2*y^2 + x*y
sage: p.poly(x).coefficients()
[[y, 1], [y^2, 2]]
</code></pre>
<p>Multivariate polynomial rings have a method <code>.polnomial()</code></p>
<pre><code>sage: A.<x,y> = QQ[]
sage: p = x*y + x^2*y^2
sage: p.polynomial(x)
y^2*x^2 + y*x
sage: p.polynomial(x).list()
[0, y, y^2]
</code></pre>
<p>Both methods collect only with respect to one variable, so you may need to iterate.</p>
https://ask.sagemath.org/question/10936/multivariate-polynomials-again-specifying-variables/?comment=16423#post-id-16423I would have said that
[c.polynomial(y) for c in p.polynomial(x)]
is not too much typing to achieve want you want, but I just tried it and it looks buggy. I'll investigate further.Sat, 18 Jan 2014 06:35:58 +0100https://ask.sagemath.org/question/10936/multivariate-polynomials-again-specifying-variables/?comment=16423#post-id-16423Comment by Alasdair for <p>Symbolics have a method <code>.poly</code>:</p>
<pre><code>sage: var('x,y')
(x, y)
sage: p = x*y + x^2*y^2
sage: p.poly(x)
x^2*y^2 + x*y
sage: p.poly(x).coefficients()
[[y, 1], [y^2, 2]]
</code></pre>
<p>Multivariate polynomial rings have a method <code>.polnomial()</code></p>
<pre><code>sage: A.<x,y> = QQ[]
sage: p = x*y + x^2*y^2
sage: p.polynomial(x)
y^2*x^2 + y*x
sage: p.polynomial(x).list()
[0, y, y^2]
</code></pre>
<p>Both methods collect only with respect to one variable, so you may need to iterate.</p>
https://ask.sagemath.org/question/10936/multivariate-polynomials-again-specifying-variables/?comment=16424#post-id-16424Thanks for that! I did in fact check out the polynomial method earlier; it seems however to only work on one variable; whereas I want something similar which works on multiple variables simultaneously.Thu, 16 Jan 2014 21:32:23 +0100https://ask.sagemath.org/question/10936/multivariate-polynomials-again-specifying-variables/?comment=16424#post-id-16424