# Applying ReplaceAll to I (imaginary unit) [closed]

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?

• I get E^-I -- seems to work (V10.1 and V9.0.1). May 13, 2015 at 14:03
• Your addition is incorrect. I should not necessarily be entered as Complex[0,1] May 14, 2015 at 15:23
• 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 actually Exp[-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...] May 14, 2015 at 20:18
• [...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. May 14, 2015 at 20:25
• 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. May 15, 2015 at 7:02

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

• Thank you! Can you tell me how to revert FullForm[] afterwards? May 13, 2015 at 14:59
• Of course, I understand. However, if I precede in my calculation I have a mixture of "normal form" expressions and FullForm and this generates problems when evaluating scalar products like FullForm[{1, 2, 3}].{1, 2, 3} May 13, 2015 at 15:21
• aah, now I understand, I thought it's necessary to have it in FullForm before replacement, thank you! May 13, 2015 at 15:29
• Is is difficult to add a reverse procedure? May 13, 2015 at 15:33