Newsgroups: comp.lang.apl
Path: watmath!watserv1!utgpu!news-server.csri.toronto.edu!rpi!think.com!mips!darwin.sura.net!haven.umd.edu!socrates!socrates!rockwell
From: rockwell@socrates.umd.edu (Raul Deluth Miller-Rockwell)
Subject: Re: %. Torture test ;; Looping ;; General easy ?s
In-Reply-To: vpcsc4@sfsuvax1.sfsu.edu's message of Sat, 23 May 1992 21:35:29 GMT
Message-ID: <ROCKWELL.92May24124335@socrates.umd.edu>
Sender: rockwell@socrates.umd.edu (Raul Deluth Miller-Rockwell)
Organization: Traveller
References: <1992May23.213529.28082@csus.edu>
Distribution: NA
Date: Sun, 24 May 1992 17:43:35 GMT
Lines: 115

Hints and techniques applicable to the Hilbert torture test.

First off, to transform an expression of the form
 f1 =. ( 8 8 $ |. i. 8) !  8 8 $ 8 # 8 + i. 8 
into a function re-write the expression in terms of a dummy variable.
In J, 'x.' represents the left argument and 'y.' represents the right
argument.  Further,   string : ''  will result in  string  being used
as the monadic definition of a function.  Also,  string : 11  has
a similar result.  These definitions may be assigned to local names
within some encompassing function as easily as they may be assigned to
global names.

For example,
 f1 =. '((y.,y.) |. i. y.) ! (y.,y.) $ y. # y. + i. y.' : 11
changes f1 so it is a monadic function.

Also, note that there are several ways to take advantage of J to
simplify this expression.

For example, the expression 
 f2 =. ( 8 8 $ 8 # |. i. 8) !  8 8 $ 8 + i. 8 
is basically an outer product
 f2 =. (|. i. 8) !/ 8 + i. 8

Similarly, f1 may be expressed as an outer product
 f1 =. (|. i. 8) !~/~ 8 + i. 8

Also, the expression for f3,
 f3 =. f3 * f3 =. ( 8 8 $ 8 # i. 8) ! +/~ i. 8
may be simplified
 f3 =. *~ (i. 8) !"_1 +/~ i. 8

The notation !"_1 creates a function from ! but which provides "item
extension".  Item extension is analogous to scalar extension, but it
requires that the leading dimension of the argument arrays match, and
passes the elements along that dimension to !, to be treated in
whatever manner is defined for the ! function.

Next, assuming we've transformed the definitions of f1 through f5 such
that they are all monadic functions, executing the statement
 my_hilbert =. f1 * f2 * f3 * f4 * f5
will define my_hilbert as a monadic function.  This is a classic
example of J's fork.

The other expressions have a different structure, but it shouldn't be
hard to transform them to functions:
 hilbert    =. '+/~ i. y.' : 11
 Js_hilbert =. '%. % >: hilbert y.' : 11
 myrow_norm =. '>./ +/"1 ((my_hilbert y.) +/ .* (hilbert y.)) - (y.,y.)$1, (y.+1)#0':11

or, more compactly, 
 myrow_norm =. '>./ +/"1 ((my_hilbert +/ .* hilbert)y.) - (,~y.)$(2+y.){. 1':11
and, finally,
 Jsrow_norm =. '>./ +/"1 ((Js_hilbert +/ .* hilbert)y.) - (,~y.)$(2+y.){. 1':11

Next, these need to all be put together.  One way I mentioned earlier:
write an function which builds each of these sub functions as local
functions, then applies the result.  However, nothing so tedious is
necessary, in this case.

Because each of the above definitions is tacitly defined (either
directly, or using :11 to compile to tacit form), we can define
 compare =. (my_rownorm , Js_rownorm) f.

The expression inside the parenthesis is a fork.  All the names used
in defining compare are tacitly available.  'f.' can build a tacit
definition completely in terms of J primitives, allowing all the
constituent names to be reused without affecting the definition of
'compare'.  For example, once you've defined 'compare', you can
expunge the names from the workspace using 
 4!:55 ;: 'f1 f2 f3 f4 f5 hilbert my_hilbert Js_hilbert my_rownorm Js_rownorm'
without altering the definition of compare.

To execute compare for a variety of sizes, you could use
 (compare"0) 1 2 3 4 5 6 7 8 9 10
Alternatively, since it really only makes sense to define compare for
scalar arguments, compare could be redefined
 compare =. compare f. " 0
Or, the original definition of compare could have been given as
 compare =. (my_rownorm , Js_rownorm) f. " 0
or equivalently
 compare =. (my_rownorm , Js_rownorm) " 0 f. 

It's possible that the development of compare could be cleaned up
further.  For example, common sub-expressions could be named.  Also,
many of the common sub-expressions have alternate formulations.  For
instance,
 |. i. 8
is equivalent to
 i. - 8

Finally, about the rank of verbs, this is explained in the front
matter of the dictionary (sections 2B and the first three paragraphs
of section 3).  Basically, for a definition of the form
  function(_ _)
  etc.
usage of the statement 
 function "_ _
should be identically equivalent to the usage
 function

Similarly, for
  function(_)
  etc.

It is sometimes useful to play with very simple functions, such as ']'
or "purely structural" functions, such as '<@,&<' [which is equivalent
to '< (< x.) , (< y.)' : 11].  Giving these functions specific ranks,
and applying them to small arrays such as i. 3 or i. 3 3, and so on,
can give a great deal of insight into the meaning of the rank
operation.

-- 
Raul Deluth Miller-Rockwell                   <rockwell@socrates.umd.edu>
The U.S. government went another thousand million dollars into debt today.
