Re: Re: NestListEffects function not working in 6.0

*To*: mathgroup at smc.vnet.net*Subject*: [mg77170] Re: [mg77059] Re: NestListEffects function not working in 6.0*From*: DrMajorBob <drmajorbob at bigfoot.com>*Date*: Tue, 5 Jun 2007 06:39:58 -0400 (EDT)*References*: <f3ofsh$p6j$1@smc.vnet.net> <19984802.1180984441947.JavaMail.root@m35>*Reply-to*: drmajorbob at bigfoot.com

I suspect you're calling the function improperly... but I can't test that theory without know how you're calling it. I've written a replacement, anyway, that I think does what you want - a lot more efficiently. Clear[nestListEffects, sideFX, nestFX] Options[nestListEffects] = {returnValues -> {#1 &, 1}, sideEffects -> {}}; nestListEffects::usage = "nestListEffects[f, expr, n, returnValues -> {g, k}] returns a list \ of every kth element of NestList[f, expr, n] after the application of \ g. nestListEffects[f, expr, n, sideEffects -> {{g1, k1}, {g2, \ k2},...}] applies gi after every ki steps, but does not affect return values."; sideFX[expr_, i_Integer][{gg_, kk_Integer}] := Divisible[i, kk] && gg[expr, i] nestFX[f_, g_, k_Integer, sides_List][{expr_, i_Integer}] := Module[{e = f@expr}, Divisible[i, k] && Sow@g[e, i]; Scan[sideFX[e, i], sides]; {e, i + 1} ] nestListEffects[f_, expr_, n_Integer, opts___Rule] := Module[{sides, ret, g, k}, {ret, sides} = {returnValues, sideEffects} /. {opts} /. Options[nestListEffects]; {g, k} = ret; Reap[Nest[nestFX[f, g, k, sides], {expr, 1}, n]][[-1, 1]] ]; Here's a test: nestListEffects[2 # &, 1, 100, returnValues -> {{##} &, 20}, sideEffects -> {{Print[{##}] &, 15}}] {32768,15} {1073741824,30} {35184372088832,45} {1152921504606846976,60} {37778931862957161709568,75} {1237940039285380274899124224,90} {{1048576, 20}, {1099511627776, 40}, {1152921504606846976, 60}, {1208925819614629174706176, 80}, {1267650600228229401496703205376, 100}} And here's a call that DOESN'T work properly: nestListEffects[2 # &, 1, 100, returnValues -> {{##} &, 20}, sideEffects -> {Print[{##}] &, 15}] {{1048576, 20}, {1099511627776, 40}, {1152921504606846976, 60}, {1208925819614629174706176, 80}, {1267650600228229401496703205376, 100}} I think that's precisely the kind of mistake you're making with your original function. Bobby On Sat, 02 Jun 2007 03:16:22 -0500, Jon Davis <jon_davis at mac.com> wrote: > Sorry about the lack of specificity regarding problems in my last post. > More details on my problem with NestListEffects follow. > > The NestListEffects function is basically an enhanced version of > NestList. It does the same thing as NestList (applies a function f to > an expression n times). It also does some other nifty stuff. For > example, if I pass SideEffects -> {{printCounter, 10}} to > NestListEffects as the opts___ argument, then the function prints out > the number of iterations completed as NestList runs. For example, if n > is 200 and SideEffects ->{{printCounter, 10}}, then I'll get a count > output every 10 iterations of NestList. That is, in the notebook as > NestList is processing, the numbers 0 10 20 ... 200 will print, > sequentially, as the 200 steps are completed. This is very handy for > complex functions and large data sets because it can take several hours > for a simulation to complete and I can see how far things have > progressed. > > NestListEffects worked in Mathematica 4.0 for sure. It doesn't seem to > work in Mathematica 6.0. The simulations run correctly, so NestList is > still properly applied. However, the numbers don't print out every 10 > iterations of the simulation... > > Hope this helps! > > > On 2007-06-01 01:55:13 -0500, Jon Davis <jdavis at bus.wisc.edu> said: > >> I'm coming back to Mathematica after a couple of years and I just >> upgraded to 6.0. I have relied on a function for quite awhile that I >> got from a book by Richard Gaylord and Louis D'Andria titled >> "Simulating Society". The function is very useful for agent-based >> simulations and for some reason, it doesn't seem to work in version >> 6.0... No incompatibilities are identified by the version advisory >> scanner, so I'm not sure what to do. Can anyone help? Functions follow. >> Any help is deeply appreciated!!! I can provide a notebook with example >> usage on request. >> >> NestListEffects::usage = "NestListEffects[f, expr, n, ReturnValues >> =E2=86= >> =92 >> {g, k}] returns a list of every kth element of NestList[f, expr, n] >> after the application of g. NestListEffects[f, expr, n, SideEffects >> =E2=86= >> =92 >> {{g1, k1}, {g2, k2},...}] applies gi after every ki steps, but does not >> effect return values."; >> >> ReturnValues::usage = "ReturnValues is an option to NestListEffects >> that determines what is returned and how often."; >> >> SideEffects::usage = "SideEffects is an option to NestListEffects that >> applies functions without effecting return values. The option >> SideEffects does not affect return values"; >> >> Options[NestListEffects]={ReturnValues-> {#&, 1}, SideEffects-> {}}; >> >> NestListEffects[f_, expr_, n_, opts___]:= If[({ReturnValues, >> SideEffects} /. {opts} /. Options[NestListEffects]) === {{#&, 1}, { >> }}, >> NestList[f, expr, n], >> (* else *) >> Module[{c = 0, lis = {}, side, ret}, {ret, side} = {ReturnValues >> , >> SideEffects} /. {opts} /. Options[NestListEffects]; >> If[side === {}, side = {{Null, Infinity}}]; >> Nest[( If[Mod[c, ret[[-1]]] === 0, lis = Join[lis, {First [ret >> ][#, c]}]]; >> Table[If[Mod[c, side[[i, -1]]] === 0, side[[i, 1]][#, c]], >> {i, >> Length[side]}]; >> c++; >> #)&[f[#]]&, expr, n]; >> lis] >> ]; > > > -- = DrMajorBob at bigfoot.com