in reply to Processing data with lot of math...
Some (untested) pseudo-code that may help (assuming from your description that you have separate arrays for each of X, Y, and Z):
- calculate distance from origin (or some other point) for each point in your set
@dist = map sqrt($x[$_]**2+$y[$_]**2+$z[$_]**2), 0..$#x;
or, for some $x0, $y0, $z0,
@dist = map sqrt(($x[$_]-$x0)**2+($y[$_]-$y0)**2+($z[$_]-$z0)**2), 0..$#x;
- sort points by that distance
@indices = sort {$dist[$a]<=>$dist[$b]} 0..$#dist;
@dist = @dist[@indices];
@x = @x[@indices];
@y = @y[@indices];
@z = @z[@indices];
@name = @name[@incides];
- loop through your points, for each point checking distance of the following points only until the distance
from origin has increased by your cutoff
for my $one (0..$#@dist-1) {
my $two = $one;
while (++$two < @dist && $dist[$two]-$dist[$one] <= $cutoff) {
print "found pair: ($x[$one],$y[$one],$z[$one]) and
($x[$two],$y[$two],$z[$two])\n"
if sqrt(($x[$one]-$x[$two])**2+($y[$one]-$y[$two])**2+($z[
+$one]-$z[$two])**2) <= $cutoff;
}
}
This limits how much work the interior loop has to do
for each iteration of the exterior loop (unless you have
points mosty lying within cutoff distance from the surface of a sphere around your chosen origin, so some knowlege of your data will help choose a good origin).
Update: added code fragments
Update: sort the names with the points :)
Re: Re: Processing data with lot of math...
by QM (Parson) on May 12, 2004 at 23:00 UTC
|
Why use sqrt? There is no added value in it; instead, transform the cutoff value.
-QM
--
Quantum Mechanics: The dreams stuff is made of
| [reply] [d/l] |
|
| [reply] |
|
Oops, my reply lies above. Please forgive me this time. Was not aware what I was doing..., presumed that the reply will come at the bottom. :)
| [reply] |
Re: Re: Processing data with lot of math...
by BrowserUk (Patriarch) on May 12, 2004 at 18:53 UTC
|
p( 10, 10, 10 );
p( 14.142, 10, 0 );
p( 10, 14.142, 0 );
Using the origin as the base, all three points have a calculated distance of 17.32 and will sort adjacent to each other, but they are obviously in completely different positions and the distance between each of the three points varies greatly.
Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
| [reply] [d/l] |
|
Look again.
The point is to avoid having both loops go over all the points. The distance from origin is just one way of easily filtering out some (with luck, most) points for the inner loop (since the distance between two points is guaranteed to be greater than or equal to the difference in their distances from a third point).
The distance between $one and $two is still checked.
| [reply] |
|
Okay, I see you made a few updates since I read your original and went off to verify my thoughts.
I guess it depends upon the number and density of the points as to how effective the filtering would be.
I was picturing something like a buckyball. If the origin was at the centre of the buckyball, then every atom (molecule??) would either be filtered in or filtered out, unless the constraining value was extremly tight.
Of course, you can move the origin, and then you'd get a greater or lesser ring of potentials, depending upon the relationship between the chosen origin and the position of the atom under test.
Or with a buckytube, you get rings, ovals, or two long parallel lines of potentials.
Then again, bucky-anything is probably a fairly rare case:)
Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
| [reply] |
|
|
|
|