From: uno on
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.

From: uno on
I found the answer, if anyone is interested.

Functions:

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

Thanks anyway.
Best.

From: Bill Rowe on
On 6/26/10 at 3:09 AM, dxcqp2000(a)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}


From: Bob Hanlon on

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 <dxcqp2000(a)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?
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.




From: uno on
Thanks a lot, Bill and Bob :-)