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?
HoldPattern[]but never bothered to learn it in detail until your suggestion. I believe this timeHoldPattern[]and related issues have left me a deep enough impression. – AlbertLew Apr 22 '22 at 14:36