Re: Get list of function variables?
- To: mathgroup at smc.vnet.net
- Subject: [mg83576] Re: Get list of function variables?
- From: Albert <awnl at arcor.net>
- Date: Fri, 23 Nov 2007 05:34:58 -0500 (EST)
- References: <fhjs5v$4t3$1@smc.vnet.net> <fhu7j5$7b1$1@smc.vnet.net> <fi3jvl$bqs$1@smc.vnet.net>
Hi Jason > I am aware that the "solutions" I posted do not hold in general. My > basic desire for the getVariables function was to help generate a new > function with those variables (and another set of the same size). I > wanted a notebook that could give the the error function for ANY > function the user inputed without having to do any work. Maybe there's > a better way than how I did it (but I have a working notebook so I'm > better off than I was). > > In other words give some function, say, the price to carpet an "x" by > "y" by "z" room at "w" per square unit area: > f(w,x,y,z)=w(x*y*z) > I want the sigma error on f given sigma errors on the variables. So a > new function "ef" > ef(x,y,z,ex,ey,ez) > where ef is defined by the usual error propagation equation a sum over > partial derivatives squared. The "e" prefix denotes the sigma error on > the corresponding variable. Note the new function recognizes w as a > parameter. > > If I went about this in some ass-backwards fashion by grabbing the > variables from f directly, I'm a little bit embarrassed but would be > interested in knowing a better way. I think for what you want to achieve you should really look into pure functions and define your functions as those, e.g.: f=Function[{w,x,y,z},w[x*y*z]] those are easier to pass around as objects (since you don't need to care about wasting your namespace with symbols that hold the downvalues), the function getVariables is much simpler, since you just need to extract the first argument: getVariables[f_]:=First[f] or maybe if you want your code to be more robust: getVariables[f]:=Extract[f,{1},Hold] of course then you need to handle the extra Hold... Finally when you need to calculate derivatives, this is also very convenient and can be done with using either named or positional arguments, where the later directly returns a function again, so you don't need to mess around with reconstructing functions from expressions in a save way: D[f[w,x,y,z],x] or with positional arguments: Derivative[0, 1, 0, 0][f] hth, albert