       Re: Extracting variables nested in functions

• To: mathgroup at yoda.physics.unc.edu
• Subject: Re: Extracting variables nested in functions
• From: PERKINS TYLER R <perkins at spot.colorado.edu>
• Date: Tue, 15 Oct 91 17:46:35 -0600

```>      Does anyone know a method for extracting the variables from an expression
> which has some of its variables buried in functions, such as x in Cos[x/Pi],
> or b and x in Log[b, x]?  The built-in Variables seems to look for
> subexpressions in terms of which the expression is a _polynomial_, so it
> includes parts like Cos[x/Pi] in the list of "variables" it returns.
>
>                                         Robby Villegas
>                                         Knox College

Try the following:

In:= Globs[ expr_ ] := Cases[
Level[ expr, {-1} ],     (* "Leaves" of expr.  Excludes heads. *)
_Symbol ? (Context[#] == "Global`" &)	(* Match Global`s only *)
]

In:= Globs[ 2 Cos[x/Pi] y^2 z ]

Out= {y, z, x}

But note that this WILL list Global` variables which happen to have rules
associated with them, as long as they don't appear as the head of a subexpression
of expr.  For instance,

In:= Globs[ x y[Globs] ]

Out= {x, Globs}

Notice that y is NOT listed, which might indeed be a variable of interest, and
Globs IS listed, which is probably not of interest, because it has rules
associated with it.  I think the following would fix this, but only in 2.0:
(I don't have 2.0 yet, so I can't test it.)

Globs[ expr_ ] := Cases[
_Symbol ? (                                          (* Mma. 2.0 only! *)
DownValues[#] == {} && UpValues[#] == {} &
)
]

Tyler Perkins