MathGroup Archive 2006

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

Search the Archive

Re: Re: Extracting terms from an equation

The short answer is that MemberQ[expr,x] by default "looks" only at  
level 1 of expr but FreeQ[#,x] looks at all levels. This behaviour in  
both cases can be modified by providing level specification.  The  
long answer follows below.

You can see the difference by comparing what will happen when you  
apply !FreeQ[#,x]& and MemberQ[#,x]& to a single term, for example:

!FreeQ[#,x]&[a x^2]



MemberQ[#,x]&[a x^2]


The reason why the former returned True is that a x^2 is not free of  
x (at some level).  However, in the second case you are asking if x  
is a member of the expression

FullForm[a x^2]

FullForm[a x^2]


In this expression the Head (which is times) does not matter so you  
can replace it by List  So, in effect you are asking if x is a member  
of the list {a,x^2}. But it clearly is not a "member" of this list  
(or of the original expression at level 1). In other words the  
"default" concept of "member" in MemberQ applies only to the elements  
at level 1 of the expression you are testing. However,

MemberQ[#,x,Infinity]&[a x^2]


So if you want MemberQ to work above just as !FreeQ does you should  

Cases[a + b*x^2 + c*y^2 + d*x*y + e*x^3, _?(MemberQ[#1, x, Infinity]  
& )]

{b*x^2, e*x^3, d*x*y}

And, by the way, FreeQ by default does the opposite of MemberQ; it  
looks at all levels. So you can also make FreeQ look only at level 1  
if you want, and get the same answer as MemberQ gives:

Cases[a + b*x^2 + c*y^2 + d*x*y + e*x^3, _?( !FreeQ[#1, x,{1}] & )]

{d x y}

So actually they are quite equivalent.


On 23 Sep 2006, at 17:44, dimmechan at wrote:

> Can someone explain the difference in the following?
> Cases[a + b*x^2 + c*y^2 + d*x*y + e*x^3, _?( !FreeQ[#1, x] & )]
> {b*x^2, e*x^3, d*x*y}
> Cases[a + b*x^2 + c*y^2 + d*x*y + e*x^3, _?(MemberQ[#1, x] & )]
> {d*x*y}
> Thanks
> Î?/Î? Coleman, Mark έγÏ?αÏ?ε:
>> Greetings,
>> I'd like to find a general way to extract all of the terms from an
>> equation that involve a given variable. For instance, consider the
>> equation
>> myEquation == Exp[a + b x^2 + c y^2 + d x y + e x^3]]
>> I'd like to define a function such that
>>    myFunction[myEquation,x] returns the result {b x^2, d x y, e x^3}
>> Thanks,
>> -Mark

  • Prev by Date: Re: Re: Locating common subexpressions
  • Next by Date: Re: Table command strange output when 'i' over 16
  • Previous by thread: Re: Re: Extracting terms from an equation
  • Next by thread: Warning Message wrt NMinimize function