7
$\begingroup$

Just try this sample:

I /. HoldPattern@I -> a
(* I *)

Why does it fail to replace I with a? I've checked // Hold // FullForm but found nothing useful.

$\endgroup$
  • 6
    $\begingroup$ Because I evaluates internally as Complex[0,1], so I /. HoldPattern[Complex[0, _]] -> a would work, or even I /. HoldPattern[Complex[0, 1]] -> a $\endgroup$ – Jason B. Feb 25 '16 at 14:53
  • 4
    $\begingroup$ It's funny that I /. (Unevaluated@I) -> a returns a. Of course 2 I /. (Unevaluated@I) -> a already does not do anything. $\endgroup$ – Rolf Mertig Feb 25 '16 at 14:55
  • 4
    $\begingroup$ I feel obliged to suggest this interesting read as somewhat relevant here. $\endgroup$ – Leonid Shifrin Feb 25 '16 at 15:06
  • 2
    $\begingroup$ @AlexeyPopkov AtomQ doesn't have a Hold* attribute so I think one need AtomQ@Unevaluated@I to check if I is an atom, the result is still True anyway. $\endgroup$ – xzczd Feb 25 '16 at 15:45
  • 3
    $\begingroup$ "There is no 'I' in Complex[0,1]", as my coach used to tell us... $\endgroup$ – Daniel Lichtblau Feb 25 '16 at 20:57
5
$\begingroup$

@xzczd has always struck me as a very mature person, and this question hasn't really changed that opinion :-)

The answer is because I evaluates internally as Complex[0,1], so

I /. HoldPattern[Complex[0, _]] -> a 

would work, or even

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

and in a more practical example,

FourierTransform[f[tt], tt, I ww] /. 
 HoldPattern@FourierTransform[a_, t_, Complex[0, 1] w_] :> 
  myfourier[a, t, J w]
(* myfourier[f[tt], tt, J ww] *)
|improve this answer|||||
$\endgroup$

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.