MathGroup Archive 2006

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

Search the Archive

Re: Extracting terms from an equation

  • To: mathgroup at smc.vnet.net
  • Subject: [mg69770] Re: [mg69697] Extracting terms from an equation
  • From: János <janos.lobb at yale.edu>
  • Date: Fri, 22 Sep 2006 01:04:56 -0400 (EDT)
  • References: <200609211129.HAA07668@smc.vnet.net>

On Sep 21, 2006, at 7:29 AM, 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

Here is a newbie diving approach.  I am sure it can be somehow  
functionalized, but not by me and not today:

In[1]:=
aa = myEq == Exp[a + b*x^2 +
       c*y^2 + d*x*y + e*x^3];

First I find all the unique positions where x can be found in the  
equation.
In[2]:=
mmbrs = Union[Flatten[
     Table[Position[aa, x, i],
      {i, 1, Depth[aa]}], 1]]
Out[2]=
{{2, 2, 4, 2}, {2, 2, 2, 2,
    1}, {2, 2, 3, 2, 1}}

Then I dive deeper and deeper into every item where such position was  
found and chop away any surrounding layer that is not a "sum of  
parts".  "Sum of part"s are those forms that have a Head of Plus.   
When I find such "sum of parts" than I just reach a level deeper and  
Sow your "pearl" right from there.
In[3]:=
Flatten[First[Last[
     Reap[i = 1; While[
        i <= Length[mmbrs],
        cur = mmbrs[[i]];
         j = 1; While[
          j <= Length[cur],
          If[Head[First[
           Extract[aa, {Take[
           cur, j]}]]] ==
           Plus, Sow[Extract[
           aa, {Take[cur,
           j + 1]}]]; Break[
           ]]; j++; ];
         i++; ]]]]]
Out[3]=
{d*x*y, b*x^2, e*x^3}

János


----------------------------------------------
Trying to argue with a politician is like lifting up the head of a  
corpse.
(S. Lem: His Master Voice)


  • Prev by Date: Re: Warning Message wrt NMinimize function
  • Next by Date: Re: Warning Message wrt NMinimize function
  • Previous by thread: Extracting terms from an equation
  • Next by thread: Re: Extracting terms from an equation