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: [mg23225] Re: fastest way to do pair-sum / make pair-list
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Tue, 25 Apr 2000 01:40:29 -0400 (EDT)
  • References: <8dok75$jds@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Wijnand ,

First using Sum

sds1[data_] :=
  Sum[(-data[[i]] + data[[j]])^2, {i, 1, Length[data]}, {j, 1, i - 1}]

But it is quicker to use matrix operations, particularly Dot to sum squares.

sds2[data_] := #.# &[{1, -1}.{Flatten[
          NestList[Drop[#, -1] &, Drop[data, -1], Length[data] - 2], 1],
        Flatten[NestList[Drop[#, 1] &, Drop[data, 1], Length[data] - 2],
1]}]

Tests

data = Table[Random[], {300}];

sds1[data] // Timing

{9.99 Second, 7678.5}

sds2[data] // Timing

{1.32 Second, 7678.5}

Extra check

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

(a - b)^2 + (a - c)^2 + (b - c)^2 + (a - d)^2 +
  (b - d)^2 + (c - d)^2

--
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

"Wijnand Schepens" <Wijnand.Schepens at rug.ac.be> wrote in message
news:8dok75$jds at smc.vnet.net...
> 1.
>
> What is the most efficient way to calculate the sum
>
> Sum f [ (x[[i]]-x[[j]])^2 ]
> i<j
>
> ??
>
> Example:
>
> I have a vector of real numbers:
> lst = N[Range[100]];
> and a function operating on a number:
> f[r2_]:=(r2-1.0)^2
>
> Is
>
> Sum[  Sum[  f [ (lst[[i]]-lst[[j]])^2 ] , {j,i+1,Length[lst]}],
> {i,1,Length[lst-1]}]
>
> the fastest way??
>
> This kind of sum is very common in Molecular Modelling, where the total
> energy of a system is often a sum of pair-energies, which only depend on
> the distance between atoms.
> I was surprised that I didn't find anything on sums over pairs in
> Mathematica...
>
> 2.
>
> What is the most efficient way to generate a list of pairs starting from
> a list??
> Is there a standard Mathematica routine which does this?
>
> e.g.  {a,b,c,d} ----> {{a,b},{a,c},{a,d},{b,c},{b,d},{c,d}}
>
> or {x1,x2,...} -----> { {xi,xj} ...}
> with i<j
>
> Best solution I found was
> topairs[lst_] :=
>   Module[{l=Length[lst]},
>      Map[(Sequence @@ #1) &,
>              Table[ lst [[{i, j}]], {i, 1, l - 1}, {j, i + 1, l}]
>      ]
>   ]
>
> Another possibility would be
> topairs2[lst_] :=
>   Module[{l = Length[lst]},
>     Partition[
>        Flatten[
>            Table[ lst[[{i, j}]], {i, 1, l - 1}, {j, i + 1, l}]
>        ], 2
>     ]
>  ]
>
> but this doesn't have the same effect if operating on a list of lists
>   topairs[{{a1, a2}, {b1, b2}, {c1, c2}}]
> gives what we want,
>   topairs2[{{a1, a2}, {b1, b2}, {c1, c2}}]
> not
>
>




  • Prev by Date: fastest way to do pair-sum / make pair-list
  • Next by Date: Re: minimization of a function operating on a vector or reals
  • Previous by thread: fastest way to do pair-sum / make pair-list
  • Next by thread: Re: fastest way to do pair-sum / make pair-list