Suppose one defines a generic function
f[a_, b_, c_]:= a + b*c
Is there a built in function that allows to get the dependent variables? Like:
Variables[f[x, y, z]] = {x, y, z}
Thanks!
The simplest version of this would probably be something like this:
SetAttributes[Arguments, HoldAll];
Arguments[_[args___]] := HoldForm[{args}]
I used Arguments
because Variables
is a built in system symbol (although, if you know exactly what kind of functions you'll use with this, you might find that Variables
actually works for you). To demonstrate,
x = 7;
Arguments[f[x, y, z]]
(* {x, y, z} *)
Even though this looks like a simple list, if you look at the FullForm
of the result, it's this:
HoldForm[List[x, y, z]]
I don't know if that's what you want. I don't know exactly where you want to interrupt evaluation. There are many other versions depending on exactly what you want. For example, if you just want to avoid applying f
s definition but you want to allow the arguments to evaluate, then just remove the HoldForm
.
f_[args___]
, but since we don't need to reference the head, we don't need to give that pattern a name.
$\endgroup$
This is a straightforward way:
List @@ Unevaluated[f[x, y, z]]
Or for more complicated calls:
List @@ Unevaluated[f[r s, t, u]] // Variables
Or for really complicated calls:
Integrate`getAllVariables[List @@ Unevaluated[f[r Exp[s], t, u]], {}]
Probably this would work if f[..]
returns a mathematical formula (see Get all variables in an expression with Variables[])):
Integrate`getAllVariables[f[x, y, z]], {}]
But if f[]
takes a long time to evaluate, the Unevaluated[]
version will be better.
a+b*c
$\endgroup$x
has been defined as7
? Do you wantVars[f[x,y]
to return{x,y}
or{7,y}
? How should it work withVars[f[g[z],y]
?{g[z],y}
or{z,y}
? Etc. $\endgroup$Variables
that does exactly that. Is that not what you need? $\endgroup$