Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Compile

  • To: mathgroup at smc.vnet.net
  • Subject: [mg45430] Re: Compile
  • From: Maxim <dontsendhere@.>
  • Date: Wed, 7 Jan 2004 01:09:33 -0500 (EST)
  • References: <btduoc$di$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com


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

I'm afraid that still doesn't explain the difference between

Module[
  {y := If[NumericQ[x], 1, 0]},
  Plot[x + y, {x, 0, 1}, Compiled -> True]
]
(*plots x*)

and

Module[
  {y := x + If[NumericQ[x], 1, 0]},
  Plot[y, {x, 0, 1}, Compiled -> True]
]
(*plots x+1*)

Maxim Rytin
m.r at prontomail.com



  • Prev by Date: Re: Re: Arbitrary-precision numbers in patterns
  • Next by Date: For general discussion
  • Previous by thread: Re: Compile
  • Next by thread: Re: Compile