Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Get list of function variables?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg84071] Re: Get list of function variables?
  • From: Szabolcs Horvát <szhorvat at gmail.com>
  • Date: Sun, 9 Dec 2007 06:33:04 -0500 (EST)
  • References: <fhjs5v$4t3$1@smc.vnet.net> <fhu7j5$7b1$1@smc.vnet.net> <fi3jvl$bqs$1@smc.vnet.net>

Jason Quinn wrote:
> 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.

Jason,

You do need to know the number of arguments your function takes and the 
meaning of each argument (e.g. that the first argument in your example, 
w, needs to be excluded), otherwise you couldn't use the function.  So 
with this knowledge, just plug in any symbol names into the function and 
use D[], e.g.

f[w_, x_, y_, z_] = w x y z

Sqrt[D[f[w,a,b,c], {{a,b,c}}]^2 . {ea, eb, ec}^2]

This works with any kind of function in Mathematica, a pure function, or 
one defined using patterns.

I wouldn't even use functions for this purpose, but expression:  I would 
define the function that computes the error so that it takes an 
expression (f[w,x,y,z] or simply w x y z), and not the name of a 
function (f):

error[expr_, vars_List, errs_List] := Sqrt[D[expr, {vars}]^2 . errs^2]

Use it in the following way:

error[w x y z, {x, y, z}, {ex, ey, ez}]

or

error[f[w, x, y, z], {x, y, z}, {ex, ey, ez}]

Here it is made very clear which symbols represent the function 
variables (x, y, and z), which are simply parameters (w), and which are 
the errors (ex, ey, and ez).  And the user of the function has the 
freedom to choose these names.

As I understand, you tried to avoid the user having to explicitly name 
the variables (one can generate random names instead, e.g. with 
Unique[], or try to extract some names from the function definition) and 
having to explicitly input the number of arguments the function takes 
(again, if the functions were defined in a certain way, one could try to 
extract this information from the function definition).  While it is 
possible to do this (provided that there are strict rules and 
conventions about how the function may be defined), it is error-prone, 
unreliable, and difficult to implement.  I would consider it an ugly 
hack.  Of course, this is a subjective opinion, but there are really 
many things that can go wrong:  Just imagine that the function was 
defined using a pattern named 'x', f[x_] := x^2, but later the global 
symbol 'x' was given a value.  Now if we want to extract 'x' from the 
definition of 'f' and use it to construct a new function (Function[{x}, 
...]), we would have to go through a *lot* of trouble to avoid 'x' being 
evaluated during the calculations ...

Szabolcs


  • Prev by Date: Re: Adding description to plots
  • Next by Date: Re: Adding description to plots
  • Previous by thread: Re: BarChart[{1, 2}, Frame->True]
  • Next by thread: Re: Get list of function variables?