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: [mg92055] Re: How to separate a sum into terms
  • From: "David Park" <djmpark at comcast.net>
  • Date: Thu, 18 Sep 2008 06:17:37 -0400 (EDT)
  • References: <gaqf4g$doa$1@smc.vnet.net>

Here is your expression:

expr = (a + b - c + f[1] - g[a]/g[b]);

The Head of the expression is:

Head[expr]
Plus

But you would like to have a List instead of a sum (Plus) of the items. Do 
this by applying List to the expression. This replaces the Head, Plus, by a 
new Head, List.

Apply[List, expr]
{a, b, -c, f[1], -(g[a]/g[b])}

Or use the Mathematica shortcut, @@, for Apply:

List @@ expr
{a, b, -c, f[1], -(g[a]/g[b])}

Or Apply directly to the expression:

List @@ (a + b - c + f[1] - g[a]/g[b])
{a, b, -c, f[1], -(g[a]/g[b])}

In this particular case you could also do a simple substitution:

a + b - c + f[1] - g[a]/g[b] /. Plus -> List
{a, b, -c, f[1], -(g[a]/g[b])}

But that would not work if, for example, g had a sum for an argument. So 
Apply is the best method.


-- 
David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/


"Slava Rychkov" <slava.rychkov at gmail.com> wrote in message 
news:gaqf4g$doa$1 at smc.vnet.net...
> Hi!
>
> 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?
>
> Thanks,
> -- 
> Vyacheslav Rychkov                https://mail.sns.it/~rychkov
> Scuola Normale Superiore          Tel: +39-050-509068 (office)
> Classe di Scienze                      +39-050-3820086  (home)
> Piazza dei Cavalieri, 7                +39-3403925168 (mobile)
> 56100 Pisa                        Fax: +39-050-509045
> Italy                             E-mail: Rychkov at sns.it
> 



  • Prev by Date: Re: Problem with replacement rules
  • 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