# Pattern matching with powers

The elements in my list look like A^a B^b C^c D^d , but some powers are zero or one. I am trying to write a matching pattern that selects elements such that a+b>c+d.

My naive attempt:

{A^2 B^2 C^1, B C, A B C^2 D}
% // Cases[#, A^a_ B^b_ C^c_ D^d_ /; a + b > c + d] &

• C is a reserved symbol in Mathematica.
– Syed
Commented Nov 17, 2022 at 12:41
• Oh ok thanks. This was just a minimal example though. Commented Nov 17, 2022 at 12:46

test = {a^4, a^3 b, a^3 c, a^3 d, a^2 b^2, a^2 b c, a^2 b d, a^2 c^2,
a^2 c d, a^2 d^2, a b^3, a b^2 c, a b^2 d, a b c^2, a b c d,
a b d^2, a c^3, a c^2 d, a c d^2, a d^3, b^4, b^3 c, b^3 d, b^2 c^2,
b^2 c d, b^2 d^2, b c^3, b c^2 d, b c d^2, b d^3, c^4, c^3 d,
c^2 d^2, c d^3, d^4}


$$\left\{a^4,a^3 b,a^3 c,a^3 d,a^2 b^2,a^2 b c,a^2 b d,a^2 c^2,a^2 c d,a^2 d^2,a b^3,a b^2 c,a b^2 d,a b c^2,a b c d,a b d^2,a c^3,a c^2 d,a c d^2,a d^3,b^4,b^3 c,b^3 d,b^2 c^2,b^2 c d,b^2 d^2,b c^3,b c^2 d,b c d^2,b d^3,c^4,c^3 d,c^2 d^2,c d^3,d^4\right\}$$

etest := Exponent[#, a] + Exponent[#, b] >
Exponent[#, c] + Exponent[#, d] &

Pick[test, etest /@ test]


OR

Cases[test, _?etest]


Result:

$$\left\{a^4,a^3 b,a^3 c,a^3 d,a^2 b^2,a^2 b c,a^2 b d,a b^3,a b^2 c,a b^2 d,b^4,b^3 c,b^3 d\right\}$$

• Beautiful. Thanks! Commented Nov 17, 2022 at 13:02
• I have corrected the formula for etest and updated the answer.
– Syed
Commented Nov 17, 2022 at 13:05

Make sure to add . as in _. at end of pattern

ClearAll[a, b, c, d]
lis = {a^2 b^2 c^1, b c, a b c^2 d, a^5 b c^2 d, a b^99 c^2 d,a b^3 c^2 d}
Cases[lis, a^a_. b^b_. c^c_. d^d_. /; a + b > c + d]


• Doesn't work. Doesn't pick up terms such as {a b}. Use Exponent Commented Feb 8, 2023 at 10:39