       Re: Function compile problems

• To: mathgroup at smc.vnet.net
• Subject: [mg26157] Re: [mg26128] Function compile problems
• From: David Withoff <withoff at southpark.wolfram.com>
• Date: Thu, 30 Nov 2000 01:04:02 -0500 (EST)
• Sender: owner-wri-mathgroup at wolfram.com

```> I have the following code that seems to work fine when not compiled:
>
> n = 4; g[{r_, t_}] := r Cos[n t];
> xy = Table[{x, y}, {x, -1, 1, 0.5}, {y, 1, -1, -0.5}];
> Map[g[#] &, xy, {2}] // N
>
> The output should look as follows:
> {{0.653644, 0.416147, -1., 0.416147, 0.653644}, {0.326822, 0.208073, -0.5,
>     0.208073, 0.326822}, {0., 0., 0., 0., 0.}, {-0.326822, -0.208073,
>     0.5, -0.208073, -0.326822}, {-0.653644, -0.416147,
>     1., -0.416147, -0.653644}}
>
> Now if I compile the same code through the following program:
>
> test1 = Compile[{{n, _Integer}},
>       g[{r_, t_}] := r Cos[n t];
>       xy = Table[{x, y}, {x, -1, 1, 0.5}, {y, 1, -1, -0.5}];
>       Map[g[#] &, xy, {2}]//N
>       ];
>
> test1, I get the following error message:
>
> CompiledFunction::"cfte": "Compiled expression  0.6536436208636119
> should be a rank 1 tensor of  "machine-size real numbers."

Try:

Compile[{{n, _Integer}},
g[{r_, t_}] := r Cos[n t];
xy = Table[{x, y}, {x, -1, 1, 0.5}, {y, 1, -1, -0.5}];
Map[g, xy, {2}],
{{_g, _Real}}]

The third argument {{_g, _Real}} in Compile tells the compiler
that the output of g will be a real number.  Without this type
specification the compiler has to guess at the output type
for g, and guesses that the result will be a rank 1 tensor.

In the current version of Mathematica (Version 4) functions
like Map and Table automatically compile calculations that can
be compiled, making it unnecessary for you to work out the use
of Compile yourself.  If the input without Compile is already
being compiled automatically, then using Compile yourself
probably would not lead to any improvement in performance.

Dave Withoff
Wolfram Research

```

• Prev by Date: RE: Equivalent functions generate different plots
• Next by Date: speed of evaluation
• Previous by thread: Re:Function compile problems
• Next by thread: Re: Function compile problems