If I want to count the number of times ^ that occurs in the expression x , this is easy:
Count[x, _Power, {0, Infinity}]
Suppose I want to count only -1 instances raised to some extent. How can i do this?
I tried
Count[(-1)^n + 2^n, _Power[-1, _], {0, Infinity}]
and even
Count[Plus[Power[-1, n], Power[2, n]], _Power[-1, _], {0, Infinity}]
but both gave 0.
Origin of the question: I create a ComplexityFunction that allows certain expressions like Power[-1, anyComplicatedExpressionHere] and Sqrt[5] (related to my problem), but severely punishes other uses of Power and Sqrt .
source share