0

Recently I have met with some confusing abnormalities when trying to determine if there is a specific operator (usually Plus[] ) in an expression by programming. The method I came up with was to use Count[expr, Operator Function[__], levelspec],and the levlespec is 1 by default so it can be omitted for my purpose. And my notion was that, positive results greater than 0 indicates the existence of such operator.

The confusing abnormalities I have encountered are:

1.The results counting from the expr itself and all other known forms are in discrepancy. Previously I thought when doing Count[], what MMA counts is the FullForm of the expr, but it turned out to be false since the counting result from FullForm is usually the same with other known forms but just at odds with that from the expr itself. So I was wondering what on earth form does MMA count from when doing it on the expr itself.

For instance,

With[{expr = 1/Sqrt[(x - a)^2 + y^2 + z^2]}, 
 Table[Count[i, 
   Plus[__]], {i, {FullForm[expr], StandardForm[expr], 
    InputForm[expr], OutputForm[expr], TraditionalForm[expr], expr}}]]

returns {1, 1, 1, 1, 1, 2}.

With[{expr = x^3 + y^3 + z^3}, 
 Table[Count[i, 
   Plus[__]], {i, {FullForm[expr], StandardForm[expr], 
    InputForm[expr], OutputForm[expr], TraditionalForm[expr], expr}}]]

returns {1, 1, 1, 1, 1, 3}.

2.When there is totally no such operators in the expr, the Count[] can still give me a positive result greater than 0, how come? At some point, it made me feel my computer was haunted.

For example,

With[{expr = z r^2}, 
 Table[Count[i, 
   Plus[__]], {i, {FullForm[expr], StandardForm[expr], 
    InputForm[expr], OutputForm[expr], TraditionalForm[expr], expr}}]]

and

With[{expr = Sqrt[a/b]}, 
 Table[Count[i, 
   Plus[__]], {i, {FullForm[expr], StandardForm[expr], 
    InputForm[expr], OutputForm[expr], TraditionalForm[expr], expr}}]]

both return{1, 1, 1, 1, 1, 2}.But the exprs z r^2 and Sqrt[a/b] do not have any Plus[__] at all, how can Count[] find 1 or 2 Plus[__] in them?

Any ideas about the two abnormalities found above?

AlbertLew
  • 483
  • 2
  • 7
  • You are barking up the wrong tree. Your problem does not is not the expression, but the fact that your pattern is evaluated. To prevent this enclose it in "HoldPattern" – Daniel Huber Apr 20 '22 at 15:07
  • Possible duplicate: (241301). Also related: (251177) – Michael E2 Apr 20 '22 at 15:29
  • Thanks @DanielHuber and @Michael E2, I have realized the key issue here. I had long noticed the function HoldPattern[] but never bothered to learn it in detail until your suggestion. I believe this time HoldPattern[] and related issues have left me a deep enough impression. – AlbertLew Apr 22 '22 at 14:36

1 Answers1

1

Your Plus[__] is getting evaluated to just __. You could wrap it in HoldPattern, or you could just use _Plus (meaning an expression with head Plus). Alternatively, since it seems you only care about the presence of something, you could use MemberQ (using a level specification of Infinity). Specifically, replace your Count expression with MemberQ[i, _Plus, Infinity]. As a side note, the *Form functions are intended for display purposes, and I'd be wary of using them for any sort of semantic/analytic computation.

lericr
  • 27,668
  • 1
  • 18
  • 64
  • Thanks @lericr, I have realized the key issue here. I had long noticed the function HoldPattern[] but never bothered to learn it in detail until your suggestion. I believe this time HoldPattern[] and related issues have left me a deep enough impression. – AlbertLew Apr 22 '22 at 14:37
  • By the way, your MemberQ[] seems attractive, but it does not work on some simple example, like MemberQ[a + b + c, _Plus, Infinity] gives me False, I don't know why. – AlbertLew Apr 22 '22 at 14:41
  • 1
    The expression a+b+c is really Plus[a, b, c], in which case the Plus is the head of the expression, and MemberQ excludes the head of the expression by default when determining membership. You can change the levelspec like this: MemberQ[a + b + c, _Plus, {0, Infinity}]. There may be other ways that are better--I wasn't suggesting that MemberQ was the ultimate solution, just an alternate that might be worth considering. – lericr Apr 22 '22 at 14:58
  • Got it. I would have never known that it can actually designate the level specification to be 0 in MemberQ[]. Thanks for your help. Have a nice day! – AlbertLew Apr 22 '22 at 15:39