开发者

Pull out minus sign to get a unified list?

Given the following list:

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

I would like to get as a result:

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

To clarify: I'd like to transform the first list in such a way that the first term in each element is normalized to a plus sign and throw away any elements that can be obtained from the final result by multiplying with -开发者_如何转开发1.

I have tried Collect[] and FactorTerms[] and some other functions that look remotely like they would be able to do what I need, but they never touch minus signs ....

Any help is greatly appreciated.


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}


Replace each by its negative if the syntactic sign of the first element is negative. Then take the union. 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


It looks like you want to eliminate the elements that are duplicate modulo an overall 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 elements in the list matters, you can use DeleteDuplicates in place of Union.


Here's an attempt.

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

is intended to be mapped over a list; takes a single item (of the list) as input, replaces it by its negative if the first element has a negative sign. So nTerm[-a + b + c] gives a - b - c, which is left invariant by nTerm: nTerm[a - b - c] gives back its argument.

Next,

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

takes a list as argument, removes those list elements that might be obtained from another list element 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 whole lot to an input list; thus, li = {a + b, c + d + e, -a + b, a - b, -c - d - e}; processList[li] gives {a + b, c + d + e, a - b}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜