The Rank/Atop operator:

An algorithm must be seen to be believed. –Don Knuth

The somewhat startled-looking Rank operator, , has a reputation for being one of the harder ones to grasp. It sits on the ‘J’ key, a handy mnemonic, as it was an invention borrowed from the J-language. Well, it makes sense to me.

If is given a right operand function it becomes Atop. We’ll talk about that after we’ve covered the Rank version.

Other resources on Rank and Atop:

⎕IO  0
]box on
]rows on
Was ON
Was OFF

In essence, the simple use of Rank drills into the argument array and applies its operand to sub-cells of a specified rank only.

Consider the following rank 3 array:

]DISPLAY m  3 3 327?27
≢⍴m
┌┌→───────┐ ↓↓ 3 0 21│ ││19 15 9│ ││24 6 17│ ││ │ ││26 8 20│ ││ 4 14 12│ ││11 23 5│ ││ │ ││18 7 2│ ││13 16 1│ ││22 25 10│ └└~───────┘
3

Let’s say we want to drop the first row of each layer of this array. In this case, we could use a bracket-axis specification to drop:

1[1]m ⍝ Apply drop ↓ to axis 1, i.e. the y-axis, with ⎕IO←0
19 15 9 24 6 17 4 14 12 11 23 5 13 16 1 22 25 10

However, this is now ⎕IO dependent, and the bracket-axis spec has some other undesirable properties, including the fact that it cannot be applied to user-defined functions.

The Rank operator allows us to solve this in a different way:

12m
19 15 9 24 6 17 4 14 12 11 23 5 13 16 1 22 25 10

You can think of Rank in this case being closer to the problem statement: drop the first major cell of each sub-cell that has a rank one less than the argument. Or: in our 3D array, apply the drop to each of its constituent 2D arrays.

Note that Rank doesn’t equal depth, and this is part of the reason why the Rank operator can be confusing. So given a complex vector that contains a variety of scalars and higher-rank arrays, you can’t use the Rank operator to selectively apply a function to, say, just the vectors.

  V  (1 2 3)(2 21 1 1 1)(1)
┌─────┬───┬─┐ │1 2 3│1 1│1│ │ │1 1│ │ └─────┴───┴─┘
1V ⍝ NOTE: this is wrong!
3

In this case, V is of course already a rank 1 array (i.e. a vector). But wait, there is more confusion to be had! The right operand doesn’t even have to be a scalar at all.

We already saw that if the right operand of is a scalar, we are specifying which rank sub cells we want a function to apply to. For dyadic usage, we instead specify which sub-cells of the left argument should be paired up with which sub-cells of the right argument. It takes a while to wrap your head around this.

Here’s an example:

1 2 3,0 1  'hello'
1 hello 2 hello 3 hello

This says that the left argument of the derived function should be paired at rank 0 (that is each element) with the right argument at rank 1 (that is the whole vector in this case). If this feels reminiscent of an outer product, you’re not wrong.

In other words, Rank pairs up the arguments of the function it derives. We can examine which elements would be paired up by using the following incantation in place of our function:

rankpairs  ,

if you know what that means, you probably don’t need to read this tutorial. If not, don’t worry – treat it as a debug statement:

1 2 3 rankpairs0 1  'hello'
┌─────────┬─────────┬─────────┐ │┌─┬─────┐│┌─┬─────┐│┌─┬─────┐│ ││1│hello│││2│hello│││3│hello││ │└─┴─────┘│└─┴─────┘│└─┴─────┘│ └─────────┴─────────┴─────────┘

In the section on array indexing earlier, we introduced the concept of Sane Indexing without any further explanation on how it was working. Here it is again, as a tacit function:

I⍨∘⍨⍤0 99 ⍝ Sane indexing, a.k.a select

Now that we understand a bit about Rank, we can at least make a start in taking that apart. So we have a Rank 0 99 to the right, stating that we should combine the left argument at rank 0 (scalars) with the right argument at “rank 99”. Rank 99 means “full rank”, as Dyalog has a max rank of 99. So we should combine “each scalar” to the left with “the whole thing, at whatever rank it happens to be” to the right.

The actual indexing is done by the leading Squad (). Let’s de-selfie the expression and make it a dfn. Although we haven’t covered tacit functions yet, we can have the interpreter help us out a bit to see how that expression actually parses:

]box on -trains=tree ⍝ Visualise tacit functions as a parse tree
Was ON -trains=tree
⍨∘⍨⍤0 99
⍤ ┌┴┐ ⍨ 0 99 ┌─┘ ∘ ┌┴┐ ⍨ ⊃ ┌─┘ ⌷

If we separate out the Rank expression, after a bit of head-scratching, we arrive at

sane{()}
3 sane 'abcdefg'
d
1 2 3 sane0 99'abcdefg'
bcd

Hopefully, we can now see a bit clearer how it works – we simply pass the disclosed left argument vector to Squad, and Rank does the appropriate combination of left and right: each left element, to the whole (full-rank) of the right.

Here’s another example. Given two integer arrays of the same shape, can we replicate (or compress) the rows of one from the other? We might be excused if we think that we can replicate-reduce, but this doesn’t work:

]DISPLAY A  3 42 1 1 1 1 2 1 1 1 1 2 1
]DISPLAY B  3 41 2 3 4 1 2 3 4 1 2 3 4
┌→──────┐ ↓2 1 1 1│ │1 2 1 1│ │1 1 2 1│ └~──────┘
┌→──────┐ ↓1 2 3 4│ │1 2 3 4│ │1 2 3 4│ └~──────┘

(note: all expressions in the next cell will generate syntax or rank errors)

A∘//B     ⍝ Nope... SYNTAX ERROR
{A/}/B   ⍝ Nope... RANK ERROR
A{/}/B ⍝ Nope... SYNTAX ERROR
SYNTAX ERROR: The function does not take a left argument
      A∘//B     ⍝ Nope... SYNTAX ERROR
        ∧
RANK ERROR
      {A/⍵}/B   ⍝ Nope... RANK ERROR
        ∧
SYNTAX ERROR: The function does not take a left argument
      A∘{⍺/⍵}/B ⍝ Nope... SYNTAX ERROR
            ∧

Instead, we need to reach for Rank:

A/⍤1B
1 1 2 3 4 1 2 2 3 4 1 2 3 3 4

If we spell it out in plain words, perhaps the use of Rank suggests itself: combine each major cell on the left with the corresponding major cell on the right, and apply the dyadic function “replicate”. Every time you think “combine each major cell on the left…”, you should think Rank.

As we’ve seen elsewhere, APL has a somewhat unholy mix of leading and trailing axis operators, mainly for historical reasons. Rank allows us to stick to the leading axis versions [Kromberg and Hui]:

Leading axis

Trailling axis

Operation

(⊖⍤1)⍵

⌽⍵

reverse

⍺(⊖⍤1)⍵

⍺⌽⍵

rotate (scalar ⍺)

(f⌿⍤1)⍵

f/⍵

reduce

(f⍀⍤1)⍵

f\⍵

scan

Rank drills

In his paper APL Excercises, Roger Hui sets out the following drills for Rank. See if you can predict the results before revealing the answers:

,t2 3 4⍴⍳24
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
,3t
]DISPLAY ,2t
]DISPLAY ,1t
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 1 2 3 4 5 6 7 8 9 10 11│ │12 13 14 15 16 17 18 19 20 21 22 23│ └~──────────────────────────────────┘
┌┌→──────────┐ ↓↓ 0 1 2 3│ ││ 4 5 6 7│ ││ 8 9 10 11│ ││ │ ││12 13 14 15│ ││16 17 18 19│ ││20 21 22 23│ └└~──────────┘
3t
2t
1t
┌───────────┐ │ 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 1 2 3│12 13 14 15│ │4 5 6 7│16 17 18 19│ │8 9 10 11│20 21 22 23│ └─────────┴───────────┘
┌───────────┬───────────┬───────────┐ │0 1 2 3 │4 5 6 7 │8 9 10 11 │ ├───────────┼───────────┼───────────┤ │12 13 14 15│16 17 18 19│20 21 22 23│ └───────────┴───────────┴───────────┘
 ,¨ 3t
]DISPLAY  ,¨ 2t
]DISPLAY  ,¨ 1t
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 1 2 3 4 5 6 7 8 9 10 11│ │12 13 14 15 16 17 18 19 20 21 22 23│ └~──────────────────────────────────┘
┌┌→──────────┐ ↓↓ 0 1 2 3│ ││ 4 5 6 7│ ││ 8 9 10 11│ ││ │ ││12 13 14 15│ ││16 17 18 19│ ││20 21 22 23│ └└~──────────┘
]DISPLAY +⌿⍤3t
]DISPLAY +⌿⍤2t
]DISPLAY +⌿⍤1t
┌→──────────┐ ↓12 14 16 18│ │20 22 24 26│ │28 30 32 34│ └~──────────┘
┌→──────────┐ ↓12 15 18 21│ │48 51 54 57│ └~──────────┘
┌→───────┐ ↓ 6 22 38│ │54 70 86│ └~───────┘
]DISPLAY 3t
]DISPLAY 2t
]DISPLAY 1t
┌┌→──────────┐ ↓↓12 13 14 15│ ││16 17 18 19│ ││20 21 22 23│ ││ │ ││ 0 1 2 3│ ││ 4 5 6 7│ ││ 8 9 10 11│ └└~──────────┘
┌┌→──────────┐ ↓↓ 8 9 10 11│ ││ 4 5 6 7│ ││ 0 1 2 3│ ││ │ ││20 21 22 23│ ││16 17 18 19│ ││12 13 14 15│ └└~──────────┘
┌┌→──────────┐ ↓↓ 3 2 1 0│ ││ 7 6 5 4│ ││11 10 9 8│ ││ │ ││15 14 13 12│ ││19 18 17 16│ ││23 22 21 20│ └└~──────────┘

If you got all those right, well done – Roger’s paper has got many more for you to pit your wits against.

Atop

As we mentioned above, from version 18 of Dyalog, can also be Atop if given a right operand function instead of array. We’ll see more of different kinds of Atop later in the tacit chapter, but for now it’s a functional composition rule, stating that (for the dyadic case),

X fg Y  f X g Y

It is similar enough to Jot, , to be confusing. Whilst monadic is an atop, dyadic is a beside. Behold:

fg X  f(gX)  fgX
fg X  f(gX)  fgX
X fg Y  X f g Y
X fg Y  f X g Y

Another way to think about it is that in the dyadic case, the difference between and is which operand function (f or g) gets the left argument. In the monadic case, Atop is equivalent for and , so it’s worth using for both monadic and dyadic atops for consistency.

Here is a simple example of a dyadic Atop,

1 2 3 -+ 4 5 6 ⍝ Negate the sum of two vectors
¯5 ¯7 ¯9

which, following the X f⍤g Y f X g Y rule, is the same as

-1 2 3+4 5 6 ⍝ Negate the sum of two vectors
¯5 ¯7 ¯9

or, as a tacit formulation (which we’ll cover in more detail later),

1 2 3 (-+) 4 5 6 ⍝ Negate the sum of two vectors
¯5 ¯7 ¯9