MathGroup Archive 2006

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

Search the Archive

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

  • To: mathgroup at
  • Subject: [mg65583] Re: [mg65570] Problem with compiled function (is this a bug?)
  • From: "Carl K. Woll" <carlw at>
  • Date: Sun, 9 Apr 2006 04:32:08 -0400 (EDT)
  • References: <> <> <>
  • Sender: owner-wri-mathgroup at

Szabolcs Horvát wrote:
> Thank you. Your reply was really helpful.
> However, it seemed really strange that using the Outer function could
> result in such an impressive speedup. I experimented a little bit with
> the functions and found that the speedup was due to using N[ ].

Yes, you certainly don't want to find square roots of integers, and then 
find their integer part. It is much faster to find the integer part of 
the square root of the floating point approximation to the integer.

> If I work with machine precision numbers, the uncompiled Table[ ] is just
> as fast:
> cpl3[x_, y_] := Ceiling[Sqrt[Outer[Plus, (Range[x] - x/2.0)^2,
> (Range[y] - y/2.0)^2]]]
> fun[x_, y_] := Table[Norm[{i, j} - {x , y}/2.0], {i, 1, x}, {j, 1, y}]

Notice the typo in the following, and the missing Ceiling wrapper that 
you gave cpl3:

> fun2[x_, y_] := Table[Sqrt[(i - x/2.0)^2 + (i - y/2.0)^2], {i, 1, x}, {j, 1, y}]


You also changed the specifications for these functions, by subtracting 
x/2.0 instead of Ceiling[x/2].

> In[46]:=
> Timing[fun[1000,1000];]
> Out[46]=
> {2.875 Second,Null}
> In[47]:=
> Timing[fun2[1000,1000];]
> Out[47]=
> {0.578 Second,Null}
> In[53]:=
> Timing[cpl3[1000,1000];]
> Out[53]=
> {0.734 Second,Null}

On my machine, cpl3 is 10 times faster than the corrected version of 
fun2. It's usually a good idea to check that the different algorithms 
give the same answer. In your case, this would have helped you discover 
the errors in fun2.

Carl Woll
Wolfram Research

> It was a good idea not to use Norm[ ]. I used Norm[ ] because I
> assumed that, as a built-in function, it would be faster than
> Sqrt[x^2+y^2], but foolishly I didn't test it.
> Szabolcs Horvat
> On 4/8/06, Carl K. Woll <carlw at> wrote:
>>szhorvat at 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
>>>cpl = Compile[{{x, _Real}, {y, _Real}}, Table[Ceiling[Norm[{i, j} -
>>>    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
>>>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.
>>   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
>>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?)