MathGroup Archive 2001

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

Search the Archive

Re: Finding variables in a very long expression

  • To: mathgroup at
  • Subject: [mg31391] Re: Finding variables in a very long expression
  • From: "Ersek, Ted R" <ErsekTR at>
  • Date: Wed, 31 Oct 2001 19:59:12 -0500 (EST)
  • Sender: owner-wri-mathgroup at

Jose Flanigan wanted to know how to find all variables in an expression.
Consider expr below.

   expr=Sqrt[x*y]+x*z+w Sqrt[x]z^(1/n)+Pi/3;

The simple line below almost works, but the list includes Pi which isn't a
variable.  The third argument of Cases here is a level specification.  You
can read about Cases and level specification at my web site (URL below).

   Union[Cases[expr, _Symbol, {0, -1}]]

   {n, Pi, v, x, y, z}

The next line eliminates symbols such as Pi.

   Union[Cases[expr, _Symbol?( !NumericQ[#]& ), {0, -1} ]]

   {n, v, x, y, z}

--- The Plot Thickens ---
At the 1999 Developer Conference Robby Villegas explained that this sort of
thing gets tricky when you are working with symbols that were previously
removed.  Consider the lines below where the symbol (v) is removed.  If I
had my way removing a symbol would make the kernel work as if the symbol
never had any values, but that's not the way it works.


   Union[Cases[expr, _Symbol?( !NumericQ[#]& ), {0, -1} ]]

   {n, Removed[v], x, y, z}

If you want to make sure your list of variables is free of Removed Symbols
use the next line (based on Robby Villegas talk). 

   Union[Cases[expr, _Symbol?( !NumericQ[#] && NameQ[ToString[#]]& ), {0,
-1} ]]

   {b, x, y, z}

   Ted Ersek
  Check Mathematica Tips, Tricks at

  • Prev by Date: Re: select data in formatted datafiles
  • Next by Date: Re: Rijndael
  • Previous by thread: AW: graphics on the same line
  • Next by thread: how to be as efficient as Sort[list] when list is {{x1,y1},{x2,y2},...}