# Unexpected behavior of Variables [duplicate]

I apologize in advance if this is a duplicate. Variables has a strange behavior when it encounters powers:

  w = s1^(n + 2) s2;
Variables[w]
(*{s1, s1^n, s2}*)


I'd have expected {s1, s2, n},

On the other hand

 w = s1^2 s2;
Variables[w]
(*{s1, s2}*)


yields what one expects. I wonder if there is a way to get the expected result in the first example.

## marked as duplicate by Bob Hanlon, m_goldberg, J. M. will be back soon♦Apr 19 '17 at 0:29

• Perhaps this is the issue: Under the Possible Issues section of the documentation for Variables, it says: "Variables looks for variables only inside sums, products, and rational powers". That indicates to me that since it can't assume that n+2 is rational, it fails. – march Apr 18 '17 at 22:04
• Block[{n = 2}, Variables[w]] gives the answer you are looking for (the 2 can be replaced with an arbitrary integer). Does this suit your needs? – mikado Apr 18 '17 at 22:13
• @march yes, thanks, that's the issue. I'm wondering if there is a way to fix this, i.e. to get the expected result. – user46676 Apr 18 '17 at 22:13
• @mikado well, this does not list n. Of course I can find out by hand what the variables are, but I'm wondering if Mathematica can do it for me. – user46676 Apr 18 '17 at 22:15
• The question is what is a variable. You could do Union@Cases[w, _Symbol,Infinity], but if you also want to get an f from, say f[n], then you might want to use Union@Select[Cases[w, _Symbol, Infinity, Heads -> True], Context[#] =!= "System" &]' – Rolf Mertig Apr 18 '17 at 22:27

So, even if you put Variables[E^x] the result will show {}. In you example, s1^(n + 2) s2 is the same as s1^n s1^2 s2 and the independet variables for that expression are s1, s1^n and s2.