# How to collect terms with positive powers in polynomial

I am trying to collect all terms with non-negative powers of $x$ in polynomials like $\frac{1}{x^2}\left(a x^2+x^{\pi }+x+z\right)^2$

First expand the polynomial

Expand[1/x^2 (x + x^π + a x^2 + z)^2, x]


This gives $a^2 x^2+2 a x^{\pi }+2 a x+2 a z+2 x^{\pi -2} z+x^{2 \pi -2}+2 x^{\pi -1}+\frac{z^2}{x^2}+\frac{2 z}{x}+1$.

Now try to select those terms with positive or zero powers of x. My best guess is

Plus@@Cases[%, (Times[___, Power[x, g_.], ___] /; g >= 0)]


However, this only yields the term $a^2 x^2$.

And why does this not work for the other terms? How can I collect the other terms with positive or zero powers of x?

• FWIW, these expressions are not what is usually meant by "polynomial." By definition, a polynomial has only natural numbers ($0,1,2,\ldots$) among the powers of its variables. Neither are these rational expressions, which are polynomial fractions. The conventional term that comes closest to what may be intended here is "posynomial." – whuber Apr 10 '13 at 15:50
• @whuber actually they are called Laurent polynomials – Spawn1701D Apr 10 '13 at 18:12
• @Spawn1701D I don't think so: such objects would not include fractional or irrational powers, for instance. – whuber Apr 10 '13 at 19:04
• @whuber have a look here for more details. – Spawn1701D Apr 10 '13 at 19:06
• @Spawn1701D Read carefully: irrational and fractional powers won't even make sense in most fields $\mathbb{F}$. BTW, Mathworld tends not to be the best choice of references, as attested by its sloppy (and incorrect) definition of polynomial: although from the article it is clear that only non-negative integers can be possible powers (as in its equation 2), nowhere does it actually state that! – whuber Apr 10 '13 at 19:09

Alternatively, you can use Pick and Exponent:

list = List @@ Expand[1/x^2 (x + x^Pi + a x^2 + z)^2, x];
Pick[list, Positive[Exponent[#, x] & /@ list]]

(*{2 a x,a^2 x^2,2 x^(-1+Pi),2 a x^Pi,x^(-2+2 Pi),2 x^(-2+Pi) z}*)


Or a little shorter, since Exponent has attribute Listable (thanks to Mr. Wizard for pointing that out):

Pick[list, Positive@Exponent[list, x]]

• +1 This seems like the correct approach. Also, I believe you can write: Pick[list, Positive @ Exponent[list, x]] – Mr.Wizard Apr 10 '13 at 13:54
• One-liner: Select[Expand[1/x^2 (x + x^\[Pi] + a x^2 + z)^2], Positive[Exponent[#, x]] &] – J. M. will be back soon Apr 10 '13 at 14:00
• @J.M. Very nice! Perhaps not as fast a Pick however? – Mr.Wizard Apr 10 '13 at 14:04
• @Mr.Wizard, I suppose timings might be in order... :) – J. M. will be back soon Apr 10 '13 at 14:06
• @einbandi Also in your answer I am missing $1$ and $2az$. You can include them by changing Positive[] into NonNegative[] – sjdh Apr 10 '13 at 15:25

Your pattern is evaluating in an undesired way; you can use HoldPattern to avoid this:

expr = Expand[1/x^2 (x + x^π + a x^2 + z)^2, x];

Plus @@ Cases[expr, (HoldPattern[Times[___, Power[x, g_.], ___]] /; g >= 0)]

2 a x + a^2 x^2 + 2 x^(-1 + π) + 2 a x^π + 2 x^(-2 + π) z


Because of the attributes of Times you do not need two ___ patterns, which was the source of the problem above (they evaluated to ___^2). EDIT: Also, the pattern above misses the term x^(2π - 2). We can instead write:

Tr @ Cases[expr, x^g_. _. /; g >= 0]

2 a x + a^2 x^2 + 2 x^(-1 + π) + 2 a x^π + x^(-2 + 2 π) + 2 x^(-2 + π) z


In a comment sjdh states that he expects 1 and 2 a z terms to be present in the output.
Perhaps this is closer to his intent:

DeleteCases[expr, x^g_. _. /; g < 0]

1 + 2 a x + a^2 x^2 + 2 x^(-1 + π) + 2 a x^π + x^(-2 + 2 π) + 2 a z + 2 x^(-2 + π) z

• @Mr.Wizzard $1$ and $2az$ also have a non negative power of $x$. Why are they not included in your answer? I thought MatchQ[1, x^_.] would give True, but it yields False. – sjdh Apr 10 '13 at 15:16
• @sjdh I updated my answer; please tell me if this gives the output you desire. – Mr.Wizard Apr 10 '13 at 15:26
• @sjdh Also, the default value for Power is one, not zero, which I believe explains your non-match above. – Mr.Wizard Apr 10 '13 at 15:36
• @Mr.Wizzard Using DeleteCases is a good idea. This works. Thanks – sjdh Apr 11 '13 at 5:53

Expand[1/x^2 (x + x^π + a x^2 + z)^2, x] /. x^_?Negative -> 0

$1+2 a x+a^2 x^2+2 x^{-1+\pi }+2 a x^{\pi }+x^{-2+2 \pi }+2 a z+2 x^{-2+\pi } z$
p = {Expand[1/x^2 (x + x^Pi + a x^2 + z)^2]};