Array Languages #23: Shape, Rank, Axes and Dimensions
2348 words • 12 min read • Abstract

Episode 3 of my video series on array programming moves past lists. Episode 2 turned loops into map, filter and reduce over whole lists, and a list is only one kind of array. Every array has a shape, a rank and axes, and this episode is about all three.
| Resource | Link |
|---|---|
| Video | Shape, Rank, Axes and Dimensions: Array Languages #3![]() |
| Earlier episodes | 1: Why Another Programming Language? (video) 2: From Scalar Loops to Array Operations (video) |
| All the videos | X_eTaL-video’s page |
| Challenges | LeetCode 1672, 1351 |
| Try X_eTaL | In your browser |
| Code | X_eTaL, X_eTaL-video |
| The series | Array Languages: every post |
| Comments | Discord |
Shape and rank
A scalar is a single value. It has no axes, so its shape is an empty list. X_eTaL can draw a result in a box, and the box shows that clearly: the ⊖ on the top edge marks a list with no items, and the ~ on the bottom marks a list of numbers.
s̲hape 42
┌⊖┐
│ │
└~┘
A vector has one axis. r̲avel turns the scalar into a vector of one item, and its shape is a list holding one number, 1:
s̲hape r̲avel 42
┌→┐
│1│
└~┘
Rank is the number of axes, which is the length of the shape: tally the shape and you have it. X_eTaL will soon have it built in; until then, the episode defines it. ᵘ marks a name the user defined, and ⍵ is the argument on the right:
ᵘr̲ankOf ← { t̲ally s̲hape ⍵}
ᵘr̲ankOf 42 ⍝ 0
ᵘr̲ankOf r̲avel 42 ⍝ 1
A matrix has two axes, rows and columns:
M ← 2 3 r̲eshape r̲ange 6
s̲hape M ⍝ 2 3
ᵘr̲ankOf M ⍝ 2
M is 1 2 3 over 4 5 6, with shape 2 3. It holds six values, but its rank is 2. Add one more axis and two tables, each three by four, make an array of shape 2 3 4 and rank 3. Higher ranks follow the same rule.
Along an axis
Each axis has a number. In a matrix, axis 1 runs down the rows, and axis 2 runs along each row. Many operations work along an axis, and in X_eTaL a subscript after the name picks which one. (The code here is shown as X_eTaL draws it, as the video does. To try it in the browser editor, type the ASCII it is drawn from: := for ←, r_/_2 for r̲/₂, * for ×, and # for ⍝.)
'+ r̲/ M ⍝ 5 7 9
'+ r̲/₁ M ⍝ 5 7 9
'+ r̲/₂ M ⍝ 6 15
Reduce works along axis 1 unless told otherwise, so the first two both give 5 7 9, the sums down the columns. With subscript 2 it sums along each row instead: 6 15.
Rotate, reverse and transpose work the same way. 1 o̲-₁ M rotates along axis 1, so the rows trade places; 1 o̲-₂ M shifts each row left by one, giving 2 3 1 over 5 6 4. r̲ev₂ M reverses each row. And o̲\ M, transpose, reverses the order of the axes: rows become columns, and the 2 3 matrix becomes 3 2.
Three dimensions
The same subscripts work on the rank 3 array:
T ← 2 3 4 r̲eshape r̲ange 24
s̲hape '+ r̲/₁ T ⍝ 3 4
s̲hape '+ r̲/₃ T ⍝ 2 3
Reduce along axis 1 adds the two tables together, a 3 4 result. Reduce along axis 3 sums each row, a 2 3 result. Transpose reverses all three axes, so 2 3 4 becomes 4 3 2; with two subscripts, o̲\₂₃, it swaps just those two (2 4 3); and a list on the left of t̲ranspose can put the axes in any order (3 1 2 t̲ranspose T has shape 3 4 2).
Ravel goes the other way, turning any array back into a vector: r̲avel T is the 24 values in order, rank 1 again. With subscript 2, r̲avel₂ M unrolls a matrix column by column: 1 4 2 5 3 6.
Two challenges, along axes
The episode tries the ideas on two short coding challenges from LeetCode.
Richest Customer Wealth (1672): each row is a customer and each column a bank account. Sum along axis 2 for each customer’s total, then take the largest:
accounts ← 3 2 r̲eshape 1 5 7 3 3 5
'+ r̲/₂ accounts ⍝ 6 10 8
'm̲ax r̲/ '+ r̲/₂ accounts ⍝ 10
The answer is 10.
Count Negative Numbers in a Sorted Matrix (1351): compare the grid with zero for a mask of ones and zeros, then reduce along both axes at once, with subscript ₁₂:
grid ← 3 4 r̲eshape 3 2 1 -1 1 1 -1 -2 -1 -1 -2 -3
'+ r̲/₁₂ grid < 0 ⍝ 7
There are 7.
Tensors
In machine learning, arrays like these are called tensors. A color image might have shape 480 640 3: height, width and three color channels. Stack 32 of them and the batch gains an axis, shape 32 480 640 3, rank 4, and '× r̲/ s̲hape images counts its 29,491,200 values. A GPU works on many values at once, and shape and axes tell it how a tensor is laid out. The ideas are the same ones, and later episodes come back to tensors.
How the video draws the code
The code on the slides isn’t typeset by hand. You type X_eTaL in plain ASCII, M := 2 3 r_eshape r_ange 6, and X_eTaL’s own xetal render draws it as notation: M ← 2 3 r̲eshape r̲ange 6. The underlined letter marks a name as a function, the _2 becomes the subscript axis in r̲/₂, and the # of a remark becomes ⍝.
The video is built in X_eTaL-video, which pins one build of X_eTaL so the slides always match the language they show. Before a lesson renders, its build script collects every line of X_eTaL in the lesson and runs them through xetal render --html. That returns each line as HTML spans, one class for each kind of token:
<span class="c-symbol">'+</span> <span class="c-builtin">r̲/₂</span> M <span class="c-comment">⍝ 6 15</span>
The renderer, written with Remotion, gives each class its color (built-in functions blue, numbers gold, remarks gray), and a line that doesn’t lex as X_eTaL stops the build instead of reaching the screen. The arrays drawn as grids come from X_eTaL too: the lesson’s data file is run with the same pinned xetal, and the slides draw the numbers it prints. The code blocks in this post are colored the same way. The blog’s highlighter follows the same token classes as xetal render --html.
The boxes come from X_eTaL too, drawn the way APL2’s DISPLAY drew them: xetal --box boxes every result, and d̲isplay boxes one. A matrix shows how to read them:
┌→────┐
↓1 2 3│
│4 5 6│
└~────┘
That’s M. The → on the top edge marks the last axis, the one that runs along each row, and the ↓ on the left edge marks the first axis, down the rows. A vector gets only the →, so the number of arrows is the rank, and the ~ on the bottom edge means the items are numbers. An empty list gets ⊖ instead of →, as s̲hape 42 showed. The episode’s slides show their results boxed this way, as in the rotate beside this section.
Coming up, and an exercise
The next episode is about broadcasting: how an operation combines arrays of different shapes. The preview starts with the simplest case. 10 × M multiplies every item by ten, because a single value extends to every item, and M + M combines two arrays of the same shape item by item. In X_eTaL that is the whole rule, and other shapes are an error.
Array languages draw this line in different places. APL extends a single value too, and an axis can say how a vector meets a matrix. J and BQN match axes from the front, so each item of a vector goes with one row; K works much the same way, since there a matrix is a list of rows. NumPy, for Python, matches axes from the back. The next episode puts them side by side.
The exercise: a video clip has 300 frames, each 480 by 640 with three colors. Work out its shape and its rank, then count its values. Everything in this post can be tried in the browser editor.
Closing
In future videos I plan to include more interesting coding problems and better visualizations as I add more tools to help with code experimentation and understanding.
Part 23 of the Array Languages series. View all parts
Comments or questions? SW Lab Discord or YouTube @SoftwareWrighter.
