Re: Compile

*To*: mathgroup at smc.vnet.net*Subject*: [mg45393] Re: Compile*From*: Andrzej Kozlowski <akoz at mimuw.edu.pl>*Date*: Tue, 6 Jan 2004 04:16:50 -0500 (EST)*Sender*: owner-wri-mathgroup at wolfram.com

This seems correct and, I think, agrees with the observations I made in my last message on this topic. However, you do not refer to one point I made, which I think is somewhat intriguing. Compare the following two (long) outputs: Trace[Module[{subs = x -> x^2}, Plot[x /. subs, { x, 0, 1}, Compiled -> True]], TraceInternal -> True] // InputForm and Trace[Module[{subs = x -> x^(1/3)}, Plot[ x^3 /. subs, {x, 0, 1}, Compiled -> True]], TraceInternal -> True] // InputForm (InputForm is useful here). In the second example you will see the appearance of terms like: CompiledFunction[{_Real}, {{3, 0, 0}, {3, 0, 3}}, {0, 0, 4, 0, 0}, {{1, 5}, {29, 0, 0, 1}, {29, 0, 1, 2}, {21, Function[{x}, subs$20], 3, 0, 0, 3, 0, 1}, {22, ReplaceAll, 3, 0, 2, 3, 0, 1, 3, 0, 3}, {2}}, Function[{x}, x^3 /. subs$20], Evaluate][ 4.166666666666666*^-8] Note that this is a a badly compiled CompiledFunction (the thing to look is the fourth term which in a properly compiled CompiledFunction should contain only contains sublists of integers (and sometimes floating point numbers and Boolean variables). The remark you make at the end of your posting seems to apply to this case, if you consider this sort of thing as a "compiled function" (it is at best a badly compiled one). In the output of Maxim's example CompiledFunction does not appear, so it looks lie no compilation at all took place (?) Andrzej Kozlowski On 5 Jan 2004, at 22:19, Fred Simons wrote: > Many people already commented on Maxim Retin's strange example: > > subs = x->x^2 > Plot[ x /. subs, {x, 0,1}, Compiled->...] > > produces different plots depending on the value used in the option > Compiled. > > I think that the value of an example like this one is that it may help > a > user of Mathematica in getting a better understanding of the way > Mathematica > works. Nobody, not even Maxim himself, will ever try to make a plot of > x^2 > in this way. > > Here are some observations that I made about this example. > > In a command like Plot[f[x], {x, 0, 1}] the symbol x is a local > variable. > The way Mathematica deals with these local variables depends on the > command > in which it is used. I will shortly discuss three examples: Table, > NIntegrate and Plot.. > > The output of the following command > > y:=x; > Table[Information[x];Print[y]; x, {x, 1, 3}] > > shows that in the function Table in each step a value is assigned to > the > local variable x; no immediate substitution in the first argument takes > place. > > The function NIntegrate behaves differently: > > y:=x; > NIntegrate[Information[x];Print[y]; x, {x, 0,1}, Compiled\[Rule]True] > > Both with Compiled->True and Compiled->False, no matter how many steps > are > used in the integration procedure, the first argument is called only > once > and no value has been assigned to the local variable x. (Mathematica 4 > behaves differently). Using Trace in the next command, we see how > Mathematica proceeds. It evaluates the first argument symbolically, > taking > into account global variables that may occur in the first argument, > and uses > the outcome in the numerical integration. This is Maxim's example: > > subs = x -> x^2; > NIntegrate[ x /. subs, {x, 0, 1}, Compiled -> True] > > Compiled->False yields the same result. > > Now we turn to Plot: > > y:=x; > Plot[Information[x];Print[y]; x, {x, 0,1}, PlotPoints->3, > Compiled->True] > > With the option Compiled->True, the values for the local variable are > substituted into the (compiled) first argument of Plot, no assignment > to the > local variable takes place. > > y:=x; > Plot[Information[x];Print[y]; x, {x, 0,1}, PlotPoints->3, > Compiled->False] > > Hence with Compiled->False, the values are assigned to the local > variable > and no immediate substitution into the (uncompiled) first argument > occurs. > > Now the behaviour of Maxim's example(s) is easy to predict: > > subs=x\[Rule]x^2; > Plot[x/.subs, {x, 0, 1}, Compiled\[Rule]True] > > The values of x are substituted (no assignment to x) into the > (compiled) > first argument. That (compiled) argument contains an unevaluated > variable > subs. Hence evaluation of subs results in x->x^2 and the plot is a > straight > line. > > subs=x\[Rule]x^2; > Plot[x/.subs, {x, 0, 1}, Compiled\[Rule]False] > > In this situation, values are assigned to the local variable x and > therefore > evaluation of the first argument results in x^2 for any of these > values. The > plot is a parabola. > > subs=x\[Rule]x^2; > f[x_]:= x /. subs; > Table[f[x], {x, 1, 5}] > Table[f[t], {t, 1, 5}] > > In the last command, assignments are done to the local variable t and > therefore subs remains symbolically. > > My final remark is that the first argument in Maxim's example can be > compiled and that the result is a compiled function in which a global > variable subs occurs. The above observations and conclusions remain > valid > also for a compiled first argument. I cannot find any reason why > Mathematica > would not compile the first argument when Compiled->True. But I cannot > show > that Mathematica indeed did compile. > > Fred Simons > Eindhoven University of Technology > >