Pull out a minus sign to get a single list?

Given the following list:

{a + b, c + d + e, - a + b, a - b, - c - d - e} 

I would like to get the result:

 {a + b, a - b, c + d + e} 

To clarify: I would like to convert the first list so that the first term in each element is normalized to a plus sign and throws out any elements that can be obtained from the final result by multiplying by -1 .

I tried Collect[] and FactorTerms[] and some other functions that look remotely, as they could do what I need, but they never touch minus signs ....

Any help is greatly appreciated.

+6
source share
4 answers

Use FactoredTermsList :

 In[5]:= FactorTermsList /@ {a + b, c + d + e, -a + b, a - b, -c - d - e} Out[5]= {{1, a + b}, {1, c + d + e}, {-1, a - b}, {1, a - b}, {-1, c + d + e}} In[6]:= DeleteDuplicates[%[[All, 2]]] Out[6]= {a + b, c + d + e, a - b} 
+10
source

Replace each with its own negative if the syntax of the first element is negative. Then take an alliance. Example:

 ll = {a + b, c + d + e, -a + b, a - b, -c - d - e} Out[444]= {a + b, c + d + e, -a + b, a - b, -c - d - e} Union[Map[ If[Head[#] === Plus && Head[#[[1]]] === Times && NumberQ[#[[1, 1]]] && #[[1, 1]] < 0, Expand[-#], #] &, ll]] {a - b, a + b, c + d + e} 

Daniel Lichtblau

+4
source

It looks like you want to remove elements that are duplicated modulo a common sign. At least in this particular case, the following will work:

 In[13]:= Union[ FullSimplify@Abs [{a + b, c + d + e, -a + b, a - b, -c - d - e}]] /. Abs[x_] :> x Out[13]= {a - b, a + b, c + d + e} 

If the order of the items in the list matters, you can use DeleteDuplicates instead of Union .

+2
source

Here is an attempt.

 ClearAll[nTerm]; nTerm[t_] := If[MatchQ[t[[1]], Times[-1, _]], -t, t] 

designed to be displayed above the list; takes one element (list) as input, replaces it with negative if the first element has a negative sign. So, nTerm[-a + b + c] gives a - b - c , which remains invariant on nTerm : nTerm[a - b - c] returns its argument.

Further

 ClearAll[removeElements]; removeElements[lst_] := DeleteDuplicates[lst, (#1 \[Equal] #2) || (#1 \[Equal] -#2) &] 

takes a list as an argument, removes those list items that can be obtained from another list item by negation: removeElements[{1, 2, 3, -2, a, -a, "GWB", -"GWB"}] gives {1, 2, 3, a, "GWB"} (!). Finally,

 ClearAll[processList]; processList[lst_] := removeElements[nTerm /@ lst] 

applies the entire lot to the input list; thus li = {a + b, c + d + e, -a + b, a - b, -c - d - e}; processList[li] li = {a + b, c + d + e, -a + b, a - b, -c - d - e}; processList[li] gives {a + b, c + d + e, a - b}

+2
source

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


All Articles