MathGroup Archive 2000

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

Search the Archive

Re: fastest way to do pair-sum / make pair-list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg23260] Re: [mg23170] fastest way to do pair-sum / make pair-list
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Sat, 29 Apr 2000 22:05:03 -0400 (EDT)
  • References: <200004210348.XAA19772@smc.vnet.net> <FHsN4.409$UI4.20185@ralph.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Carl,
For pair creation, pairs2, below, seems to be about three times as fast as
the function, pairs, that you give.
It is taken from my recent posting on this thread, apart from the final
Transpose.

(1) Carl Woll
mask[n_] :=
  Module[{r = Range[n - 1]},
    Flatten[Range @@@ Transpose[{r(n + 1) - (n - 1), r n}]]]

pairs[n_List] := Module[{m, allpairs}, m = mask[Length[n]];
    allpairs = Flatten[Outer[List, n, n], 1];
    allpairs[[m]]]

pairs[{a, b, c, d}]

        {{a, b}, {a, c}, {a, d}, {b, c}, {b, d}, {c, d}}

(2) Allan Hayes

pairs2[data_] := (*AH*)
  Transpose[Apply[Join,
          NestList[#, #[data], Length[data] - 2]] & /@ {Drop[#, -1] &,
        Drop[#, 1] &}]


pairs2[{a, b, c, d}]

        {{a, b}, {b, c}, {c, d}, {a, c}, {b, d}, {a, d}}

Timings: {length of data, pairs, pairs2}

data[n_] := Table[Random[], {n}];

Do[Print[{10 2^n, (pairs[#]; // Timing // First)/Second,
          (pairs2[#]; // Timing // First)/Second}] &[data[10 2^n]], {n, 0,
6}]

{10, 0., 0.}

{20, 0.11, 0.}

{40, 0.22, 0.05}

{80, 0.22, 0.11}

{160, 0.72, 0.22}

{320, 3.02, 0.99}

{640, 12.96, 4.34}

--
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565

"




  • Prev by Date: Re: Startup Notebook?
  • Next by Date: RE: Re: Plotting bounded domains
  • Previous by thread: Re: fastest way to do pair-sum / make pair-list
  • Next by thread: Re: fastest way to do pair-sum / make pair-list