MathGroup Archive 2007

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

Search the Archive

Re: Compiled function changes somehow.

  • To: mathgroup at smc.vnet.net
  • Subject: [mg78608] Re: Compiled function changes somehow.
  • From: "Michael Weyrauch" <michael.weyrauch at gmx.de>
  • Date: Thu, 5 Jul 2007 04:06:04 -0400 (EDT)
  • References: <f6fqn6$8mo$1@smc.vnet.net>

Hello,

  to my opinion this has simply to do with different numerical precision used in both cases.

If you forth exactly the same precision in both calculations using e.g.

N[n/m, 8]

you will find that both results are exactly the same. The Union[] you used simply fooled you about that..

Michael Weyrauch


"Nacho" <ncc1701zzz at gmail.com> schrieb im Newsbeitrag news:f6fqn6$8mo$1 at smc.vnet.net...
> Hello.
> 
> I have been playing with Compile to accelerate some simple code and I
> have found some differences that I cannot explain.
> 
> The standard/compiled code is as follows:
> 
> standard[max_] :=
>  Table[n/m, {n, 1, max}, {m, 1, max}] // N // Flatten;
> 
> compiled =
>  Compile[{{max, _Integer}},
>   Table[n/m, {n, 1, max}, {m, 1, max}] // N // Flatten];
> 
> So I can do the same calculations with both codes:
> 
> In[19]:= standardresult = standard[1000]; // Timing
> 
> Out[19]= {2.969, Null}
> 
> In[20]:= compiledresult = compiled[1000]; // Timing
> 
> Out[20]= {0.422, Null}
> 
> 
> The second is much faster, as expected. But are the results the same?
> Apparently, yes:
> 
> In[21]:= standardresult == compiledresult
> 
> Out[21]= True
> 
> In[22]:= standardresult === compiledresult
> 
> Out[22]= True
> 
> 
> But when I use Union in the lists to count the number of different
> elements, I have this surprise:
> 
> Length[Union[standardresult]]
> Length[Union[compiledresult]]
> 
> Out[23]= 608383
> Out[24]= 634815
> 
> So they are not exactly the same...  I think that the correct answer
> comes from the uncompiled version. It is the same if I remove the //N
> so it can be compared symbolically.
> 
> Is this behaviour expected? Am I missing something?
> 
> Both seem to be Machine Precision, but obviously there are some little
> differences. This happens with V5.2 and V6.0.
> 
> Any hint?
> 
> Thank you.
> 
>


  • Prev by Date: Zoom2D
  • Next by Date: Re: Labeling individual plot points using PlotMarkers
  • Previous by thread: Re: Compiled function changes somehow.
  • Next by thread: Re: Compiled function changes somehow.