Split an expression into a set of terms

I have a long expression that I would like to break down into a set of terms. For example, I have:

a + b - c + d + 4*e - 3*f 

I want to split the expression by adding / subtracting into:

 {a, b, -c, d, 4*e, -3*f} 

My motivation for this is that I want to deal with the term of the original expression. Is it possible?

Edit: the above examples are VERY simplified compared to what I actually have in Mathematica, I'm just not sure how to write Math here.

+6
source share
4 answers

To break an expression, you need to use Level . Level gives you a list of subexpressions, and you can specify the level to which you want to return the subexpressions. In this case, you need levelspec 1.

 In[1]:= expr = a + b - c + d + 4 e - 3 f; In[2]:= Level[expr, 1] Out[2]= {a, b, -c, d, 4 e, -3 f} 

An example with a slightly more complex expression:

 In[3]:= expr2 = a^2 + 5 bc/ef - Sqrt[g - h] - Cos[i]/Sin[j + k]; In[4]:= Level[expr2, 1] Out[4]= {a^2, (5 bc)/ef, -Sqrt[g - h], -Cos[i] Csc[j + k]} 
+9
source

You can also use MonomialList if the expression is a polynomial:

 In[56]:= MonomialList[a + b - c + d + 4*e - 3*f] Out[56]= {a, b, -c, d, 4 e, -3 f} 

(Does not work on polynomials such as Yoda expr2 .)

+4
source

Since no one mentioned this, it is equivalent to building the Yoda Level[expr, 1] , you should use Apply to replace the head of the expression with List :

 In[1]:= expr = a + b - c + d + 4 e - 3 f; In[2]:= List @@ expr Level[expr, 1] == % Out[2]= {a, b, -c, d, 4 e, -3 f} Out[3]= True In[4]:= expr2 = a^2 + 5 bc/ef - Sqrt[g - h] - Cos[i]/Sin[j + k]; In[5]:= List @@ expr2 Level[expr2, 1] == % Out[5]= {a^2, (5 bc)/ef, -Sqrt[g - h], -Cos[i] Csc[j + k]} Out[6]= True 

The two methods do basically the same thing and have the same timings (using my version of the middle sync function )

 In[1]:= SetOptions[TimeAv, Method -> {"MinNum", 80000}, "BlockSize" -> 20000]; In[7]:= List @@ expr // TimeAv Total wall time is 0.244517, total cpu time is 0.13 and total time spent evaluating the expression is 0.13 The expression was evaluated 80000 times, in blocks of 20000 runs. This yields a mean timing of 1.625*10^-6 with a blocked standard deviation of 2.16506*10^-7. Out[7]= {1.625*10^-6, {a, b, -c, d, 4 e, -3 f}} In[8]:= Level[expr, 1] // TimeAv Total wall time is 0.336927, total cpu time is 0.16 and total time spent evaluating the expression is 0.16 The expression was evaluated 80000 times, in blocks of 20000 runs. This yields a mean timing of 2.*10^-6 with a blocked standard deviation of 3.53553*10^-7. Out[8]= {2.*10^-6, {a, b, -c, d, 4 e, -3 f}} 
+4
source

You can also use Replace :

 In[65]:= Replace[a + b - c + d + 4*e - 3*f, HoldPattern[Plus[a___]] :> {a}] Out[65]= {a, b, -c, d, 4 e, -3 f} 

You need to use HoldPattern (or some equivalent trick) so that Plus[a__] does not evaluate the value of a__ , which leads to a simple transfer of the first argument in the list instead of creating a list of Plus arguments.

+3
source

Source: https://habr.com/ru/post/898890/


All Articles