---
title: Operators and axes
description: Combine functions and work along axes
skip_showdoc: true
---
An operator takes one or two values, called operands, and produces a derived function. For example, [`/`](https://bpl.fast.ai//glyphs/slash.html) turns its operand [`+`](https://bpl.fast.ai//glyphs/plus.html) into the summing function `+/`. We'll use operators to choose axes, combine functions and repeat calculations.

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

## Axes and agreement

```bpl
mat←3 2ρι6
mat
```

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

Agreement is how the axes of two arguments line up. BPL uses leading agreement, matching axis 0 with axis 0, axis 1 with axis 1, and so on. Here the three values on the left line up with the three rows of the matrix. Each value is compared with every item in its row.

```bpl
0 3 4=mat
```

```output
$t $f
$f $t
$t $f
```

```bpl
mat←[10 20 30◇40 50 60]
mat
```

```output
10 20 30
40 50 60
```

Plus follows the same agreement rule. Add 1 to the first row and 2 to the second.

```bpl
mat+1 2
```

```output
11 21 31
42 52 62
```

To work along chosen axes, use [`⍠`](https://bpl.fast.ai//glyphs/quad-colon.html). Its left operand is a function and its right operand is an axis number or list of axes. `+⍠1` applies [`+`](https://bpl.fast.ai//glyphs/plus.html) to the rows, along axis 1. Each row is paired with the whole right-hand vector.

```bpl
mat+⍠1[1 2 3]
```

```output
11 22 33
41 52 63
```

Numeric subscripts choose numbered axes: `+₁` means `+⍠1`. An inverse can work along the same chosen axis. This subtracts the fixed vector from each row, undoing the addition above.

```bpl
1 2 3+⁻₁[11 22 33◇41 52 63]
```

```output
10 20 30
40 50 60
```

## Comma functions

The comma functions join arrays and change how their axes are arranged.

### [`,`](https://bpl.fast.ai//glyphs/comma.html) (Comma)

#### monadic [`,`](https://bpl.fast.ai//glyphs/comma.html) (Ravel)

```bpl
cube←2 2 2ρι8
cube
```

```output
0 1
2 3

4 5
6 7
```

Ravel returns the items as a vector.

```bpl
,cube
```

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

Ravel joins only the outer axes into one. Nested items stay nested.

```bpl
,[[1 2] [1 2]]
```

```output
(1 2◇1 2)
```

Ravel loses the original shape. Its inverse accepts a vector and leaves it unchanged.

```bpl
,⁻1 2 3
```

```output
1 2 3
```

#### Ravel along chosen axes

```bpl
text←"ABC"
text
```

```output
"ABC"
```

Ravel with an empty axis list, [`⍬`](https://bpl.fast.ai//glyphs/zilde.html), appends a new axis of length 1. The string becomes a column.

```bpl
,⍠⍬ text
```

```output
A
B
C
```

```bpl
ρtext
```

```output
[3]ₓ
```

The shape changes from `3` to `3 1`.

```bpl
ρ,⍠⍬ text
```

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

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

```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
```

Consecutive axes can be merged. Merging axes 0 and 1 turns the two groups of three rows into six rows.

```bpl
,₀ ₁m
```

```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
ρ,₀ ₁m
```

```output
[6 4]ₓ
```

```bpl
,₁ ₂m
```

```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
ρ,₁ ₂m
```

```output
[2 12]ₓ
```

#### dyadic [`,`](https://bpl.fast.ai//glyphs/comma.html) (Catenate)

Catenate joins arrays.

```bpl
1 2 3,4 5 6
```

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

The join is along the last axis. Catenate repeats a single value to fill the other axes.

```bpl
cube,99
```

```output
0 1 99
2 3 99

4 5 99
6 7 99
```

With a fixed left argument, Catenate's inverse removes those items from the start of the right argument. The right argument must begin with the fixed items.

```bpl
1 2,⁻1 2 3 4
```

```output
3 4
```

#### Catenate along a chosen axis

```bpl
rect←3 2ρι6
rect
```

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

```bpl
ρrect
```

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

Add the item 10 to the end of each row.

```bpl
rect,10
```

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

Choose axis 0 to join along the columns instead. This adds a row. Parentheses show the derived function `,₀` between its two arguments.

```bpl
rect(,₀)10
```

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

To stack arrays along a new axis, put them in rows of a bracket literal.

```bpl
heading←"HEADING"
[heading◇7ρ'-']
```

```output
HEADING
-------
```

### [`⍪`](https://bpl.fast.ai//glyphs/comma-bar.html) (Comma bar)

#### monadic [`⍪`](https://bpl.fast.ai//glyphs/comma-bar.html) (Table)

Table turns a number into a 1×1 matrix and a vector into a column. For higher-rank arrays it keeps the first axis and merges the remaining axes.

```bpl
⍪1
```

```output
1
```

We can make a column from a string.

```bpl
⍪"ABCDE"
```

```output
A
B
C
D
E
```

```bpl
2 3 4ρ"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
```

```output
ABCD
EFGH
IJKL

MNOP
QRST
UVWX
```

The first axis stays length 2. The remaining lengths, 3 and 4, multiply to give 12 columns.

```bpl
ρ⍪2 3 4ρ"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
```

```output
[2 12]ₓ
```

```bpl
⍪2 3 4ρ"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
```

```output
ABCDEFGHIJKL
MNOPQRSTUVWX
```

Table's inverse turns a one-column matrix back into a vector. Other matrices stay unchanged.

```bpl
⍪⁻⍪1 2 3
```

```output
1 2 3
```

#### dyadic [`⍪`](https://bpl.fast.ai//glyphs/comma-bar.html) (Catenate first)

With two arguments, [`⍪`](https://bpl.fast.ai//glyphs/comma-bar.html) joins along the first axis.

```bpl
1 2 3⍪4 5 6
```

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

```bpl
cube⍪99
```

```output
 0  1
 2  3

 4  5
 6  7

99 99
99 99
```

With a fixed left argument, Catenate first's inverse removes those leading rows.

```bpl
1 2⍪⁻[1 2◇3 4]
```

```output
3 4
```

## Operator glyphs

### [`⍨`](https://bpl.fast.ai//glyphs/tilde-diaeresis.html) Self / Commute

#### Swapping arguments

Commute swaps a function's arguments. `3-⍨2` means `2-3`.

```bpl
3-2
```

```output
1
```

```bpl
2-3
```

```output
¯1
```

```bpl
3-⍨2
```

```output
¯1
```

Commute's inverse undoes the swapped call. Keeping 3 fixed on the left, the inverse finds the 2 that gave the result `¯1`.

```bpl
3-⍨⁻¯1
```

```output
2
```

[Replicate](../glyphs/hash.md) takes its mask on the left. With Commute, we can write the data first and then calculate its mask. The mask `≠v` marks the first occurrence of each value.

```bpl
v←22 10 22 22 21 10 5 10
v#⍨≠v
```

```output
22 10 21 5
```

With one argument, Self passes that argument to both sides of the function. `×⍨3` means `3×3`.

```bpl
square←×⍨
square 3
```

```output
9
```

Self has known inverses for addition and multiplication. `+⍨⁻` halves its argument; `×⍨⁻` takes its square root. Taking the minimum or maximum of a number with itself leaves it unchanged, so `⌊⍨⁻` and `⌈⍨⁻` return their argument unchanged.

```bpl
+⍨⁻10
×⍨⁻9
⌈⍨⁻5
```

```output
5
3
5
```

#### A constant function

With a number or array as its operand, [`⍨`](https://bpl.fast.ai//glyphs/tilde-diaeresis.html) makes a function that always returns that value.

```bpl
zero←0⍨
2 zero 5
```

```output
0
```

A constant function's inverse accepts only that constant and returns it.

```bpl
zero⁻0
```

```output
0
```

### [`/`](https://bpl.fast.ai//glyphs/slash.html) Reduce

#### Combining the items

Reduce inserts its function operand between the items along the last axis. With Plus, the list `1 2 3 4 5` becomes `1+2+3+4+5`.

```bpl
a←1 2 3 4 5
+/a
```

```output
15
```

Changing the operand to Times multiplies all the items together.

```bpl
×/a
```

```output
120
```

```bpl
÷/1 2 3
```

```output
1.5
```

Find the largest item.

```bpl
⌈/4 6 2
```

```output
6
```

Find the smallest item.

```bpl
⌊/4 6 2
```

```output
2
```

```bpl
×/1 2 3 4 5
```

```output
120
```

```bpl
!5
```

```output
120
```

#### A seed on the right

A seed is a starting value for a reduction. Supply it as the left argument; it goes at the right end of the inserted expression. `10+/1 2 3` is `1+2+3+10`.

```bpl
10+/1 2 3
```

```output
16
```

For subtraction, read the inserted expression from the right. `10-/1 2 3` is `1-2-3-10`, or `1-(2-(3-10))`.

```bpl
10-/1 2 3
```

```output
¯8
```

#### Empty reductions

```bpl
+/⍬
```

```output
0
```

The sum of no items is 0. An empty product is 1. With a seed, an empty reduction returns the seed.

```bpl
×/⍬
```

```output
1
```

```bpl
10+/⍬
```

```output
10
```

#### Axis

The Axis operator chooses the axis to reduce. Sum down the columns along axis 0.

```bpl
mat←[1 2 3◇4 5 6]
+/⍠0 mat
```

```output
5 7 9
```

Sum across the rows along axis 1.

```bpl
+/⍠1 mat
```

```output
6 15
```

```bpl
+/mat
```

```output
6 15
```

### [`↕`](https://bpl.fast.ai//glyphs/up-down-arrow.html) Windows

Windows makes overlapping groups of consecutive major cells. With a vector and a size of 3, Windows gives a matrix whose rows are the length-3 windows.

```bpl
3↕1 2 3 4
```

```output
1 2 3
2 3 4
```

With no left argument, Windows gives adjacent pairs.

```bpl
↕1 2 3 4
```

```output
1 2
2 3
3 4
```

Reduce each window to get a moving sum.

```bpl
+/3↕1 2 3 4
```

```output
6 9
```

For a moving average, divide those sums by the window size. Commute lets us write the divisor 3 before the sums it divides.

```bpl
3÷⍨+/3↕1 2 3 4
```

```output
2 3
```

Windows' inverse joins overlapping windows back into one array. Their overlapping items must agree. Use the same window size when inverting the dyadic form.

```bpl
↕⁻[1 2◇2 3◇3 4]
3↕⁻[1 2 3◇2 3 4]
```

```output
1 2 3 4
1 2 3 4
```

### [`\\`](https://bpl.fast.ai//glyphs/backslash.html) Scan

#### Running totals

Scan starts with the first item, then combines each result with the next item, working left to right. With Plus, Scan gives running totals.

```bpl
+\1 2 3 4 5
```

```output
1 3 6 10 15
```

```bpl
×\1 2 3 4 5
```

```output
1 2 6 24 120
```

```bpl
÷\1 2 3
```

```output
1 0.5 0.1666666667
```

The last division above is `(1÷2)÷3`. Compare that with Reduce's `1÷(2÷3)`. A scan can also start from a seed.

```bpl
10-\1 2 3
```

```output
9 7 4
```

Scan's inverse recovers the input from its running results. A seeded scan's inverse needs the same seed.

```bpl
+\⁻1 3 6
10-\⁻9 7 4
```

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

### [`⌿`](https://bpl.fast.ai//glyphs/slash-bar.html) Reduce first

Reduce first works along axis 0. Compare the row sums from `+/` with the column sums from `+⌿`.

```bpl
+/mat
```

```output
6 15
```

```bpl
+/₀mat
```

```output
5 7 9
```

```bpl
+⌿mat
```

```output
5 7 9
```

### [`⍀`](https://bpl.fast.ai//glyphs/backslash-bar.html) Scan first

Scan first works down axis 0.

```bpl
+⍀mat
```

```output
1 2 3
5 7 9
```

Scan first's inverse also works down the first axis.

```bpl
+⍀⁻[1 2 3◇5 7 9]
```

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

### [`⍤`](https://bpl.fast.ai//glyphs/jot-diaeresis.html) Rank

#### Choosing cells

A cell consists of an array's last few axes. A row is a rank-1 cell; a matrix is a rank-2 cell. Rank specifies the cells passed to a function. Here `+⌿¹`, the short form of `+⌿⍤1`, sums each row of a cube. Superscripts give up to three cell ranks, separated by spaces: `f⁰ ¹` is `f⍤[0 1]`; `f⁰ ¹ ²` is `f⍤[0 1 2]`. Negative ranks count back from the argument’s rank, so `f⁻¹` means rank ¯1, not inverse. Bare `f⁻` is inverse.

```bpl
cube←2 3 4ρ↦24
cube
+⌿¹ cube
```

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

13 14 15 16
17 18 19 20
21 22 23 24
10 26 42
58 74 90
```

```bpl
⎕←{⎕←ω ◇ +⌿ω}¹ cube
```

```output
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
21 22 23 24
10 26 42
58 74 90
```

The dfn prints each row before summing it. The axes outside the cells form the frame; here the six row sums are assembled into a 2×3 matrix.

Compare this with a column sum on one matrix.

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

```output
15 18 21 24
```

At rank 2, each 3×4 matrix formed by the cube's last two axes is passed to `+⌿`.

```bpl
+⌿² cube
```

```output
15 18 21 24
51 54 57 60
```

With two arguments, we can choose a cell rank for each. Here the left argument supplies individual numbers and the right supplies rows. The dfn prints each pair before adding it.

```bpl
⎕←1 2 3 {⎕←[α ω] ◇ α+ω}⁰ ¹ mat
```

```output
[1 [1 2 3 4]]
[2 [5 6 7 8]]
[3 [9 10 11 12]]
 2  3  4  5
 7  8  9 10
12 13 14 15
```

Inverting a ranked function applies its inverse to the same cells. `-⁻ ⁰` is inverse Negate at rank 0: the space keeps the minus separate from the rank digit.

```bpl
-⁻ ⁰[¯1 ¯2 ¯3]
```

```output
1 2 3
```

### Atop

Two functions in a train make an atop. It applies the function on the right, then applies the function on the left to its result. `f⁝g y` is `f(g y)`. With two arguments, `x(f⁝g)y` is `f(x g y)`.

```bpl
f←*÷
```

```bpl
f 3
```

```output
1.395612425
```

```bpl
2 f 3
```

```output
1.947734041
```

The operator [`∘`](https://bpl.fast.ai//glyphs/jot.html) spells an atop explicitly: `*∘÷` is the same function as `*÷`.

Atop's inverse undoes its two steps in reverse order. Here negating a reciprocal is undone by negating, then taking the reciprocal.

```bpl
(-∘÷)⁻¯0.5
```

```output
2
```

### [`↣`](https://bpl.fast.ai//glyphs/right-tail-arrow.html) Before and [`↢`](https://bpl.fast.ai//glyphs/left-tail-arrow.html) After

#### Binding an argument

Binding fixes one of a function's arguments, leaving the other for a later call. After binds an argument on the right. `*↢2` squares its argument.

```bpl
sqr←*↢2
```

```bpl
sqr 3
```

```output
9
```

Before binds an argument on the left. `2↣*` raises 2 to its argument.

```bpl
pow_2←2↣*
```

```bpl
pow_2 3
```

```output
8
```

Inverses apply to bound functions too. `2↣×⁻` undoes doubling. A numeric superscript immediately after a number applies to that number, not to the bound function: `+↢3⁻¹` adds a third. Bare `+↢3⁻` tries to invert the number and raises `DOMAIN`. Put parentheses around the bound function, or a space before [`⁻`](https://bpl.fast.ai//glyphs/superscript-minus.html), to invert it as a whole.

```bpl
2↣×⁻10
(+↢3)⁻10
+↢3 ⁻ 10
```

```output
5
7
7
```

Binding Catenate's right argument fixes the items at the end. The inverse removes them. Catenate first works the same way along its first axis.

```bpl
,↢3 4 ⁻1 2 3 4
⍪↢3 4 ⁻[1 2◇3 4]
```

```output
1 2
1 2
```

#### Combining functions

With a function on each side, After calls the left function with the argument on its left and the right function's result on its right. `f↢g y` means `y f(g y)`. We can scale a list by dividing it by its maximum.

```bpl
scale←÷↢ ⌈/
```

```bpl
a←3 5 8 2 7
```

```bpl
scale a
```

```output
0.375 0.625 1 0.25 0.875
```

After's dyadic form `x f↢g y` is `x f(g y)`. This negates 3 before adding it to 10.

```bpl
10+↢-3
```

```output
7
```

Before transforms the left argument. With one argument, `f↣g y` means `(f y)g y`.

```bpl
-↣+5
```

```output
0
```

```bpl
10-↣+3
```

```output
¯7
```

Before's dyadic form `x f↣g y` is `(f x)g y`. The last example negates 10 before adding 3.

With a fixed left argument, these two-function combinations also invert. The fixed value stays 10; the inverse recovers the right argument, 3.

```bpl
10(+↢-)⁻7
10(-↣+)⁻¯7
```

```output
3
3
```

`×↢*` multiplies its argument by its exponential, computing `x×*x`.

```bpl
×↢*1
```

```output
2.718281828
```

Unlike most monadic After combinations, `×↢*` has a known inverse, which finds the `x` that gives a requested `x×*x`.

```bpl
×↢* ⁻1
```

```output
0.5671432904
```

Here is the direction of `3j4` again. After divides the original number by its magnitude.

```bpl
÷↢|3j4
```

```output
0.6ⱼ0.8
```

A function can also be displayed without calling it. The display shows the operators and operands from which it was built.

```bpl
÷↢|
```

```output
÷↢|
```

### [`⍥`](https://bpl.fast.ai//glyphs/circle-diaeresis.html) Over

#### Transforming both arguments

Over transforms both arguments before combining them. `x f⍥g y` is `(g x)f(g y)`. With one argument, it works like Atop.

```bpl
f←*⍥÷
```

```bpl
*÷3
```

```output
1.395612425
```

```bpl
f 3
```

```output
1.395612425
```

```bpl
2 f 3
```

```output
0.793700526
```

```bpl
÷2 * ÷3
```

```output
0.793700526
```

Over's inverse undoes the combining function and then the transformation. It works with one argument, or with the same fixed left argument.

```bpl
f⁻ f 3
2 f⁻ 2 f 3
```

```output
3
3
```

Here both arguments are replaced with their factorials before dividing, giving the number of ways to choose and order four items from ten.

```bpl
10÷⍥!6
```

```output
5040
```

```bpl
!10 ÷ !10-4
```

```output
5040
```

### [`⍣`](https://bpl.fast.ai//glyphs/star-diaeresis.html) Power

#### Repeating a function

```bpl
inc←≥
start←0
```

```bpl
inc start
```

```output
1
```

Power applies a function a given number of times. `inc⍣3 start` means `inc inc inc start`. With a count of zero, Power returns the starting value.

```bpl
inc⍣3 start
```

```output
3
```

Write Power's repeat count after [`⍣`](https://bpl.fast.ai//glyphs/star-diaeresis.html): `inc⍣2⁝5` increments twice, giving 7. On a function, numeric superscripts instead select Rank, as in `+/¹mat` for row sums. On a number, they are arithmetic powers: `5²` is 25.

```bpl
inc⍣2⁝5
5²
```

```output
7
25
```

Repeating increment gives addition. With a function as its right operand, Power computes its count once from the original arguments. [`⊣`](https://bpl.fast.ai//glyphs/left-tack.html) returns the left argument. [Unary](../glyphs/superscript-u.md) [`ᵘ`](https://bpl.fast.ai//glyphs/superscript-u.html) makes `inc` ignore that left argument during each step:

```bpl
add←incᵘ⍣⊣
```

```bpl
2 add 3
```

```output
5
```

Repeating addition gives multiplication. In a dyadic Power call, the left argument stays fixed throughout.

```bpl
mult←{α(add⍣ω)0}
```

```bpl
3 mult 4
```

```output
12
```

#### Undoing a function

With a negative count, Power applies the inverse function. Here decrement undoes increment. The separator [`⁝`](https://bpl.fast.ai//glyphs/tricolon.html) (Alt-:) keeps the count and the argument apart without ending the run.

```bpl
dec←inc⍣¯1
```

```bpl
dec 3
```

```output
2
```

```bpl
inc⍣¯3⁝5
```

```output
2
```

Inverting a repeated function negates its repeat count. This undoes three increments.

```bpl
inc⍣3 ⁻10
```

```output
7
```

The superscript [`⁻`](https://bpl.fast.ai//glyphs/superscript-minus.html) is shorthand for `⍣¯1`. Inverting square gives square root.

```bpl
sqr←*↢2
```

```bpl
sqr⁻9
```

```output
3
```

Repeating multiplication gives exponentiation.

```bpl
pow←{α(mult⍣ω)1}
```

```bpl
2 pow 3
```

```output
8
```

#### Iterating to a fixed point

A fixed point is a value that a function leaves unchanged. This function adds 1 to the reciprocal of its argument. Repeating it approaches the golden ratio, about 1.618.

```bpl
f←1+÷
```

```bpl
f 1
```

```output
2
```

```bpl
f 2
```

```output
1.5
```

```bpl
f 1.5
```

```output
1.666666667
```

After 15 steps, the approximation is already close.

```bpl
f⍣15⁝1
```

```output
1.618034448
```

The literal [`∞`](https://bpl.fast.ai//glyphs/infinity.html) means infinity. With [`∞`](https://bpl.fast.ai//glyphs/infinity.html) as its count, Power repeats until successive results match. Float comparisons allow a small relative difference, called the tolerance.

```bpl
f⍣∞⁝1
```

```output
1.618033989
```

#### Keeping the intermediate values

Pass a list of counts to Power to keep the result after each count. For a count of 0, Power gives the starting value.

```bpl
twice←2↣×
twice⍣(ι5)1
```

```output
1 2 4 8 16
```

A function scalar `gˢ` makes Power stop once `g` returns true. The predicate receives the new value as [`α`](https://bpl.fast.ai//glyphs/alpha.html) and the previous value as [`ω`](https://bpl.fast.ai//glyphs/omega.html):

```bpl
inc⍣{α≥5}ˢ0
```

```output
5
```

### [`¨`](https://bpl.fast.ai//glyphs/diaeresis.html) Each

#### Working on nested items

```bpl
a←[[1 2 3 4] [5 6 7]]
a
```

```output
(1 2 3 4◇5 6 7)
```

Each applies its function to each item. Here the items are themselves arrays; `+/¨` sums each of them.

```bpl
+/¨a
```

```output
10 18
```

```bpl
b←[[1 2 3] [4 5 6]]
b
```

```output
(1 2 3◇4 5 6)
```

With two arguments, Each pairs their items. Add 2 to the first vector and 3 to the second.

```bpl
2 3+¨b
```

```output
(3 4 5◇7 8 9)
```

Each's inverse applies the operand's inverse to each item.

```bpl
-¨⁻[[¯1 ¯2] [¯3]]
```

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

## Linear algebra

### [`.`](https://bpl.fast.ai//glyphs/dot.html) Inner product

#### Pair, then reduce

The function to the right of the dot combines paired items. The function to its left reduces those results. A dot product multiplies each pair and adds the products.

`1 2 3+.×4 5 6` is `(1×4)+(2×5)+(3×6)`.

```bpl
1 2 3+.×4 5 6
```

```output
32
```

We can also compare each item with 3, then combine the answers with And.

```bpl
3∧.=3 3 3 3
```

```output
$t
```

For matrices, each row on the left pairs with each column on the right. Their dot products form the matrix product.

```bpl
mat←[1 2◇3 4]
mat+.×mat
```

```output
 7 10
15 22
```

### [`⊗`](https://bpl.fast.ai//glyphs/circle-times.html) Outer product

Outer product combines every item on the left with every item on the right. Here each left item multiplies the whole right vector, giving one row of a multiplication table.

```bpl
1 2 3×⊗4 5 6 7
```

```output
 4  5  6  7
 8 10 12 14
12 15 18 21
```

With one argument fixed, an outer product's inverse recovers the other argument. The matrix `[11 21◇12 22]` contains the sums of each item of `1 2` with each item of `10 20`.

```bpl
1 2+⊗⁻[11 21◇12 22]
```

```output
10 20
```

### [`⌹`](https://bpl.fast.ai//glyphs/domino.html) Matrix inverse / Matrix divide

#### Inverting a matrix

With rational inputs, marked with `ᵣ`, the inverse and linear solve below give rational results. Multiplying either result by the matrix gives exact values, with no floating-point roundoff.

```bpl
mat←[1 2◇3 4]ᵣ
inv←⌹mat
inv
```

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

Multiplying the inverse by the original matrix gives the identity matrix. It has 1s on its main diagonal, from top-left to bottom-right, and 0s elsewhere. Multiplying by it leaves a matrix unchanged.

```bpl
inv+.×mat
```

```output
1ᵣ 0ᵣ
0ᵣ 1ᵣ
```

#### Solving a linear system

A linear system asks which vector `x` gives a known vector `b` when multiplied by a known matrix `a`. Matrix divide `b⌹a` solves it, finding `x` such that `a+.×x` is `b`. Without an exact solution, it gives a least-squares solution, minimizing the sum of squared differences between `a+.×x` and `b`.

```bpl
solution←[5 6]ᵣ⌹mat
solution
```

```output
¯4ᵣ 9ᵣ2
```

```bpl
mat+.×solution
```

```output
5ᵣ 6ᵣ
```

Matrix inverse is its own inverse: `⌹⁻` applies [`⌹`](https://bpl.fast.ai//glyphs/domino.html) again.

```bpl
⌹⁻inv
```

```output
1ᵣ 2ᵣ
3ᵣ 4ᵣ
```

## Custom operators

A derivative measures how quickly a function's result changes near a point. We can approximate it by dividing the difference between two nearby results by the distance between their arguments. Here `d` is that distance, called the step size. A smaller step brings the estimate for square at 3 closer to 6.

```bpl
f←*↢2
```

```bpl
d←0.01
x←3
d÷⍨ f⁝x+d - f⁝x
```

```output
6.01
```

```bpl
d←0.0001
d÷⍨ f⁝x+d - f⁝x
```

```output
6.0001
```

Use [`⍶`](https://bpl.fast.ai//glyphs/alpha-underbar.html) inside a dfn to make an operator. [`⍶`](https://bpl.fast.ai//glyphs/alpha-underbar.html) is the left operand. The derived function takes the point as [`α`](https://bpl.fast.ai//glyphs/alpha.html) and the step size as [`ω`](https://bpl.fast.ai//glyphs/omega.html).

```bpl
grad←{(⍶α+ω - ⍶α)÷ω}
3 f grad 0.01
```

```output
6.01
```

With Commute, we can put the step size first in the division.

```bpl
grad←{ω÷⍨ ⍶α+ω - ⍶α}
3 f grad 0.01
```

```output
6.01
```

A two-operand operator uses [`⍹`](https://bpl.fast.ai//glyphs/omega-underbar.html) for its right operand. This one applies its right operand, then its left, like Atop.

```bpl
compose←{⍶ ⍹ ω}
- compose square 3
```

```output
¯9
```

### Giving a function its inverse

Attach an inverse to any function's name with `f⁻←g`. Here `shift` adds 3, and we supply the function that subtracts 3.

```bpl
shift←3+
shift⁻←¯3+
shift⁻10
```

```output
7
```

### [`∂`](https://bpl.fast.ai//glyphs/partial.html) Derivative

The built-in Derivative operator differentiates the operations inside its operand. It needs no step size.

```bpl
f∂3
```

```output
6
```

For a function that returns one number from a vector, Derivative gives a gradient: a vector with one derivative for each input number.

```bpl
{+/ω×ω}∂1 2 3
```

```output
2 4 6
```
