Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2008

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

Search the Archive

Re: How to separate a sum into terms

  • To: mathgroup at smc.vnet.net
  • Subject: [mg92056] Re: How to separate a sum into terms
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Thu, 18 Sep 2008 06:17:49 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <gaqf4g$doa$1@smc.vnet.net>

Slava Rychkov wrote:

> I want to separate a Mathematica expression written as a sum into 
> separate terms
> E.g. for
> 
> a+b-c+f[1]-g[a]/g[b]
> 
> I want to get a list
> 
> {a,b,-c,f[1],-g[a]/g[b]}
> 
> Is there a simple way to do this?

Yes, by changing the head of the original expression from Plus to List 
thanks to the function *Apply[]*. (Keep in mind that, in Mathematica, 
everything is an expression of the form head[e1, e2, ...] where the head 
can be itself composed and possibly followed by zero, one, or more 
elements.) For instance,

In[12]:= a + b - c + f[1] - g[a]/g[b]

Out[12]=
                    g[a]
a + b - c + f[1] - ----
                    g[b]

In[13]:= % // FullForm

Out[13]//FullForm= Plus[a, b, Times[-1, c], f[1], Times[-1, g[a], 
Power[g[b], -1]]]

In[14]:= Apply[List, %]

Out[14]=
                    g[a]
{a, b, -c, f[1], -(----)}
                    g[b]

In[15]:= % // FullForm

Out[15]//FullForm= List[a, b, Times[-1, c], f[1], Times[-1, g[a], 
Power[g[b], -1]]]

Regards,
-- Jean-Marc


  • Prev by Date: Re: How to separate a sum into terms
  • Next by Date: Re: Functional programming?
  • Previous by thread: Re: How to separate a sum into terms
  • Next by thread: Re: How to separate a sum into terms