MathGroup Archive 1999

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

Search the Archive

Re: Re: Variables[] in general expressions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg20053] Re: [mg20037] Re: [mg20024] Variables[] in general expressions
  • From: "Andrzej Kozlowski" <andrzej at tuins.ac.jp>
  • Date: Wed, 29 Sep 1999 03:33:19 -0400
  • Sender: owner-wri-mathgroup at wolfram.com

I just realized the correct answer to this question is a bit controversial.
As a mathematician I would think that a function can be a variable just as
well as anything else so I do not think my previous answer is really right.
For example, what are the variables in f[x,g[y,z]]? I would say they should
be {f,x,g,y,z} ( though I suppose some would only consider {x,y,z} to be the
right answer).

Anyway, a version of my code that gives what I now think is the right answer
is:

variables[expr_] :=
Union[Cases[expr, _Symbol?(FreeQ[Attributes[#], Protected] &),
Infinity,Heads->True]]

This gives

In[25]:=
variables[f[x, g[y, z]]]
Out[25]=
{f, x, g, y, z}
--
Andrzej Kozlowski
Toyama International University
JAPAN
http://sigma.tuins.ac.jp
http://eri2.tuins.ac.jp


----------
>From: "Andrzej Kozlowski" <andrzej at tuins.ac.jp>
>To: mathgroup at smc.vnet.net
>Subject: [mg20053] [mg20037] Re: [mg20024] Variables[] in general expressions
>Date: Sat, 25 Sep 1999 18:46:04 -0400
>

> Here is one definition that seems to work:
>
> In[12]:=
> variables[expr_] :=
>   Cases[expr, _Symbol?(FreeQ[Attributes[#], Protected] &), Infinity]
>
> The idea is that a variable should be something that is a Symbol but does
> not have the attribute Protected, which all built-in symbols like Plus,
> Power etc have.
>
> It works in your example:
>
> In[13]:=
> variables[y + Exp[x]]
> Out[13]=
> {x, y}
>
> I think it will always work unless WRI has forgotten to give the attribute
> protected to some built-in symbol.
> --
> Andrzej Kozlowski
> Toyama International University
> JAPAN
> http://sigma.tuins.ac.jp
> http://eri2.tuins.ac.jp
>
>
> ----------
>>From: Ken Rice <kmr1004 at hermes.cam.ac.uk>
To: mathgroup at smc.vnet.net
> To: mathgroup at smc.vnet.net
>>To: mathgroup at smc.vnet.net
>>Subject: [mg20053] [mg20037] [mg20024] Variables[] in general expressions
>>Date: Sat, Sep 25, 1999, 3:40 PM
>>
>
>> Hi there,
>>
>> Is there any way of extracting a list of variables from a general
>> expression, not just a polynomial? I want something which will do,
>> e.g.
>>
>> In AllVariables[y + Exp[x]]
>> Out {y,x}
>>
>> Apologies if I'm just being dim.
>>
>> Ken Rice
>> _____________________________________________________
>> Kenneth Rice
>> Churchill College
>> Cambridge
>> CB3 0DS, UK
>> E-mail: kmr1004 at cam.ac.uk
>>
>>
> 


  • Prev by Date: Calculus of variations
  • Next by Date: Mathematica 4.0.1 Front End crashes my Mac
  • Previous by thread: Re: Variables[] in general expressions
  • Next by thread: Keyboard trouble in Mathematica 3.0