From: uno on 26 Jun 2010 03:09 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 26 Jun 2010 08:41 I found the answer, if anyone is interested. Functions: Take[] Length[] Position[] FullForm[] TreeForm[] Thanks anyway. Best.
From: Bill Rowe on 27 Jun 2010 04:56 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 27 Jun 2010 04:58 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 28 Jun 2010 02:29 Thanks a lot, Bill and Bob :-)
|
Next
|
Last
Pages: 1 2 3 Prev: and sampling a distribution Next: coordinates on a single line of output corresponding to each |