From the APL Wiki:
A cell is a subarray which is formed by selecting a single index along some number of leading axes and the whole of each trailing axis. Cells are classified by their rank, which may be between 0 (scalars) and the array's rank (in which case the cell must be the entire array). Cells with rank k are called k-cells of an array. A major cell is a cell whose rank is one less than the entire array, or a 0-cell of a scalar.
If the text above feels confusing, don’t worry. Possibly after this chapter, and almost certainly after chapter 7, you will be able to read it again and say to yourself “oh yeah, that makes sense”. What you need to know for now is that arrays are arranged like rectangles in many dimensions. The three simplest cases should feel somewhat familiar to you.
0 ⍝ A scalar
0
'APL' ⍝ A vector
APL
0 1 2∘.*⍳5 ⍝ A matrix
0 0 0 0 0
1 1 1 1 1
2 4 8 16 32
Now let us look at an array with 3 dimensions. We will call it a cuboid
:
⍴cuboid←2 3∘.+3 4 5∘.×4 5 6 7
2 3 4 ← trailing (last) axis
↑
leading (first) axis
In the array cuboid
defined above, there are 2
major cells, which are those of rank ¯1+≢⍴cuboid
.
2 3 4⍴⎕A
ABCD
EFGH
IJKL
MNOP
QRST
UVWX
≢2 3 4⍴⎕A ⍝ Tally counts the major cells
2
The dimensions of an array are also known as axes. The most major cells, the rank k-1
cells for an array of rank k
, lie along the first axis. The least major cells are columns which lie along the last axis.
For more details on the APL array model in Dyalog and other array languages, see the APL Wiki article on the array model.
Now that you know how to describe the structure of an array in terms of its sub-arrays, let us look at how to apply functions to sub-arrays.
We have seen two pairs of first- and last-axis primitives.
n←2 3⍴1 2 3 1 0 ¯1
n
1 2 3
1 0 ¯1
+/n ⍝ Sum along the last axis
6 0
+⌿n ⍝ Sum along the first axis
2 2 2
'-'⍪2 3⍴'DYALOG' ⍝ Catenate first
---
DYA
LOG
'|',2 3⍴'DYALOG' ⍝ Catenate last
|DYA
|LOG
Some functions and operators can be used along specified axes using the function axis operator []
(more duplicitous symbols).
Compare the behaviour of the monadic function ⊂
enclose when applied with the rank operator ⍤
versus when it is applied using bracket axis []
.
⊂⍤1⊢3 2 4⍴⎕A
┌────┬────┐
│ABCD│EFGH│
├────┼────┤
│IJKL│MNOP│
├────┼────┤
│QRST│UVWX│
└────┴────┘
⊂⍤2⊢3 2 4⍴⎕A
┌────┬────┬────┐
│ABCD│IJKL│QRST│
│EFGH│MNOP│UVWX│
└────┴────┴────┘
⊂⍤3⊢3 2 4⍴⎕A
┌────┐
│ABCD│
│EFGH│
│ │
│IJKL│
│MNOP│
│ │
│QRST│
│UVWX│
└────┘
⊂[1]⊢3 2 4⍴⎕A
┌───┬───┬───┬───┐
│AIQ│BJR│CKS│DLT│
├───┼───┼───┼───┤
│EMU│FNV│GOW│HPX│
└───┴───┴───┴───┘
⊂[2]⊢3 2 4⍴⎕A
┌──┬──┬──┬──┐
│AE│BF│CG│DH│
├──┼──┼──┼──┤
│IM│JN│KO│LP│
├──┼──┼──┼──┤
│QU│RV│SW│TX│
└──┴──┴──┴──┘
⊂[3]⊢3 2 4⍴⎕A
┌────┬────┐
│ABCD│EFGH│
├────┼────┤
│IJKL│MNOP│
├────┼────┤
│QRST│UVWX│
└────┴────┘
For a more in-depth look at the relationship between function rank and function axis, watch the Dyalog webinars on Selecting from Arrays and The Rank Operator and Dyadic Transpose.
A list of functions with bracket-axis definitions can be found on the APL Wiki page for function axis.
Although you have seen them, we have yet to mention nested arrays. They will be discussed in more detail later, but for now you should know arrays with boxed display (when ]boxing
is on) are nested arrays and they can contain any other array. Non-nested arrays are called simple arrays.
2 3⍴'DYALOG' ⍝ A simple numeric matrix
DYA
LOG
3⍴'DY' 'AL' 'OG' ⍝ A nested vector of character vectors
┌──┬──┬──┐
│DY│AL│OG│
└──┴──┴──┘
2 2⍴⍳4 ⍝ A simple numeric matrix
1 2
3 4
3⍴(1 2)(3 4)(5 6) ⍝ A nested vector of numeric vectors
┌───┬───┬───┐
│1 2│3 4│5 6│
└───┴───┴───┘