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?
Answer
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]]
Comments
Post a Comment