Re:Function compile problems
- To: mathgroup at smc.vnet.net
- Subject: [mg26177] Re:[mg26128] Function compile problems
- From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
- Date: Thu, 30 Nov 2000 01:04:17 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Chris tried to use the following definition of a function using compile and
got error messages.
-------------------
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
];
----------------------
Compile chokes for two reasons.
(1) You can't use global variables such as (g, xy) inside Compile.
(2) You can't use a pattern such as {r_,t_} inside Compile.
A version that will use compiled evaluation is given below.
Notice use of (N) isn't needed since I ensure Table returns approximate
numbers.
test2 =Compile[{{n, _Integer}},
Map[ First[#] * Cos[n*Last[#]]&,
Table[{x, y}, {x, -1.0, 1.0, 0.5}, {y, 1.0, -1.0, -0.5}], {2}
]
];
------------------
It would be nice to use
Apply[ #1, Cos[n,#2]&, Table[.....], {}]
but compiled evaluation only supports Apply when the first argument is List,
Plus, or Times.
For much more on this subject look up Compile on my website:
http://www.verbeia.com/mathematica/tips/Tricks.html
------------------
Regards,
Ted Ersek