I have a long list of generated values from which I want to remove "weird" ones like Sin[Sin[1]] and Pi^2/4. However, trying to remove the powers of Pi fails on this value:

MatchQ[Sqrt[2 Pi], (_ Pi)^_] (* False *)

The FullForm of the expression is

Power[Times[2, Pi], Rational[1, 2]]

I can only match the expression when I explicitly specify the 2:

 (_Integer Pi)^_  (* False *)
 (2 Pi)^_         (* True *)

What's going on here?

  • $\begingroup$ I found out what's happening, but I don't know why it's happening. Points to anyone who can explain. $\endgroup$ Feb 22 '15 at 21:08
  • $\begingroup$ An interesting question would be: when does (x y)^power automatically split into x^power y^power and when does it stay together? $\endgroup$
    – Szabolcs
    Feb 22 '15 at 21:59

A little more digging resulted in the answer: my pattern is being evaluated to


Using HoldPattern prevents this behavior and gives the expected result:

MatchQ[Sqrt[2 Pi], HoldPattern[(_ Pi)^_]] (* True *)
  • $\begingroup$ Interesting! I would not have hit me, that the Power-rules are applied even for patterns. Good find! $\endgroup$
    – Jinxed
    Feb 23 '15 at 1:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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