Re: How to Compile this code (multiple random walks)
- To: mathgroup at smc.vnet.net
- Subject: [mg118090] Re: How to Compile this code (multiple random walks)
- From: Derek Yates <yatesd at me.com>
- Date: Tue, 12 Apr 2011 05:56:30 -0400 (EDT)
- References: <inuncc$2dl$1@smc.vnet.net>
To my knowledge NestWhileList is not compilable, so I am surprised that you say it worked for a single counter. You may need to use SetSystemOptions["CompileOptions"->"CompileReportExternal"->True] to get a warning message about not being able to compile. There is a work around for NestWhileList. FixedPointList is compilable, and by using the option SameTest carefully, you can replicate the NestWhileList behaviour. Here is some code that appears to compile properly (using Mathematica 8) and does most of what you want in compiled form. The final counter for the number of steps is not compiled. The code below is also slightly more general than yours, in that it uses the length of the input (start) vector to decide how many random walk counters there are. With[{compiledfunc=Compile[{{start,_Real,1}}, Most@FixedPointList[#+(If[#<0.5,-1,1]&)/ @RandomReal[{0,1},Length[start]]&,start, SameTest- >(Module[{b=False},Scan[If[Not[-5.<#<5.],Return[b=True]]&,#];b]&)]]}, f[start_]:=Transpose[{Range[0,Length[#]-1],#}]&@compiledfunc[start]] I don't know of any way to include the path counter into the compile as the result you generate is not a tensor. If you are planning to use the code in Mathematica, then I doubt the performance hit of not having that last step in the Compile will be very great. If you are wanting to compile it to C code, then you probably need to think of a different format to return the results.