# How to keep some of the results of the NestList

I'm using some iterative arithmetics to calculate wave propagation with the help of NestList. I have to use a small step size for iteration to guarantee the accuracy, which lead to too much data (e.g, 10^6 points) for further plotting, and the results consumes too much memories. Is there any clever way to cope with it? I didn't intend to use loops.

This is a simplified example:

NestList[Sin, 1, 10^6]


One of my idea is to use Sow and Reap. But I've no idea to integrate them with NestList or Nest

Any idea???

-
You've tried NestList[Nest[f, #, k] &, start, m]? – J. M. Jun 21 '12 at 5:18
Very good idea. – yulinlinyu Jun 21 '12 at 5:29
@J.M. Surely deserves to be an answer ;-) – Vitaliy Kaurov Jun 21 '12 at 5:31

Due to insistent public demand:

If, in a sequence of iterates $\{x,f(x),f(f(x)),\dots\}$, one only needs every $k$-th iterate (say, for $k=3$, you want $\{x,f(f(f(x))),f(f(f(f(f(f(x)))))),\dots\}$), then one can cleverly combine Nest[] and NestList[] like so:

NestList[Nest[f, #, k] &, start, n]


which yields a list containing the zeroth, $k$-th, $2k$-th, ... $nk$-th iterates.

-
J.M. I'm liking your style recently! +1 – Mr.Wizard Jun 22 '12 at 19:24
Fold[f[#1] &, x, Range[#]] & /@ Range[0, 9, 3]
(* or *)
Nest[f, x, #] & /@ Range[0, 9, 3]
(* both give: *)
{x, f[f[f[x]]], f[f[f[f[f[f[x]]]]]], f[f[f[f[f[f[f[f[f[x]]]]]]]]]}


EDIT: a variation on the second method:

nestSkip[f_, x_, stepsize_Integer, numsteps_Integer] :=
Nest[f, x, stepsize #] & /@ Range[0, numsteps]
(* examples: *)
nestSkip[g, y, 2, 2]
(* ==>  {y, g[g[y]], g[g[g[g[y]]]]} *)
nestSkip[# + 5 &, 2, 3, 3]
(* ==> {2, 17, 32, 47}  *)

-
Another very clever solution. – yulinlinyu Jun 21 '12 at 8:31
Thank you @yulinlinyu. – kglr Jun 21 '12 at 8:35

Certainly not as elegant as JMs excellent solution, but it does use Sow and Reap as the OP requested and avoids repeated recalculation of intermediate results.

Clear[selectiveNestList];
selectiveNestList[function_, step_, iterations_, initialValue_] :=
Reap[Nest[
With[{s = function[First@#]},
If[Mod[Last@#, step] === 0,
Sow[s]; {s, Last@# + 1}, {s, Last@# + 1}]] &, {initialValue,
0}, iterations   ]][[2, All, All, 1]] // First


And in use:

selectiveNestList[Cos, 2, 9, 4]


{4, Cos[Cos[4]], Cos[Cos[Cos[Cos[4]]]],Cos[Cos[Cos[Cos[Cos[Cos[4]]]]]], Cos[Cos[Cos[Cos[Cos[Cos[Cos[Cos[4]]]]]]]]}

-