# Ignore higher order terms in an expression

I am trying to ignore terms that are higher order (third or more) in an expression. For example, for the input function:

$$f_{in}= \alpha\beta + \alpha^2 + \beta^2 + \alpha\beta^2/2 + \alpha^2 \beta$$

I would like the output function to be

$$f_{out}= \alpha\beta + \alpha^2 + \beta^2$$

To this end, I defined the rule:

rule = α^i_*β^j_ :> 0 /; (i + j) > 2;

but it doesn't seem to do the trick. Is there a simple fix?

• ResourceFunction["MultivariateTaylorPolynomial"][x + x*y + y^2 + x*y^2 - x^2*y, {x, y}, 2] Out[42]= x + x y + y^2 Commented Jul 13 at 20:28

Rules do not understand mathematics, they understand structures of expressions. So, without help, they don't understand that in $$\alpha^2\beta$$ you can read it as $$\alpha^2\beta^1$$, and therefore your pattern doesn't match. If you include in f a term like $$\alpha^2 \beta^2$$ your rule will kill it.

To help Mathematica understand what you meant you can tell it that it can assume the default in the pattern matching. (the default depends on the thing being matched).

To do that, you add a . to the end of each part of the variable pattern being matched. So, you are looking for

rule = \[Alpha]^i_.*\[Beta]^j_.:>0/;(i+j)>2;


which indeed gives the result you want.

• the [Alpha] ought to be \[Alpha] Commented Jul 13 at 14:43
• Thanks for the answer. This updated rule still doesn't seem to get rid of the 0.5* [Alpha]*[Beta]^2 term. Is there a reason why? Commented Jul 13 at 14:51
• Never mind. That way me being stupid. Thank you very much! Commented Jul 13 at 15:19
• No problem! Glad to help! Commented Jul 13 at 15:52

Assuming the parts of the expression will consist of things that look like $$\alpha ^i \beta^j$$ for non-negative $$i , j$$ We can also use Select:

expr = α β + α^2 + β^2 + α β^2/2 + α^2 β;
vars = {α, β};

Select[expr, Total@Exponent[#, vars] <= 2 &]

(*α^2 + α β + β^2*)


If \[Alpha], \[Beta] are small of equal order try

fin = \[Alpha] \[Beta] + \[Alpha]^2 + \[Beta]^2 + \[Alpha] \[Beta]^ 2/2 + \[Alpha]^2  \[Beta]
fout=Normal[Series[fin /. {\[Alpha] -> eps \[Alpha], \[Beta] -> eps \[Beta]}, {eps, 0,2}]] /. eps -> 1
(*\[Alpha]^2 + \[Alpha] \[Beta] + \[Beta]^2*)

• That works. Thanks a lot! But I wonder what is wrong with my rule. Understand that would probably help me understand how symbolic notation in Mathematica works Commented Jul 13 at 15:12
expr = α β + α^2 + β^2 + α β^2/2 + α^2 β;

vars = {α, β};


Another way is to use PolynomialDegree by Dennis M Schneider and DeleteCases:

PolynomialDegree = ResourceFunction["PolynomialDegree"];

DeleteCases[expr, x_ /; PolynomialDegree[x, vars] > 2]


α^2 + α β + β^2

Comments about the use of pattern α^i_.*β^j_.:

This pattern matches any product of α and β with optional exponents. If the exponents are not specified, they default to 1, e.g.:

ReplaceAll[α  β^3, α^i_.  β^j_. :> {i, j}]


{1, 3}

Note that in FullForm α is represented without an explicit exponent, while β^3 shows the exponent:

FullForm[α β^3]


Times[α, Power[β, 3]]

Finally, compare the pattern with and without the use of the point, taking FullForm in each case:

FullForm[α^i_  β^j_]

FullForm[α^i_.  β^j_.]


Times[Power[α, Pattern[i, Blank[]]], Power[β, Pattern[j, Blank[]]]]

Times[Power[α, Optional[Pattern[i, Blank[]]]], Power[β, Optional[Pattern[j, Blank[]]]]]