MathGroup Archive 2009

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

Search the Archive

Re: Fast calculation of pair correlation function

  • To: mathgroup at
  • Subject: [mg101423] Re: [mg98626] Fast calculation of pair correlation function
  • From: DrMajorBob <btreat1 at>
  • Date: Tue, 7 Jul 2009 05:04:44 -0400 (EDT)
  • References: <>
  • Reply-to: drmajorbob at

The code you've posted doesn't set any "histogr" elements or give any  
output (nor do anything else), so it's not entirely clear what you meant  
to do.

(One can only hope that the "equivalent C code" isn't likewise doing  
nothing, at higher speed.)

I'm guessing this is close to your need:

delta = 1;
data = RandomReal[{0, 10}, {1000, 3}];
Timing[histogr =
      Ceiling[Norm[data[[i]] - data[[j]]]/delta], {i, 1,
       Length[data] - 1}, {j, i + 1, Length[data]}];]

{4.57758, Null}

or (no divide by delta gives a nicer density function)

Timing[histogr =
      Norm[data[[i]] - data[[j]]], {i, 1, Length@data - 1}, {j, i + 1,

{3.93137, Null}

Drawing the histogram is slower than computing the distances, and I've  
left that out of the Timing.


On Tue, 14 Apr 2009 05:19:43 -0500, markus <markusg.phys at>  

> Hi,
> I have a quite large list (length ~ 10,000) of coordinates (positions
> of particles from a simulation), e.g. data={{x1,y1,z1},
> {x2,y2,z2},...}, and I am looking for fast way to calculate the "pair
> correlation" or "radial distribution function", which tells me, for
> each value of r, how many particles are separated by a distance r (+-
> epsilon).
> Usually, this can be calculated by something like:
> Do[Do[histogr[[ Ceiling[Norm[ data[[i]]-data[[j]] ] / delta] ]], {j, i
> +1, Length[data]}], {i, 1, Length[data]-1}]
> where histogr is just a list whose index corresponds to the distance
> r.
> Unfortunately I have found that for lists of length >1,000, this way
> of calculating is very slow (it can take ~minutes), compared to the
> equivalent C code, which proceeds in a few seconds. I have also tried
> to "Compile" the function, but the speed does not increase...
> Does anybody know some fast way of calculating the pair distribution
> function in Mathematica?

DrMajorBob at

  • Prev by Date: Re: LogLinearPlot strange "features"
  • Next by Date: Re: Re: Collect exponents only
  • Previous by thread: Re: Re: Polynomial rewriting question
  • Next by thread: Re: Fast calculation of pair correlation function