Re: "In progress" saving of data collected using Reap/Sow
- To: mathgroup at smc.vnet.net
- Subject: [mg66312] Re: "In progress" saving of data collected using Reap/Sow
- From: Maxim <m.r at inbox.ru>
- Date: Tue, 9 May 2006 02:35:06 -0400 (EDT)
- References: <e2v5pu$n64$1@smc.vnet.net><e36vft$n7c$1@smc.vnet.net> <e39k5i$cn9$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On Wed, 3 May 2006 06:55:46 +0000 (UTC), Ray Koopman <koopman at sfu.ca>
wrote:
> Expanding on my previous post, here are three ways to unravel z into
> a simple list of all the past results. The first way is fastest, but
> doesn't work if each result itself is a list.
>
> In[1]:= n = 8
> y = {}; Do[AppendTo[y,result[i]],{i,n}]; y
> z = {}; Do[z = {z,result[i]},{i,n}]; z
> Flatten[z] === y
> Nest[FlattenAt[#,1]&,z,n] === y
> Table[Last@Nest[First,z,n-i],{i,n}] === y
>
> Out[1]= 8
> Out[2]= {result[1],result[2],result[3],result[4],
> result[5],result[6],result[7],result[8]}
> Out[3]= {{{{{{{{{},result[1]},result[2]},result[3]},result[4]},
> result[5]},result[6]},result[7]},result[8]}
> Out[4]= True
> Out[5]= True
> Out[6]= True
>
> In[7]:= n = 8
> y = {}; Do[AppendTo[y,{i,-i}],{i,n}]; y
> z = {}; Do[z = {z,{i,-i}},{i,n}]; z
> Flatten[z] === y
> Nest[FlattenAt[#,1]&,z,n] === y
> Table[Last@Nest[First,z,n-i],{i,n}] === y
>
> Out[7]= 8
> Out[8]= {{1,-1},{2,-2},{3,-3},{4,-4},
> {5,-5},{6,-6},{7,-7},{8,-8}}
> Out[9]= {{{{{{{{{},{1,-1}},{2,-2}},{3,-3}},{4,-4}},
> {5,-5}},{6,-6}},{7,-7}},{8,-8}}
> Out[10]= False
> Out[11]= True
> Out[12]= True
>
Two heads are better than one:
In[1]:= z = h[]; Do[z = h[z, {i, -i}], {i, 10^5}] // Timing
Out[1]= {0.188*Second, Null}
In[2]:= (z = List @@ Flatten[z, Infinity, h];) // Timing
Out[2]= {0.062*Second, Null}
Maxim Rytin
m.r at inbox.ru