Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2001
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2001

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

Search the Archive

RE: Finding variables in a very long expression (corrected)

  • To: mathgroup at smc.vnet.net
  • Subject: [mg31409] RE: Finding variables in a very long expression (corrected)
  • From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
  • Date: Sat, 3 Nov 2001 05:29:15 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

> 	Yesterday I replied to Jose Flanigan's question about 
> 	how to find all variables in an expression.
> 	In that reply I wrote ....
> 	-------------
> 	> Consider expr below.
> 	> 
> 	> In[1]:=
> 	>   expr=Sqrt[x*y]+x*z+w Sqrt[x]z^(1/n)+Pi/3;
> 	>   w=v;
> 	>
> 	> 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).
> 	>
> 	> In[3]:=
> 	>   Union[Cases[expr, _Symbol, {0, -1}]]
> 	>
> 	> Out[3]=
> 	>   {n, Pi, v, x, y, z}
> 	>
> 	>
> 	> The next line eliminates symbols such as Pi.
> 	>
> 	> In[4]:=
> 	>   Union[Cases[expr, _Symbol?( !NumericQ[#]& ), {0, -1} ]]
> 	>
> 	> Out[4]=
> 	>   {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.
> 	>
> 	> In[5]:=
> 	>   Remove[v]
> 	>
> 	> In[6]:=
> 	>   Union[Cases[expr, _Symbol?( !NumericQ[#]& ), {0, -1} ]]
> 	>
> 	> Out[6]=
> 	>   {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). 
> 	>
> 	> In[7]:=
> 	>   Union[Cases[expr, _Symbol?
> 	>              ( !NumericQ[#] && NameQ[ToString[#]]& ), {0, -1} 
> 	>         ]]
> 	>
> 	> Out[7]=
> 	>   {b, x, y, z}
> 	>
> 	>
> 	-----------------------
> 	However in the the next example the approach used 
> 	above doesn't find the symbols (f) and (g).
> 
> 	In[1]:=  expr=D[ x^3+f[x]+g'[x]+Abs[x], x];
> 
> 
> 	In[2]:=  Union[Cases[expr, _Symbol?(!NumericQ[#]&), {0,-1} ] ]
> 
> 	Out[2]=  {x}
> 
> 
> 	The next line returns a list of symbols we are differentiating.
> 	This line really demontrates how versitle Cases[..] can be. You 
> 	could use Union on the result to elliminate duplicates if there 
> 	are any.
> 
>   In[3]:= Cases[expr, Derivative[_][func_]:>func, {0,-1}, Heads->True]
> 
>   Out[3]=  {Abs, f, g}
> 
> 
>   -----------
>   The next few lines give another approach.  In the first line below 
>   I don't bother to ensure I only get non-numeric symbols because 
>   symbols such as Pi will be discarded with my last step.
> 
>   In[4]:= lst=Union[ Cases[expr, _Symbol, {0,-1}, Heads->True] ]
> 
>   Out[4]=  {Abs, Derivative, f, g, Plus, Power, Times, x}
> 
> 
>   You probably only want the user symbols {f, g, x}. 
>   The code below will take care of that.
> 
>   In[5]:= MyVariables=Names["Global`*"];
>           Select[lst, MemberQ[MyVariables, ToString[#] ]& ]
> 
> 	Out[5]:= {f, g, x}
> 
> 
> 	You could if you want use this last step on the list 
> 	{Abs, f, g} above if you prefer that approach and want 
> 	to get rid of (Abs). In todays post I don't bother to 
> 	ensure Removed symbols are discarded, but you can do 
> 	that if you wish.
> 	-------
> 
> 	Regards,
> 	   Ted Ersek
> 	  Check Mathematica Tips, Tricks at
> 	  http://www.verbeia.com/mathematica/tips/Tricks.html
> 


  • Prev by Date: Re: Finding variables in a very long expression
  • Next by Date: Re: Aligning equal signs
  • Previous by thread: RE: Re: Finding variables in a very long expression
  • Next by thread: Clearing Subscripted Variables?