Re: Just another Mathematica "Gotcha"
- To: mathgroup at smc.vnet.net
- Subject: [mg120801] Re: Just another Mathematica "Gotcha"
- From: Daniel Lichtblau <danl at wolfram.com>
- Date: Thu, 11 Aug 2011 05:10:37 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
I've been informed (politely) that my reading is suspect. Correction below. ----- Original Message ----- > From: "Daniel Lichtblau" <danl at wolfram.com> > To: mathgroup at smc.vnet.net > Sent: Wednesday, August 10, 2011 5:47:33 AM > Subject: Re: Just another Mathematica "Gotcha" > ----- Original Message ----- > > From: "AES" <siegman at stanford.edu> > > To: mathgroup at smc.vnet.net > > Sent: Tuesday, August 9, 2011 6:19:34 AM > > Subject: Just another Mathematica "Gotcha" > > Seems as if the following two expression should yield the same > > output > > -- seems that way to me anyway -- but they don't. I'll hide the > > actual outputs down below so Mathematica gurus (or "ordinary users") > > can make their predictions as to which one does what. > > > > In[1]:= Series[a+(b1+b2)x,{x,0,1}] //Normal /.{b2->0} > > > > In[2]:= Series[a+(b1+b2)x,{x,0,1}] /.{b2->0} //Normal > > > > My conclusions: > > > > 1) By any normal rules of interpretation or ordinary interpretations > > of these statements, they both should do the same same thing. > > > > 2) This is just another Mathematica "Gotcha" -- and not a > > particularly forgivable one. > > Bunk. > > You forgot an important third variant. > > Series[a + (b1 + b2) x, {x, 0, 1}] /. {b2 -> 0} // Normal Bad reading on my part. I thought the second variant had the replacement inside the Series. So my "third" was identical to the second. The same basic point remains: the parsing has to respect operator precedence. I also missed at least one alternative "fix" which I will indicate below. > I think you will expect it to give a + b1*x. Which it does. There is > an important point to be made about this, to wit... > > > Out[1]= a+(b1+b2) x > > > > Out[2]= a+b1 x > > ... this third variant and your first cannot possibly have the same > behavior in general. For a fundamental reason, having essentially > nothing to do with Mathematica per se. It is this. One of the infix // > or /. must have higher precedence than the other. So either the Normal > will not be applied (because it would bind to {b2->0}) or the > replacement would not (because it parsed as (Normal/.{b2->0}) which > simply evaluates to Normal). > > So which of the following would you like to see? > > (1) No infix \\ ? > > (2) No infix \. ? > > (3) Swapping the precedence of those two infix operators (and perhaps > getting a surprise from my third variant)? A fourth possibility would be to elevate precedence of // ever so slightly, so that it is equal to that of /. and relatives. Since both associate to the left, if they have equal precedence then both would operate on the Series before the last one operated at all. I still would view this as an extremely dangerous change, albeit less so than (3) above. One reason is that in between those precedences lies postfix Function (that is, the & operator) and the plus-equal et al infix operators. > [...] Daniel Lichtblau Wolfram Research