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


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

Just another Mathematica "Gotcha"

Started byAES <siegman@stanford.edu>
First post2011-08-09 11:24 +0000
Last post2011-08-11 09:11 +0000
Articles 13 — 9 participants

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


Contents

  Just another Mathematica "Gotcha" AES <siegman@stanford.edu> - 2011-08-09 11:24 +0000
    Re: Just another Mathematica "Gotcha" David Bailey <dave@removedbailey.co.uk> - 2011-08-10 10:45 +0000
    Re: Just another Mathematica "Gotcha" Richard Fateman <fateman@cs.berkeley.edu> - 2011-08-10 10:50 +0000
      Re: Just another Mathematica "Gotcha", and HoldForm bug Richard Fateman <fateman@cs.berkeley.edu> - 2011-08-11 09:12 +0000
      Re: Just another Mathematica "Gotcha", and HoldForm DrMajorBob <btreat1@austin.rr.com> - 2011-08-12 09:06 +0000
      Re: Just another Mathematica "Gotcha", and HoldForm bug "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-08-12 09:05 +0000
      Re: Just another Mathematica "Gotcha", and HoldForm bug "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-08-12 09:06 +0000
        Re: Just another Mathematica "Gotcha", and HoldForm bug Richard Fateman <fateman@cs.berkeley.edu> - 2011-08-27 12:18 +0000
      Re: Just another Mathematica "Gotcha", and HoldForm Murray Eisenberg <murray@math.umass.edu> - 2011-08-12 09:13 +0000
    Re: Just another Mathematica "Gotcha" "Kevin J. McCann" <kjm@KevinMcCann.com> - 2011-08-10 11:00 +0000
    Re: Just another Mathematica "Gotcha" Roger Wilson <rogerhw999@gmail.com> - 2011-08-11 07:26 +0000
    Re: Just another Mathematica "Gotcha" Simon <simonjtyler@gmail.com> - 2011-08-11 07:26 +0000
    Re: Just another Mathematica "Gotcha" DrMajorBob <btreat1@austin.rr.com> - 2011-08-11 09:11 +0000

#4232 — Just another Mathematica "Gotcha"

FromAES <siegman@stanford.edu>
Date2011-08-09 11:24 +0000
SubjectJust another Mathematica "Gotcha"
Message-ID<j1r5d3$fi4$1@smc.vnet.net>
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

[toc] | [next] | [standalone]


#4262

FromDavid Bailey <dave@removedbailey.co.uk>
Date2011-08-10 10:45 +0000
Message-ID<j1tngq$phr$1@smc.vnet.net>
In reply to#4232
On 09/08/2011 12:24, 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 conclusion is that you have simply ignored the precedence of the 
various operators:

Series[a+(b1+b2)x,{x,0,1}]//Normal/.{b2->0}//Hold//FullForm

Hold[ReplaceAll[Normal,List[Rule[b2,0]]][Series[Plus[a,Times[Plus[b1,b2],x]],List[x,0,1]]]]

I don't know how you think the Mathematica language should be changed to 
avoid mistakes of this sort, but of course, you do not need to use a 
single operator in your code if you prefer!

David Bailey
http://www.dbaileyconsultancy.co.uk

[toc] | [prev] | [next] | [standalone]


#4269

FromRichard Fateman <fateman@cs.berkeley.edu>
Date2011-08-10 10:50 +0000
Message-ID<j1tnpf$pnu$1@smc.vnet.net>
In reply to#4232
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


[toc] | [prev] | [next] | [standalone]


#4302 — Re: Just another Mathematica "Gotcha", and HoldForm bug

FromRichard Fateman <fateman@cs.berkeley.edu>
Date2011-08-11 09:12 +0000
SubjectRe: Just another Mathematica "Gotcha", and HoldForm bug
Message-ID<j206d7$72j$1@smc.vnet.net>
In reply to#4269
Why one might be confused

In[6]:= A + B /. x_ -> M[x]
Out[6]= M[A + B]

In[7]:= A + B // M
Out[7]= M[A + B]

In[8]:= A + B /. x_ -> M[x] // F
Out[8]= F[M[A + B]]

In[9]:= A + B // M /. x_ -> F[x]
Out[9]= F[M][A + B]


Note the identicality of Out[6] and Out[7].
Note that Out[8] and Out[9] are different.

Since neither //  nor /. are "traditional" mathematical
syntax, one's intuition may be unreliable. Simple examples
such as 6,7 above, might lead one to believe they have
equal precedence and are treated nominally from left to right.

Reading up on the details by looking at Mathematica's
description of syntax and operator precedence is difficult,
since it is simultaneously daunting and incomplete.
One is therefore left with experimentation. Simple experiments
are not enough, though, as shown by 6, which suggests that
/. applies rule(s) to everything to the left, and 7 which
suggests that // applies an operator to everything
to the left.  Both of these are false. See a=b/.f;  a=b//f.

In looking at examples, I think I found a bug, but maybe
it is a feature?

Did you know that

HoldForm[x //@ y]      returns  x//@y
   but
HoldForm[x //  y]      returns  y[x]

?
(version 7.0).

While it is easy to place blame on "user error", I think "design error"
contributes to difficulties as well.

RJF

[toc] | [prev] | [next] | [standalone]


#4334 — Re: Just another Mathematica "Gotcha", and HoldForm

FromDrMajorBob <btreat1@austin.rr.com>
Date2011-08-12 09:06 +0000
SubjectRe: Just another Mathematica "Gotcha", and HoldForm
Message-ID<j22qf2$gpe$1@smc.vnet.net>
In reply to#4269
Again, no experimentation is needed.

If you want to know what an operator operates upon in a statement,  
double-click on that operator.

Done.

Bobby

On Thu, 11 Aug 2011 04:09:42 -0500, Richard Fateman  
<fateman@cs.berkeley.edu> wrote:

> Why one might be confused
>
> In[6]:= A + B /. x_ -> M[x]
> Out[6]= M[A + B]
>
> In[7]:= A + B // M
> Out[7]= M[A + B]
>
> In[8]:= A + B /. x_ -> M[x] // F
> Out[8]= F[M[A + B]]
>
> In[9]:= A + B // M /. x_ -> F[x]
> Out[9]= F[M][A + B]
>
>
> Note the identicality of Out[6] and Out[7].
> Note that Out[8] and Out[9] are different.
>
> Since neither //  nor /. are "traditional" mathematical
> syntax, one's intuition may be unreliable. Simple examples
> such as 6,7 above, might lead one to believe they have
> equal precedence and are treated nominally from left to right.
>
> Reading up on the details by looking at Mathematica's
> description of syntax and operator precedence is difficult,
> since it is simultaneously daunting and incomplete.
> One is therefore left with experimentation. Simple experiments
> are not enough, though, as shown by 6, which suggests that
> /. applies rule(s) to everything to the left, and 7 which
> suggests that // applies an operator to everything
> to the left.  Both of these are false. See a=b/.f;  a=b//f.
>
> In looking at examples, I think I found a bug, but maybe
> it is a feature?
>
> Did you know that
>
> HoldForm[x //@ y]      returns  x//@y
>    but
> HoldForm[x //  y]      returns  y[x]
>
> ?
> (version 7.0).
>
> While it is easy to place blame on "user error", I think "design error"
> contributes to difficulties as well.
>
> RJF
>


-- 
DrMajorBob@yahoo.com

[toc] | [prev] | [next] | [standalone]


#4339 — Re: Just another Mathematica "Gotcha", and HoldForm bug

From"Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com>
Date2011-08-12 09:05 +0000
SubjectRe: Just another Mathematica "Gotcha", and HoldForm bug
Message-ID<j22qc6$gnd$1@smc.vnet.net>
In reply to#4269
On Thu, 11 Aug 2011 10:12:07 +0100, Richard Fateman  
<fateman@cs.berkeley.edu> wrote:

> Simple experiments
> are not enough, though, as shown by 6, which suggests that
> /. applies rule(s) to everything to the left, and 7 which
> suggests that // applies an operator to everything
> to the left.  Both of these are false. See a=b/.f;  a=b//f.
>

Looking at the FullForm is simple, albeit not an experiment. Also simple  
(though for some reason not documented) is querying the precedence of the  
operators directly:

Precedence[ReplaceAll] = 110.

Precedence[Postfix] = 70.

which is to say, of course, that

Precedence[ReplaceAll] > Precedence[Postfix] = True

which is as direct a statement of this fact as I think you'll ever find.

> In looking at examples, I think I found a bug, but maybe
> it is a feature?
>
> Did you know that
>
> HoldForm[x //@ y]      returns  x//@y
>    but
> HoldForm[x //  y]      returns  y[x]
>
> ?
> (version 7.0).
>

//@ is a sigil representing the infix form of MapAll. An interpretation of  
the first expression as postfix immediately followed by prefix function  
application which you seem to be suggesting as a possibility would not be  
meaningful: what would its (syntactically correct) FullForm be?

> While it is easy to place blame on "user error", I think "design error"
> contributes to difficulties as well.
>

Yes, I dare say that many practically-minded users would find it easier to  
solve their real-world problems in Mathematica if it were based on Prolog  
or some similarly widely used language in which the order in which  
relations are stated is inconsequential. (Being serious for a moment,  
users do have the option to code in FullForm if they wish precedence to be  
explicit. For some reason, though, most seem not to prefer this...)

[toc] | [prev] | [next] | [standalone]


#4340 — Re: Just another Mathematica "Gotcha", and HoldForm bug

From"Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com>
Date2011-08-12 09:06 +0000
SubjectRe: Just another Mathematica "Gotcha", and HoldForm bug
Message-ID<j22qe4$gop$1@smc.vnet.net>
In reply to#4269
On Thu, 11 Aug 2011 17:01:50 +0100, Oleksandr Rasputinov  
<oleksandr_rasputinov@hmamail.com> wrote:

> On Thu, 11 Aug 2011 10:12:07 +0100, Richard Fateman  
> <fateman@cs.berkeley.edu> wrote:
>
>> In looking at examples, I think I found a bug, but maybe
>> it is a feature?
>>
>> Did you know that
>>
>> HoldForm[x //@ y]      returns  x//@y
>>    but
>> HoldForm[x //  y]      returns  y[x]
>>
>> ?
>> (version 7.0).
>>
>
> //@ is a sigil representing the infix form of MapAll. An interpretation  
> of the first expression as postfix immediately followed by prefix  
> function application which you seem to be suggesting as a possibility  
> would not be meaningful: what would its (syntactically correct) FullForm  
> be?
>

On further consideration it strikes me that I may have misunderstood what  
you meant here. HoldForm is simply printing the expressions in InputForm,  
so for example we also have

HoldForm[MapAll[x,y]]

printing:

x //@ y

Conversion between two equivalent syntactic forms (postfix notation, and  
InputForm) is not considered to be a form of evaluation; it is simply the  
way in which the expressions are parsed. If you want an expression that  
prints in a particular way you can use

HoldForm@Postfix[x // y]

or indeed

HoldForm@FullForm[MapAll[x, y]]

both of which print retaining the expressions in the forms specified.

[toc] | [prev] | [next] | [standalone]


#4603 — Re: Just another Mathematica "Gotcha", and HoldForm bug

FromRichard Fateman <fateman@cs.berkeley.edu>
Date2011-08-27 12:18 +0000
SubjectRe: Just another Mathematica "Gotcha", and HoldForm bug
Message-ID<j3ana5$hcb$1@smc.vnet.net>
In reply to#4340
On 8/12/2011 2:06 AM, Oleksandr Rasputinov wrote:

>>>
>>>(RJF) Did you know that
>>>
>>> HoldForm[x //@ y]      returns  x//@y
>>>     but
>>> HoldForm[x //  y]      returns  y[x]
>>>
>>> ?
>>> (version 7.0).
>>>
>>
.. snip..

>
> On further consideration it strikes me that I may have misunderstood what
> you meant here.

Yes. but you got it right, below.

  HoldForm is simply printing the expressions in InputForm,
> so for example we also have
>
> HoldForm[MapAll[x,y]]
>
> printing:
>
> x //@ y
>
> Conversion between two equivalent syntactic forms (postfix notation, and
> InputForm) is not considered to be a form of evaluation;
I agree.
  it is simply the
> way in which the expressions are parsed.

Not really parsed, but displayed.  a[b] ,   a@b,   and b//a   are parsed 
into the same thing.
I view  Postfix, Prefix, InputForm  as output specifiers; hints to the 
display program on what to do, not actual conversions.  I suppose 
Mathematica could be doing something else, but this is the way I'd do 
it.  That is, I'd store Postfix[a[b]], but write the display program to
show it as b//a.


I find the specifications defective, anyway.  For instance,
Postfix[f[a,b]]  comes out  as  Postfix[f[a,b]]  instead of
the form   Sequence[a,b]//f,   which is in fact a postfix version of f[a,b].


  If you want an expression that
> prints in a particular way you can use
>
> HoldForm@Postfix[x // y]

Not really useful. That expression is not x//y.  It is 
HoldForm[Postfix[y[x]]].

...

The point of my posting is that there is an inconsistency in Mathematica 
that says that

x//y  is normally displayed as y[x].
x/.y  is normally displayed as x/.y  not ReplaceAll[x,y]

doing FullForm[everything]  is probably the only way to consistently
reduce the clutter of mystery, and the display program is clearly able 
to make more or less informed choices on preferred formats for any 
construct.  I think that f[a,b] is much preferable to Sequence[a,b]//f.
I think that, especially for novices, ReplaceAll[x,y] has more mnemonic
content than x/.y.



Someone's note (DanL?) raised the Dot issue.

More gotchas, but not nearly as likely to occur, are these puzzles.
One might wonder how to parse
x . y
x .. y
x...y
x . .. y
x.. . y
x....y
x.....y
x......y
x.......y

All of which are syntactically valid. Of lines 3,4,5,  two of them have
the same meaning.  Can you tell which?

Also consider the confusing lines below
3. ..4
3.. .4
3....4
3... .4
3. ...4

which are NOT the same at all when 3, 4 are x, y.

Hint.  FullForm[x..]  is Repeated[x],  and FullForm[x...] is 
RepeatedNull[x].

RJF


[toc] | [prev] | [next] | [standalone]


#4351 — Re: Just another Mathematica "Gotcha", and HoldForm

FromMurray Eisenberg <murray@math.umass.edu>
Date2011-08-12 09:13 +0000
SubjectRe: Just another Mathematica "Gotcha", and HoldForm
Message-ID<j22qrh$gtc$1@smc.vnet.net>
In reply to#4269
But in the first two examples, you have only one "postifx" operator in 
each case, whereas in the second two you have two such. Only there you 
have to deal with precedence, really.

Also, I find nothing especially difficult about looking up Mathematica's 
syntax and operator precedence with respect, at least, to this issue.

On 8/11/11 5:09 AM, Richard Fateman wrote:
> Why one might be confused
>
> In[6]:= A + B /. x_ ->  M[x]
> Out[6]= M[A + B]
>
> In[7]:= A + B // M
> Out[7]= M[A + B]
>
> In[8]:= A + B /. x_ ->  M[x] // F
> Out[8]= F[M[A + B]]
>
> In[9]:= A + B // M /. x_ ->  F[x]
> Out[9]= F[M][A + B]
>
>
> Note the identicality of Out[6] and Out[7].
> Note that Out[8] and Out[9] are different.
>
> Since neither //  nor /. are "traditional" mathematical
> syntax, one's intuition may be unreliable. Simple examples
> such as 6,7 above, might lead one to believe they have
> equal precedence and are treated nominally from left to right.
>
> Reading up on the details by looking at Mathematica's
> description of syntax and operator precedence is difficult,
> since it is simultaneously daunting and incomplete.
> One is therefore left with experimentation. Simple experiments
> are not enough, though, as shown by 6, which suggests that
> /. applies rule(s) to everything to the left, and 7 which
> suggests that // applies an operator to everything
> to the left.  Both of these are false. See a=b/.f;  a=b//f.
>

-- 
Murray Eisenberg                     murray@math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower      phone 413 549-1020 (H)
University of Massachusetts                413 545-2859 (W)
710 North Pleasant Street            fax   413 545-1801
Amherst, MA 01003-9305

[toc] | [prev] | [next] | [standalone]


#4289

From"Kevin J. McCann" <kjm@KevinMcCann.com>
Date2011-08-10 11:00 +0000
Message-ID<j1tobu$puj$1@smc.vnet.net>
In reply to#4232
I think that in the first, Mathematica is applying your rule to Normal, not to 
the output. Put parens around it (Series ...//Normal)/.b2->0 and it works.

Kevin

On 8/9/2011 7: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
>

[toc] | [prev] | [next] | [standalone]


#4294

FromRoger Wilson <rogerhw999@gmail.com>
Date2011-08-11 07:26 +0000
Message-ID<j2006n$5ki$1@smc.vnet.net>
In reply to#4232
On Aug 9, 12:24 pm, AES <sieg...@stanford.edu> 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

Its a case of operator priority.  /. has a higher priority than // so,
in the first case, you're doing Normal /. {b2->0} first which
evaluates to Normal.

(Series[a+(b1+b2)x,{x,0,1}]//Normal)/.b2->0   gives   a+b1 x

[toc] | [prev] | [next] | [standalone]


#4295

FromSimon <simonjtyler@gmail.com>
Date2011-08-11 07:26 +0000
Message-ID<j2007m$5l0$1@smc.vnet.net>
In reply to#4232
To anyone that knows their operator precedence, the result is not a surprise at all. In your first version, the ReplaceAll is acting on the Normal operator, not the entire expression.

Basically, postfix (//) has the lowest precedence, so that you can chuck it at the end of any expression. This is good for things like //Simplify, //MatrixForm, etc...

[toc] | [prev] | [next] | [standalone]


#4301

FromDrMajorBob <btreat1@austin.rr.com>
Date2011-08-11 09:11 +0000
Message-ID<j206c8$722$1@smc.vnet.net>
In reply to#4232
AES failed to take into account the precedence of operators??

Bobby

On Wed, 10 Aug 2011 05:49:57 -0500, Kevin J. McCann <kjm@kevinmccann.com>  
wrote:

> I think that in the first, Mathematica is applying your rule to Normal,  
> not to
> the output. Put parens around it (Series ...//Normal)/.b2->0 and it  
> works.
>
> Kevin
>
> On 8/9/2011 7: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
>>
>


-- 
DrMajorBob@yahoo.com

[toc] | [prev] | [standalone]


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


csiph-web