Volume 24, N°2
This article is the fourth in a series expounding the joys of functional calculation. Functional calculation does with operations applied to functions and numbers what numerical calculation does with functions applied to numbers. The functional notation used as the vehicle in this series is provided by a freely-available calculation tool called J.
This article reviews the numerical calculation capabilities of J which enable lists and tables to be used in calculations. To understand the functional calculation capabilities proper, structural capabilities – dealing with tables and lists – must first be understood. The capabilities described in this article will be illustrated and explained in detail in the next article.
Calculation is generally reckoned to be the systematic manipulation of numeric values. Previous articles in this series have reviewed the arithmetic functions available in J, the interpreter used here as the vehicle for numerical calculation.
The numerical calculation described so far is merely an elaboration of what might be achieved by a conventional electronic calculator. There is an elaboration of ways numbers can be expressed, and an elaboration of functions that can be applied to any number, but that is all.
However rich the set of arithmetic functions available to the user, they cannot do much more than an ordinary pocket calculator can do, unless those arithmetic functions can be applied to lists of numbers, and to lists of lists. Functions that manipulate lists and tables, functions beyond the merely arithmetical, are essential to support the arithmetic functions, and these functions can be called structural.
The next article will illustrate the use of these structural functions. They provide an elaboration still within the realms of numerical calculation, namely the ability to structurally manipulate more than one number at a time. Of course, the arithmetic functions already described can also be applied to lists and tables of numbers.
In the first place, constant lists of numbers can be keyed in simply by
separating the elements of the list by blanks.
So what ?
Well, the scalar functions can be applied to compatible lists, that is,
to lists with the same number of elements: 3 4 5*6 4 2
yields 18 16 10, for instance.
And if one of the arguments is a scalar (not a list, but a naked number)
then it is coupled to each element of the other
argument: 60%3 4 5 yields 20 15 12, for instance.
But other functions are needed, functions that allow lists to be built easily, that allow lists of lists to be built, that allow lists of different kinds to be built. The following table gives the symbols and names for some such functions.
| $ | shape | reshape | ||||||
| # | tally | copy | #. | unbits | undigits | #: | bits | digits |
| ? | deal | |||||||
| < | box | +. | cartesian | ": | format | format | ||
| > | unbox | *. | polar | |||||
| ; | raze | link | i. | integers | ;: | words |
The reshape function is the most useful of these, and its explanation covers many important issues about structures.
$ specifies what shape is to be given
to the result, the right argument supplying the item or items to be used in
the result, and these items are reused cyclically if necessary.
Thus, 5$6 yields 6 6 6 6 6, and
5$6 7 yields 6 7 6 7 6.
2 3$4 yields
a two item list, each item of which is itself a list of three 4s.
This is very much like a matrix or table with two rows and three columns.
If a table is reshaped then the items that go into the new structure are
the rows of the table.
Thus the expression 4$3 2$1 will yield a table of four rows and
two columns.
$ yields the shape of its argument, always a list,
so the shape of a shape yields a single item list which gives the number of
dimensions of the original argument, its rank.
The rank of a table is two, of a list, one, and of a scalar, zero.
0, that is, it may hold no elements
at all.
Such a structure is called an empty list, and is of particular interest
under calculation.
Consider, for example, what the average of an empty list should be.
Consider also that a table might be empty because it has no rows, or empty
because it has no columns, and not necessarily both.
1,
while the shape of a scalar is the empty list.
The other functions in the table have more particular properties, and their explanation also raises some interesting issues.
2#3 4 yields 3 3 4
4, and 2j1 1j2#3 4 yields 3 3 0 4 0 0.
1, though their shapes are different. The tally of
an empty list will always be 0, but the tally of an empty
table will only be 0 if it has no rows.
i. is like a reshape but the
argument gives the shape of the result, and the elements being reshaped
are the nonnegative integers taken in sequence. Deal randomly
selects from the non-negative integers smaller than its right argument as
many as specified by its left argument. Of course the results of both
roll (monadic ?) and deal are only pseudorandom, and the
pseudorandom seed can be both inspected and set.
$'this' yields 4.
The two format functions are used to convert numbers to character strings,
while do can convert character strings to numbers.
Incidentally, there is a special name a. which stands for
the alphabet, the list of all possible characters.
In contrast to functions that build structures from typically lesser structures, like scalars, there are functions that rearrange structures, or extract lesser structures from greater. Such functions are given in the following table.
| |. | reverse | rotate | |: | transpose | transpose | |||
| , | ravel | append | ,. | knit | stitch | ,: | itemise | laminate |
| /: | sort up | |||||||
| \: | sort down | |||||||
| { | from | {. | head | take | {: | tail | ||
| }. | behead | drop | }: | curtail | ||||
| ~. | nub |
Functions that merely rearrange their components are those shown in the first four rows of the table.
a. to define the collating sequence of
characters in the case of a character structure for right argument.
In particular, x/:x will yield a list of the items of x
in ascending sequence, and x\:x in descending.
1 2,3 4 yields a list of four items,
while 1 2,.3 4 yields a two by two table.
2 3$4 5 does not yield the same result
as 2$,:3$4 5, though the shape of the results is the same.
The functions given in the lower section of the preceding table typically pick out items from their right argument.
0s in the case of a numeric structure; with blanks in the case
of a character structure; or with empty boxes in the case of a box structure.
Thus, 3{.6 yields 6 0 0,
and _3{.4 5 yields 0 4 5.
One little trap is that although the result of head looks the same
as the result of one take, their shapes are different.
1{x will pick out the second item of x.
Interestingly, _1{x will pick out the last element of x.
Finally, there are a few interesting functions which, like shape and tally, extract data about their arguments.
| %. | invert | project | ||||||
| = | classify | -: | match | |||||
| e. | raze in | member | ~: | sieve | ||||
| i. | index | /: | grade up | |||||
| \: | grade down |
These are something of a mixed bunch, invert and project being simply a generalisation of matrix inversion and matrix division.
The functions given in the vertically centre section of the table all produce
boolean values, that is, values composed only of 0s
and 1s, like the results of the comparison functions.
1 if they match in every respect,
and a 0 otherwise.
(~:x)#x yields the nub of x.
e.i.4 will produce the unit matrix of size four,
while $e.i.2 3 will yield 2 3 6.
For structures of boxes, membership of the elements within each box are
marked for each element of the raze.
Given that so many results depend on a comparison of numbers, it is important
to realise that these comparisons are done with a certain amount of tolerance to
allow for the imperfection of traditional computer arithmetic, remembering that,
for example, present day computers cannot store the value 1r3 exactly.
However, the J interpreter does provide for the comparison tolerance
to be respecified, even to zero.
The functions given in the bottom section of the table all yield an index to items of the left or only argument.
(i.5)i.2 3 3.5 4 5 6 yields
2 3 5 4 5 5.
(/:x){x will yield the list of x’s items
in ascending sequence.
This article is like a list of ingredients that can be used for structural calculation using the notation provided by J. These ingredients allow lists of numbers and tables (lists of list) of numbers to be manipulated.
The arithmetic functions described and illustrated in previous articles can also be applied to lists and tables. Together, the arithmetic functions and structural functions provide a very powerful capability for relatively complex numerical calculations. This capability is needed as the basis for functional calculation proper, as will be described in later articles of this series.
However, the next article is devoted to illustrating simple use of the structural functions described above.
Next meetings
Vector is the journal of the British APL Association. The BAA promotes the APLs, terse programming languages derived from Iverson’s mathematical notation. (more…)
23 Aug–1 Sep
q training courses
New York
13-16 Sep

APL 2010
Berlin
13-23 Sep
q training courses
London
27 Sep–4 Oct
q training courses
Singapore
18-28 Oct
q training courses
New York
8-18 Nov
q training courses
London
8-20 Dec
q training courses
New York
Web design by
Lambent Technology