# I've got a big expression I need to dissect a certain way

I've got a BIG polynomial (7 indeterminates, total degree 12), and I want to collect all the terms therein that contain only EVEN powers of all the indeterminates. Is there any reasonably simple way to do this? By "simple," I mean short of traversing the whole expression tree, testing each term to see if it contains only even powers, and collecting them one-by-one in a list (which, BTW, I also don't really know how to do)! If it makes any difference, I haven't been using the sage.rings.polynomial module, just generic Sage expressions.

edit retag close merge delete

Can you convert it to an element of a polynomial ring? (Or better yet, create it as such in the first place.) Then it's easy. Please post some of the terms, to make the question easier to answer.

I suppose I could convert it to (or more easily, just recreate it as) an element of a multivariate polynomial ring over the integers, but before I do, I'd like to know how that makes it "easy."

Anyway here's three of the terms (which I don't think is going to tell you much that you don't already know): -A_123^5A_124^3A_12_34^4 - 2A_123^4A_124^4A_12_34^4 - A_123^3A_124^5*A_12_34^4 + ... In this case, I want to keep the 2nd term and throw out the other two.

Sort by » oldest newest most voted If you are willing to work with polynomials, you can do this. There may be ways to speed up the following. First define the polynomial ring and the indeterminates:

sage: R.<A_123, A_12_34, A_124> = QQ[]


Now define the expression:

sage: a = -A_123^5*A_124^3*A_12_34^4 - 2*A_123^4*A_124^4*A_12_34^4 - A_123^3*A_124^5*A_12_34^4


Note that "for m in a" prints out useful information: pairs (coefficient, monomial):

sage: for m in a: print(m)
(-1, A_123^5*A_12_34^4*A_124^3)
(-2, A_123^4*A_12_34^4*A_124^4)
(-1, A_123^3*A_12_34^4*A_124^5)
sage: sum(m*m for m in a)
-A_123^5*A_12_34^4*A_124^3 - 2*A_123^4*A_12_34^4*A_124^4 - A_123^3*A_12_34^4*A_124^5
sage: sum(m*m for m in a) == a
True


Now pick out the terms where all of the exponents are even:

sage: sum(m*m for m in a if all(x%2 == 0 for x in m.exponents()))
-2*A_123^4*A_12_34^4*A_124^4

more