Re: Re: Compile

*To*: mathgroup at smc.vnet.net*Subject*: [mg45546] Re: [mg45511] Re: Compile*From*: "Fred Simons" <f.h.simons at tue.nl>*Date*: Tue, 13 Jan 2004 04:04:00 -0500 (EST)*References*: <bto1j0$2ip$1@smc.vnet.net> <200401102143.QAA11555@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

In message[45511] Maxim Rytin wrote > Evidently, it doesn't work that way. > > At the very least, please get my name right the next time... > > Maxim Rytin > m.r at prontomail.com First I apologize that I misspelled Maxim's name. One of the reasons I like Mathematica is that I nearly always can predict what will happen. Occasionally my prediction turns out to be wrong. In that situation normally there is something I do not understand well enough or there is a bug in Mathematica. So I am interested in strange behaving examples with the intentention to improve my understanding. Many people already pointed out that Maxim's examples have no practical meaning. Using Evaluate solves the problems and is well documentated. But Maxim's examples do show some strange behaviour and therefore are worth to be investigated. On the other hand, Mathematica is a so complicated piece of software that I think practically nobody understands everything to the bottom. I am not an expert, just a user and I accept that there are details I will never understand. But Maxim seem to be convinced that in my message I evidently wrote a lot of nonsense. Let us see how evident that is by reconsidering the arguments. Plot is a scoping construct; in a command such as Plot[something, {x, a, b}] the variable x is local. Moreover, it has attribute HoldAll, so it uses unevaluated arguments. I can think of three ways in which a value for the local variable will be transferred to the first argument: by assignment only, by substitution only or both by assignment and substitution. The output of the following command demonstrates that with Compiled->True the transfer is done by substitution only; no assignment takes place. With Compiled->False, assignment to the local variable x is used and no substitution. So Plot uses two different ways in dealing with the local variable, depending of the setting of the option Compiled. y:=x; Plot[Print[{y,Hold[x]}];x^3, {x,0,1}, Compiled\[Rule]True] Do I completely understand this? I have to admit not. When I execute Plot[x,{x,0,1}] I suppose that x is somehow compiled. But in using Trace with TraceInternal->True I cannot find any trace of a compiled function. Even worse, the result of trace does not even contain the values of x that are used. In the same way examining Plot[Print[x];x, {x,0,1}] does show the values of x that are used but not a compiled function; instead some LinkObjects. So I think Mathematica has constructed some or other piece of internal code in which somewhere the compiled first argument occurs and, when needed, values for the variable x are substituted. It might very well be that when the first argument of Plot compiles nicely, i.e. without Print statements or external variables etc., that piece of internal code does not need the local variable x at all and simply produces the graph. That would explain why in the first case Trace does not trace any occurence of x and we cannot find a compiled function, it is hidden in the code. Anyway it is very clear that no assignment takes place to the local variable and that there is no reason to switch to uncompiled code. In the behaviour of these two examples I cannot find anything to draw Maxim's conclusion that it does not work this way. Now reconsider the example y:=(Print[x];Sin[x]); Plot[y, {x, 0, 3}] In the output we first see two times the symbol x printed and then a lot of numerical values. I think it works the following way. Mathematica starts with creating some internal code containg the compiled first argument of Plot and therefore in this case containing the symbol y. Then it starts evaluating that piece of code, but since no value has been assigned to the local variable x the value of y is not numerical. So Mathematica uses the uncompiled code for the first argument of Plot and the result is Sin[x], which is not numerical either. As explained in my previous message, this results in two times printing the symbol x. So the internal code for making the plot fails and Mathematica starts to do something else. It starts assigning values to the local variable which strongly suggests that Mathematica switches to the uncompiled mode. That turns out to be incorrect. When we use Trace, we first see a call to some LinkObject, probably the piece of internal code. Then Mathematica starts using numerical values for the variable x but in combination with the compiled function of the first argument. We might say that Mathematica now switches to the uncompiled mode with a compiled first argument but it is more likely that the internal implementation of Plot takes care of this. Only the developers of the Plot function can explain what is exactly going on here. For myself, I have a model that predicts what will happen when I use a Plot command with a pathological first argument; Hartmut Wolf has a slightly different model. As long as my simple model predicts what happens, I am happy, as I think Hartmut is with his model. Nobody can claim that a model is the truth. But as long as it gives a simple explanation of the observed phenomena, it is useful. Fred Simons Eindhoven University of Technology

**References**:**Re: Compile***From:*Maxim <dontsendhere@.>