4
$\begingroup$

If I have an expression, let us say something like this:

Cos[3 x] + π/cofe Exp[y^(c z)] 

how can I get all non-number symbols in the expression which are:

{x, cofe, y, c, z} 

I tried this:

Cases[Cos[3 x] + π/cofe Exp[y^(c z)], _Symbol] 

but it didn't work.

$\endgroup$
2
  • 1
    $\begingroup$ Possible duplicate: Is there an analogue of the Variables command for general expressions?. In short, does this do what you want?: Variables @ Level[expr, {-1}] (where expr is your expression). $\endgroup$ Commented Jun 5, 2014 at 8:02
  • $\begingroup$ @Mr.Wizard thank you. this is what I am looking for. $\endgroup$ Commented Jun 5, 2014 at 15:47

1 Answer 1

11
$\begingroup$
expr = Cos[3 x] + \[Pi]/cofe Exp[y^(c z)]; Cases[expr, _Symbol?(! NumericQ[#] &), Infinity] // Union (* {c, cofe, x, y, z} *) 

In this case the Union is not necessary; however, in general it is needed to eliminate duplicates.

$\endgroup$
2
  • $\begingroup$ This is a nice answer and I am puzzled there is no built-in for this sort of thing but Variables (for polynomial expressions) is the closest there is. (+1) $\endgroup$ Commented Jun 5, 2014 at 7:33
  • $\begingroup$ An alternative: Cases[expr, Except[_?NumericQ, _Symbol], ∞] // Union $\endgroup$ Commented Jun 6, 2014 at 0:53

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.