0

Why does

I/.I->-I

return

 -I

but

Exp[-I]/.I->-I

yields

Exp[-I]

? As mentioned below the problem seems to be the improper input type of the complex unit: Instead of I one should use Complex[0,1]. But if this is true, why does the first replacement above work at all? This behavior is inconsistent, or is there a good explanation for this result?

pawel_winzig
  • 1,577
  • 1
  • 12
  • 21
  • 1
    I get E^-I -- seems to work (V10.1 and V9.0.1). – Michael E2 May 13 '15 at 14:03
  • @MichaelE2: For me the replacement works for E^I, too, but not for Exp[I] – pawel_winzig May 13 '15 at 14:07
  • In 10.1.0 Exp[I] /. I -> -I returns E^-I. – Mr.Wizard May 13 '15 at 14:25
  • @pawel_winzig I got what Mr.Wizard got. In fact, what you say you get, Exp[I], should not be possible, because Exp[I] evaluates to E^I automatically. – Michael E2 May 13 '15 at 14:29
  • @MichaelE2: Well, seems strange but this is what I get as output. – pawel_winzig May 13 '15 at 14:33
  • Then I would say you have another problem, namely that Exp does not evaluate properly, which might be connected to the replacement not working. Does Exp[x] return Exp[x] or E^x? (Try restarting the kernel, etc., too.) This is what it looks like for me: http://i.stack.imgur.com/tzQmP.png – Michael E2 May 13 '15 at 14:50
  • @MichaelE2: I get the proper transformation from Exp[]-> E^ BUT

    Exp[-a I] /. I -> -I E^(-I a)

    however:

    Exp[-a x] /. x -> -x E^(x a) is evaluated correctly

    – pawel_winzig May 13 '15 at 14:58
  • That is a different problem than you asked about! This Exp[I]/.I->-I works, but Exp[-I]/.I->-I does not. Just like Exp[2] /. 2 -> -2 works but Exp[-2] /. 2 -> -2 does not. – Michael E2 May 13 '15 at 15:04
  • BTW, have you seen Conjugate? E.g. Exp[-a I] /. z_Complex :> Conjugate[z] and Conjugate[Exp[-I]]. – Michael E2 May 13 '15 at 15:08
  • @MichaelE2: Both replacements have the same form so I thought: Since it works for x it has to work for "I" too. As I understand Bill, the issue is solved with FullForm[]. However, I thought that InputForm[] will reverse the latter after the replacement. This seems not the case, still searching for the solution... – pawel_winzig May 13 '15 at 15:09
  • Possible duplicate: http://mathematica.stackexchange.com/questions/1586/replace-rule-does-not-match – Michael E2 May 13 '15 at 15:09
  • Also related: http://mathematica.stackexchange.com/questions/46004/replace-every-minus-to-plus-in-expression – Michael E2 May 13 '15 at 15:13
  • 1
    Your addition is incorrect. I should not necessarily be entered as Complex[0,1] – Sjoerd C. de Vries May 14 '15 at 15:23
  • @SjoerdC.deVries: Did you read the other posts? What is then, in your opinion, the mistake? I mean, it's clear that the behavior is inconsistent. But since I'm not the designer of the called function I have no clue what is going on there. – pawel_winzig May 14 '15 at 15:41
  • 1
    The question you asked in the original post is not the question you actually have (as stated in your comments). Exp[I]/.I->-I works for me in 8, 9, and 10 (and for everybody else too). It seems your problem is actuallyExp[-I] /. I -> -I which does not get you the result you expect. The point here is that ReplaceAll looks for structural matches in terms of the internal form of the expression. This internal form may differ from the way it is printed. 1-x for instance, is represented as Plus[1,Times[-1,x]]. You have to take this into account when replacing. [continues...] – Sjoerd C. de Vries May 14 '15 at 20:18
  • 1
    [...continued]. In your case (as stated in the comments) you want to replace -I, which is represented as Complex[0,-1] and I as Complex[0,1]. So -I/.I->-1 does nothing as it is equivalent to Complex[0,-1] /. Complex[0,1]->Complex[0,-1]. No match, so no replacement. – Sjoerd C. de Vries May 14 '15 at 20:25
  • @SjoerdC.deVries: I understand, thank you for clarifying this to me. It would be nice if this could be stressed in the Documentation. – pawel_winzig May 15 '15 at 05:59
  • 1
    I agree the documentation could be clearer on that (it must be somewhere, but not on the ReplaceAll page). Luckily we have this covered in our popular Mathematica Pitfalls question. – Sjoerd C. de Vries May 15 '15 at 07:02

1 Answers1

1

ReplaceAll works on "the structure", not the "pretty printed" form.

FullForm[I]

gives

Complex[0, 1]

and

I /. Complex[0, 1] -> Complex[0, -1]

gives

-I
Bill
  • 12,001
  • 12
  • 13