Re: Just another Mathematica "Gotcha"
- To: mathgroup at smc.vnet.net
- Subject: [mg120789] Re: Just another Mathematica "Gotcha"
- From: Richard Fateman <fateman at cs.berkeley.edu>
- Date: Wed, 10 Aug 2011 06:46:25 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <j1r5d3$fi4$1@smc.vnet.net>
On 8/9/2011 4:24 AM, AES wrote:
> 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.
>
> Out[1]= a+(b1+b2) x
>
> Out[2]= a+b1 x
>
The reason is the first form is parsed as..
Series[a + (b1 + b2) x, {x, 0, 1}] // ( Normal /. {b2 -> 0} )
This is not an error in Series, Normal, or Rule, but a misunderstanding
of the syntax of Mathematica, a natural consequence of its complexity.
Adding parentheses fixes this particular problem:
( Series[a + (b1 + b2) x, {x, 0, 1}] // Normal ) /. {b2 -> 0}
A suggestion: never use // . Or use it only at the very end, e.g.
stuff //N
Another suggestion. If ..stuff.. computes the wrong thing, look at
FullForm[ Hold[..stuff..]]
Yet another. Except for the obvious algebraic stuff like a*b+c, maybe
{a,b,c}, :=, and (perhaps) Patterns, just use FullForm.
RJF