I've timed on the 100 points found here:
https://www.reddit.com/r/dailyprogrammer/comments/3l61vx/20150916_challenge_232_intermediate_where_should/
10 timespacex'close p'
0.00100026 928512
10 timespacex'closest p'
0.000720736 659712
10 timespacex'( # ~ [: ( ] = <. / ) ( [: + / " 1 *: @ - / ) " _1 ) @: ( { ~ 2
combT # ) p'
0.00617379 403328
10 timespacex'( { ~ ( {. , >: @: + / ) @: ( ] ( ( <. / @ ] I. @: = ] ) {. @
,. ( <. / @ ] I. @: = [ > @ { ~ <. / @ ] I. @: = ] ) ) <. / every ) @: ( ( [: +
/ *: @ | @ - / @ ,: ) tritable ) ) p'
0.011479 236544
10 timespacex'close2 p'
0.000985472 657664
where close2 is same as Raul's but tacit.
close2 =: (#~ +/@(= [: <./ 0-.~,)@:(+/"1)@:([: *: -"1/~))
Its neat that complex magnitude is fastest of all.
----- Original Message -----
From: Jose Mario Quintana <[email protected]>
To: Programming forum <[email protected]>
Cc:
Sent: Wednesday, September 16, 2015 7:07 PM
Subject: Re: [Jprogramming] An adverb for a loopy algorithm
(close -: closest) P
1
st=. (, */&.:>@:(1 2&{))@:(] ; 7!:2@:] ; 6!:2)
666 st&> 'close P' ; 'closest P'
┌─────────┬─────┬─────────────┬───────────┐
│close P│39680│5.35053579e_5│2.1230926 │
├─────────┼─────┼─────────────┼───────────┤
│closest P│23296│2.94027437e_5│0.684966316│
└─────────┴─────┴─────────────┴───────────┘
closest
] #~ +/@(= <./@:(0 -.~ ,))@:|@:(-/~)@:(1 0j1 +/ .*~ ])
On Wed, Sep 16, 2015 at 5:58 PM, Raul Miller <[email protected]> wrote:
> combT =: ([: ; ([ ; [: i.@>: -~) ((1 {:: [) ,.&.> [: ,&.>/.
> >:&.>@:]):(0 {:: [) (<i.1 0),~ (< i.0 0) $~ -~)
> |spelling error
>
> But this works:
> require'stats'
> comb=: combT
>
> Meanwhile... have you timed any of this?
>
> Consider a dead simple approach:
> close=: 3 :'y #~ +/@(= [: <./ 0-.~,)+/"1 *: -"1/~y'
>
> close p
> 6.42201 5.83321
> 6.62593 6.08499
>
> timespacex 'close p'
> 5.13156e_5 39680
>
> timespacex '({~ ({. , >:@:+/)@:(] ((<./@] I.@:= ]) {.@,. (<./@]
> I.@:= [ >@{~ <./@] I.@:= ])) <./ every)@:(([: +/ *:@-/@,:)tritable))
> p'
> 0.000201414 35712% 5.13156e_5 39680
>
> timespacex '(#~ [: (] = <./) ([:+/"1 *:@-/)"_1)@:({~ 2 comb #) p'
> 0.000491026 35584
>
> Thanks,
>
> --
> Raul
>
>
> On Wed, Sep 16, 2015 at 4:17 PM, 'Pascal Jasmin' via Programming
> <[email protected]> wrote:
> > One much better approach, is to use combinations index
> >
> > combT =: ([: ; ([ ; [: i.@>: -~) ((1 {:: [) ,.&.> [: ,&.>/.
> >:&.>@:]):(0 {:: [) (<i.1 0),~ (< i.0 0) $~ -~)
> >
> > (#~ [: (] = <./) ([:+/"1 *:@-/)"_1)@:({~ 2 combT #) p
> >
> >
> >
> > ----- Original Message -----
> > From: 'Pascal Jasmin' via Programming <[email protected]>
> > To: Programming Forum <[email protected]>
> > Cc:
> > Sent: Wednesday, September 16, 2015 3:49 PM
> > Subject: [Jprogramming] An adverb for a loopy algorithm
> >
> > 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
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm