MathGroup Archive 2003

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

Search the Archive

Re: Compile

  • To: mathgroup at smc.vnet.net
  • Subject: [mg45255] Re: Compile
  • From: Maxim <dontsendhere@.>
  • Date: Wed, 24 Dec 2003 04:35:56 -0500 (EST)
  • References: <brplr9$7tf$1@smc.vnet.net> <200312210842.DAA18989@smc.vnet.net> <bs95la$c8a$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Andrzej Kozlowski wrote:

> On 21 Dec 2003, at 17:42, Maxim wrote:
>
> > Probably unrelated, but consider
> >
> > In[1]:=
> > Module[{subs = x -> x^2},
> >   Plot[x /. subs, {x, 0, 1}, Compiled -> True]
> > ]
> > Module[{subs = x -> x^2},
> >   Plot[x /. subs, {x, 0, 1}, Compiled -> False]
> > ]
> >
> > (straight line with the default setting of Compiled->True and parabola
> > with
> > Compiled->False). Or
> >
> > In[3]:=
> > Module[{y := If[NumericQ[x], x, 1]},
> >   Plot[x y, {x, 0, 1}, Compiled -> True]
> > ]
> > Module[{y := If[NumericQ[x], x, 1]},
> >   Plot[x y, {x, 0, 1}, Compiled -> False]
> > ]
> >
> > (same, straight line in the first case and parabola in the second).
> > The question is
> > probably what exactly Mathematica tries to wrap in Compile and how it
> > then deals with
> > 'external' expressions depending on the iterator variable.
> >
> > In any case it seems that we have yet another untransparent internal
> > transformation that
> > leaves us guessing about the result.
> >
> > Maxim Rytin
> > m.r at prontomail.com
> >
> >
> >
> I can't see anything at all surprising here. In both cases this simply
> amounts to the fact that with the option Compiled ->False the argument
> of Plot is evaluated, in other words you get the same answers as with
>
> Module[{subs = x -> x^2}, Plot[Evaluate[x /. subs],
>     {x, 0, 1}, Compiled -> True]]
>
> etc.
>
> What's more, this is actually described in the documentation:
>
> Since Plot needs to evaluate your function many times, it is important
> to make each evaluation as quick as possible. As a result, Mathematica
> usually compiles your function into a low$B!>(Blevel pseudocode that can be
> executed very efficiently. One potential problem with this, however, is
> that the pseudocode allows only machine$B!>(Bprecision numerical operations.
> If the function you are plotting requires higher$B!>(Bprecision operations,
> you may have to switch off compilation in Plot. You can do this by
> setting the option Compiled -> False. Note that Mathematica can only
> compile $B!H(Binline code$B!I(B; it cannot for example compile functions that you
> have defined. As a result, you should, when possible, use Evaluate as
> described in Section 1.9.1 to evaluate any such definitions and get a
> form that the Mathematica compiler can handle.
>
> Andrzej Kozlowski

Not exactly. An elementary test shows that with both settings for Compiled the first
argument of Plot is evaluated in a non-standard way, as it should be (Mathematica Book,
A.4.2):

In[1]:=
Plot[Random[], {x, 0, 1}, Compiled -> True]
Plot[Random[], {x, 0, 1}, Compiled -> False]

(plots an irregular curve, not a straight line for one pre-evaluated value of Random[]).
The documentation paragraph you quote (1.9.2) just recommends that you should use
Evaluate whenever appropriate.

And of course, if executing CompiledFunction fails, it should correctly fall back to
uncompiled code (I'd prefer it if the evaluation were simply aborted in such case, but
that's a different matter), not give wrong results.

Maxim Rytin
m.r at prontomail.com



  • Prev by Date: Re: Re: Transpose matrix does not work when MatrixForm is used, why?
  • Next by Date: Mathematica Can't calculate Determinant of Matrix?
  • Previous by thread: Re: Re: Compile
  • Next by thread: Re: Compile