Re: mathematica expressions in Compile
- To: mathgroup at smc.vnet.net
- Subject: [mg62006] Re: mathematica expressions in Compile
- From: Peter Pein <petsie at dordos.net>
- Date: Wed, 9 Nov 2005 03:45:20 -0500 (EST)
- References: <dkpovc$reu$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
boyandshark schrieb: > Hello - > > I was wondering what is happening in the following example : > > Compile[{}, Module[{i = 0, nums}, nums = {0, 0, 0}; i = > First[Ordering[{1., 2., 3.}]]; nums[[i]]++; nums]]; > > I get an error when trying to evaluate this statement because > Mathematica complains about assigning i the value of the > First[Ordering[_]] expression - Compile seems to think > First[Ordering[_]] in this example is a real number whereas I > initialized i to be an integer. I can solve this simply by > > Compile[{}, Module[{i = 0, nums}, nums = {0, 0, 0}; i = > Round[First[Ordering[{1., 2., 3.}]]]; nums[[i]]++; nums]]; > > My question is - how come Compile knows that the value of Round is an > integer, but does not know that the First[Ordering[_]] is an integer? > > Thank you. > Hi, Compile *should* know the return type of Ordering, but obviously doesn't. You have to tell Compile about this: Compile[{}, Module[{i = 0, nums = {0, 0, 0}}, i = First[Ordering[{1., 2., 3.}]]; nums[[i]]++; nums], {{_Ordering, _Integer, 1}}][] gives {1,0,0} without error- or warning-message. Peter