finding the 2 closest points in a set, has a loopy algorithm of "for each 
point, compare it with all of the points to its right"  

p =: 0 ". every cutLF 0 : 0
6.42201  5.83321 
3.15448  4.06327 
8.89456 0.352235 
6.00479  7.07121 
8.10462  9.19487 
9.63448  4.00534 
6.74378 0.791349 
5.56034  9.27039 
4.67282  8.45993 
0.301042   9.4069 
6.62593  6.08499 
9.0307  2.37372 
9.36324  1.80147 
2.67396  1.62207 
4.76667  1.94554 
7.43839  6.05369 
)

A J adverb to do this,

tritable =: 1 : '<"_1 u"1 each <@}.\.'

this results in a boxed triangular table which conveniently avoids fills

a slower alternative is to filter "1 1/~ (self table) by upper triangle

-.@(1#~"0 >:@i.) 4 
0 1 1 1 
0 0 1 1 
0 0 0 1 
0 0 0 0 


  ({~ ({. , >:@:+/)@:(] ((<./@] I.@:= ]) {.@,. (<./@] I.@:=  [ >@{~ <./@] I.@:= 
])) <./ every)@:(([: +/ *:@-/@,:)tritable)) p 
6.42201 5.83321 
6.62593 6.08499


The one thing I don't like about the adverb is that it hard codes "1.  I'm 
unsure if there isn't a better way to do it.

The code to fish out which points have the minimum distance is also improvable 
I'm sure. 
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to