0
$\begingroup$

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?

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

1 Answer 1

1
$\begingroup$

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
$\endgroup$
4
  • $\begingroup$ Thank you! Can you tell me how to revert FullForm[] afterwards? $\endgroup$ May 13, 2015 at 14:59
  • $\begingroup$ 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} $\endgroup$ May 13, 2015 at 15:21
  • $\begingroup$ aah, now I understand, I thought it's necessary to have it in FullForm before replacement, thank you! $\endgroup$ May 13, 2015 at 15:29
  • $\begingroup$ Is is difficult to add a reverse procedure? $\endgroup$ May 13, 2015 at 15:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.