I'm looking for a way to extract a list of variables from an expression, for example with an input like:
Leff= (mc dm^2 + mc/12*(h^2 + 3 R^2) + ma da^2 + ma/12 La^2)/(mc dm + ma da)
I want this output:
{mc, dm, ma, da, La, h, R}.
The built-in Mathematica function Variables can do this, but it doesn't work with more complex expressions containing trascendental functions. Any help would be very appreciated.
Answer
Assuming you don't have any built-in symbols in that list, you could simply do:
DeleteDuplicates@Cases[Leff, _Symbol, Infinity]
(* {da, ma, dm, mc, La, h, R} *)
If you do have symbols from built-in contexts or packages, you can simply pick out only those that are in the Global` context with:
With[{globalQ = Context@# === "Global`" &},
DeleteDuplicates@Cases[Leff, _Symbol?globalQ, Infinity]
]
If you have a different default working context (e.g. local to notebook/cell or in a package), change the pattern test to the following, instead of globalQ:
currentContextQ = Context@# === $Context &
Comments
Post a Comment