This is nice!

You can easily make a distance table:

     (0 0, 2 _3 ,: _1 2) dis/ 1 1,_1 _2,:3 4
1.41421 2.23607       5
4.12311 3.16228 7.07107
2.23607       4 4.47214

and reflex:

   dis/~ 1 1,_1 _2,:3 4
      0 3.60555 3.60555
3.60555       0  7.2111
3.60555  7.2111       0

The tacit version of dis:

dist=.+/&.:*:@:-"1

There is a benefit with many vectors:

  a=.?1000 1000 $ 100
   ts 'dist/~ a'
3.89076069 8424320
   ts 'dis/~ a'
11.76892645 8336600192
   (dist/~ a)-:dis/~ a
1
   
Esa (my first name, I can't get it right in the forum)
-----Original Message-----
From: Programming [mailto:programming-boun...@forums.jsoftware.com] On Behalf 
Of 'Skip Cave' via Programming
Sent: Saturday, October 28, 2017 5:48 AM
To: programm...@jsoftware.com
Subject: Re: [Jprogramming] Euclidean Distance

Raul,

Yes, rank was the problem. It hit me while I was eating dinner
but your solution was different than what I expected (they usually are):

Raul:

     dis =.4 :'%:+/*:x-y' "1

     (0 0, 2 _3 ,: _1 2) dis 1 1,_1 _2,:3 4

1.41421 3.16228 4.47214


       0 0 dis 1 1,_1 _2,:3 4

1.41421 2.23607 5


I need to study this more. My solution was:

     dis =. 4 :'%:+/"1 *:x-y'


     (0 0, 2 _3 ,: _1 2) dis 1 1,_1 _2,:3 4

1.41421 3.16228 4.47214


     0 0 dis 1 1,_1 _2,:3 4

|length error: dis

|     %:+/"1*:x -y


So my scheme doesn't work for one-to-many. It just works on one-to-one

& many-to-many (both nouns must be the same size).

I had no idea you could put the rank operator outside the quotes!

Where does the language description doc discuss this trick?


Skip

Skip Cave
Cave Consulting LLC

On Fri, Oct 27, 2017 at 7:03 PM, Raul Miller <rauldmil...@gmail.com> wrote:

> The items that dis is expecting are rank 1 vectors. So it might make
> sense to include a rank 1 specification in its definition:
>
>    dis =: 4 :'%:+/*:x-y'"1
>
> Does that work for this example?
>
>    0 0 dis 1 1,_1 _2,:3 4
> 1.41421 2.23607 5
>
> ... seems so ...
>
> Thanks,
>
> --
> Raul
>
>
> On Fri, Oct 27, 2017 at 7:11 PM, 'Skip Cave' via Programming
> <programm...@jsoftware.com> wrote:
> > In the 2014-11-11 NYCJUG meeting
> > <http://code.jsoftware.com/wiki/NYCJUG/2014-11-11> there was a
> discussion
> > on Euclidean Distance. The verb 'dis' was used to demonstrate how to to
> > calculate the Euclidean distance between two vectors.
> >
> > 0 0 dis 1 1 NB. Euclidean distance 1.41421 1 1,_1 _2,:3 4 NB. Table of
> 2-D
> > points 1 1 _1 _2 3 4 0 0 dis 1 1,_1 _2,:3 4 NB. Distances from origin
> > 1.41421 2.23607 5 0 0 0 dis 1 1 1 NB. Handles higher dimensions 1.73205
> > I have a NLP project that needs to calculate the
> > Euclidean distance between points in a multi-dimensional
> > space. I now the basic formula - square the differences,
> > sum the squares, take the square root of the sum:
> >
> >    dis =. 4 :'%:+/*:x-y'
> >
> >    0 0 dis 1 1
> >
> > 1.41421
> >      0 0 0 dis 1 1 1
> >
> > 1.73205
> >
> > Looking good....
> >
> >    0 0 dis 1 1,_1 _2,:3 4
> >
> > |length error: dis
> >
> > | %:+/*:x -y
> >
> > Ooops!
> >
> > How can I make dis handle multiple pairs of vectors?
> >
> > like:
> >
> >   (0 0, 2 _3 ,: _1 2) dis 1 1,_1 _2,:3 4
> >
> > 1.41421  3.16228  4.47214
> >
> > Skip Cave
> > Cave Consulting LLC
> > ----------------------------------------------------------------------
> > 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

Reply via email to