[Date Index]
[Thread Index]
[Author Index]
Re : Unpredictability (was (Re: Unevaluated, Plus and Times (was Re: Mathematica language issues))
*To*: mathgroup at smc.vnet.net
*Subject*: [mg53177] Re : Unpredictability (was (Re: Unevaluated, Plus and Times (was Re: Mathematica language issues))
*From*: Jean-Michel Collard <jmcollard at free.fr>
*Date*: Tue, 28 Dec 2004 06:30:17 -0500 (EST)
*Sender*: owner-wri-mathgroup at wolfram.com
About Mathematica language issues and predictability :
i=5
Print[++i * ++i] will always return 42 as Print[++i * i++] will always return 36.
And it's predictable and will always return these values.
In ISO C9x a construction like :
int i=5;
printf("%d",++i * ++i); is unpredictable.
It may return 0 , -45621245 or even 42 !
Such a construction is not forbidden but inpredictable (cf C99 Rationale V5.10).
I think in Mathematica it shoudln't be allowed at all.
Of course who wants to Print[++i * i--] ?
Jean-Michel
Fred Simons wrote:
> The discussion on the behaviour of Unevaluated in cases where it should not
> be used lasts already for a long time. Though irrelevant there are some
> interesting aspects in it. The general behaviour of Unevaluated has been
> explained by Andrzej Kozlowsky. Let me reformulate it very short and
> therefore inaccurate: when during the evaluation process the expression
> changes, the arguments that originally were wrapped in Unevaluated are not
> rewrapped. It is very important to note that changes only due to attributes
> do not count as an change.
>
> Examples have been given to demonstrate that Times and Plus behave
> differently. For example the expression Unevaluated[z]+2. Mathematica looks
> at z+2. The terms are reordered due to the attribute Orderless so
> Mathematica arrives at 2+z. That is the end of the evaluation. The change is
> only due to the attributes so we expect rewrapping of Unevaluated, that is
> the result 2+Unevaluated[z]. But the outcome is 2+z.
>
> In my previous mail I tried to explain this behaviour from the fact that the
> evaluation of expressions with head Plus or Times is slightly different.
> According to Bobby, that explanation was not very clear, so in this mail I
> try to do it better.
>
> There is evidence that as soon as the head Times or Plus of an expression is
> recognized, Mathematica does some sort of pre-evaluation of the arguments.
> All numerical arguments are multplied or added and the result is placed
> befor the other arguments. Only after this pre-evaluation the attributes of
> Times and Plus are applied and the evaluation continues.
>
> I will only consider the function Plus. The pre-evaluation of the arguments
> can be seen from the following function preplus, in which the rules used by
> Mathematica are 'caught':
>
> Attributes[preplus]={HoldAllComplete};
> preplus[x___] := Block[{Plus}, plus@@Plus[x]]
>
> Now we can imitate the evaluation of an expression with head Plus by first
> doing the pre-evaluation with the function preplus and then apply Plus to
> the result.
>
> Here are some examples.
>
> Evaluation of the expression 2+Unevaluated[z] is done in the following way:
>
> In[12]:=
> preplus[2,Unevaluated[z]]
> Plus @@ %
>
> Out[12]=
> plus[2,Unevaluated[z]]
> Out[13]=
> 2+Unevaluated[z]
>
> The pre-evaluation does not change the arguments, so in the input of Plus
> the argument z is still wrapped in Unevaluated. Plus do not change the
> expression so at the end the argument z is wrapped in Unevaluated.
>
> The expression 1+1+Unevaluated[z] gives a different result:
>
> In[14]:=
> preplus[1,1,Unevaluated[z]]
> Plus @@ %
>
> Out[14]=
> plus[2,z]
> Out[15]=
> 2+z
>
> The pre-evaluation changed the arguments so Unevaluated has completely
> disappeared in the input of Plus.
>
> The expression 2+Unevaluated[z+2]:
>
> In[16]:=
> preplus[2, Unevaluated[z+2]]
> Plus @@ %
>
> Out[16]=
> plus[2,Unevaluated[z+2]]
> Out[17]=
> 4+z
>
> Here the pre-evaluation has no effect, so after applying the attributes Flat
> and Orderless, Plus is called with arguments 2, 2 and z. Plus has no rules
> for these combination of argument, so it has to return Plus[2,2,
> Unevaluated[z]]. But then the evaluation of course continues:
>
> In[18]:=
> preplus[2, 2, Unevaluated[z]]
> Plus @@ %
>
> Out[18]=
> plus[4,z]
> Out[19]=
> 4+z
>
> Pre-evaluation of the arguments gives a new set of arguments, so z is no
> longer wrapped in Unevaluated, and the outcome is 4+z.
>
> My conclusion is that the result of all curious examples with head Plus or
> Times where some of the arguments supplied with Unevaluated can be predicted
> in the way Andrzej has described when one takes into account the
> pre-evaluation, as I did in the above examples.
>
> Fred Simons
> Eindhoven Ubiversity of Technology
>
--
"They that can give up essential liberty to obtain a little temporary
safety deserve neither liberty nor safety." -- B. Franklin, 1759
Prev by Date:
**Re: Mathematica is destroying my sanity....**
Next by Date:
**Matrix Operator form using Mathematica**
Previous by thread:
**Re: shuffling 10^8 numbers**
Next by thread:
**Matrix Operator form using Mathematica**
| |