How do I go about replacing terms with large powers of a variable in an arbitrary series? For instance, I would like to transform $1 + a x + b x^2 + c x^3 + d x^4 + e x^5$ to $1 + a x + b x^2$, dropping terms with $x$ to the power of 3 or higher. Preferably, I like to learn how to do this with _ or other functional programming features of Mathematica.
Asked
Active
Viewed 350 times
3
Milad P.
- 197
- 7
1 Answers
5
expr = 1 + a x + b x^2 + c x^3 + d x^4 + e x^5;
expr /. Power[x, _?(# > 2 &)] :> 0
(* or expr /. Power[x, n_ /; n > 2] :> 0 *)
1 + a x + b x^2
Notes: see PatternTest (?) and Condition (/;)
Alternatively,you can use Series:
Normal@Series[expr, {x, 0, 2}]
1 + a x + b x^2
kglr
- 394,356
- 18
- 477
- 896
-
Even though correct, I don't like the first option. I was looking for what you have as your second option. Thanks! I suggest switching the order and explaining what
?does. In case you find it unnecessary, I'll leave this link for others to learn about?: PatternTest. – Milad P. May 30 '17 at 07:39 -
"Even though correct, I don't like the first option." - @Milad, can you explain why? The second snippet only works for explicitly expanded out polynomials, while the first can still work even if the polynomial is in factored form. – J. M.'s missing motivation May 30 '17 at 07:58
-
Excellent point @J.M. But that would not affect my question in hand. In the first method, rather than setting the large powers to zero, one is writing the series as another series in the Big O notation (where the large powers reside) and dropping it by expressing it in the
Normalform. However, the second method does exactly what I was asking in the question; It actually DROPS large powers. Also, I wanted to learn about_?testwhich I didn't know existed before this asking this question. – Milad P. May 30 '17 at 08:58
expr /. x^n_ /; n>2 -> 0? – jjc385 May 30 '17 at 01:51