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.
Variables, it says: "Variableslooks for variables only inside sums, products, and rational powers". That indicates to me that since it can't assume thatn+2is rational, it fails. – march Apr 18 '17 at 22:04Block[{n = 2}, Variables[w]]gives the answer you are looking for (the2can be replaced with an arbitrary integer). Does this suit your needs? – mikado Apr 18 '17 at 22:13n. Of course I can find out by hand what the variables are, but I'm wondering if Mathematica can do it for me. – Apr 18 '17 at 22:15Union@Cases[w, _Symbol,Infinity], but if you also want to get anffrom, sayf[n], then you might want to useUnion@Select[Cases[w, _Symbol, Infinity, Heads -> True], Context[#] =!= "System" &]' – Rolf Mertig Apr 18 '17 at 22:27Union@Cases[w, _Symbol,Infinity]is what I was looking for. If you post it I'll be happy to accept it as the answer. – Apr 18 '17 at 22:32Variables[Level[w, {-1}]]– Bob Hanlon Apr 18 '17 at 23:22