Re: Compile

• To: mathgroup at smc.vnet.net
• Subject: [mg45392] Re: Compile
• From: "Fred Simons" <f.h.simons at tue.nl>
• Date: Tue, 6 Jan 2004 04:16:48 -0500 (EST)
• Sender: owner-wri-mathgroup at wolfram.com

```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.

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

```

• Prev by Date: Re: Mathematica exports curves in pieces to Illustrator
• Next by Date: NIntegrate with singular endpoints
• Previous by thread: Re: Compile
• Next by thread: Re: Compile