Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1999

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

Search the Archive

Re: Variables[] in general expressions

  • To: mathgroup at
  • Subject: [mg20041] Re: Variables[] in general expressions
  • From: "Allan Hayes" <hay at>
  • Date: Sat, 25 Sep 1999 18:46:06 -0400
  • References: <7shptg$>
  • Sender: owner-wri-mathgroup at

Ken Rice <kmr1004 at> wrote in message
news:7shptg$1r2 at
> 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

What is a variable? For mathematics, taking it to be a symbol that is not a
head and is not a
system constant, and allowing for some other exclusions, we might use

AllVariables[expr_, exclusions_List:{}] :=
  Complement[Cases[expr, _Symbol, {-1}, Heads -> True],
    Cases[expr, h_Symbol[___] -> h, Infinity, Heads -> True],
    {Catalan, Degree, E, EulerGamma, Glaisher, GoldenRatio, Khinchin, Pi},
    ]This gives

AllVariables[y + Exp[x]]

{x, y}

AllVariables[y + Exp[x], {x}]


Note that Exp[a] is converted to E^a on evaluation.

Exp[a] // InputForm


However we might also want to allow for programming constructs like
With[{x=a} Sin[x]]

Allan Hayes
Mathematica Training and Consulting
Leicester UK
hay at
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565

  • Prev by Date: Re: Select cell
  • Next by Date: Re: TeXForm in v4 better?
  • Previous by thread: Re: Variables[] in general expressions
  • Next by thread: Re: Variables[] in general expressions