Re: Re: Extracting terms from an equation

*To*: mathgroup at smc.vnet.net*Subject*: [mg69830] Re: [mg69786] Re: Extracting terms from an equation*From*: Andrzej Kozlowski <akoz at mimuw.edu.pl>*Date*: Sat, 23 Sep 2006 23:45:53 -0400 (EDT)

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] True but In[16]:= MemberQ[#,x]&[a x^2] Out[16]= False 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] Times[a,Power[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, In[19]:= MemberQ[#,x,Infinity]&[a x^2] Out[19]= True So if you want MemberQ to work above just as !FreeQ does you should evaluate: In[20]:= Cases[a + b*x^2 + c*y^2 + d*x*y + e*x^3, _?(MemberQ[#1, x, Infinity] & )] Out[20]= {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: In[22]:= Cases[a + b*x^2 + c*y^2 + d*x*y + e*x^3, _?( !FreeQ[#1, x,{1}] & )] Out[22]= {d x y} So actually they are quite equivalent. Andrzej On 23 Sep 2006, at 17:44, dimmechan at yahoo.com 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 >