Re: Fast calculation of pair correlation function
- To: mathgroup at smc.vnet.net
- Subject: [mg101423] Re: [mg98626] Fast calculation of pair correlation function
- From: DrMajorBob <btreat1 at austin.rr.com>
- Date: Tue, 7 Jul 2009 05:04:44 -0400 (EDT)
- References: <200904141019.GAA07830@smc.vnet.net>
- Reply-to: drmajorbob at bigfoot.com
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 = Flatten@Table[ Ceiling[Norm[data[[i]] - data[[j]]]/delta], {i, 1, Length[data] - 1}, {j, i + 1, Length[data]}];] Histogram@histogr {4.57758, Null} or (no divide by delta gives a nicer density function) Timing[histogr = Flatten@Table[ Norm[data[[i]] - data[[j]]], {i, 1, Length@data - 1}, {j, i + 1, Length@data}];] Histogram@histogr {3.93137, Null} Drawing the histogram is slower than computing the distances, and I've left that out of the Timing. Bobby On Tue, 14 Apr 2009 05:19:43 -0500, markus <markusg.phys at googlemail.com> wrote: > 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 bigfoot.com