I would like to get a List
(ideally a set -- discarding repetition -- but assuming there's no direct way to do this I'll just use Union
) of the leaves from a given expression.
For example, the expression
ArcTan[(-1 + 2*x)/Sqrt[3]]/Sqrt[3]
has a LeafCount
of 18:
so I would like something like
{-1, 2, 3, x, ArcTan, Plus, Power, Rational, Times}
Actually, I really just want the functions so
{ArcTan, Plus, Power, Rational, Times}
would be ideal -- but presumably there's some not-too-difficult way to filter these when I have them.
I've had some luck with
H[s_] := If[LeafCount[s] == 1, s, Head[s]]
H /@ Level[expr, 1, Heads -> True]
H /@ Level[expr, 2, Heads -> True]
(* ... *)
but I feel like there must be a better way.
You could use Cases
for this:
In[176]:=
Cases[ArcTan[(-1 + 2*x)/Sqrt[3]]/Sqrt[3], h_[___] :> h,
{0,Infinity}] // DeleteDuplicates
Out[176]= {Rational, Power, Times, Plus, ArcTan}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With