
Re: Compiling numerical iterations
Posted:
Feb 25, 2013 2:19 AM


Hello. My ideas are very similar to Ray's ideas. It seems you are doing a random walk 1,000 times, and adding the positions repeatedly. I may be wrong, but I interpret a little differently. Here's what I'm thinking, Your steps are + 1, but your adding +.1 each time. (ie /10)
tab=Table[0,{1000}]; Do[tab = tab + NestList[#+RandomChoice[{+1,1}]&,0,10001]/10,{1000}];
// Or perhaps without the division
tab=Table[0,{1000}]; Do[tab = tab + NestList[#+RandomChoice[{+.1,.1}]&,0,10001],{1000}];
I like the FoldList idea also. Perhaps a different approach:
FoldList[Plus,0,RandomInteger[{1,1},10001]] / 10
If I run Ray's excellent code, here are the first 5 items of his corr array using the same Seed Number.
num=1000; SeedRandom[1234567890] AbsoluteTiming@Length[ = tab=FoldList[Plus,0,Total@Sign@Table[Most@RandomReal[{1,1},num],{1000}]] ] = AbsoluteTiming@Length[corr=Table[Take[tab,{k,num*9/101+k}],{k,num/10}]. Drop[tab,num/10]/(num*10*9)]
ray = corr[[1;;5]] {3019993/1875, 36211327/22500, 7236173/4500, 4018739/2500, 36160711/22500}
I believe the constant is
n=1000/10*9 900
Constant = 1/n, or 1/900
Ray uses a constant of 1/ 90000.
I may be wrong, but I "think" it should be 1/900.
Here's my idea once the tab array is calculated. I'll use Ray solution for comparison. You can skip the initialization of the corr array of all zero's at this point.
CorrMe=Partition[tab,1000/10*9,1]//Most; CorrMe=Map[Dot[CorrMe[[1]],#]&,CorrMe] / 90000;
me =CorrMe[[1;;5]] {3019993/1875, 36211327/22500, 7236173/4500, 4018739/2500, 36160711/22500}
me == ray True
= = = = = = = = = = HTH :>) Dana DeLouis Mac & Mathematica 9 = = = = = = = = = =
> tab = Table[0, {i, 1, num}]; > > For[l = 1, l <= 1000, l++, > a = 0; > For[i = 1, i <= num, i++, > tab[[i]] = tab[[i]] + a/10; > > If[RandomReal[{1, 1}] < 0, a = a + 1, a = a  1]; > > ] > > ] >
On Saturday, February 23, 2013 7:01:22 AM UTC5, firlefranz wrote: > Hi, > > > > to speed up some calculations I'd like to use this relatively new method of compiling equations or export them to C. But since I'm very new in these things, I have really problems to use this ccompiling function. > > Can someone show me on this simple part of code (calculation of a correlation function of a 1D random walk), how it should be implemented? > > > > num = 1000; > > tab = Table[0, {i, 1, num}]; > > corr = Table[0, {i, 1, num/10}]; > > For[l = 1, l <= 1000, l++, > > a = 0; > > For[i = 1, i <= num, i++, > > tab[[i]] = tab[[i]] + a/10; > > If[RandomReal[{1, 1}] < 0, a = a + 1, a = a  1]; > > ] > > ] > > For[k = 1, k <= num/10, k++, > > For[n = 1, n <= num/10*9, n++, > > corr[[k]] = corr[[k]] + tab[[n]]*tab[[n + k  1]]/num*10/9]; > > ] > > ListPlot[{corr}, Joined > False] > > > > Best regards > > Cornelius

