MathGroup Archive 1991

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

Search the Archive

Re: Extracting variables nested in functions

  • To: mathgroup at
  • Subject: Re: Extracting variables nested in functions
  • From: PERKINS TYLER R <perkins at>
  • 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[1] and x[1] in Log[b[1], x[1]]?  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[1]:= Globs[ expr_ ] := Cases[
                Level[ expr, {-1} ],     (* "Leaves" of expr.  Excludes heads. *)
                _Symbol ? (Context[#] == "Global`" &)	(* Match Global`s only *)

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

Out[2]= {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[3]:= Globs[ x y[Globs] ]

Out[3]= {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[
	Level[ expr, {-1}, Heads->True ],
	_Symbol ? (                                          (* Mma. 2.0 only! *)
		DownValues[#] == {} && UpValues[#] == {} &

	Tyler Perkins
	perkins at

  • Prev by Date: Re: Switch and List: How to combine?
  • Next by Date: Mathematica: A Practical Approach (A Workshop)
  • Previous by thread: Re: Extracting variables nested in functions
  • Next by thread: Question about x_h