J-ottings 7: Control Structures
Vector 12:2, page 21
Transcribed from the printed issue; not yet reviewed. Read it in the PDF of the issue, page 21
With the release of the J Release 2 interpreter as Freeware (hereafter referred to as JFW which conveniently stands for either J FreeWare or J For Windows), J has reached a crossroads in more ways than one. In the first place, the amateur / education / non-commercial J user who it is assumed might be reading this column, must decide whether to opt for J7 (that is the last release of DOS J) or for JFW. If he/she cannot, or does not, run Microsoft Windows, then the choice is of course made. Where the choice is available, my vote is for JFW, primarily because it includes control structures. However, DOS J fanciers should not stop reading; a template for the equivalent form of a simple loop in a dyadic defined verb is the following:
JFW:
J7:
As an example, here are two equivalent defined verbs which compute continued fractions iteratively, e.g. 2 + 1/(2 + 1/(2 + … 100 times. The left argument is the number of iterations, the right argument is the start value for the fraction, so that for this example x. = 100 and y. = 2.
JFW:
J7:
The JFW version shows what might be regarded as a "nice" layout for the loop — for those who like maximum compression, the loop body could be written
— shades of APL one-liners, aided by the use of the verb [ as statement separator. It is stressed that the above verb is written to demonstrate a loop — if the result alone were the goal, then the natural J way to write it would be to use the power adverb
To test whether the benefit of control structures is paid for by performance overhead, an "empty loop" (monadic) was implemented under both interpreters.
JFW:
J7:
(Note: In J7, it is necessary to define the null dyadic option explicitly, but this is not the case in JFW.)
The JFW version runs almost twice as fast as the J7 version, indicating that the control structures per se give a performance improvement. However, with some algorithms, J7 is faster than JFW — all that can be said with certainty is that the two interpreters are different.
Apart from control structures, the other major difference between J7 and JFW is the definition of amend. This is illustrated using one of the simplest numerical algorithms which cannot be expressed without a loop — even in J! Choleski computes the "square root" of a square symmetric matrix, that is, for a given matrix M, the Choleski matrix C has the property that C′C = M where dash denotes transpose.
J7¶
tr=. |: NB. transpose
ip=. +/ .* NB. inner product
sq=. ip~tr NB. "square" of matrix, i.e. M'M
csq=.[ { ({sq) NB. inner prod of one column of matrix
rd=.(>:@[) }.{ NB. drop i+1 elements from ith row -
NB. rd stands for "right of diagonal"
choleski=.0 : 0
i=._1 [ z=.($y.)$0 NB. Initialization
lab) $.=. >((#y.)=i=.i+1){$.;end NB. Start of loop
z=.(%:( (t=.<i,i){y.)-i csq z)(i *>:#y. )}z NB. Diagonal element
z=.(((i rd y.)-i rd sq z)%t{z)(i rd i.$y.)}z NB. Assign to rt of diag
$.=.lab NB. Back to start of loop
end)z NB. Publish result
:
''
)
NB. (i rd sq M) is products of col i of M with all cols to its right
tm=.3 3$10 2 4 2 5 3 4 3 9
]um=.choleski tm
3.16228 0.632456 1.26491
0 2.14476 1.02576
0 0 2.51949
tm-:(tr um)ip um
1
JFW¶
Instead of rd define
each=.&.>
rdi=.(>:@[ }.,each)i.@# NB. rdi = "right of diag" scatter inds
choleski=.3 : 0
i=._1 [ z=.($y.)$0 NB. Initialization
while. (#y.)>i=.i+1 do. NB. Loop through rows
z=.(%:(t{y.)-i csq z) (t=.<i,i) }z NB. Amend diagonal element
if. i~:_1+#y. do. NB. Except for last one,
amend
z=.(((p{y.)-p{sq z)%t{z) (p=.i rdi z) }z NB. elements right of diag
end.
end.
)
NB. (i.0)(i.0)}z is valid in J7 but NOT in JFW, which provides an
opportunity for introducing the if. control word in the above.
Compare the third and fourth lines in J7 with the third and fifth lines in JFW, all of which update z using amend. The overall structure of amend (see J-ottings 6) is the same in both JFW and J7, namely
However in J7 "indices" are interpreted as "linear indices", whereas in JFW "indices" means a sequence of boxed scatter indices, thus making indices consistent across both { and }.
The verbs rd and rdi share the same fork structure:
The composition >:@[ means "add one to the left argument", which in composition with }. (drop) identifies elements to the right of a matrix diagonal.
In the fourth line of the J7 version, rd is used repeatedly with different matrices. In particular, within the indices component of amend, it is used with i. to produce linear indices. In JFW rdi produces the corresponding scatter indices. Both t and p are assigned within indices components, and then used for selection in the corresponding data components, thereby demonstrating index consistency.
The arrival of JFW raises broader issues concerning the educational value of J as an executable notation. In the days when APL was trying to make its way in the computing world, the smart way to condemn it without going to the trouble of finding out anything about it was to repeat the glib assertions:
"Needs special equipment";
"Costs a lot"; and
"Hasn’t any control structures".
JFW has removed all of these objections, and it will be interesting to observe the excuses which the computer science fraternity will doubtless bring forward for ignoring J in its turn.
The potential of APL as an executable notation for conveying mathematical and more general data-structuring ideas has long been proclaimed by small bands of enthusiasts around the world. Now that the above objections have been removed, JFW can provide even greater generality and power for achieving the above goals. Howard Peelle at the University of Massachusetts, has for the purpose of in-service teacher training produced admirable workshop material for Teaching Mathematics with J. Briefly the principle is to introduce a minimal amount of J, and use the combination of experimentation, discussion and reflection to focus attention on mathematical concepts. However, efforts of this sort in curriculum development need encouragement and funding to sustain enthusiasm.
In many ways the ground is less fertile now than it was in the APL days. First, computer algebra systems such as Maple and Derive have achieved considerably greater popularity than executable notations. Unquestionably these have brought many conceptually simple but computationally heavy problems within reach of students, which is fine if results as opposed to methods are the primary goal. Secondly, the pendulum of educational fashion seems to be swinging away from knowledge towards concepts such as "Vocational Qualifications" and "Generic Skills". (Arguably data-structuring and data-manipulation are themselves generic skills — given the laxity of spelling these days it might even be possible to promote them as J-eneric skills!)
Thirdly, in the UK, teaching by administrative overload seems to be taking over at all levels from teaching by inspiration. Consistent uniformity, and customer delight elicited by satisfaction surveys are of course to be applauded when mass-producing manufactured items, and such qualities are rightly recognised as indicators of excellence in this domain. However, it is disturbing to see the same indicators being accepted in an uncritical way in the education process, where excellence ought to mean something of a quite different sort. Cambridge University, for example, has long enjoyed a reputation for providing an environment in which budding excellence in mathematics can flower. However, in the words of the Professor of Applied Mathematics and Theoretical Physics there, there has been since 1990 a steady but quite rapid deterioration in the levels of preparedness of first year students which means that it is no longer possible to teach all the material. We must, I suppose, be grateful that the Greeks never hit on Total Quality Management, which if in place, would presumably have put paid at an early stage to the careers of Archimedes, Pythagoras and their like!
Such matters heighten the significance of the crossroads at which J has now arrived. Who and what is J for, and what should its place be in the broader spectrum of science? — these are questions which are now ripe for debate. Please send your comments to the editor.