# Replace every Minus to Plus in Expression

Thought this is a rather simple thing, but I was not able to find a solution. I have a polynomial expression like:

 expr1 = a^2 - b + c * d
expr2 = a - b
expr3 = -a + b - c


and I want to replace every - sign to +:

 expr1new = a^2 + b + c * d
expr2new = a + b
expr3new = a + b + c


The problem is that

FullForm[a - b]
(* Plus[a, Times[-1, b]] *)


thus I cannot use simple replacement. Do you know a simple way for that?

Update This example does not work with simple replacements:

expr4 = (1 - I) a;
expr4 /. {-1 -> 1}
FullForm[expr4]

(* (1 - I) a
Times[Complex[1, -1], a] *)

• Do you really mean every minus? So given this input expr = a - b + Sqrt[-x]; which can be complex depending on x, you want the output to become real (depending on x)? as in expr = a + b + Sqrt[x]; – Nasser Apr 13 '14 at 15:20
• @Kuba: Thanks, i was not careful. I added the example which does not work for me. – NicoDean Apr 13 '14 at 15:24
• @Nasser: I only use complex multivariable polynomial expressions. (thanks, I added the note). – NicoDean Apr 13 '14 at 15:25

You have found the snags and you're right -- it's a simple matter. You just need the right rules.

For a pure symbolic expression you can use Kuba's suggestion.

a^2 - b + c*d /. -1 -> 1

a^2 + b + c d


For dealing with complex numbers you can use

(1 - b I) a /. x_Complex /; Im[x] < 0 -> Conjugate[x]

(1 + b I) a


If your expressions are more complicated than these, you might need more elaborate rules. But I offer more without knowing what form the more complicated expression take.

• Nice trick, decomposing the complex into Re and Im. This works. And it can be generalized to: expr = expr /. {x_Complex /; Re[x] < 0 -> -Conjugate[x], Im[x] < 0 -> Conjugate[x], -1 -> 1} - that seems to work for all cases. Thanks. – NicoDean Apr 13 '14 at 15:46

For most of these cases, judicious use of the undocumented function InternalSyntacticNegativeQ[] works nicely:

Replace[{a^2 - b + c d, a - b, -a + b - c}, x_?InternalSyntacticNegativeQ :> -x, {-1}]
{a^2 + b + c d, a + b, a + b + c}


It will have trouble with complex numbers, however:

(3 - 4 I) (a - b I) E^(I t) /. x_?InternalSyntacticNegativeQ :> -x
(-3 + 4 I) (a - I b) E^(I t)


so just use Conjugate:

(3 - 4 I) (a - b I) E^(I t) /. x_Complex /; Negative[Im[x]] :> Conjugate[x]
(3 + 4 I) (a + b I) E^(I t)


However, none of these can deal with -3 + 4 I; thus, a separate rule for real parts is necessary:

Replace[-3 + 4 I, {x_Complex /; Negative[Re[x]] :> -I Conjugate[I x]}, {-1}]
3 + 4 I

Fold[Replace[#, #2, {-1}] &, (-3 - 4 I) (a - b I) Exp[-I t],
{x_Complex /; Negative[Re[x]] :> -I Conjugate[I x],
x_Complex /; Negative[Im[x]] :> Conjugate[x],
x_?InternalSyntacticNegativeQ :> -x}]
(3 + 4 I) (a + I b) E^(I t)


Allways you can use pattern replace

expr1 = a^2 - b + c*d
expr2 = a - b
expr3 = -a + b - c

expr/. Times[-1, x_] :> Times[1, x]

(*
a^2 + b + c * d
a + b
a + b + c
*)


PlusToMinus[expr_] := ToExpression[StringReplace[ToString[expr], "+" -> "-"]]

• This is quite a clever idea, though I don't think you need the _Plus pattern since there are other legal expressions (like (a + b) (a - b)) that wouldn't work when you match like this. Just a regular blank _ pattern should be fine. Also, the question was about replacing minus to plus, not plus to minus. I'm also wondering if there are any situations where this string replacement could produce bad results, but I can't think of any right now. – Sjoerd Smit Jul 26 at 9:39
• What will happen for the following input: -x 0.0000000000001? – Shadowray Jul 26 at 9:49