Re: More /.{I->-1} craziness
- To: mathgroup at smc.vnet.net
- Subject: [mg106058] Re: [mg106017] More /.{I->-1} craziness
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Thu, 31 Dec 2009 03:18:07 -0500 (EST)
- References: <200912300915.EAA17299@smc.vnet.net>
Since other instances of this issue has been just discussed extensively in a previous long thread, I will only add a slightly different viewpoint. Namely, what can be done to make the rules work. On Wed, Dec 30, 2009 at 12:15 PM, AES <siegman at stanford.edu> wrote: > The more I play with these I->-I substitution rules, the more seemingly > wildly inconsistent results emerge. For example: > > In[1]:= -I/.I->-I > > Out[1]= -I > Complex numbers (<I> included) have a pattern Complex[a,b]. The pattern you try assumes that - I is internally represented as -Complex[0,1], while it is in fact Complex[0,-1]. Now, <I> is a locked symbol (one of the very few) with special properties. In particular, from the following it is obvious that once the input <I> is processed, some evaluation takes place, since \[ImaginaryI] gets replaced by Complex[0,1]. In[41]:= FullForm[I] Out[41]//FullForm= Complex[0,1] In[44]:= Hold[I]//FullForm Out[44]//FullForm= Hold[\[ImaginaryI]] Your rule will work with a following (admittedly inconvenient) modification: In[23]:= With[{I = Complex[0, 1]}, Unevaluated[-I] /. I :> -I] Out[23]= I Alternatively you can use: In[39]:= Unevaluated[-I] /. HoldPattern[I] -> -I Out[39]= I In the latter case, we forbid <I> to evaluate in both the original expression and the pattern, and then it works. My point is that this is not a bug (in this particular case). Pattern-matching is based on syntax, not on semantics, so to use it correctly one should have a pretty good idea of the internal representation of participating expressions in Mathematica, as well as on possible evaluations that may take place. This is certainly not intuitive, but I think the cases you list represent exceptions rather than a rule, and are (for I and Infinity) related to a rather special nature of the objects represented by these symbols. > > In[3]:= -E/.E->-E > > Out[3]= << The Esc e e Esc symbol >> > This one works fine for me: In[6]:= -E/.E->-E Out[6]= E > > In[4]:= -Pi/.Pi->-Pi > > Out[4]= \[Pi] > This seems ok > > In[5]:= -Infinity/.Infinity->-Infinity > > Out[5]= -\[Infinity] > The same story here as for the case with <I>. Again, you can see the origin of this puzzle by looking at the FullForm before and after the evaluation: In[33]:= Hold[-Infinity]//FullForm Out[33]//FullForm= Hold[Times[-1,Infinity]] In[45]:= Infinity//FullForm Out[45]//FullForm= DirectedInfinity[1] You see that once we allow Infinity to evaluate, it becomes DirectedInfinity[1], while -Infinity becomes DirectedInfinity[-1]. And the possible solution which leads to "expected" pattern-matching: In[38]:= Unevaluated[-Infinity] /. HoldPattern[Infinity] -> -Infinity Out[38]= \[Infinity] I think that there are not many more objects in Mathematica which are as tricky as <I> or Infinity in terms of pattern-matching. I also think that this is the case where the overall consistency of design requires that the user gets (accidentally) exposed to some internals like above (because here the line between internals and exposed to the user interface becomes very blurred, given that a lot of Mathematica is written in Mathematica), which are not as simple or intuitive as the general Mathematica functionality intentionally exposed to the end user. It would perhaps be nice if such cases were more systematically documented, but they have nothing to do with bugs, as Daniel Lichtblau and many others already explained. Hope this helps. Regards, Leonid > > (In/Out[2] is removed because it was an irrelevant cell.) > >
- References:
- More /.{I->-1} craziness
- From: AES <siegman@stanford.edu>
- More /.{I->-1} craziness