Rank is elucidated through an executable model and annotated examples.

Rank

Verb (function) rank was introduced by Iverson [1978 §6], further developed in Iverson [1983, 1987, 1995], and implemented in SHARP APL, SHARP APL/HP, SAX, A, and J (see Bernecky et al. [1983, 1987], Hodgkinson [1986], Steinbrook [1986], Whitney [1989], and Hui et al. [1990], respectively).

A verb of rank r is defined on arguments with rank bounded by r ; the extension to higher-rank arguments is the same for all verbs. The rank conjunction " (operator) augments the default ranks of a verb by user-specified ranks. It provides for the generalization of a verb to higher-rank arrays, and could justifiably be called the generalization or extension operator; it also provides for consistent application to lower-rank arrays, subsuming and superseding the anomalous bracket-axis operator.

Various aspects of rank are here discussed in terms of a model in J, updated from Hui [1987 §A.2, 1992 §3.2]. The notation is defined in Iverson [1995], and additional annotated examples can be found in Section 2.

Frames and Cells

A rank r splits the argument shape into the frame and the cell shape; a positive or zero r specifies the number of trailing cell axes, while a negative r specifies the negative of the number of leading frame axes.

rk    =: #@$  
er    =: (0:>.(+rk))`(<.rk) @. (0<:[)  
fr    =: -@er }. $@]  
cs    =: -@er {. $@]  
boxr  =: ]`(<@$ , [ $: */@[}.])@.(*@#@])
cells =: fr $ cs boxr ,@]

For rank r and argument y , the phrase r er y computes the effective rank (non-negative and bounded by #$y); r fr y computes the frame and r cs y the cell shape; and r cells y computes the array of cells with shape r fr y , each cell individually boxed and shaped s=: r cs y  (r cells y  <"r y). The recursively-defined verb s boxr y produces the list of such cells.

The model is shown in action on x*"0 _1 y , the atoms (scalars) of x times the items of y :

   x=: 1 2 3
   y=: i.3 2

   y                            x *"0 1 y
0 1                           0  1
2 3                           4  6
4 5                          12 15

   0 er x                       _1 er y
0                            1
   0 fr x                       _1 fr y
3                            3
   0 cs x                       _1 cs y
                             2
   0 cells x                    _1 cells y
┌─┬─┬─┐                      ┌───┬───┬───┐
│1│2│3│                      │0 1│2 3│4 5│
└─┴─┴─┘                      └───┴───┴───┘

Agreement

In the dyad v"r , commonly the left and right frames match, that is, the two cell arrays have the same shape; if not, several design choices are possible:

In scalar agreement, one frame must be empty, and the single cell is reshaped using the other frame; in suffix agreement, one frame must be a suffix of the other, and again the list of cells is reshaped using the other frame; finally, in prefix agreement, one frame must be a prefix of the other, and each cell is reshaped with the excess in the other frame. All three agreements are proper generalizations of scalar extension in APL\360, with cells acting the role of scalars. Agreement results in the two cell arrays having the same shape ("the frame").

Prefix agreement is adopted in J as suggested by Whitney [1992], because it best fits the emphasis on leading axes.

pfx   =: <.&rk
agree =: (pfx {. $@[) -: (pfx {. $@])
frame =: [:`($@([^:(>&rk))) @. agree 
rag   =: frame $ ([: */ rk@]}.$@[) # ,@]
lag   =: rag~

rag and lag apply to both cell arrays (the results of cells in the previous section), producing cell arrays with the same shape. If v"r itself were used in the model, rag could be defined more directly from the specification: (rk@]}.$@[) $"1 0 ] — each cell is reshaped with the excess in the other frame. In the continuing example, rag and lag have no effect because the left and right frames match.

   ] xc=: 0 cells x             ] yc=: _1 cells y
┌─┬─┬─┐                      ┌───┬───┬───┐
│1│2│3│                      │0 1│2 3│4 5│
└─┴─┴─┘                      └───┴───┴───┘
   ] xa=: xc lag yc             ] ya=: xc rag yc
┌─┬─┬─┐                      ┌───┬───┬───┐
│1│2│3│                      │0 1│2 3│4 5│
└─┴─┴─┘                      └───┴───┴───┘

Assembly

After agreement, the phrase v&.> applies v under > to corresponding boxed left and right argument cells, to produce an array of boxed result cells. It remains to assemble the overall result from the individual results.

Cells are brought to a common rank by adding leading unit axes, then to a common shape by padding. The overall shape is fm,sir , where fm is the frame and sir is the common shape of the individual results. This is a design choice: the individual results could be required to have a common shape without further intervention, but this permissive assembly proves useful. For example, open > on a list of boxed words yields a matrix with the words padded to a common length.

mrk   =: >./@:(rk&>)@,  
crank =: mrk ,:@]^:(-rk)&.> ]
msh   =: >./@:( $&>)@, 
cshape=: <@msh {.&.> ] 
asm   =: > @ cshape @ crank  

rank  =: 2 : 0
 'mm ll rr'=. 3$&.|.y
 ([: asm [: x&.> mm&cells) : ([: asm ll&cells@[ (lag x&.> rag) rr&cells@])
)

The conjunction rank (with a 2-line body) integrates the model components. The left argument x is the verb v ; the right argument y is reshaped from the right to exactly 3 numbers and assigned to mm , ll , and rr .

   ] za=: xa*&.>ya
┌───┬───┬─────┐
│0 1│4 6│12 15│
└───┴───┴─────┘
   asm za
 0  1
 4  6
12 15
   x * rank 0 _1 y
 0  1
 4  6
12 15

Zero Frame

If the frame contains 0 (as in 3 *"1 i. 0 4), there are no argument cells to apply v to, and the shape of a result cell (the value of sir) is indeterminate. Pesch [1986] describes a variety of strategies to address this problem. In J, the shape is calculated if v is uniform; otherwise v is applied to a cell of fills.

Annotated Examples

Rank Model

rk    =: #@$  
er    =: (0:>.(+rk))`(<.rk) @. (0<:[)  
fr    =: -@er }. $@]  
cs    =: -@er {. $@]  
boxr  =: ]`(<@$ , [ $: */@[}.])@.(*@#@])
cells =: fr $ cs boxr ,@]
 
pfx   =: <.&rk
agree =: (pfx {. $@[) -: (pfx {. $@])
frame =: [:`($@([^:(>&rk))) @. agree 
rag   =: frame $ ([: */ rk@]}.$@[) # ,@]
lag   =: rag~

mrk   =: >./@:(rk&>)@,  
crank =: mrk ,:@]^:(-rk)&.> ]
msh   =: >./@:( $&>)@, 
cshape=: <@msh {.&.> ] 
asm   =: > @ cshape @ crank  

rank  =: 2 : 0
 'mm ll rr'=. 3$&.|.y
 ([: asm [: x&.> mm&cells) : ([: asm ll&cells@[ (lag x&.> rag) rr&cells@])
)

Frames and Cells

   ] y=: i.2 3 4
 0  1  2  3
 4  5  6  7
 8  9 10 11

12 13 14 15
16 17 18 19
20 21 22 23

   0 (er;fr;cs) y    NB. effective rank; frame; cell shape
┌─┬─────┬┐
│0│2 3 4││
└─┴─────┴┘
   0 cells y         NB. rank 0 cells aka 0-cells
┌──┬──┬──┬──┐
│0 │1 │2 │3 │
├──┼──┼──┼──┤
│4 │5 │6 │7 │
├──┼──┼──┼──┤
│8 │9 │10│11│
└──┴──┴──┴──┘

┌──┬──┬──┬──┐
│12│13│14│15│
├──┼──┼──┼──┤
│16│17│18│19│
├──┼──┼──┼──┤
│20│21│22│23│
└──┴──┴──┴──┘

   1 (er;fr;cs) y
┌─┬───┬─┐
│1│2 3│4│
└─┴───┴─┘
   1 cells y
┌───────────┬───────────┬───────────┐
│0 1 2 3    │4 5 6 7    │8 9 10 11  │
├───────────┼───────────┼───────────┤
│12 13 14 15│16 17 18 19│20 21 22 23│
└───────────┴───────────┴───────────┘

   2 (er;fr;cs) y
┌─┬─┬───┐
│2│2│3 4│
└─┴─┴───┘
   2 cells y
┌─────────┬───────────┐
│0 1  2  3│12 13 14 15│
│4 5  6  7│16 17 18 19│
│8 9 10 11│20 21 22 23│
└─────────┴───────────┘

   3 (er;fr;cs) y
┌─┬┬─────┐
│3││2 3 4│
└─┴┴─────┘
   3 cells y
┌───────────┐
│ 0  1  2  3│
│ 4  5  6  7│
│ 8  9 10 11│
│           │
│12 13 14 15│
│16 17 18 19│
│20 21 22 23│
└───────────┘

   _1 (er;fr;cs) y
┌─┬─┬───┐
│2│2│3 4│
└─┴─┴───┘
   _1 cells y        NB. rank-_1 cells aka _1-cells aka major cells aka items
┌─────────┬───────────┐
│0 1  2  3│12 13 14 15│
│4 5  6  7│16 17 18 19│
│8 9 10 11│20 21 22 23│
└─────────┴───────────┘

   _1 cells i.3 4
┌─────────┬───────────┐
│0 1  2  3│12 13 14 15│
│4 5  6  7│16 17 18 19│
│8 9 10 11│20 21 22 23│
└─────────┴───────────┘
   _1 cells i.3 4
┌───────┬───────┬─────────┐
│0 1 2 3│4 5 6 7│8 9 10 11│
└───────┴───────┴─────────┘
   _1 cells i.4
┌─┬─┬─┬─┐
│0│1│2│3│
└─┴─┴─┴─┘
   _1 cells 3
┌─┐
│3│
└─┘

   _2 cells y
┌───────────┬───────────┬───────────┐
│0 1 2 3    │4 5 6 7    │8 9 10 11  │
├───────────┼───────────┼───────────┤
│12 13 14 15│16 17 18 19│20 21 22 23│
└───────────┴───────────┴───────────┘

Rank Monad

The computation to be modelled is i."_1 y .

   y=: 6 4 9
   1 (er;fr;cs) y    NB. effective rank; frame; cell shape
┌─┬┬─┐
│1││3│
└─┴┴─┘
   ] yc=: _1 cells y
┌─┬─┬─┐
│6│4│9│
└─┴─┴─┘
   ] zc=: i.&.>yc    NB. apply i. to each cell
┌───────────┬───────┬─────────────────┐
│0 1 2 3 4 5│0 1 2 3│0 1 2 3 4 5 6 7 8│
└───────────┴───────┴─────────────────┘

   asm zc            NB. permissive assembly
0 1 2 3 4 5 0 0 0
0 1 2 3 0 0 0 0 0
0 1 2 3 4 5 6 7 8

   i. rank _1 y
0 1 2 3 4 5 0 0 0
0 1 2 3 0 0 0 0 0
0 1 2 3 4 5 6 7 8

Agreement I

The computation to be modelled is x,"1 y , append vectors to vectors.

   ] x=: 'PQ'
PQ
   ] y=: 3 4 $'abcdefghijkl'
abcd
efgh
ijkl

   1 er x                       1 er y    NB. effective rank
1                            1
   1 fr x                       1 fr y    NB. frame
                             3
   1 cs x                       1 cs y    NB. cell shape
2                            4

   NB. argument cells before agreement
   ] xc=: 1 cells x             ] yc=: 1 cells y
┌──┐                         ┌────┬────┬────┐
│PQ│                         │abcd│efgh│ijkl│
└──┘                         └────┴────┴────┘

   NB. argument cells after agreement
   ] xa=: xc lag yc             ] ya=: xc rag yc
┌──┬──┬──┐                   ┌────┬────┬────┐
│PQ│PQ│PQ│                   │abcd│efgh│ijkl│
└──┴──┴──┘                   └────┴────┴────┘

   ] za=: xa,&.>ya   NB. apply , to corresponding cells
┌──────┬──────┬──────┐
│PQabcd│PQefgh│PQijkl│
└──────┴──────┴──────┘

   asm za            NB. assemble to produce the required result
PQabcd
PQefgh
PQijkl

   x , rank 1 y
PQabcd
PQefgh
PQijkl

Agreement II

The computation to be modelled is x,"0 1 y , append scalars to vectors.

   ] x=: 3 2$'012345'
01
23
45
   ] y=: 3 4$'abcdefghij'
abcd
efgh
ijab

   0 er x                       1 er y    NB. effective rank
0                            1
   0 fr x                       1 fr y    NB. frame
3 2                          3
   0 cs x                       1 cs y    NB. cell shape
                             4

   NB. argument cells before agreement
   ] xc=: 0 cells x             ] yc=: 1 cells y
┌─┬─┐                        ┌────┬────┬────┐
│0│1│                        │abcd│efgh│ijab│
├─┼─┤                        └────┴────┴────┘
│2│3│                
├─┼─┤                
│4│5│                
└─┴─┘                

   NB. argument cells after agreement
   ] xa=: xc lag yc             ] ya=: xc rag yc
┌─┬─┐                        ┌────┬────┐
│0│1│                        │abcd│abcd│
├─┼─┤                        ├────┼────┤
│2│3│                        │efgh│efgh│
├─┼─┤                        ├────┼────┤
│4│5│                        │ijab│ijab│
└─┴─┘                        └────┴────┘

   ] za=: xa,&.>ya   NB. apply , to corresponding cells
┌─────┬─────┐
│0abcd│1abcd│
├─────┼─────┤
│2efgh│3efgh│
├─────┼─────┤
│4ijab│5ijab│
└─────┴─────┘

   asm za            NB. assemble to produce the required result
0abcd
1abcd

2efgh
3efgh

4ijab
5ijab

   x , rank 0 1 y
0abcd
1abcd

2efgh
3efgh

4ijab
5ijab

Agreement III — Outer Product

The computation to be modelled is x*"0 _ y , multiply scalars in x by the entire of y .

   x=: 1 2 3 4
   y=: 8 5 7
   
   0 er x                       _ er y    NB. effective rank
0                            1
   0 fr x                       _ fr y    NB. frame
4
   0 cs x                       _ cs y    NB. cell shape
                             3
   
   NB. argument cells before agreement
   ] xc=: 0 cells x             ] yc=: _ cells y
┌─┬─┬─┬─┐                    ┌─────┐
│1│2│3│4│                    │8 5 7│
└─┴─┴─┴─┘                    └─────┘

   NB. argument cells after agreement
   ] xa=: xc lag yc             ] ya=: xc rag yc
┌─┬─┬─┬─┐                    ┌─────┬─────┬─────┬─────┐
│1│2│3│4│                    │8 5 7│8 5 7│8 5 7│8 5 7│
└─┴─┴─┴─┘                    └─────┴─────┴─────┴─────┘

   ] za=: xa*&.>ya   NB. apply * to corresponding cells
┌─────┬────────┬────────┬────────┐
│8 5 7│16 10 14│24 15 21│32 20 28│
└─────┴────────┴────────┴────────┘

   asm za            NB. assemble to produce the required result
 8  5  7
16 10 14
24 15 21
32 20 28
   
   x * rank 0 _ y
 8  5  7
16 10 14
24 15 21
32 20 28

Permissive Assembly

The computation to be modelled is x{."0 1 y .

   ] y=: ];._1 '/Barlett, Sue/Doe, John/Other, A.N.'
Barlett, Sue
Doe, John   
Other, A.N. 
   $y
3 12
   ] x=: y i."1 ','  NB. index of comma in each row
7 3 5

   0 (er;fr;cs) x    NB. effective rank; frame; cell shape
┌─┬─┬┐
│0│3││
└─┴─┴┘
   1 (er;fr;cs) y
┌─┬─┬──┐
│1│3│12│
└─┴─┴──┘

   ] xc=: 0 cells x
┌─┬─┬─┐
│7│3│5│
└─┴─┴─┘
   ] yc=: 1 cells y
┌────────────┬────────────┬────────────┐
│Barlett, Sue│Doe, John   │Other, A.N. │
└────────────┴────────────┴────────────┘

   xc -: xa=: xc lag yc
1
   yc -: ya=: xc rag yc
1

   ] za=: xa{.&.>ya  NB. apply {.
┌───────┬───┬─────┐
│Barlett│Doe│Other│
└───────┴───┴─────┘

   asm za            NB. permissive assembly
Barlett
Doe    
Other  
   $ asm za
3 7

   x {."0 1 y
Barlett
Doe    
Other  
   (y i."1 ',') {."0 1 y
Barlett
Doe    
Other  
   
   surname=: (] i."1 ','"_) {."0 1 ]
   surname y
Barlett
Doe    
Other  

References



Contributed by RogerHui. Substantially the same text previously appeared as a part of Hui, R.K.W., Rank and Uniformity, APL95, APL Quote-Quad, Volume 25, Number 4, 1995-06. link

Jwiki: Essays/Rank (last edited 2008-12-08 10:45:34 by )