[Date Index]
[Thread Index]
[Author Index]
Re: How to compile this module?
*To*: mathgroup at smc.vnet.net
*Subject*: [mg80934] Re: How to compile this module?
*From*: Szabolcs <szhorvat at gmail.com>
*Date*: Thu, 6 Sep 2007 05:17:43 -0400 (EDT)
*Organization*: University of Bergen
*References*: <fbliht$or0$1@smc.vnet.net>
P_ter wrote:
> I have some code in a Module and I would like to compile it so that Mathematica does not give complaints in execution:
>
> The following is compiled by Mathematica:
> myC = Compile[{{lst, _Integer}, {W, _Integer}},
> Module [ {i, j = 1, len = Length@lst, s, res},
> Reap[For[i = 1, i <= len, i++,
> While[lst[[j]] - lst[[i]] <= W && j < len, j++];
> Sow[j - i]]; res]
> ], {{res, _Integer, 4}}
> ]
> If Mathematica executes: myC[lst,2], it gives a message that the argument lst at position 1 should be a machine-size integer.
> Can anyone help?
There is something in your code I do not understand: what is the purpose
of that stray 'res' at the end of Reap?
The first problem is that 'lst' is a 1D list of Integers, so instead of
{lst, _Integer}, use {lst, _Integer, 1}.
The second problem is that Reap and Sow cannot be compiled (IMO they
were not really meant to be used with this procedural style of
programming). You can see this if you check the InputForm of the
compiled function (InputForm[myC]). One solution is to use AppendTo
instead.
(See this page:
http://support.wolfram.com/mathematica/kernel/Symbols/System/Compile.html )
This function works:
myC = Compile[{{lst,_Integer,1},{W,_Integer}},
Module[{i,j=1,len=Length@lst,ols={}},
For[i=1,i<=len,i++,
While[
lst[[j]]-lst[[i]]<=W && j<len,
j++
];
AppendTo[ols,j-i]
];
ols
]]
Now I just wish I could figure out how to prevent Mathematica from
treating 'ols' as a vector of Reals without initialising it to {0}.
Specifying its type in Compile does not seem to work.
Szabolcs
Prev by Date:
**Re: random points!**
Next by Date:
**Re: RE: What is $MaxNumber on a 64 bit Computer?**
Previous by thread:
**Re: How to compile this module?**
Next by thread:
**Re: How to compile this module?**
| |