Re: Fast alternative to Nest[.] or NestList[.]? (version 6.0)
- To: mathgroup at smc.vnet.net
- Subject: [mg81889] Re: Fast alternative to Nest[.] or NestList[.]? (version 6.0)
- From: Mark Fisher <particlefilter at gmail.com>
- Date: Sat, 6 Oct 2007 04:32:38 -0400 (EDT)
- References: <fe4ut8$1f5$1@smc.vnet.net>
On Oct 5, 5:07 am, kristoph <kristophs.p... at web.de> wrote: > Dear all, > > I have a timing issue considering the listing below. I'm applying a > Nest function to simulate some data. So far it takes about 0.5 seconds > for each simulation.I was wondering if there is a very fast > alternative to the build-in function Nest or NestList. > > I would be very grateful for any comments or insights regarding > cutting running time. > Please do not be deterred from the program most of the parameters are > just inputs for the simulation. At the end there is > Timing[Nest[iterate, {5, 5}, periods]] which I need much faster than > it is already. > > Thank you very much in advance, > Kristoph > > (* this is input which is required for the simulation*) > div=Table[RandomInteger[{10}],{i,1,10},{j,1,10}]; > inv=2; > zus=Length[div]; > wert=Dimensions[div][[2]]; > periods = 500; > > (*some precalculations*) > reldiv = Table[div[[i, k]]/Total[div[[i]]], {i, 1, Length[div]}, {k, > 1, wert}]; > gop = Table[Mean[reldiv[[All, i]]], {i, 1, wert}]; > eq = Table[1/wert, {i, 1, wert}]; > lambda = Transpose[{gop, eq}]; > h = 81; > g = 81; > rho[h_, g_] := {{h/200, 0}, {0, g/200}}; > verm = {a, b}; > theta[a_, b_] =Simplify[Table[(lambda[[k, i]]*rho[h, g][[i, > i]]*verm[[i]])/Sum[lambda[[k, j]]*rho[h, g][[j, j]]*verm[[j]] , {j, 1, > inv}], {i, 1, inv}, {k, 1, wert}] ]; > > preCalc[a_, b_] = > Inverse[IdentityMatrix[inv] - theta[a, b].lambda.rho[h, g]].theta[a, > b] // Simplify; > iterate[{a_, b_}] := > N[preCalc[a, b].div[[Ceiling[zus*RandomReal[]]]]]; > relverm[a_, b_] := (rho[h, g][[1, 1]] a)/Total[rho[h, g].{a, b}]; > > (*this I need to be fast*) > Timing[Nest[iterate, {5, 5}, periods]] Hi, This give me a factor of 20 speedup: cf = Compile[{a, b}, preCalc[a, b] // Evaluate]; Timing[Nest[(cf @@ #).div[[Ceiling[zus*RandomReal[]]]] &, {5, 5}, periods]] --Mark