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

Re: getting my neighbours in an N-dimensional space

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


in reply to getting my neighbours in an N-dimensional space

Hmmm, tye's solution provides insight to another:

# You might already have this type of function available. # If not, it's needed for the next part: sub same_coord { my ($aref, $bref) = @_; my @acoord = @$aref; my @bcoord = @$bref; return 0 if ( scalar @acoord != scalar @bcoord ); foreach my $i ( 0..$#accord ) { return 0 if ( $acoord[$i] != $bcoord[$i] ); } return 1; } sub get_neighbors { my @coord = @_; my @n = ( [] ); foreach my $i ( 0..$#coord ) { @n = map { @left = @$_; map { [ @left, $_ ] } ( $coord[$i]-1..$coord[$i]+1) } @n; } @n = grep { !same_coord( \@coord, $_ ) } @n; return @n; }

-----------------------------------------------------
Dr. Michael K. Neylon - mneylon-pm@masemware.com || "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


Comment on Re: getting my neighbours in an N-dimensional space
Download Code
Re: Re: getting my neighbours in an N-dimensional space
by dash2 (Hermit) on Jan 21, 2002 at 21:33 UTC
    I like this one best. That is a clever way of hiding recursion within a loop, rather than farming it out to some subroutine.

    Although closures are admittedly sexy.

    dave hj~

      Note that I didn't choose a closure because it is sexy. I chose it because I wanted to produce an iterator so that I didn't have to worry about when 3**$N would get too large that I start running out of memory. Perhaps you were only worried about very small values of $N. And I just prefer iterators. (:

              - tye (but my friends call me "Tye")
      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 - mneylon-pm@masemware.com || "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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2014-12-26 10:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (171 votes), past polls