0
$\begingroup$

Is there a way to distinguish between functions defined by the user and Mathematica built-in functions (such as Sin, Power etc.)?

For example, how can I select symbols and functions in a list such as

{a,Sqrt[b²+f[x]²],g[h[y]]}
(*{a,b,f,x,g,h,y}*)
$\endgroup$
0
1
$\begingroup$

I quickly hacked something together. The comment didn't helped much for me since it serves only for a special case while you want a more general functionality.

extractSmbFnc[expr_]:=(If[Head[#]=!=Symbol,Head[#],#]&)/@DeleteDuplicates[Cases[{expr},_?(If[Head[#]===Symbol,Context[#],If[MatchQ[Head[#],_Symbol],Context[Evaluate@Head[#]]]]==="Global`"&),Infinity]]

This is a simple function which can be called with the expression you want to analyze:

testList={a,Sqrt[b^2+f[x]^2],g[h[y]],Pi,Mod[x,2]};
extractSmbFnc[testList]

{a,b,x,f,y,h,g}

$\endgroup$
2
  • 1
    $\begingroup$ Symbols are not always Global` , custom packages generate symbols in their context. Also, when working with CellContext set local to Notebook there is no Global context there. I'm not sure what is wrong with discarding System` . $\endgroup$
    – Kuba
    Dec 1 '16 at 10:53
  • $\begingroup$ Nothing is wrong with it. As I said, i quickly hacked it together and my first thought laid on Global so i went with it. OP can change it to System if he needs to. $\endgroup$ Dec 1 '16 at 13:52

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