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!
2 Answers
$\begingroup$
$\endgroup$
1
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]] &
-
$\begingroup$ Oh...silly me...of course!!..taking absolute values would give a short way to that!. $\endgroup$ Commented Feb 8, 2017 at 4:30
$\begingroup$
$\endgroup$
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} *)
DeleteDuplicates[expr, #1 === -#2 &]
. The problem is that if the two expression don't have the same structure, it might not work. Maybe wrapping theSlot
s inSimplify
would do the trick. $\endgroup$DeleteDuplicates[{1/(b - a), -1/(a - b)}, #1 === -#2 &]
. $\endgroup$