Re: extracting variables from an expression
- To: mathgroup at smc.vnet.net
- Subject: [mg44147] Re: [mg44111] extracting variables from an expression
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Fri, 24 Oct 2003 04:24:16 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
On Thursday, October 23, 2003, at 08:15 PM, Peter Schinko wrote: > Hi, > > I'm looking for a function that extracts all variables from a boolean > expression. > For example: > x || y || z > should return something similar to {x, y, z}. > > Is there a similar function in Mathematica? If so, how does it work? > > Thanks very much > Peter > > > > There is no such built-in function (the function Variables returns the variables of a polynomial). However, it is not too hard to define such a function. There are a number of ways to do this. My preferred way makes use of two facts: 1. Every "variable" should be a "symbol" 2. Almost all built in symbols (which you do not want to use as variables) have the attribute "Protected". The exceptions, such as Derivative are unlikely to cause problems. So the following definition should work in all reasonable situations: Vars[expr_] := Cases[expr, _Symbol?( !MemberQ[Attributes[#1], Protected] & ), Infinity] expr = !((x || y) && z); Vars[expr] {x, y, z} Andrzej Kozlowski Yokohama, Japan http://www.mimuw.edu.pl/~akoz/