# DeleteDuplicates With Negative Entries

I'm working now with xAct in Mathematica obtaining huge list of contractions of a metric tensor with vector fields. When making contractions with the Levi-Civita tensor, I realized that there are terms in which the only difference is a negative sign. Since they are at the Lagrangians, the negative sign does not matter. The question is: there is any option in DeleteDuplicates (or perhaps any other command) to force eliminate terms which only differs in a sign?. Thanks a lot!

• Something like this? DeleteDuplicates[expr, #1 === -#2 &]. The problem is that if the two expression don't have the same structure, it might not work. Maybe wrapping the Slots in Simplify would do the trick. – march Feb 7 '17 at 23:42
• For instance, this doesn't work: DeleteDuplicates[{1/(b - a), -1/(a - b)}, #1 === -#2 &]. – march Feb 7 '17 at 23:44
• Hi @march the example works, since $-1/(a-b) =1/(b-a)$, so it does not delete it. – Alejandro Guarnizo Feb 8 '17 at 0:02

A refinement (IMO) of David's code:

expressionlist = {
2 + 4 x^2,
2 (1 + 2 x^2),
-(x + 1)^2,
x^2 + 2 x + 1,
Sin[x]^2 + Cos[x]^2,
1
};

DeleteDuplicates[Simplify@expressionlist, FullSimplify[# == #2 || -# == #2] &]

{2 + 4 x^2, -(1 + x)^2, 1}


Also try FullSimplify[Abs[#] == Abs[#2]] &

• Oh...silly me...of course!!..taking absolute values would give a short way to that!. – Alejandro Guarnizo Feb 8 '17 at 4:30
FullSimplify /@
DeleteDuplicates[
expressionlist,
(FullSimplify[#1] === FullSimplify[#2] ||
FullSimplify[#1] === -FullSimplify[#2] ) &]


Example:

expressionlist = {
2 + 4 x^2, 2 (1 + 2 x^2),
-(x + 1)^2, x^2 + 2 x + 1,
Sin[x]^2 + Cos[x]^2, 1};


(* {2 + 4 x^2, -(1 + x)^2, 1} *)