Re: Get list of function variables?
- To: mathgroup at smc.vnet.net
- Subject: [mg83285] Re: [mg83258] Get list of function variables?
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Sat, 17 Nov 2007 05:09:35 -0500 (EST)
- References: <200711161030.FAA04624@smc.vnet.net>
On 16 Nov 2007, at 19:30, Jason Quinn wrote: > This seems simple but how do I get the arguments to an arbitrary > function? Ideally something like getVariables[f] that just accepts the > function name as its argument. > > If I have f[x_,y_]:=a*x^2+b*y^2, getVariables[f] should return {x,y} > If I have g[n_,m_]:=a*n^2+b*m^2, getVariables[g] should return {n,m} > If I have h[w_,x_,y_,z_]:=A*w*x*y*z+B, getVariables[h] should return > {w,x,y,z} > > Thanks for any suggestions, > Jason > To tell the truth, I do not think you question makes much mathematical sense. Note that f[x_]:=x^2 and f[z_]:=z^2 are two definitions of exactly the same function but they have different "variables" so it does not make sense to speak of the "variables" of f. What you see is not really any kind of "variables" but Mathematica patterns used to define a "function". However, if you really want to find names of the patterns used in the definition, it can be done, e.g.: patternNames[f_] := First /@ Cases[DownValues[f], HoldPattern[Verbatim[HoldPattern][f[z__]]] :> z, Infinity] Now: f[x_, y_] := a*x^2 + b*y^2 patternNames[f] {x, y} h[w_, x_, y_, z_] := A*w*x*y*z + B patternNames[h] {w, x, y, z} Andrzej Kozlowski
- References:
- Get list of function variables?
- From: Jason Quinn <jason.lee.quinn@gmail.com>
- Get list of function variables?