A hand presses a wooden stamp onto a printed grid, the same blue mark in square after square; a second, empty grid on another sheet lies askew beside it

Episode 4 of my video series on array programming is about what happens when two arrays meet. Episode 3 gave every array a shape, a rank and axes. Add two arrays, and the question is what their shapes have to do with each other. Array languages answer it in different ways, and this episode compares them.

Arrays as cubes, rendered in Blender: the scalar 7 as a bare number, shape empty; one cube, shape 1; a row of three cubes 10 20 30, shape 3; two rows of three, shape 2 3; a numbered block of cubes, shape 2 3 4; and two separate piles, shape 2 2 2 2
Arrays as cubes, from scalar to rank 4, as the episode recaps episode 3. X_eTaL works out every cube and number and writes the Blender script; Blender renders it. Click to enlarge.

A single value

Start with a single value and a vector. One plus the vector adds one to every item: the single value extends to each item. This is called scalar extension.

v ← 10 20 30
1 + v   ⍝ 11 21 31

It works with any shape. Ten times a matrix multiplies every item by ten, and the result has the matrix’s shape. As in episode 3, results are shown boxed, the way xetal --box draws them:

M ← 2 3 r̲eshape r̲ange 6
10 × M
┌→───────┐
↓10 20 30│
│40 50 60│
└~───────┘

The same shape, or an error

Two arrays of the same shape combine item by item: each item meets the item in the same place.

1 2 3 + v   ⍝ 11 22 33
M + M
┌→──────┐
↓2  4  6│
│8 10 12│
└~──────┘

Now try the matrix and the vector. M has shape 2 3 and v has shape 3. In X_eTaL that is an error, and the message names both shapes:

M + v
error[shape-mismatch]: shapes differ: 2 3 and 3

So the whole rule in X_eTaL fits in three lines: one value extends to every item, the same shape goes item by item, and any other shapes are an error.

Why an error?

The rule is short, so a result is easy to predict. A mismatch is often a mistake, and an error shows it at once, naming both shapes. To combine other shapes, you say how.

Saying how

The episode's slide: M plus the transposed reshape of w, drawn as boxes, 1 2 3 4 5 6 plus 100 100 100 200 200 200 equals 101 102 103 204 205 206
From the video: a vector down the columns. Click to enlarge.

To add the vector to each row, say so. r̲eshape repeats the vector to the matrix’s shape, and then the shapes match:

M + 2 3 r̲eshape v
┌→───────┐
↓11 22 33│
│14 25 36│
└~───────┘

Columns take one more step. w has two values, one for each row: reshape it to three by two, then transpose, and each row holds one value. The sum adds one value along each row, so each column gets the same two values:

w ← 100 200
M + o̲\ 3 2 r̲eshape w
┌→──────────┐
↓101 102 103│
│204 205 206│
└~──────────┘

The rank form says the row case another way. The 1 means each row of the matrix, and the small function in braces adds the vector to its argument, ⍵ (a later episode covers functions like this one):

1 '{ ⍵ + v } r̲ank M
┌→───────┐
↓11 22 33│
│14 25 36│
└~───────┘

And sometimes you want every pair. t̲able combines each item of one vector with each item of the other, so three values and two values give a three by two result:

v '+ t̲able w
┌→──────┐
↓110 210│
│120 220│
│130 230│
└~──────┘

Other array languages

The episode's APL slide: M plus 10 20 30 is a RANK ERROR; M plus with axis 2 in brackets adds 10 20 30 to each row
From the video: APL says a rank error, and an axis in brackets says how. Click to enlarge.

Other array languages draw this line in different places. M has two rows and three columns, so three values match its last axis and two values match its first:

Language Matches M + 10 20 30 M + 100 200
X_eTaL the same shape error error
APL the same shape error error
J, BQN axes from the front error 100 to the first row, 200 to the second
K axes from the front (a matrix is a list of rows) error 100 to the first row, 200 to the second
NumPy axes from the back 10 20 30 to each row error

Every one of them extends a single value. Each also has its own way to say how:

  • APL: both lines are a rank error, since the ranks differ. An axis number in brackets says how: plus on axis 2 puts the vector on each row, and on axis 1 two values go down the columns. The rank operator is another way; rank 1 pairs the vector with each row.
  • J: two values meet the two rows, one value each, and three values are a length error. To put three values on each row, J uses rank: rank 1 means each row.
  • BQN follows the same rule as J, and its rank modifier works the same way.
  • K: a matrix is a list of rows, so two values meet the two rows. “Each right” puts three values on each row.
  • NumPy: three values meet each row with nothing to add, and two values are an error. To go down the columns, add an axis of length one: the two values become a column, and that column stretches across the matrix.

That stretching has a cost. A column of three plus a row of three gives a three by three table. If you meant item by item, there is no error; you get an answer of the wrong shape.

Blender cubes: a column of 1 2 3, shape 3 1, plus a row of 1 2 3, shape 1 3, equals a three by three grid of 2 3 4, 3 4 5, 4 5 6, shape 3 3
NumPy's stretch: a column plus a row makes a table, not an error. Click to enlarge.

The same table in X_eTaL, step by step

The episode's slide: the transposed reshape 1 1 1, 2 2 2, 3 3 3 plus the reshape 1 2 3 three times equals 2 3 4, 3 4 5, 4 5 6; below it, the same with table
From the video: stretch each one, then add. Click to enlarge.

In X_eTaL nothing stretches by itself. A column plus a row is an error, because the shapes differ:

(3 1 r̲eshape 1 2 3) + 1 3 r̲eshape 1 2 3
error[shape-mismatch]: shapes differ: 3 1 and 1 3

So we stretch each one ourselves. Reshape repeats the row three times, down the rows; reshape and transpose repeat the column across. Now both are three by three, and plus adds them item by item. The result is the same table that NumPy gave:

(o̲\ 3 3 r̲eshape 1 2 3) + 3 3 r̲eshape 1 2 3
┌→────┐
↓2 3 4│
│3 4 5│
│4 5 6│
└~────┘

t̲able does it in one step, and gives the same box: 1 2 3 '+ t̲able 1 2 3.

The trade-off

Each choice has a cost. Automatic rules are short to write, but a wrong shape can still give an answer. A strict rule asks for one more step, and in return a mismatch shows itself.

Coming up, and an exercise

The next episode is about array idioms: thinking without loops. It takes small problems and solves each one twice, first with a loop, then with whole arrays.

Before then, try this in the browser editor. Make a times table for one to nine with t̲able. Then add ten, twenty and thirty to each row of a matrix, and one hundred and two hundred to each column. (To type it, use the ASCII it is drawn from: := for ←, r_eshape for r̲eshape, * for ×, _r for ⍵.)

Closing

One thing I find interesting when studying a language is learning its idioms: the short, familiar phrases experienced programmers reach for without thinking. Each language in this episode has its own idiom for “add this to each row”, and the next episode introduces some more X_eTaL idioms.