MathGroup Archive 2011

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

Search the Archive

Re: How to Compile this code (multiple random walks)

  • To: mathgroup at smc.vnet.net
  • Subject: [mg118096] Re: How to Compile this code (multiple random walks)
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Tue, 12 Apr 2011 05:57:35 -0400 (EDT)

On 4/11/11 at 7:06 AM, mfific at gmail.com wrote:

>I would appreciate help with Compile function. Below is the code
>that I could not compiled. The output are the three random walk
>counters with (-5, 5) bounds, along with a counter for a number of
>steps. The compile does not work here, I assume because the output
>is a matrix (it works if there is only one counter). But I need
>these three counters running in parallel for later development. Any
>help would be appreciated.

>Transpose[{Range[0, Length[#] - 1], #}] &@
>NestWhileList[(# + {If[Random[] > .5, 1, -1], If[Random[] > .5, 1,
>-1], If[Random[] > .5, 1, -1]} ) &, {0, 0, 0}, -5 < #[[1]] < 5 && -5
>< #[[2]] < 5 && -5 < #[[3]]
>< 5 & ]

I cannot answer your question about Compile as I basically don't
use it. My experience has been, there isn't all that much speed
gained with well chosen functional algorithm. The following code
will have the same output characteristics as your code but will
run faster

MapIndexed[Join[#2 - 1, {#1}] &,
  NestWhileList[(# + Sign[RandomInteger[1, {3}] - .5]) &, {0, 0, 0},
   Max[Abs@#] < 5 &]]

Here, I am taking advantage of several things. First, generating
three random values at a time is more efficient than generating
one random value at a time. Since RandomInteger[] by default
returns either 0 or 1 with equal probability,
Sign[RadomIngeger[]-.5] will return -1 or 1 with equal
probability. So Sign[Random[1,{3}] -.5] will return the a 3
element list equivalent to your list of If statements but will
be faster. Similarly, Max[Abs@#]& should be faster than
individually looking at each element of the list. Lastly, I've
used MapIndexed here since it is more specific to what you are
trying to do than your approach. However, this may not be any
faster since both Range and Transpose execute very quickly.

One other thing you might consider. That would be to generate a
2D array of integers, i.e.,

MapIndexed[Join[#2 - 1, #1] &,
  NestWhileList[(# + Sign[RandomInteger[1, {3}] - .5]) &, {0, 0, 0},
   Max[Abs@#] < 5 &]]

The output from this code will be structured differently than
your code. But since it is a flat array of just integers it will
be possible to make it a packed array which likely will produce
some efficiencies in code that uses the result from this code.
Also, a code producing a flat array might work better with
Compile. But, the length of the list generated by this code
likely won't be much longer than 10-20 rows at most (I am
guessing a bit here since I haven't taken the time to do a
thorough analysis). Consequently, it is unlikely you will be
able to get any significant speed improvement by using Compile.



  • Prev by Date: How to change the page margin?
  • Next by Date: Re: from Rumen, MEXICO, petition
  • Previous by thread: Re: How to Compile this code (multiple random walks)
  • Next by thread: Re: How to Compile this code (multiple random walks)