11
$\begingroup$

I want to get rid of the sign in front of the element, but the symbol variables are involved, so I can't use the Abs function to get rid of the sign.

I want this list {-1, -2, +3, 4, -x, +x, -Fx[3], +Fx[3]} to be processed to {1, 2, 3, 4, x, x, Fx[3], Fx[3]}.

In addition, What should I do when the list contains a ± sign? For example, when the list is {-1, -2, +3, 4, -x, +x, -Fx[3], +Fx[3], -Fx[-3], ±Fy[-2] ,±Fy[±x]}.

$\endgroup$
  • 2
    $\begingroup$ Related: (9637), (26971), (46004) $\endgroup$ – Mr.Wizard Apr 2 at 10:53
  • 1
    $\begingroup$ @Mr.Wizard What should I do when the list contains a ± sign? I've updated the question. $\endgroup$ – user216 Apr 2 at 22:40
  • 2
    $\begingroup$ user216, please see my updated answer. @user1066 That's nice! Why not post a formal Answer? $\endgroup$ – Mr.Wizard Apr 3 at 11:25
15
$\begingroup$

I am sure there are many ways to do this. One possible way could be

ClearAll[x,Fx];
expr = {-1, -2, +3, 4, -x, +x, -Fx[3], +Fx[3]};
Times[Internal`SyntacticNegativeQ /@ expr /. {True -> (-1), False -> 1}, expr]

gives

 {1, 2, 3, 4, x, x, Fx[3], Fx[3]}
| improve this answer | |
$\endgroup$
7
$\begingroup$

What about

expr   /. y_ /; y < 0 -> -y 
(*{1, 2, 3, 4, x, x, Fx[3], Fx[3]}*)
| improve this answer | |
$\endgroup$
  • $\begingroup$ Clever, but beware something like -Fx[-1] $\endgroup$ – Mr.Wizard Apr 2 at 10:30
  • $\begingroup$ @Mr.Wizard obviously to simple! Other problems arise if you consider expressions like -g[x]+f[-x] $\endgroup$ – Ulrich Neumann Apr 2 at 10:39
  • 1
    $\begingroup$ It fits the given example however, and I like terse, so you already have my up-vote. :-) $\endgroup$ – Mr.Wizard Apr 2 at 10:39
  • $\begingroup$ Me too, thanks. $\endgroup$ – Ulrich Neumann Apr 2 at 10:40
7
$\begingroup$

Not as terse as Ulrich's replacement, but perhaps more robust:

list = {-1, -2, +3, 4, -x, +x, -Fx[3], +Fx[3], -Fx[-1], +Fx[-1]};

Replace[a : -_ | _?Negative :> -a] /@ list
{1, 2, 3, 4, x, x, Fx[3], Fx[3], Fx[-1], Fx[-1]}

Updated for your latest example:

list2 = {-1, -2, +3, 4, -x, +x, -Fx[3], +Fx[3], -Fx[-1], +Fx[-1], ±Fy[-2], ±Fy[±x]};

Replace[{a : -_ | _?Negative :> -a, ±a_ :> a}] /@ list2
{1, 2, 3, 4, x, x, Fx[3], Fx[3], Fx[-1], Fx[-1], Fy[-2], Fy[±x]}
| improve this answer | |
$\endgroup$
6
$\begingroup$
ClearAll[removeSigns]

removeSigns = # Sign[#] /. Sign -> (1 &) &;

removeSigns @ {-1, -2, +3, 4, -x, +x, -Fx[3], +Fx[3]}
{1, 2, 3, 4, x, x, Fx[3], Fx[3]}

Also

ClearAll[removeSigns2, removeSigns3, removeSigns4, removeSigns5]

removeSigns2 = Abs[#] /. Abs -> (# &) &

removeSigns3 = ReplaceAll[Abs -> Identity] @* Abs

removeSigns4 = Map[Replace[Abs[x_] :> x]]@*Abs (*thanks: Mr.Wizard *)

removeSigns5 = Function[{x}, If[Internal`SyntacticNegativeQ[x], -x, x], Listable];
| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Consider also: Replace[Abs[x_] :> x] /@ Abs[list] -- this would not strip inner appearances of Abs. $\endgroup$ – Mr.Wizard Apr 2 at 10:58
2
$\begingroup$
Sqrt[lst lst]//PowerExpand

{1, 2, 3, 4, x, x, Fx[3], Fx[3]}

(Originally posted as a comment)

| 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.