1
$\begingroup$

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!

$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$
    – march
    Commented Feb 7, 2017 at 23:42
  • $\begingroup$ For instance, this doesn't work: DeleteDuplicates[{1/(b - a), -1/(a - b)}, #1 === -#2 &]. $\endgroup$
    – march
    Commented Feb 7, 2017 at 23:44
  • $\begingroup$ Hi @march the example works, since $-1/(a-b) =1/(b-a)$, so it does not delete it. $\endgroup$ Commented Feb 8, 2017 at 0:02

2 Answers 2

2
$\begingroup$

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]] &

$\endgroup$
1
  • $\begingroup$ Oh...silly me...of course!!..taking absolute values would give a short way to that!. $\endgroup$ Commented Feb 8, 2017 at 4:30
1
$\begingroup$
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} *)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.