v←10 20 30 40 50
v.[[0 ¯1]]
v₋₁10 50
50
BPL has three ways to select. I⌷A takes selectors for leading axes and gives every combination of them. A.[I J] pairs its arrays of positions item by item, as NumPy’s advanced indexing does. P⊃A follows a path into nested arrays. Each section below shows an everyday task and the form that does it. Axis keys covers selecting by key.
Positions count from 0. A negative position counts from the end, as in Python:
Reading at a position outside its axis, or at one that is not a whole number, returns the fill. Writing there does nothing:
Reverse ⌽ reverses the last axis:
: selects one whole axis, and ∞ every axis that the other selectors leave:
¯∞ inserts an unnamed, unkeyed axis of length one without consuming an input axis. It works with both ⌷ and .[], as Python’s None does:
⌷ selects every combination of its positions. Here are rows 1 and 2 with columns 0 and 3:
.[] pairs its arrays instead. With the same two arrays, .[] reads the items of m at row 1, column 0 and at row 2, column 3:
Arrays of positions with different axis names don’t pair. Each keeps its axis, so selecting with them gives a block, as in xarray’s broadcasting by dimension name:
Use .[] to pick one item from each row. k holds a column number for each row of m, as in NumPy’s m[np.arange(3), k]:
To compute a classifier’s loss, read the probability of each sample’s true class. Each column of P holds one sample’s probabilities for three classes. L holds each sample’s true class. The loss is the mean negative log of those probabilities:
0.7 0.8 0.5 0.8
0.3740273068
The same pick works with named axes. labels holds one class number for each sample, along the sample axis b. Because labels and b share that axis name, their positions pair item by item. Pn.[labels b] uses the selectors in the order of Pn’s axes: labels gives the row and b gives the column, as in P.[L ι4ₓ]:
When the same array appears twice, .[] pairs it with itself, which gives a diagonal:
⍋⍠"i" sc gives the row positions that sort each column of sc. gr has both of sc’s axis names. sc.[gr] uses gr to select rows, and pairs gr’s j axis with sc’s columns. Each column of sc is read in sorted order, as NumPy’s take_along_axis does with argsort:
⍸ gives the coordinates where a condition holds. Passing those coordinates to .[] reads the item at each one:
Assigning through the same selector changes those items:
⊃ follows a path into nested arrays, one level for each item of the path. Here is item 2 of item 1:
Index ⌷ also takes arrays of paths. Use ¯∞ to place new axes before the paths or among the selected cells’ axes.
Adding two indices with different axis names builds a table of positions. Reading a vector through that table gives sliding windows:
To take the differences between neighbours, read the vector twice, one position apart, and subtract:
Arithmetic on positions can produce a position outside the axis. At the start, e-1 is ¯1, the position of the last item:
Use •edge to control what happens to positions outside the axis. 4•edge e-1 replaces each position outside an axis of length 4 with NaN. Reading at NaN returns the fill because NaN isn’t a whole number, as described under positions. Use ⌷⍠["fill":v] to read v instead of the fill at those positions. Reading u through these positions shifts it right, with a zero at the start:
•edge "clamp" gives a function that moves each position outside the axis to the nearest end instead. "wrap", "reflect" and "symmetric" are the other modes:
On an axis of length 6, here is where each mode sends the positions from ¯2 to 5:
Any selection can be an assignment target:
To write a block, use ⌷ with a space before ←. The space makes the whole run before it the target:
Modified assignment applies its function once for each time a position appears. Repeated keyed positions work too. The indexing rules describe when a selection drops keys. Counting a histogram takes one line:
Writing at a position that isn’t a whole number, such as NaN, does nothing. Passing bin numbers through •edge first therefore drops the values that fall outside the bins, here 7 and ¯1:
Writing does nothing wherever reading returns a fill. 5↑abc reads two fills past the end of abc. Assigning to 5↑abc changes only the three items of abc:
Reading the first item of an empty string returns a fill. @ can therefore change the first letter of each word, empty words included, with no special case. 1•c converts to uppercase:
f@g applies f to the selection that g makes, then writes the result back:
After a function, .[I] selects from its result. On its own, .[I] is equivalent to ⊢.[I], a selector function that Under can use directly:
⊢.name reads a field in the same way, as {ω.name} does:
A named index on its own selects along the axis with the same name, wherever the index appears in the selector. sales.[mo cy] reads months with mo and cities with cy. The result is sales itself:
$t
•index gives an index along an axis with a fresh name. Selecting with two fresh indices gives a grid. Subtracting q.[ci] from q.[ri] gives the difference between every pair of items in q: