Re: Newbie question: pairswise function application
- To: mathgroup at smc.vnet.net
- Subject: [mg24015] Re: Newbie question: pairswise function application
- From: Hartmut Wolf <hwolf at debis.com>
- Date: Tue, 20 Jun 2000 03:07:31 -0400 (EDT)
- Organization: debis Systemhaus
- References: <8i1t6n$jn9@smc.vnet.net> <Pine.GSO.4.21.0006131745370.18990-100000@flip.eecs.umich.edu> <tXg25.30$x6.4664@ralph.vnet.net> <8ihv93$lfd@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Dear Allan, a little remark to be made, see below: Allan Hayes schrieb: > > I have been concerned that the solution in my earlier posting did not give > the requested order. > The following slighty slower version does give the requested order and, on > my tests, is about three times as fast as fastest other solution posted. > > pairs[data_, f_] := > MapThread[f, > {Join @@ (NestList[Rest, Table[1, {# - 1}], > # - 2]Drop[data, -1]), > Join @@ (NestList[Drop[#, 1] &, Drop[data, 1], # - 2]) > } &[Length[data]] > > ] > > If only the pairs are needed then the following seems to be particularly > fast > > pairs[data_] := Transpose[ > {Join @@ (NestList[Rest, Table[1, {# - 1}], > # - 2]Drop[data, -1]), > Join @@ (NestList[Drop[#, 1] &, Drop[data, 1], # - 2]) > } &[Length[data]] > > ] > your function indeed is fast In[20]:= Print[Timing[pairs[#, Times]][[1]]] & /@ (Range[100 #] & /@ Range[8]); >From In[20]:= 0.191 Second >From In[20]:= 0.55 Second >From In[20]:= 1.181 Second >From In[20]:= 2.143 Second >From In[20]:= 3.365 Second >From In[20]:= 5.277 Second >From In[20]:= 7.401 Second >From In[20]:= 9.764 Second however has a less fortunate time complexity than what I had proposed earlier: In[15]:= Print[Timing[MapThread[Function[y, Times[#1, y]] /@ #2 &, Through[{First, Rest}[NestList[Rest, #, Length[#]]]]] // Flatten][[1]]] & /@ (Range[100 #] & /@ Range[8]); >From In[15]:= 0.371 Second >From In[15]:= 0.591 Second >From In[15]:= 0.901 Second >From In[15]:= 1.302 Second >From In[15]:= 1.843 Second >From In[15]:= 2.413 Second >From In[15]:= 3.224 Second >From In[15]:= 4.056 Second Kind regards, Hartmut