Re: Compile question
- To: mathgroup at smc.vnet.net
- Subject: [mg73864] Re: Compile question
- From: Oliver Ruebenkoenig <ruebenko at uni-freiburg.de>
- Date: Fri, 2 Mar 2007 06:33:08 -0500 (EST)
- References: <es6d6h$rq1$1@smc.vnet.net>
a Ray, the problem is in the empty list you return. that is not of type integer. 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, List[0], List[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} {0} you might want to return a -1 and catch that or something the like. hope this helps, oliver On Thu, 1 Mar 2007, 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.} > {} > > > Oliver Ruebenkoenig, <ruebenko AT uni-freiburg.de>