Skip to main content

pattern matching - Except[] with levelspec


I am trying to determine all the variables used in list. For This I use:


DeleteDuplicates[Cases[l1,_Symbol,-1]]

This is great except that \Pi etc. are symbols too. I want to add Except to this Cases command to discard numeric symbols but I'm not sure where to add it.


I tried different combinations, but I either get errors, or it will interpret it differently to what I intended.


Alternatively, is there a better way of achieving this?



Answer



As an example, let us consider the following expression:



expr= Log[ 3 Sin[x] + 2 Exp[Pi+ 4 a b + 1/7]];

This is not a polynomial, so the function Variables cannot be used. On level -1 we have the atoms:


Cases[expr,_, {-1}]

(* {2,E,1/7,4,a,b,\[Pi],3,x} *)

Observe that 1/7 is an atom! We restrict ourselves to symbols:


Cases[expr,_Symbol, {-1}]


(* {E,a,b,\[Pi],x} *)

This is not restrictive enough; we only want the symbols that do not have a value. Using Kuba's advice to use function composition:


Cases[ expr, _Symbol?(Not @*  NumericQ), {-1}]

(* {a,b,x} *)

Comments