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

| improve this answer | |
$\endgroup$
  • $\begingroup$ Oh...silly me...of course!!..taking absolute values would give a short way to that!. $\endgroup$ – Alejandro Guarnizo Feb 8 '17 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} *)

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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