5
$\begingroup$

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] *)
$\endgroup$
  • 1
    $\begingroup$ 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]; $\endgroup$ – Nasser Apr 13 '14 at 15:20
  • $\begingroup$ @Kuba: Thanks, i was not careful. I added the example which does not work for me. $\endgroup$ – NicoDean Apr 13 '14 at 15:24
  • $\begingroup$ @Nasser: I only use complex multivariable polynomial expressions. (thanks, I added the note). $\endgroup$ – NicoDean Apr 13 '14 at 15:25
5
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – NicoDean Apr 13 '14 at 15:46
3
$\begingroup$

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

Replace[{a^2 - b + c d, a - b, -a + b - c}, x_?Internal`SyntacticNegativeQ :> -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_?Internal`SyntacticNegativeQ :> -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_?Internal`SyntacticNegativeQ :> -x}]
   (3 + 4 I) (a + I b) E^(I t)
$\endgroup$
1
$\begingroup$

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
*)
$\endgroup$
1
$\begingroup$

How about

PlusToMinus[expr_] := ToExpression[StringReplace[ToString[expr], "+" -> "-"]]
$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Sjoerd Smit Jul 26 at 9:39
  • $\begingroup$ What will happen for the following input: -x 0.0000000000001? $\endgroup$ – Shadowray Jul 26 at 9:49
  • $\begingroup$ @Shadowray maybe just remove the plus pattern, I just put it to look fancy. Then you just get an identity function $\endgroup$ – user2757771 Jul 26 at 18:02

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.