Subject: Re: Hilbert Matrix
From: richard.levine@canrem.com (Richard Levine)
Date: Tue,  8 Aug 95 03:34:00 -0500
Organization: CRS Online  (Toronto, Ontario)

Further to the thread on "Hilbert Matrix", here are a few
comments from studying R. Hui's algorithm for permutations,
and the subsequent discussion.

perm =. 1 0&$`([: ,/ 0&,.@($:&.<:) {"2 1 \:"1@=@i.) @. *

I agree with J. Weigang's comments, that to begin to understand
this tacit expression, the first thing is
to understand the meaning of the various J symbols being
used, and his notes on this were quite helpful.

Two other techniques that I have found very helpful are the
following.

(1) Don't forget to use the "tree" display.

J will show you quite clearly the way the expression is
parsed without the need for you to bother with "rules of
parsing"; for example:

tree =. 5!:4@<
tree'perm'
(display not shown)

The fork structure and the individual phrase groups within
the expression are shown quite clearly.

(Some prefer the "boxed" display 5!:2 to show the parsing
structure.  In the Windows version of J, both displays are
menu options.)

(2) Execute the expression.

One thing that gives me confidence in studying an expression
is to verify that it works, even though I don't know why.
For example:

perm 2
perm"0 (1 2 3)

A handy expression that shows each result in a vector of
boxes is (<@perm)"0 and the results can be easily compared.

9!:7 '+++++++++|-'
(<@perm)"0 (1 2 3)
+-+---+-----+
|0|0 1|0 1 2|
| |1 0|0 2 1|
| |   |1 0 2|
| |   |1 2 0|
| |   |2 0 1|
| |   |2 1 0|
+-+---+-----+

(<@perm)"0 (0 1 2 3 4)  NB. result not shown

UNDERSTANDING THE EXPRESSION - WHY DOES IT WORK?

I believe we should not expect that a tacit expression be
the means of explaining an algorithm.  Usually mathematical
expressions have an accompanying commentary (not included,
obviously, within the expression itself).

In my case, I find permutations, especially recursive
definitions of same, give me a headache and I need lots of
commentary!!  For example, and coincidentally, J. P. Benkard
wrote a 3-page article on "Generating a List of Permutations
in Odometer Order" using English and APL2 for the BIG APL
(NY/SIGAPL Newsletter) June 1995.  The article was a model
of clarity, but to me was no easier than the discussions of
the perm tacit definition.

To understand expressions, including recursive expressions,
I find it helpful to use the following "tracing" verb
(function) called "show" for showing intermediate results.

"Show" is used as follows: (NB. results not shown)
show 10+20+30
1 show 10+20+30
2 show 10 + 1 show 20+30

NB. show partial results in tacit expression for "mean"
      (+/ % #) i. 10
4.5

      ( (2&show@(+/)) % (1&show@#) ) i.10
+---------+--+
|result 1 |10|
+---------+--+
+---------+--+
|result 2 |45|
+---------+--+
4.5

NB. Note parentheses around +/ in the above expression
NB. 2&show@+/ would be parsed as (2&show@+)/

NB. Definition of show follows ...

show =. 3 : 0
NB. Monadic case
NB. Write to screen the argument y. with standard label
NB. Then return y.
(1!:2&2) 'result '; y.
y.
:
NB. Dyadic case
NB. Same as monadic except the label is numbered
(1!:2&2) ('result ',( ": x. ),' '); y.
y.
)

One final comment:  J. Weigang writes:

The hook, fork, conjunctions, and cap ([:), seven syntactic
elements in all, serve to make up for the inability to
insert one reference to the argument y. in the middle of the
expression.

I would suggest the following rephrasement:

The hook, fork, conjunctions, and cap used together in this
expression [for perm] HAVE THE NICE PROPERTY that a
reference to the argument y. in the middle of the expression
IS NOT NECESSARY.

I hope this message was not too long.  At least it did not
contain lists of American magazines, comments on baldness,
or pages of macro dumps. ((:>) - no flames plse)
