Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Re: Re: getting my neighbours in an N-dimensional space

by Masem (Monsignor)
on Jan 22, 2002 at 21:47 UTC ( #140692=note: print w/replies, xml ) Need Help??

in reply to Re: Re: getting my neighbours in an N-dimensional space
in thread getting my neighbours in an N-dimensional space

As one of your later posts implies you wanted points strictly closer to the origin, here's a quick function that helps to do that:
sub closer_to_origin { my ( $start, $test ) = @_; my $sd = 0; $sd += $_*$_ foreach (@$start); my $td = 0; $td += $_*$_ foreach (@$test); return ( $td < $sd ); }
Thus, in my original code block, where I have the grep, you can simply do:
my @closer_ns = grep { closer_to_origin( \@point, $_ ) } @n;
to get the closer neighbors. That routine basically calculates the distance from the point in question to the origin via the general distance formula, though without the final, expensive, square root as it's not necessary for magnitude comparison. This also blocks the original point from showing up (strictly closer, not same-or-closer distance) I'm sure that you can also modify tye's closure routine to include this check such that farther points don't pop up from each iteration loop.

Dr. Michael K. Neylon - || "You've left the lens cap of your mind on again, Pinky" - The Brain
"I can see my house from here!"
It's not what you know, but knowing how to find it if you don't know that's important

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://140692]
[erix]: ICYMI: it's The Future

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2018-05-24 12:31 GMT
Find Nodes?
    Voting Booth?