12

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

8 Answers8

16

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]}
Nasser
  • 143,286
  • 11
  • 154
  • 359
7

What about

expr   /. y_ /; y < 0 -> -y 
(*{1, 2, 3, 4, x, x, Fx[3], Fx[3]}*)
Ulrich Neumann
  • 53,729
  • 2
  • 23
  • 55
7

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]}
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
6
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];
kglr
  • 394,356
  • 18
  • 477
  • 896
5
Sqrt[lst lst]//PowerExpand

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

(Originally posted as a comment)

user1066
  • 17,923
  • 3
  • 31
  • 49
2

Position-based solutions

1.

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

p = Position[list, _Times | _?Negative, 1];

Using ReplaceAt (new in 13.1)

ReplaceAt[x_ :> -x, p] @ list

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

2.

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

p = Position[list, _Times | _?Negative | _PlusMinus, 1];

ReplaceAt[{x_PlusMinus :> x[[1]], x_ :> -x}, p] @ list

{1, 2, 3, 4, x, x, Fx[3], Fx[3], Fx[-1], Fx[-1], Fy[-2], Fy[±x]}

eldo
  • 67,911
  • 5
  • 60
  • 168
2
list1 = {-1, -2, +3, 4, -x, +x, -Fx[3], +Fx[3], -Fx[-1], +Fx[-1]};

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

Using Abs, FactorList and ReplaceAll:

f = Function[a, If[NumericQ@a, Abs@a, 
    FactorList[a /. b_ /; Head[b] === PlusMinus :> b[[1]]][[2, 1]]], Listable];

f@list1

f@list2

Results:

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

{1, 2, 3, 4, x, x, Fx[3], Fx[3], Fx[-1], Fx[-1], Fy[-2], Fy[±x]}

E. Chan-López
  • 23,117
  • 3
  • 21
  • 44
1

Just for fun:

func = (ToBoxes@# /. RowBox[{"-" | "±", a_}] :> a // ToExpression) &;

func@{-1, -2, +3, 4, -x, +x, -Fx[3], +Fx[3]} (* {1, 2, 3, 4, x, x, Fx[3], Fx[3]} *)

func@{-1, -2, +3, 4, -x, +x, -Fx[3], +Fx[3], -Fx[-3], ±Fy[-2], ±Fy[±x]} (* {1, 2, 3, 4, x, x, Fx[3], Fx[3], Fx[-3], Fy[-2], Fy[±x]} *)

xzczd
  • 65,995
  • 9
  • 163
  • 468