3

How can one add the number of powers of x in expressions like the following?

x^2f[x]g[x^3]

or

x^2g[x^4]

or

x^2g[x^2f[x^2]]

The counting is such that all these examples must return 6. I was thinking of using Count with some pattern, but I didn't manage to construct a pattern for this.

sjdh
  • 3,907
  • 8
  • 25
  • 32
  • @MitchWheat I looked up parser in the Mathematica help and at Wikipedia. Also I searched for parser + Mathematica in Stack Overflow, but it seems all rather abstract or technical to me. Can you give me a simple example of a parser in Mathematica? – sjdh Dec 17 '11 at 08:44
  • What should the `(x y)^2` return, 1 or 2? What about something like `f[x, x^2]`? Should it return 3? – Simon Dec 17 '11 at 11:16
  • 1
    @Simon (x y)^2 should return 2 and f[x, x^2] should return 3 indeed. – sjdh Dec 17 '11 at 11:39
  • @sjdh: In which case both my answer and Mr.W's answer work ok. – Simon Dec 17 '11 at 11:40

2 Answers2

3

Here's my quick hack - some of the behaviour (see the final example) might not be quite what you want:

SetAttributes[countPowers, Listable]
countPowers[expr_, symb_] := Module[{a}, 
  Cases[{expr} /. symb -> symb^a // PowerExpand, symb^n_ :> n, 
        Infinity] /. a -> 1 // Total]

Then

In[3]:= countPowers[{x^2 f[x] g[x^3], x^2 g[x^4], x^2 g[x^2 f[x^2]]}, x]

Out[3]= {6, 6, 6}

and

In[4]:= countPowers[{x^(2 I)  g[x^3], g[x, x^4], 
                       x^2 g[E^(2 Pi I x) , f[x]^x]}, x]

Out[4]= {3 + 2 I, 5, 5}
Simon
  • 14,631
  • 4
  • 41
  • 101
  • Interesting. My version appears cleaner, but does it work? I am having trouble following this one. – Mr.Wizard Dec 17 '11 at 11:30
  • I think I understand it now, and we had the same idea, but this method introduces unneeded complexity unless mine misses something. – Mr.Wizard Dec 17 '11 at 11:38
  • Yeah, my version is shit. Your soln is about 7 times faster and much neater. I originally tried to modify ruebenko's answer to use the a default `x^n_.`, but then the `x^n` contributed both as `x` and `x^n`... so I saw red and hit it with `PowerExpand`. – Simon Dec 17 '11 at 11:39
  • I wouldn't say that; it's just a little rough. – Mr.Wizard Dec 17 '11 at 11:42
  • Deja vu: http://stackoverflow.com/questions/5803432/why-message-is-not-always-traced – Mr.Wizard Dec 17 '11 at 11:50
  • @Mr.Wizard: Ha! History is mutable. (I wonder if the deleted comments are stored somewhere, or if they really do vanish into a puff of entropy?) – Simon Dec 17 '11 at 11:53
  • They are saved somewhere deep in the system. I can try to find the Meta post that states this if it interests you. – Mr.Wizard Dec 17 '11 at 11:56
  • @Mr.Wizard: That would be this meta post: http://meta.stackexchange.com/q/53480/156389 – Simon Dec 17 '11 at 11:59
  • Belated +1 (just so you know) – Mr.Wizard Dec 17 '11 at 12:26
2

Since you want to count x as an implicit power of 1, you could use this:

powerCount[x_Symbol][expr_] := 
  Tr @ Reap[PowerExpand[expr] /. {x^n_ :> Sow[n], x :> Sow[1]}][[2,1]]

powerCount[x] /@
  {
   x^2f[x]g[x^3],
   x^2g[x^4],
   x^2g[x^2f[x^2]]
  }
{6, 6, 6}

Alternatively, this could be written without Sow and Reap if that makes it easier to read:

powerCount[x_Symbol][expr_] := 
  Module[{t = 0}, PowerExpand[expr] /. {x^n_ :> (t += n), x :> t++}; t]

Either form can be made more terse using vanishing patterns, at the possible expense of clarity:

powerCount[x_Symbol][expr_] := 
  Tr @ Reap[PowerExpand[expr] /. x^n_ | x :> Sow[1 n]][[2, 1]]
Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
  • How about `powerCount[x][(x y)^(1/2)]`? – Simon Dec 17 '11 at 11:54
  • @Simon given the examples I decided to handle only non-compound instances of `x` but I suppose a case like that should be handled for robustness. Does `PowerExpand` suffice? – Mr.Wizard Dec 17 '11 at 11:59
  • It depends if the OP cares about such cases. As it is, you have probably satisfied his original request. Especially since `f[x^2]` should return 2, even though `f` could be any old crazy function. The OP's request does not make sense mathematically, so a syntactic reading is fine. – Simon Dec 17 '11 at 12:03
  • @Simon well, I added `PowerExpand` -- I'll leave it unless it is likely to cause trouble somewhere (I haven't thought that through.) – Mr.Wizard Dec 17 '11 at 12:06