Re: Get list of function variables?
- To: mathgroup at smc.vnet.net
- Subject: [mg83295] Re: Get list of function variables?
- From: Szabolcs Horvát <szhorvat at gmail.com>
- Date: Sat, 17 Nov 2007 05:14:50 -0500 (EST)
- References: <fhjs5v$4t3$1@smc.vnet.net>
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} > "Functions" in Mathematica are completely different animals than functions in C-like languages (or functions in the mathematical sense), so your request doesn't really make sense. But take a look at DownValues[]. For example, consider the following (nonsensical) definition: f[x_, y_] := x + y f[{x_, y_}] := x y f[x__] /; Length[{x}] > 4 := "something" f[_] := 0 What should getVariables[f] return? Also, x and y are localized to f[x_,y_]:=a*x^2+b*y^2, so these symbol names are useless outside the function definition. It could have been written as f[p_,q_]:=a*p^2+b*q^2. Szabolcs