[Date Index]
[Thread Index]
[Author Index]
RE: RE: Re: Compile
*To*: mathgroup at smc.vnet.net
*Subject*: [mg45488] RE: [mg45446] RE: [mg45430] Re: Compile
*From*: "Simons, F.H." <F.H.Simons at tue.nl>
*Date*: Sat, 10 Jan 2004 00:00:35 -0500 (EST)
*Sender*: owner-wri-mathgroup at wolfram.com
Maxim Retin produced two examples asking to explain why they behave as they do. As Hartmut Wolf already pointed out, the first example is completely predictable in the way discussed earlier. So let us turn to Maxim's second example:
> -----Original Message-----
> From: Wolf, Hartmut [mailto:Hartmut.Wolf at t-systems.com]
To: mathgroup at smc.vnet.net
> Sent: woensdag 7 januari 2004 23:31
> To: mathgroup at smc.vnet.net
> Subject: [mg45488] [mg45446] RE: [mg45430] Re: Compile
>
......
> Your new example shows up other "interesting things" (to those who are
> interested of course).
>
> In[27]:=
> y := (Print[x]; x + If[NumericQ[x], 1, 0]);
> Trace[Plot[y, {x, 0, 1}, Compiled -> True, AspectRatio -> Automatic,
> PlotPoints -> 3], x | y, TraceInternal -> True] // InputForm
> >From In[27]:= x
> >From In[27]:= x
> >From In[27]:= 5.*^-7
> >From In[27]:= 0.486804
> >From In[27]:= 1.
..........
> What is new, are obviously two (additional) evaluations of y
> when x has no
> value, before doing the plot. What comes to my phantasy is a
> compilator in
> desperate search of the variable x to become bound, detecting
> it when an
> exterior variable becomes evaluated, and now binding it (as
> in contrary of
> above) in a function call. (y however is not evaluated within
> the compiled
> expression.)
>
>
Here my phantasy went into a different direction.
To start with, let us simplify Maxim's example.
y:= Sin[x]
Plot[y, {x, 0, 3}]
indeed produces a plot of the sine function, as expected(?!). But a further reflection shows that this is not obvious. The option Compiled is True, so Mathematica starts with compiling y as a function of x. The compiled result contains the symbol y. Then, since Compiled is set to True, the numerical values are substituted (not assigned) into the compiled function and therefore have no effect on the symbol y. This symbol now evaluates to Sin[x] which is not numerical and therefore the graph cannot be plotted. However, we do see the graph of the sine, so my explanation along the lines of my previous message seems to be incorrect. That is what Maxim's example suggests.
To see what is going on, have a look at the following commands:
Clear[y];
fc = Compile[{{x, _Real}}, y];
y:= (Print[x]; Sin[x]);
fc[0.3]
First the symbol x is printed, then an error message that the symbol Sin[x] is not numerical, then the message that the uncompiled code will be used, which results in another evaluation of y and therefore the printing of x, and the outcome Sin[x].
Now let us look at the Plot command:
y := (Print[x];Sin[x]);
Plot[y, {x, 0, 3}]
What happens? First Mathematica compiles y as a function of x. Then evaluations starts by substituting (not assigning) a numerical value of x in the compiled function. Then y is evaluated and x is printed, the evaluation of the compiled function fails so the uncompiled code will be used, resulting in another printing of x and a non-numerical value of the function AND NOW MATHEMATICA SIMPLY SWITCHES TO THE UNCOMPILED MODE and makes the plot as if the option Compiled was set to False. Therefore the result of the plot command is what was hoped that the result would be. Obviously this is not a rigid mathematical proof that is happening, but it explains what is going on, including Maxim's examples.
Two of Maxim's examples now are particularly illustrative:
y := If[NumericQ[x], 1, 0]
Plot[y, {x,0,1}]
Evaluation of the compiled argument results in 0, so the result is the horizontal axis.
y := x+ If[NumericQ[x], 1, 0]
Plot[y, {x,0,1}]
Evaluation of the compiled argument results in x, which is not numerical. So the plotting switches to Compiled->False, and x+1 is plotted.
Fred Simons
Eindhoven University of Technology
Prev by Date:
**Re: Re: Palette rememberance**
Next by Date:
**fonts**
Previous by thread:
**RE: Re: Compile**
Next by thread:
**Re: Compile**
| |