An Example
Suppose the vector Ages contains the ages of 400 respondents to an opinion poll. We want to establish how many people there are in each of the following categories.
0 – 25 – 30 – 35 – 45 – 50 – 55 – 65 or above.
In addition we decide that those who are on a borderline will be assigned to the lower category.
Here is an extract of the data:
Ages ← 32 19 50 33 23 65 46 26 31 58 51 23 51 36 28 42
Category ← 0 25 30 35 45 50 55 65
We are going to invoke the outer product Category ∘.< Ages, and here are the first items calculated as shown above:
<
|
32
|
19
|
50
|
33
|
23
|
65
|
46
|
26
|
31
|
58
|
51
|
23
|
51
|
36
|
28…
|
0
|
1
|
1
|
1
|
1
|
1
|
1
|
1
|
1
|
1
|
1
|
1
|
1
|
1
|
1
|
1
|
25
|
1
|
0
|
1
|
1
|
0
|
1
|
1
|
1
|
1
|
1
|
1
|
0
|
1
|
1
|
1
|
30
|
1
|
0
|
1
|
1
|
0
|
1
|
1
|
0
|
1
|
1
|
1
|
0
|
1
|
1
|
0
|
35
|
0
|
0
|
1
|
0
|
0
|
1
|
1
|
0
|
0
|
1
|
1
|
0
|
1
|
1
|
0
|
…
|
…
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
If one adds up this binary table, one obtains for each row the number of people who are older than 0 years, 25 years, 30 years, etc. This is the expression:
cum ← +/Category∘.
The example above is reduced to the value cum which is: 17 14 12 8 6 4. In other words there are 12 people older than 30. But among them there are 8 older than 35. In order to know how many people are between 30 and 35, it is necessary to perform 12-8, to give 4.
If one wants to reproduce this calculation for all categories, it is necessary to perform a series of subtractions as here:
cum
17 14 12 8 6 4
1↓cum,0
14 12 8 6 4 0
cum-1↓cum,0
3 2 4 2 2 4
So, as we shall have occasion to notice again, we find that the comma joins variables together. This is a function called catenation.
If one no longer works with a small extract of data but with the 400 people, this is what one has:
cum←+/Category∘.
cum-1↓cum,0
83 65 79 79 32 17 36 9
All that without proper programming, and it works whatever the number of people or categories… what luck!
The outer product allows us to find typical solutions to some very classical problems.
I Have not Told you Everything
In the course of these pages we have flown over APL country and glimpsed certain bold ideas which explain the attraction of the language. A thousand other things remain to be seen!
It would be possible to talk about inner product, which is an extensive generalisation of matrix multiplication, of which our students retain only some formulae, learned by heart as a never-well-understood rigmarole, to reinforce “sigma of Aij Bjk”. But realistically, among life’s continuing problems, WHAT PURPOSE is served by stuffing our brains this way?
Having used APL as a teaching tool, I can assure you that one can teach linear algebra in a fast and realistic way, and show students that one can use it for comparing the management accounts of two companies. After that they will never forget it!
We ought to talk about generalised arrays, about the execute function, about… and it would take 400 pages… that is not our intention here.
Allow me to give you a quick illustration of a very unusual use: for files of data in an inverted form.
Share with your friends: |