3
$\begingroup$

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?

$\endgroup$
  • 1
    $\begingroup$ Something like DeleteDuplicates[Cases[l1 , _?(Not[NumericQ[#]] &), {-1}]]? Observe the curly brackets around the level specification. $\endgroup$ – Fred Simons Jan 31 '16 at 16:59
  • $\begingroup$ What about Variables? $\endgroup$ – Kuba Jan 31 '16 at 17:17
  • 1
    $\begingroup$ @FredSimons In such cases, I really like operators: _?(Not@*NumericQ) $\endgroup$ – Kuba Jan 31 '16 at 17:20
  • $\begingroup$ @Kuba I agree, with both your comments. $\endgroup$ – Fred Simons Jan 31 '16 at 17:32
  • 1
    $\begingroup$ Thanks. Feel free to add it as answer? $\endgroup$ – Shb Jan 31 '16 at 17:56
4
$\begingroup$

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} *)
$\endgroup$
1
$\begingroup$

You can use WolframLanguageData with Alternatives to built the Except.

numConstFunc = Except[Alternatives @@ Symbol /@
    WolframLanguageData[
      EntityClass[
       "WolframLanguageSymbol", {"FunctionalityArea", "NumericConstantFunctions"}], 
     "Name"], _Symbol];

Then

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

Cases[expr, numConstFunc, {-1}]
(* {a, b, x} *)

Hope this helps.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.