AMENDMENT: “A Change for the Better”
Vector 9:3, page 134
Transcribed from the printed issue; not yet reviewed. Read it in the PDF of the issue, page 134
(The American Heritage Dictionary of the English Language)
It used to be that whenever the computer centre manager decided to install a new version of the operating system, the FORTRAN compiler, or the APL interpreter, we were forced to interrupt what we were doing in order to adapt to the changed environment, even though we may have been content and comfortable with the old one. With a personal computer we change only when we choose to do so. Moreover, should we want to, we can have several versions of the software immediately available on the hard disk; I can, for example, call up any one of ten different versions of Iverson and Hui’s J interpreter. All perform as originally specified and each has with it several executable script files. It is helpful that J comes with two files (status.doc and xenos.doc) documenting the changes from all earlier versions.
Amend a Table¶
Because my paper on J’s Boxed Arrays [1] was based on J Version 3.3 [Reference 2 in the paper], I wish to explain where changes are required in order to execute under PC Version 5 (27 June 1992). For this purpose we can simulate the 5 by 6 array of nested cells previously used as a simple database:
]d=.<"0 i.5 6
┌──┬──┬──┬──┬──┬──┐
│0 │1 │2 │3 │4 │5 │
├──┼──┼──┼──┼──┼──┤
│6 │7 │8 │9 │10│11│
├──┼──┼──┼──┼──┼──┤
│12│13│14│15│16│17│
├──┼──┼──┼──┼──┼──┤
│18│19│20│21│22│23│
├──┼──┼──┼──┼──┼──┤
│24│25│26│27│28│29│
└──┴──┴──┴──┴──┴──┘
The numbers identifying the cells can be called linear indexes, a term suggested to me by Eugene McDonnell. Thus, defining the verb li (a fork):
the linear indices of rows 1 and 4 are given by:
And the linear index of the cell at row 2 and column 5:
The syntax now used for amend is illustrated by the following examples:
To place the array x in this cell:
x=. < 9 8 7 6,:5 4 3 2
x 17} d
┌──┬──┬──┬──┬──┬───────┐
│0 │1 │2 │3 │4 │5 │
├──┼──┼──┼──┼──┼───────┤
│6 │7 │8 │9 │10│11 │
├──┼──┼──┼──┼──┼───────┤
│12│13│14│15│16│9 8 7 6│
│ │ │ │ │ │5 4 3 2│
├──┼──┼──┼──┼──┼───────┤
│18│19│20│21│22│23 │
├──┼──┼──┼──┼──┼───────┤
│24│25│26│27│28│29 │
└──┴──┴──┴──┴──┴───────┘
Note that a noun (17) and an adverb (}) result in a verb.
Scattered indexing presents no difficulty:
(x;'London') ((2 5; 4 2) li d)} d
┌──┬──┬──────┬──┬──┬───────┐
│0 │1 │2 │3 │4 │5 │
├──┼──┼──────┼──┼──┼───────┤
│6 │7 │8 │9 │10│11 │
├──┼──┼──────┼──┼──┼───────┤
│12│13│14 │15│16│9 8 7 6│
│ │ │ │ │ │5 4 3 2│
├──┼──┼──────┼──┼──┼───────┤
│18│19│20 │21│22│23 │
├──┼──┼──────┼──┼──┼───────┤
│24│25│London│27│28│29 │
└──┴──┴──────┴──┴──┴───────┘
To expand the table and amend new rows 1 and 4:
exp=. /:@\:@[ { #@[{.]
e=. 1 0 1 1 0 1 1 exp d
t=. e (1 li d)}~ 'Bob';'Bernecky';'Toronto';35;22000; 4 5
t (4 li d)}~ 'Graham';'Woyka';'U.K.';62;35000;14 31 5 7
┌──────┬────────┬───────┬──┬─────┬─────────┐
│0 │1 │2 │3 │4 │5 │
├──────┼────────┼───────┼──┼─────┼─────────┤
│Bob │Bernecky│Toronto│35│22000│4 5 │
├──────┼────────┼───────┼──┼─────┼─────────┤
│6 │7 │8 │9 │10 │11 │
├──────┼────────┼───────┼──┼─────┼─────────┤
│12 │13 │14 │15│16 │17 │
├──────┼────────┼───────┼──┼─────┼─────────┤
│Graham│Woyka │U.K. │62│35000│14 31 5 7│
├──────┼────────┼───────┼──┼─────┼─────────┤
│18 │19 │20 │21│22 │23 │
├──────┼────────┼───────┼──┼─────┼─────────┤
│24 │25 │26 │27│28 │29 │
└──────┴────────┴───────┴──┴─────┴─────────┘
y=. ('Graham';'Woyka';'U.K.';1;2;3),:'Vin';'Grannell';'Los
Angeles';4;5;6 7 8
]z=. e (1 4 li e)}~ 1 0{ y
┌──────┬────────┬───────────┬──┬──┬─────┐
│0 │1 │2 │3 │4 │5 │
├──────┼────────┼───────────┼──┼──┼─────┤
│Vin │Grannell│Los Angeles│4 │5 │6 7 8│
├──────┼────────┼───────────┼──┼──┼─────┤
│6 │7 │8 │9 │10│11 │
├──────┼────────┼───────────┼──┼──┼─────┤
│12 │13 │14 │15│16│17 │
├──────┼────────┼───────────┼──┼──┼─────┤
│Graham│Woyka │U.K. │1 │2 │3 │
├──────┼────────┼───────────┼──┼──┼─────┤
│18 │19 │20 │21│22│23 │
├──────┼────────┼───────────┼──┼──┼─────┤
│24 │25 │26 │27│28│29 │
└──────┴────────┴───────────┴──┴──┴─────┘
Alternatively:
Although in these examples the indexes for amend are arbitrarily specified, they will often be computed as some function of the table; as, for instance, in the computation of eigenvalues by Jacobi’s method [2].
Agreement¶
Given the following definitions:
Then a consists of 2 items, each of shape 3 4 5. The mean of a is the sum of the corresponding cells of these two items divided by 2. Its shape is therefore 3 4 5:
The hook f appends the means, consequently the shape is increased from 2 3 4 5 to 3 3 4 5:
Because the rank of a is 4, the same result is produced by:
The rank conjunction (") lets us apply f to cells of rank 3, 2, or 1:
This is clearer when the results are displayed, but the space required is more than is justified here. In each case the shape of the result is the same as the shape of a except that the length of one axis is increased by 1. The example illustrates the impressive power of the rank conjunction.
Given p and q:
then we can multiply the first row of p by 2 and the second row by 3
where each cell of rank-1 in the left argument is multiplied by the corresponding rank-0 cell of the right argument. The sentence p*q formerly gave a length error, however the rule for agreement has been changed in J Version 5 so that now:
The rule is that the "frames of the arguments must match, or one must be a prefix of the other" [3, p.6]. In this case the arguments agree because the frames of the rank-1 cells of p and of the rank-0 cells of q are both 2.
Given:
Then the deviations from the mean are:
The same result is returned when dev is written as:
Because the right argument for the subtraction is rank-0 and the "ranks of a verb merely place upper limits on the ranks of the cells to which it is applied" [3, p.6], we can set the right rank to infinity (_):
If y is rank-2:
The earlier rule for agreement required the shorter frame to match the suffix of the longer frame. Consequently with
then,
which was formerly permitted [2, 4], now gives a length error. We must write, for example:
The 4 rank-1 cells (each of shape 5) are combined with the rank-1 cell (shape 5) resulting from mean y.
If y is rank-3:
then mean y is rank-2 and the deviations are computed by:
The 3 rank-2 cells (each of shape 4 5) are combined with the rank-2 cell (shape 4 5) resulting from mean y.
If y is rank 4:
then mean y is rank-3 and the deviations are computed by:
The 2 rank-3 cells (each of shape 3 4 5) are combined with the rank-3 cell (shape 3 4 5) resulting from mean y.
It is unsatisfactory that the definition of dev had to change as the rank of the argument changed. Because the computation of the mean involves summation, the rank of the result of mean y is always one less than the rank of y. In each example the rank of the result is the rank of the original argument, but the rank of the cells used in the subtraction is one less than this. Although the rank of the cells changes, the frame of the left argument is always of length 1; consequently we can generalise by referring to the frame of the cells instead of to their rank.
In J, "Negative numbers are used to specify cells whose frames are of length indicated by the magnitude of the number" [3, p.4]. In J Version 5 it is therefore possible to define a verb that applies to tables of any rank to give the deviations from the means of items:
i.e. the following all execute correctly:
Acknowledgements¶
It is a pleasure once again to thank Ken Iverson and Roger Hui for continuing to provide interesting and useful amendments to their system, and for guidance on many matters relating to this paper.
References¶
[1] Donald B. McIntyre, Using J’s Boxed Arrays. Vector, Vol.9, No.1 (July 1992) 92-105.
[2] Donald B. McIntyre, Jacobi’s Method for Eigenvalues: an illustration of J. Vector, Vol. 9 (1992) In Press.
[3] Kenneth E. Iverson, Dictionary of J, Version 5 (1992) p.1-35, with Appendices p.1-18.
[4] Donald B. McIntyre, Mastering J, APL91 Conference Proceedings, Stanford, California, August 1991. APL Quote Quad Vol. 21 Number 4 (August 1991), p.264-273.
[5] Donald B. McIntyre, Language as an intellectual tool: From hieroglyphics to APL, IBM Systems Journal, Vol.30, No. 4 (1991) p.554-581.