Rank is a generalization of looping as used in scalar (non-array-oriented) programming languages. It is also a generalization of mapcar in the language Lisp and map in modern functional programming languages, and a generalization of scalar extension, inner (matrix) product, and outer product in APL\360. The canonical implementation of rank may be the language J, but it is also available in Dyalog APL, the International Organization for Standardization (ISO) technical standard on Extended APL, and NARS2000.

Rank has several different meanings. In general, the concept of rank is used to treat an orthogonal array in terms of its subarrays. For example, a two-dimensional array may be dealt with at rank 2 as the entire matrix, or at rank 1 to work with its implicit one-dimensional columns or rows, or at rank 0 to work at the level of its individual atoms.

Rank (J programming language)

Noun rank – The rank of a noun is a nonnegative integer. Verb rank – The rank of a verb is a list of three integers. The rank conjunction – The rank conjunction (") is used to derive a verb with a specific rank.

Rank (J programming language)

Rank as a generalization of looping

Understanding rank requires knowing some very basic array-oriented programming concepts. In most array-based languages, reduction is denoted with a forward slash /. In J, the slash takes a left argument of the function and a right argument of the array to be reduced by that function. +/ 1 2 3 6

Rank (J programming language)

The result is 1 + 2 + 3, as expected.

An N-dimensional integer array can also be created with i. which takes a vector of integers as its arguments. The number of integers defines the dimension and the absolute value of each integer defines the length of the corresponding dimension. i. 3 0 1 2

i. 2 3

0 1 2

3 4 5

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

Now let's reduce a two-dimensional array by addition. +/ i. 2 3 3 5 7

The result is 0 1 2 + 3 4 5, as expected. Reduction runs down each column, adding together all the numbers in that column.

This application of +/ to a two-dimensional array corresponds to the C code fragment: for(j = 0; j < 3; ++j) { sum[j] = 0; } for(i = 0; i < 2; ++i) { for(j = 0; j < 3; ++j) { sum[j] += array[i][j]; } }

Suppose we wanted to add up the items of each row, as in the C code fragment: for(i = 0; i < 2; ++i) { sum[i] = 0; for(j = 0; j < 3; ++j) { sum[i] += array[i][j]; } }

To produce the result 3 12. We can do this in J without looping simply by using rank. +/"1 i. 2 3 3 12

To illustrate further how rank works in J, we can see the original expression is rank 2. The operator is mapped at the highest rank to the array. +/"2 i. 2 3 3 5 7

It is common to refer to the lower-dimensional arrays by these names, though they are disputed sometimes.

{ | class="wikitable" |- !style="text-align:center;" |Name !style="text-align:center;" |Rank |- |style="text-align:center;" |Atom or scalar |style="text-align:center;" |0 |- |style="text-align:center;" |Vector or list |style="text-align:center;" |1 |- |style="text-align:center;" |Table or matrix |style="text-align:center;" |2 |- |style="text-align:center;" |Tensor or cube |style="text-align:center;" |3 |}

Noun rank

Nouns, in J, are arrays. The rank of a noun is the number of dimensions of that array. The derived verb #@$ determines the rank of a noun.

Verb rank

Verbs, in J, are functions which take noun arguments and produce noun results. The rank of a verb controls how the verb is applied to nouns with ranks greater than 0. This verb rank is expressed as three numbers:

Rank for the monad case; for example, −y uses − as a monad

Rank for the left argument for the dyad case; for example, x−y uses − as a dyad

Rank for the right argument for the dyad case

In all cases, there is some underlying verb definition which applies to cells, which are subarrays of the indicated rank. Or, if the argument does not have that many dimensions, the entire argument.

In verbs, negative rank is interpreted as the rank of the noun supplied for that argument less the indicated value. (But never less than zero.)

For example, a verb with monadic rank of negative one when given an argument of rank 3, breaks the argument down into a list of rank 2 arrays. The verb's body is applied once to each of these two-dimensional subarrays.