1
$\begingroup$

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] &
$\endgroup$
2
  • 1
    $\begingroup$ C is a reserved symbol in Mathematica. $\endgroup$
    – Syed
    Nov 17, 2022 at 12:41
  • $\begingroup$ Oh ok thanks. This was just a minimal example though. $\endgroup$
    – Albercoc
    Nov 17, 2022 at 12:46

2 Answers 2

2
$\begingroup$
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\}$$

$\endgroup$
2
  • $\begingroup$ Beautiful. Thanks! $\endgroup$
    – Albercoc
    Nov 17, 2022 at 13:02
  • $\begingroup$ I have corrected the formula for etest and updated the answer. $\endgroup$
    – Syed
    Nov 17, 2022 at 13:05
2
$\begingroup$

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]

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ Doesn't work. Doesn't pick up terms such as {a b}. Use Exponent $\endgroup$
    – Albercoc
    Feb 8 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.