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.
Functions:
Take[]
Length[]
Position[]
FullForm[]
TreeForm[]
Thanks anyway.
Best.
>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}
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?
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
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.
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