Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #4307 > unrolled thread

Re: Just another Mathematica "Gotcha"

Started byDaniel Lichtblau <danl@wolfram.com>
First post2011-08-11 09:14 +0000
Last post2011-08-11 09:14 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Re: Just another Mathematica "Gotcha" Daniel Lichtblau <danl@wolfram.com> - 2011-08-11 09:14 +0000

#4307 — Re: Just another Mathematica "Gotcha"

FromDaniel Lichtblau <danl@wolfram.com>
Date2011-08-11 09:14 +0000
SubjectRe: Just another Mathematica "Gotcha"
Message-ID<j206i0$751$1@smc.vnet.net>
I've been informed (politely) that my reading is suspect. Correction below.

----- Original Message -----
> From: "Daniel Lichtblau" <danl@wolfram.com>
> To: mathgroup@smc.vnet.net
> Sent: Wednesday, August 10, 2011 5:47:33 AM
> Subject: Re: Just another Mathematica "Gotcha"
> ----- Original Message -----
> > From: "AES" <siegman@stanford.edu>
> > To: mathgroup@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



[toc] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web