MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Mathematica language issues

  • To: mathgroup at smc.vnet.net
  • Subject: [mg52971] Re: Mathematica language issues
  • From: Oliver Ruebenkoenig <ruebenko at imtek.uni-freiburg.de>
  • Date: Sat, 18 Dec 2004 03:59:47 -0500 (EST)
  • Organization: Rechenzentrum der Universitaet Freiburg, Germany
  • References: <cpuilf$hd6$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com


Dear Maxim,

> All the typical issues with the Mathematica programming language are
> still present in version 5.1:
> 
> Compile[{},
>   Module[{x = 0},
>     While[x++; EvenQ[x] ];
>     x
> ]][]

Compile[{}, Module[{x = 0}, x++; While[EvenQ[x]];
      x]][]

x++; EvenQ[x] is CompoundExpression[ x++, EvenQ[ x ] ]

While[ test ] needs a test not a CompoundExpression

> 
> still evaluates to 3, and it is easy to construct other similar
> examples:
> 

[ ... ]

> problems such as
> 
> In[3]:=
> Unevaluated[D[y, x]] /. {y -> x}
> Unevaluated[D[y, x]] /. {{y -> x}}
> 
> Out[3]=
> 1
> 
> Out[4]=
> {0}
> 
> where you can find out how many Unevaluated wrappers are required in
> each case only by trial and error, to curious glitches like


Consider:
In[1]:= Trace[ Unevaluated[D[y, x]] /. {y -> x} ]

Out[1]= {D[y, x] /. {y -> x}, D[x, x], 1}

and

In[2]:= Trace[ Unevaluated[D[y, x]] /. {{y -> x}} ]

Out[2]= {D[y, x] /. {{y -> x}}, {D[y, x] /. {y -> x}}, 
 
>    {{D[y, x], 0}, 0 /. {y -> x}, 0}, {0}}


but you must:

In[3]:= Trace[ Unevaluated[ Unevaluated[D[y, x]] ] /. {{y -> x}} ]

Out[3]= {Unevaluated[D[y, x]] /. {{y -> x}}, 
 
>  {Unevaluated[D[y, x]] /. {y -> x}}, {D[y, x] /. {y -> x}, D[x, x], 1}, 
 
>    {1}}

and you should:

In[4]:= ReleaseHold[ HoldPattern[D[y, x]] /. {{{{{{y -> x}}}}}} ]

Out[4]= {{{{{1}}}}}


> 
> In[5]:=
> Unevaluated[1 + 1]*2
> 2*Unevaluated[1 + 1]
> 
> Out[5]=
> 4
> 
> Out[6]=
> 2*Unevaluated[1 + 1]

look at the evaluation order.

In[5]:= Unevaluated[ 1 + 1 ]*2 // Trace

Out[5]= {(1 + 1) 2, 2 (1 + 1), {1 + 1, 2}, 2 2, 4}

and 

In[6]:= 2 * Unevaluated[ 1 + 1 ] // Trace

Out[6]= {2 (1 + 1), 2 Unevaluated[1 + 1]}

In the first case Unevaluated[ 1 + 1 ] is seen _twice_ by the evaluator. 
And since Unevaluated[ expr ] give expr unevaluated as an argument to 
Times and Times reorders its arguments it is seen twice.

FullForm[ Unevaluated[ Unevaluated[ 1 + 1 ]*2 ] ]

Unevaluated[Times[Unevaluated[Plus[1, 1]], 2]]

Hope that helps,

Oliver

Oliver Ruebenkoenig, <ruebenko at imtek.de>
   Phone: ++49 +761 203 7385


  • Prev by Date: Re: Mathematica language issues
  • Next by Date: Re: Mathematica slows down
  • Previous by thread: Re: Mathematica language issues
  • Next by thread: Re: Mathematica language issues