MathGroup Archive 2011

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

Search the Archive

Re: Compile and Total

  • To: mathgroup at smc.vnet.net
  • Subject: [mg120262] Re: Compile and Total
  • From: Patrick Scheibe <pscheibe at trm.uni-leipzig.de>
  • Date: Fri, 15 Jul 2011 05:06:00 -0400 (EDT)
  • References: <201107150120.VAA23682@smc.vnet.net>

Hi,

> Yet it is not something about my user function as h[x_] := x/Total[x] works:

No, it does not work. Look at

h[x_] := x/Total[x]
hC = Compile[{{x, _Real, 1}}, h[x]]
Needs["CompiledFunctionTools`"]
CompilePrint[hC]

and you see that there is a line

MainEvaluate[ Hold[h][ T(R1)0]]

which means you compiled function makes a callback to the kernel to
evaluate h. To solve the problem either use the CompilationOptions value
"InlineExternalDefinitions" (see doc CompilationOptions) or just put
your x/Total[x] inside the compiled function.

Hope this helps.

Cheers
Patrick

> h[x_] := x/Total[x]
> hC = Compile[{{x, _Real, 1}}, h[x]]
> CompiledFunction[{x}, h[x], -Compiled Code-]
> 
> hC[data]
> {0.0181818, 0.0363636, 0.0545455, 0.0727273, 0.0909091, 0.109091, 
>   0.127273, 0.145455, 0.163636, 0.181818}
> 
> It's not something about Total[] as I get the same results with Apply[Plus,
> x] instead.
> 
> Any comments or insight will be extremely gratefully received.
> 
> Best,
> Neil.
> 




  • Prev by Date: Re: Numerical accuracy/precision - this is a bug or a feature?
  • Next by Date: Re: Numerical accuracy/precision - this is a bug or a feature?
  • Previous by thread: Re: Compile and Total
  • Next by thread: Re: Compile and Total