Re: Extracting terms from an equation
- To: mathgroup at smc.vnet.net
- Subject: [mg69759] Re: [mg69697] Extracting terms from an equation
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Fri, 22 Sep 2006 01:04:38 -0400 (EDT)
On 21 Sep 2006, at 20:29, Coleman, Mark wrote: > 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 > This can be easily handled with Cases: Cases[Exp[a + b*x^2 + c*y^2 + d*x*y + e*x^3], _?( !FreeQ[#1, x] & ), {2}] {b*x^2, e*x^3, d*x*y} I am assuming that the order of "terms" does not matter to you, but of course if it does one could sort them according to a suitable criterion. Also, perhaps you have in mind more general kind of expressions, where what you call "terms" are not necessarily at level 2? For example, probably this is not what you want: Cases[Sin[Exp[a + b*x^2 + c*y^2 + d*x*y + e*x^3]], _?( !FreeQ[#1, x] & ), {2}] {e*x^3 + b*x^2 + d*y*x + c*y^2 + a} But this probably is: Cases[Sin[Exp[a + b*x^2 + c*y^2 + d*x*y + e*x^3]], _?( !FreeQ[#1, x] & ), {3}] {b*x^2, e*x^3, d*x*y} If you could specify exactly what is the most general expression you need to deal with and what exactly you mean by "terms", then I am sure the method can also handle that. Andrzej Kozlowski Tokyo, Japan