Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Processing data with lot of math...

by ysth (Canon)
on May 12, 2004 at 18:14 UTC ( [id://352855]=note: print w/replies, xml ) Need Help??


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

Replies are listed 'Best First'.
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

      Because there's no point in optimizing until you have triaged your algorithm (irremedially slow, a little too slow, or fast enough). If I were certain what form the original questioner's points stored in, I would have even used a distance() sub. Code should be written for minimum conceptual complexity and then optimized only if needed, and having a separate $cutoff_squared checking against a square distance is beyond my minimum. YMMV.

      Update: the above is obviously hypocritical; I get sucked into premature optimization and complexity fascination all the time.

        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. :)
Re: Re: Processing data with lot of math...
by BrowserUk (Patriarch) on May 12, 2004 at 18:53 UTC

    This method won't work. Take the three points

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

        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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://352855]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-04-23 12:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found