NYCJUG/2013-01-08/UseDissectOnPointsDistance

From J Wiki
Jump to navigation Jump to search

We'll now look at how we might have developed the "distance between point pairs" code using Dissect to see how this tool parallels what we did "by hand" originally.

Going back to the beginning ("Variations on a Theme") where we were attempting to apply +/ to the square of the differences, then apply the inverse of square (square root or %:) using "under" like this:

   0 0 ([: +/&.*: -) 1 1
1 1

Dissecting this expression

   ds '0 0 ([: +/&.*: -) 1 1'

gives us this result when we dissect it:

Dissect UnderAttempt00.png

So we see where we go wrong - at the summation +/ of the square of the differences - much as we saw with our simple debugging technique of manually applying an increasing number of operations.

The attempt that gave us a syntax error originally does something else when we try to dissect it:

   ds '0 0 ([: +/(&."_)*: -) 1 1'
attention interrupt
|attention interrupt: failparse
|       13!:8(1)

Apparently dissect won't attempt to parse something so obviously syntactically incorrect.

Moving on to our next attempt that gives us two results when we were expecting three:

   ([:%:[:+/[:*:[:-/]) pts
2.44949 2.44949

Dissecting this highlights the problem area: the summation across the incorrect axis again.

Dissect VariationsOnTheme1.png

Applying the fix by specifying the correct rank for summation gives us a better result:

Dissect VariationsOnTheme2.png

Now let's look at the more elegant version we found in the J phrase-book:

   NB.*dPP v distance between 2 points
   dPP=: [: +/&.:*: -
   dPP/0 0 ,: 1 1
1.41421

The dissection shows us what we have come to expect:

Dissect VariationsOnTheme3.png

However, the explanation for our final sentence - the modified version of the one from the phrase book using the infinite-rank version of "under" (&.:) - isn't very illuminating.

Dissect VariationsOnTheme-modifiedPhrase.png

Perhaps the tool does not yet handle this variant adverb.

Some experimentation shows us that using the phrase dyadically gives a more illuminating dissection:

Dissect VariationsOnTheme-modifiedPhraseDyadic.png