10
$\begingroup$

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?

$\endgroup$
8
  • 2
    $\begingroup$ 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." $\endgroup$ Commented Apr 10, 2013 at 15:50
  • $\begingroup$ @whuber actually they are called Laurent polynomials $\endgroup$ Commented Apr 10, 2013 at 18:12
  • $\begingroup$ @Spawn1701D I don't think so: such objects would not include fractional or irrational powers, for instance. $\endgroup$ Commented Apr 10, 2013 at 19:04
  • $\begingroup$ @whuber have a look here for more details. $\endgroup$ Commented Apr 10, 2013 at 19:06
  • $\begingroup$ @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! $\endgroup$ Commented Apr 10, 2013 at 19:09

4 Answers 4

7
$\begingroup$

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

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 
$\endgroup$
4
  • $\begingroup$ @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. $\endgroup$ Commented Apr 10, 2013 at 15:16
  • $\begingroup$ @sjdh I updated my answer; please tell me if this gives the output you desire. $\endgroup$ Commented Apr 10, 2013 at 15:26
  • $\begingroup$ @sjdh Also, the default value for Power is one, not zero, which I believe explains your non-match above. $\endgroup$ Commented Apr 10, 2013 at 15:36
  • $\begingroup$ @Mr.Wizzard Using DeleteCases is a good idea. This works. Thanks $\endgroup$ Commented Apr 11, 2013 at 5:53
5
$\begingroup$

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$

$\endgroup$
3
$\begingroup$
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}*) 
$\endgroup$
2
  • $\begingroup$ Can you add some comments? How is it working? What is the difference with the other answers? $\endgroup$ Commented Apr 10, 2013 at 14:59
  • $\begingroup$ @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 $\endgroup$ Commented Apr 10, 2013 at 15:46

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.