Volume 24, N°2

Functional calculation

3: Structural Ingredients

by Neville Holmes (neville.holmes@utas.edu.au)

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

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.

Building structures

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.

The other functions in the table have more particular properties, and their explanation also raises some interesting issues.

Rebuilding structures

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.

The functions given in the lower section of the preceding table typically pick out items from their right argument.

Structural data

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.

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.

Summary

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.

 

 

BAA logo

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…)

comp.lang.apl

Kenneth E. Iverson
Kenneth E. Iverson
1920-2004

Coming up

23 Aug–1 Sep
q training courses New York

13-16 Sep
Technische Universität Berlin
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

YouTube

Valid XHTML 1.0 Strict