Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sum of terms --> list

724 views
Skip to first unread message

uno

unread,
Jun 26, 2010, 3:09:58 AM6/26/10
to
Hi,

I have an expression that is a sum of terms, like
a+b+c
and I want to convert it to a list, with as many elements as the
number of terms in the sum of terms, and with each element being each
of the terms, like
{a,b,c}

Also, the same to go from a product of terms like
a*b*c
to a list like
{a,b,c}

Is there any way, in Mathematica, to do this?
I've been looking for an answer in the Help, and in the web, with no
help.

I ask this because "a*b+c+e*f*g" needs to be evaluated by an external
(.NET) program, which is not able to parse expressions. I have to
"break" everything inside Mathematica, before sending information to
that external program, which will evaluate that expression many times,
for different values of {a,b,c,d,e,f,g}.

Thank you.

uno

unread,
Jun 26, 2010, 8:41:56 AM6/26/10
to
I found the answer, if anyone is interested.

Functions:

Take[]
Length[]
Position[]
FullForm[]
TreeForm[]

Thanks anyway.
Best.

Bill Rowe

unread,
Jun 27, 2010, 4:56:58 AM6/27/10
to
On 6/26/10 at 3:09 AM, dxcq...@gmail.com (uno) wrote:

>I have an expression that is a sum of terms, like a+b+c and I want to
>convert it to a list, with as many elements as the number of terms in
>the sum of terms, and with each element being each of the terms, like
>{a,b,c}

The way to do this is to use Apply. That is

In[1]:= sum = a + b + c;
List @@ sum

Out[2]= {a,b,c}

Mathematica stores sum as:

In[3]:= FullForm[sum]

Out[3]//FullForm= Plus[a,b,c]

The @@ (shorthand for Apply) replaces the head Plus in sum with
the head List yielding the desired result. And since this works
on any expression, your product can be converted to a list in
the same manner

>Also, the same to go from a product of terms like a*b*c to a list like
>{a,b,c}

In[4]:= prod = a b c;
List @@ prod

Out[5]= {a,b,c}


Bob Hanlon

unread,
Jun 27, 2010, 4:58:54 AM6/27/10
to

expr = a + b + c;

List @@ expr

{a,b,c}

expr /. Plus -> List

{a,b,c}

expr = a*b*c;

List @@ expr

{a,b,c}

expr /. Times -> List

{a,b,c}

expr = a*b + c + e*f*g;

List @@ expr

{a b,c,e f g}

List @@ (List @@@ expr) // Quiet

{c,{a,b},{e,f,g}}

expr /. {Times -> List, Plus -> List}

{{a,b},c,{e,f,g}}


Bob Hanlon

---- uno <dxcq...@gmail.com> wrote:

=============
Hi,

I have an expression that is a sum of terms, like
a+b+c
and I want to convert it to a list, with as many elements as the
number of terms in the sum of terms, and with each element being each
of the terms, like
{a,b,c}

Also, the same to go from a product of terms like


a*b*c
to a list like
{a,b,c}

Is there any way, in Mathematica, to do this?

uno

unread,
Jun 28, 2010, 2:29:56 AM6/28/10
to
Thanks a lot, Bill and Bob :-)

Murray Eisenberg

unread,
Jun 28, 2010, 2:30:20 AM6/28/10
to
Very easy -- and you'll doubtless get lots of replies to this.

Apply[List, a + b + c]
{a, b, c}

(* same thing using special input form: *)
List @@ (a + b + c)
{a, b, c}

List @@ (a*b*c)
{a, b, c}

If, however, your expression involves several different operations,
things get more complicated. You could then, e.g., use Apply at various
levels.

On 6/26/2010 3:09 AM, uno wrote:
> Hi,
>
> I have an expression that is a sum of terms, like
> a+b+c
> and I want to convert it to a list, with as many elements as the
> number of terms in the sum of terms, and with each element being each
> of the terms, like
> {a,b,c}
>
> Also, the same to go from a product of terms like
> a*b*c
> to a list like
> {a,b,c}
>
> Is there any way, in Mathematica, to do this?
> I've been looking for an answer in the Help, and in the web, with no
> help.
>
> I ask this because "a*b+c+e*f*g" needs to be evaluated by an external
> (.NET) program, which is not able to parse expressions. I have to
> "break" everything inside Mathematica, before sending information to
> that external program, which will evaluate that expression many times,
> for different values of {a,b,c,d,e,f,g}.
>
> Thank you.
>

--
Murray Eisenberg mur...@math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower phone 413 549-1020 (H)
University of Massachusetts 413 545-2859 (W)
710 North Pleasant Street fax 413 545-1801
Amherst, MA 01003-9305

Richard Fateman

unread,
Jun 28, 2010, 2:42:59 AM6/28/10
to

If you want to send a Mathematica expression that is already parsed, use
FullForm.
Even {a,b,c} needs to have some parsing, and a FullForm expression
needs very little more.


While List@@(a+b I) gives {a, I*b},

note that List@@(3+ 4 I) does not give {3, I*4}.

This is supposed to be a feature.

Richard Fateman

unread,
Jun 29, 2010, 6:57:45 AM6/29/10
to

Nasser M. Abbasi

unread,
Jul 1, 2010, 8:26:56 AM7/1/10
to

it seems in the second case, this happens becuase the expression was
evaluated first? By holding evaluation, one can get the same result as
in the first case:

List @@ (a + b*I)
{a, I*b}

List @@ (3 + 4*I)
3 + 4*I

ReleaseHold@(List @@ (3 + Hold@4*I))
{3,4 I}


--Nasser

0 new messages