MathGroup Archive 2011

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

Search the Archive

Compile and Total

  • To: mathgroup at smc.vnet.net
  • Subject: [mg120247] Compile and Total
  • From: Neil Stewart <neil.stewart at warwick.ac.uk>
  • Date: Thu, 14 Jul 2011 21:20:19 -0400 (EDT)

Hello,

I'm having some trouble using Compile[]. I've stripped my trouble down to a
few basic examples.

Compiling Total[x] works:

data = Range[1, 10]
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

fC = Compile[{{x, _Real, 1}}, Total[x]]
CompiledFunction[{x}, Total[x], -Compiled Code-]

fC[data]
55

But compiling a function g[x_]:=Total[x] does not:

g[x_] := Total[x]
gC = Compile[{{x, _Real, 1}}, g[x]]
CompiledFunction[{x}, g[x], -Compiled Code-]

gC[data]
CompiledFunction::cfex: Could not complete external evaluation at
instruction 1; proceeding with uncompiled evaluation. >>
55

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

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: Expectation function
  • Next by Date: Keeping it real
  • Previous by thread: Re: Expectation function
  • Next by thread: Re: Compile and Total