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

edit retag close merge delete

Sort by » oldest newest most voted

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

more

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

( 2014-01-16 14:32:23 -0500 )edit

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

( 2014-01-17 23:35:58 -0500 )edit