10

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?

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
sjdh
  • 7,757
  • 5
  • 37
  • 47
  • 2
    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
  • @whuber yes you are totally correct, sorry I was stuck in the positive power statement and oblivious to your Real statement ... – Spawn1701D Apr 10 '13 at 19:15
  • I notice that you never Accepted an answer to this question. Does anything remain unaddressed or unsatisfactory? – Mr.Wizard Dec 08 '14 at 21:55
  • @Mr.Wizard Everything clear. Thanks! – sjdh Dec 10 '14 at 18:10

4 Answers4

7

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]]
einbandi
  • 4,024
  • 1
  • 23
  • 39
6

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.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • @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 05:53
5

How about this:

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$

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
Spawn1701D
  • 1,871
  • 13
  • 14
3
p = {Expand[1/x^2 (x + x^Pi + a x^2 + z)^2]};
Extract[p, Position[N@p, x^n_ /; n > 0, Infinity][[All, 1 ;; 2]]]

(* {a^2 x^2, 2 x^(-1 + Pi), 2 a x^Pi, x^(-2 + 2 Pi), 2 x^(-2 + Pi) z}*)
Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
  • Can you add some comments? How is it working? What is the difference with the other answers? – sjdh Apr 10 '13 at 14:59
  • @sjdh I don't know how it's different. I wrote this answer before any other was posted, and I think it's correct. There are only a few seconds elapsed between my post and Mr.Wizard's – Dr. belisarius Apr 10 '13 at 15:46