MathGroup Archive 2006

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

Search the Archive

Re: Problem with compiled function (is this a bug?)

  • To: mathgroup at smc.vnet.net
  • Subject: [mg65579] Re: [mg65570] Problem with compiled function (is this a bug?)
  • From: "Carl K. Woll" <carlw at wolfram.com>
  • Date: Sun, 9 Apr 2006 04:32:05 -0400 (EDT)
  • References: <200604080445.AAA25147@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

szhorvat at gmail.com wrote:
> I have a problem with the following compiled function:
> 
> cpl = Compile[{x,y}, Table[Ceiling[Norm[{i, j} - Ceiling[{
>     x, y}/2]]], {i, 1, x}, {j, 1, y}]]
> 
> When trying to use this function (for example, as idx = cpl[100,100]),
> I get the following error:
> 
> CompiledFunction::"cfte" :
> Compiled expression 49 Sqrt[2] should be a rank 1 tensor of
> machine-size integers.
> 
> CompiledFunction::cfex: External evaluation error at instruction 23;
> proceeding with uncompiled evaluation.
> 
> I get the same error even if I try to specify that all variables are
> Reals:
> 
> cpl = Compile[{{x, _Real}, {y, _Real}}, Table[Ceiling[Norm[{i, j} -
> Ceiling[{
>     x, y}/2]]], {i, 1, x}, {j, 1, y}], {{i, _Real}, {j, _Real}}]
> 
> Is this a bug in Mathematica? If not, what is the right way to compile
> this function? Does this error occur with earlier versions than 5.2?
> 
> I'd like to use this function to generate indices for a matrix whose
> values I want to average radially (ie. I'd like to compute the mean
> values in the ring around the center of the matrix). Unofrtunately this
> function takes more than 16 seconds on an 500x500 matrix on my machine
> which seems unrealistically long for such a simple function. I need to
> use the function interactively on many datasets, often much larger than
> 500x500.
> 
> Szabolcs Horvat

In my previous post I told you that using Norm wasn't wise because it 
wasn't compilable. However, if you really want to use Norm, the error 
message can be avoided by using the optional third argument of Compile.

cpl=Compile[{x,y},
   Table[Ceiling[Norm[{i,j}-Ceiling[{x,y}/2]]],{i,1,x},{j,1,
     y}],{{_Norm,_Real}}];

The third argument tells the compiler that the output of Norm is a real 
number. Without this information, the compiler thinks that Norm should 
return a vector, since it's input is a vector. Now there is no error 
message:

In[17]:=
cpl[2,3]

Out[17]=
{{1,0,1},{2,1,2}}

Of course, this function is much, much slower than the one in my 
previous post.

Carl Woll
Wolfram Research


  • Prev by Date: Re: Problem with compiled function (is this a bug?)
  • Next by Date: Re: Problem with compiled function (is this a bug?)
  • Previous by thread: Re: Problem with compiled function (is this a bug?)
  • Next by thread: Re: Problem with compiled function (is this a bug?)