MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Compile question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg73825] Compile question
  • From: "Ray Koopman" <koopman at sfu.ca>
  • Date: Thu, 1 Mar 2007 06:18:52 -0500 (EST)

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.}
{}



  • Prev by Date: Limit of Floor function
  • Next by Date: Re: Re: Symbol name extraction
  • Previous by thread: Re: Limit of Floor function
  • Next by thread: Compile question