MathGroup Archive 2007

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

Search the Archive

Re: Fast alternative to Nest[.] or NestList[.]? (version 6.0)

  • To: mathgroup at smc.vnet.net
  • Subject: [mg81900] Re: [mg81874] Fast alternative to Nest[.] or NestList[.]? (version 6.0)
  • From: DrMajorBob <drmajorbob at bigfoot.com>
  • Date: Sat, 6 Oct 2007 04:38:18 -0400 (EDT)
  • References: <9545769.1191601521352.JavaMail.root@m35>
  • Reply-to: drmajorbob at bigfoot.com

No, you can't make Nest any faster, but my "iter" function (below) is 6 or  
7 times faster than "iterate".

(The other changes are for style and shouldn't matter.)

(*this is input which is required for the simulation*)
div = RandomInteger[{10}, {10, 10}];
inv = 2;
{zus, wert} = Dimensions@div;
periods = 500;

(*some precalculations*)
reldiv = div/(Total /@ div);
gop = Mean@reldiv;
lambda = Transpose@{gop, ConstantArray[1/wert, wert]};
h = 81;
g = 81;
rho[h_, g_] = {{h/200, 0}, {0, g/200}};
verm = {a, b};
theta[a_, b_] =
   Simplify@Transpose[#/Total /@ #] &[
    Diagonal[rho[h, g]]*verm*# & /@ lambda];

preCalc[a_, b_] =
   Inverse[IdentityMatrix[inv] - theta[a, b].lambda.rho[h, g]].theta[a,
       b] // Simplify;

Clear[iter, a, b]
iter[i_Integer] := preCalc[a, b].div[[i]] // Simplify
iter[a_, b_, i_Integer] := iter[a, b, i] = iter[i]
Timing[Table[iter[a, b, i], {i, 1, zus}];]
iter[{aa_, bb_}] :=
  N[iter[a, b, Ceiling[zus*RandomReal[]]] /. {a -> aa, b -> bb}]

{0.031, Null}

(The Table above MUST be precalculated, even though it's never used.)

Timing[Nest[iter, {5, 5}, periods]]

{0.094, {70.3256, 10.3466}}

Bobby

On Fri, 05 Oct 2007 03:56:24 -0500, kristoph <kristophs.post at web.de> wro=
te:

> 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]]
>
>
>



-- =

DrMajorBob at bigfoot.com


  • Prev by Date: Re: Generic TCPIP sockets in Mathematica (Mathlink?)
  • Next by Date: Re: How to help Mathematica take an integral
  • Previous by thread: Re: Fast alternative to Nest[.] or NestList[.]? (version 6.0)
  • Next by thread: What is the purpose of the Defer Command?