Re: Compile question
- To: mathgroup at smc.vnet.net
- Subject: [mg73916] Re: Compile question
- From: dh <dh at metrohm.ch>
- Date: Sat, 3 Mar 2007 00:59:53 -0500 (EST)
- References: <es6d6h$rq1$1@smc.vnet.net>
Hi Ray,
if you replace the empty list in the Last If[..] by e.g.: {-1} it works
as expected. Why this is so I can only guess. Presumably, a compiled
function must always return the same type of expression. And an empty
array may be regarded as an empty array of reals.
Daniel
Ray Koopman wrote:
> I've been trying different ways to "walk the list" as suggested in
> the recent thread "split again". Here is some uncompiled code that
> works as I expected, but the compiled version returns reals instead
> of integers. Does anyone know why?
>
> SeedRandom[2]; z = Table[Random[],{20}]
>
> {0.238705,0.844529,0.473928,0.421515,0.512692,
> 0.0228035,0.529257,0.0507012,0.74907,0.632356,
> 0.0725195,0.251276,0.902785,0.672727,0.419794,
> 0.512577,0.404555,0.00984124,0.168643,0.50482}
>
> s4[z_,a_,b_] := Module[{n = Length@z, j = 0, k}, k = n;
> Do[If[ z[[i]] >= a, If[ i <= j+b+1, j = i, k = i-1; Break[]]], {i,n}];
> If[ k <= j+b, {}, {j+1,k} ] ]
>
> s4[z,.7,5]
> s4[z,.7,6]
> s4[z,.7,7]
>
> {3,8}
> {14,20}
> {}
>
> s4c = Compile[{{z,_Real,1},{a,_Real},{b,_Integer}},
> Module[{n = Length@z, j = 0, k}, k = n;
> Do[If[ z[[i]] >= a, If[ i <= j+b+1, j = i, k = i-1; Break[]]], {i,n}];
> If[ k <= j+b, {}, {j+1,k} ] ],
> {{n,_Integer},{j,_Integer},{k,_Integer},{i,_Integer}} ];
>
> s4c[z,.7,5]
> s4c[z,.7,6]
> s4c[z,.7,7}
>
> {3.,8.}
> {14.,20.}
> {}
>
>