---
title: Rearranging and transforming arrays
description: Reverse, rotate and transpose arrays; work with windows, selections and
  choices
skip_showdoc: true
---
Read [Searching and indexing](searching-indexing.md) first.

<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

## Rotate / transpose / flip

### [`⌽`](https://bpl.fast.ai//glyphs/circle-stile.html) (Circle Stile)

#### monadic [`⌽`](https://bpl.fast.ai//glyphs/circle-stile.html) (Reverse)

Reverse puts items in the opposite order along the last axis. On a matrix, it reverses each row:

```bpl
⌽"trams"
```

```output
"smart"
```

```bpl
mat←[0 1 2 3◇4 5 6 7◇8 9 10 11]
mat
```

```output
0 1  2  3
4 5  6  7
8 9 10 11
```

```bpl
⌽mat
```

```output
 3  2 1 0
 7  6 5 4
11 10 9 8
```

Reverse undoes itself: its inverse is Reverse again.

```bpl
⌽⁻"smart"
```

```output
"trams"
```

#### dyadic [`⌽`](https://bpl.fast.ai//glyphs/circle-stile.html) (Rotate)

Rotate turns left for a positive count and right for a negative count. Items that leave one end return at the other.

```bpl
1⌽"HatStand"
```

```output
"atStandH"
```

```bpl
3⌽"HatStand"
```

```output
"StandHat"
```

```bpl
¯2⌽1 2 3 4 5 6
```

```output
5 6 1 2 3 4
```

```bpl
mat
```

```output
0 1  2  3
4 5  6  7
8 9 10 11
```

```bpl
¯1⌽mat
```

```output
 3 0 1  2
 7 4 5  6
11 8 9 10
```

With a vector of counts, Rotate can turn each row by a different amount:

```bpl
1 ¯1 2⌽mat
```

```output
 1  2 3 0
 7  4 5 6
10 11 8 9
```

The inverse of Rotate turns in the opposite direction, using the same count:

```bpl
1⌽⁻"atStandH"
```

```output
"HatStand"
```

### `⌽₀` (First-axis reverse and rotate)

#### monadic `⌽₀` (Reverse First)

The subscript selects an axis: `⌽₀` acts along the first, while `⌽₋₁` acts along the last, as plain [`⌽`](https://bpl.fast.ai//glyphs/circle-stile.html) does. A vector has only one axis, so these give the same result there. On a matrix, `⌽₀` reverses or rotates the rows.

```bpl
⌽₀"trams"
```

```output
"smart"
```

```bpl
⌽₀mat
```

```output
8 9 10 11
4 5  6  7
0 1  2  3
```

Reverse First also undoes itself:

```bpl
⌽₀⁻⌽₀mat
```

```output
0 1  2  3
4 5  6  7
8 9 10 11
```

#### dyadic `⌽₀` (Rotate First)

```bpl
3⌽₀"HatStand"
```

```output
"StandHat"
```

```bpl
¯1⌽₀mat
```

```output
8 9 10 11
0 1  2  3
4 5  6  7
```

The inverse of Rotate First reverses the rotation, using the same count:

```bpl
¯1⌽₀⁻¯1⌽₀mat
```

```output
0 1  2  3
4 5  6  7
8 9 10 11
```

### [`⍉`](https://bpl.fast.ai//glyphs/circle-backslash.html) (Circle Backslash)

#### monadic [`⍉`](https://bpl.fast.ai//glyphs/circle-backslash.html) (Transpose)

Transpose reverses the order of the axes. A matrix's rows become its columns:

```bpl
⍉mat
```

```output
0 4  8
1 5  9
2 6 10
3 7 11
```

A `2 3 4` array becomes a `4 3 2` array:

```bpl
cube←2 3 4ρι24
cube
```

```output
 0  1  2  3
 4  5  6  7
 8  9 10 11

12 13 14 15
16 17 18 19
20 21 22 23
```

```bpl
⍉cube
```

```output
 0 12
 4 16
 8 20

 1 13
 5 17
 9 21

 2 14
 6 18
10 22

 3 15
 7 19
11 23
```

```bpl
ρ⍉cube
```

```output
[4 3 2]ₓ
```

Transpose undoes itself too:

```bpl
⍉⁻⍉mat
```

```output
0 1  2  3
4 5  6  7
8 9 10 11
```

Write the superscript `ᵀ` after an array to apply monadic Transpose. It is for arrays, not functions. Like the digit [superscripts](../scripts.md#superscripts), it is applied before functions. After a space it applies to the whole run on its left:

```bpl
matᵀ
```

```output
0 4  8
1 5  9
2 6 10
3 7 11
```

Without a space, Index reads row 1 of the transpose. With a space before `ᵀ`, it first reads row 1, then transposes that vector:

```bpl
1⌷matᵀ
```

```output
1 5 9
```

```bpl
1⌷mat ᵀ
```

```output
4 5 6 7
```

#### dyadic [`⍉`](https://bpl.fast.ai//glyphs/circle-backslash.html) (Transpose)

Each item on the left gives the destination of the corresponding input axis. With `1 0`, Transpose sends the rows to axis 1 and the columns to axis 0:

```bpl
1 0⍉mat
```

```output
0 4  8
1 5  9
2 6 10
3 7 11
```

A diagonal holds the items at matching positions along two axes: row 0 with column 0, row 1 with column 1, and so on. Sending two axes to the same destination selects their diagonal. The diagonal is as long as the shorter axis:

```bpl
0 0⍉mat
```

```output
0 5 10
```

On a rank-3 array, Transpose with `1 0 2` swaps the first two axes. Repeated destinations again select diagonals:

```bpl
ρcube
```

```output
[2 3 4]ₓ
```

```bpl
ρ1 0 2⍉cube
```

```output
[3 2 4]ₓ
```

```bpl
0 0 0⍉cube
```

```output
0 17
```

```bpl
1 0 0⍉cube
```

```output
 0 12
 5 17
10 22
```

Inverting a diagonal with [`⁻`](https://bpl.fast.ai//glyphs/superscript-minus.html) raises a DOMAIN error. When each axis has a different destination, [`⁻`](https://bpl.fast.ai//glyphs/superscript-minus.html) restores the original axis order:

```bpl
2 0 1⍉⁻2 0 1⍉cube ≡ cube
```

```output
$t
```

#### Splitting, naming and merging axes

A nested item in the left argument splits one input axis into several axes. Here the last axis of length 8 becomes two axes of lengths 2 and 4, named `"group"` and `"offset"`. Each pair in the nested item gives a destination axis and a length. Use [`∞`](https://bpl.fast.ai//glyphs/infinity.html) for the length needed to keep the number of items unchanged. The key on the group, `"packed"`, names the axis when it is merged back:

```bpl
packed←["batch":2 "row":3 "packed":8]ρι48
split←["batch":0 "row":2 "packed":["group":1 2;"offset":3 ∞]]⍉
ρsplit packed
```

```output
["batch":2 "group":2 "row":3 "offset":4]ₓ
```

The inverse form, `⍉⁻`, lists input axes in output order. A nested group merges its axes into one. This puts the group and offset axes together again:

```bpl
merged←["batch" "row" "packed":["group" "offset"]]⍉⁻split packed
merged≡packed
```

```output
$t
```

`split` has numeric destinations, so `split⁻` merges the split axes back into their original axis:

```bpl
split⁻split⁝packed ≡ packed
```

```output
$t
```

A `⍉⁻` pattern that uses names does not record the previous axis order, so it cannot be inverted with [`⁻`](https://bpl.fast.ai//glyphs/superscript-minus.html).

## Other operators

### [`⌺`](https://bpl.fast.ai//glyphs/quad-diamond.html) (Quad Diamond)

#### Dyadic operator [`⌺`](https://bpl.fast.ai//glyphs/quad-diamond.html) (Stencil)

A window is a section of an array around one position. Stencil applies its left operand to each window, with that position at the centre. Its right operand gives the window's size. At the edges, Stencil replaces missing items with fill, which is zero for these numeric arrays. Enclosing each window shows what the operand receives:

```bpl
⊂⌺3[1 2 3 4]
```

```output
(0 1 2◇1 2 3◇2 3 4◇3 4 0)
```

Replace enclosure with summation to add the neighbours:

```bpl
+/⌺3[1 2 3 4]
```

```output
3 6 9 7
```

With two sizes, Stencil uses a rectangular window. Here we sum each three-by-three window:

```bpl
{+/,ω}⌺[3 3] [1 2 3◇4 5 6◇7 8 9]
```

```output
12 21 16
27 45 33
24 39 28
```

In a two-row specification, the first row gives the window sizes and the second gives the movements, the distances between window centres. This uses three-item windows centred two positions apart:

```bpl
+/⌺[[3]◇[2]]ι8
```

```output
1 6 12 18
```

### [`⊘`](https://bpl.fast.ai//glyphs/circle-slash.html) (Valences)

A function's valence is the number of arguments it takes. Valences supplies separate functions for calls with one and two arguments. This function negates with one argument and adds with two:

```bpl
f←-⊘+
f3
```

```output
¯3
```

```bpl
10 f 3
```

```output
13
```

The inverse selects a function by the number of arguments, then applies that function's inverse. Here it negates with one argument and subtracts the fixed left argument with two:

```bpl
f⁻¯3
```

```output
3
```

```bpl
10 f⁻ 13
```

```output
3
```

### [`@`](https://bpl.fast.ai//glyphs/at.html) (Under)

`f@g` applies `f` to the selection that `g` makes, then writes the result back to the selected positions. The other items stay unchanged. Under selects along the first axis when its right operand is a list of positions. A value as the left operand replaces the selected items:

```bpl
0@[1 3] ι6
```

```output
0 0 2 0 4 5
```

```bpl
[30 10]@[1 3] ι6
```

```output
0 30 2 10 4 5
```

`÷ᵣ` gives rational reciprocals of machine integers, whereas [`÷`](https://bpl.fast.ai//glyphs/obelus.html) would floor them. Positions 1 and 3 hold 2 and 4, whose reciprocals are `1ᵣ2` and `1ᵣ4`:

```bpl
÷ᵣ@[1 3] ↦6ₓ
```

```output
1ₓ 1ᵣ2 3ₓ 1ᵣ4 5ₓ 6ₓ
```

```bpl
10 ×@[1 3] ι5
```

```output
0 10 2 30 4
```

A function inside a one-item list selects where its result is true. Here `2|` marks the odd numbers:

```bpl
0@[2|] ι6
```

```output
0 0 2 0 4 0
```

```bpl
⌽@[2|] ι6
```

```output
0 5 2 3 4 1
```

A selecting function can also be Under's right operand. Drop skips the first item; Under changes only the rest:

```bpl
-@ 1↓ 1 2 3
```

```output
1 ¯2 ¯3
```

With an invertible transformation as its right operand, Under transforms, applies its left operand, then reverses the transformation. To round down to tenths, multiply by ten, floor, then divide by ten:

```bpl
⌊@ 10× 1.25 2.78
```

```output
1.2 2.7
```

`.[I]` on its own is a selector function, which Under can take as its right operand. Here the selected positions are multiplied by ten. With a field selector such as `⊢.qty`, Under changes just that field:

```bpl
v←10 20 30 40
10× @.[[1 3]] v
```

```output
10 200 30 400
```

```bpl
order←["price":10 "qty":2]
≥@ ⊢.qty order
```

```output
["price":10 "qty":3]
```

Under's inverse keeps the selection and applies the inverse of its left operand. This decrements the selected items:

```bpl
≥@[1 3] ⁻10 20 30 40
```

```output
10 19 30 39
```

### [`⍚`](https://bpl.fast.ai//glyphs/diamond-underbar.html) (Agenda)

Agenda chooses a function from its right operand, a list of cases. Its left operand gives the position of the case to run, or a function that computes that position. Agenda runs case 0 for a false Boolean and case 1 for a true one:

```bpl
abs←0≤ ⍚[- ⊢]
abs¯3
```

```output
3
```

```bpl
abs3
```

```output
3
```

With a vector of choices, Agenda picks one case for each cell. Here negative numbers become zero, while nonnegative numbers are replaced with their square roots:

```bpl
0≤ ⍚[0 √] ¯4 9 16
```

```output
0 3 4
```

## Execute and Format

### [`⍎`](https://bpl.fast.ai//glyphs/hydrant.html) (Hydrant)

#### Monadic [`⍎`](https://bpl.fast.ai//glyphs/hydrant.html) (Execute expression)

Execute reads a string as code and returns its last result. The string can refer to names already defined here.

```bpl
⍎"1+1"
```

```output
2
```

```bpl
v←1 2 3
⍎"v"
```

```output
1 2 3
```

The results of intermediate statements are still displayed, even when the last result is assigned. This prints 2 and stores 4 in `answer`:

```bpl
answer←⍎"1+1 ◇ 2+2"
```

```output
2
```

```bpl
answer
```

```output
4
```

Execute's inverse writes a value as BPL source text that Execute can read back. The source text keeps the `ₓ` that marks exact integers:

```bpl
source←⍎⁻[1 2 3]ₓ
[source]
```

```output
["[1 2 3]ₓ"]
```

```bpl
⍎source
```

```output
[1 2 3]ₓ
```

#### dyadic [`⍎`](https://bpl.fast.ai//glyphs/hydrant.html) (Execute in)

A keyed left argument supplies names for the expression:

```bpl
["price":10.5 "qty":2]⍎"price×qty"
```

```output
21
```

### [`⍕`](https://bpl.fast.ai//glyphs/thorn.html) (Thorn)

#### Monadic [`⍕`](https://bpl.fast.ai//glyphs/thorn.html) (Format)

Format turns values into text. The numeric vector and its character representation are displayed alike:

```bpl
4 5 6
```

```output
4 5 6
```

```bpl
⍕4 5 6
```

```output
"4 5 6"
```

The vector has three numbers; the text has five characters, including its two spaces:

```bpl
[≢4 5 6;≢⍕4 5 6]
```

```output
[3 5]ₓ
```

With a single number on the left, Format uses that many decimal places, the digits after the decimal point:

```bpl
2⍕3.125 0.002
```

```output
"3.13 0.00"
```

#### Dyadic [`⍕`](https://bpl.fast.ai//glyphs/thorn.html) (Format By Specification)

A two-item vector gives the field width and decimal places. The width is the number of characters for each number, padded with spaces:

```bpl
6 2⍕3.125 0.002
```

```output
"  3.13  0.00"
```

[Indexing](../indexing.md) covers more selections, and each entry in the [glyph reference](../glyphs.md) links to its full definitions.

[Previous: Searching and indexing](searching-indexing.md) · [Study guide](../glyphs-study.md) · [Next: Numerical tools](numerical-tools.md)
