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

Monadic operator. Keys: `Alt-f`. Hanzi: `导/梯` (`導/梯`)

`f∂X` gives the Jacobian of `f` at `X`, with the keys, axis names and nesting of `(f X)×⊗X`. For a number result, that's the gradient, in `X`'s structure. `W f∂ X` differentiates `W f X` with respect to `X`. In the examples, [`⊼`](https://bpl.fast.ai//glyphs/wedge-bar.html) squares its argument.

```bpl
⊼∂3                                                  ⍝ 6
+/⊼ ∂ 1 2 3                                         ⍝ 2 4 6
cart←{[r t]←ω ◇ [r×2○t r×1○t]} ◇ -.×cart∂ 2 0.5    ⍝ 2
3 {α×ω×ω}∂ 2                                         ⍝ 12
```

[`∂`](https://bpl.fast.ai//glyphs/partial.html) differentiates through the primitives that `f` calls, inside dfns, trains and operators alike. [`∂`](https://bpl.fast.ai//glyphs/partial.html) gives float derivatives, whatever the kind of `X`. For a record `X`, the gradient is a record. Integers that wrap, at a fixed width or under static kinds, give floats of the width that `÷ₐ` gives them:

```bpl
⊼∂3ₓ                                 ⍝ 6
p←["a":1 "b":2] ◇ ⊢.a×⊢.b ∂ p       ⍝ ["a":2 "b":1]
-∂3ₓ₁                                ⍝ ¯1ₛ
```

For the derivative at each item of a function that works item by item, differentiate at rank 0, or along a unit argument in one pass:

```bpl
x←0 0.5 1
1○∂⁰ x     ⍝ 1 0.8775825619 0.5403023059
{1○x+ω}∂0   ⍝ 1 0.8775825619 0.5403023059
```

`f∂∂` differentiates `f∂`, so nested [`∂`](https://bpl.fast.ai//glyphs/partial.html) gives higher and mixed derivatives:

```bpl
{ω×ω×ω}∂∂∂2     ⍝ 6
{ω₀×ω₁}∂∂ 2 3   ⍝ [0 1◇1 0]
```

`C⌻` differentiates with respect to the point and to the coefficients `C`. `C⌻` is a train that binds `C`, the same as `C↣⌻`.

```bpl
f←[1 2 3]ₓ⌻ ◇ f∂2ₓ   ⍝ 14
{ω⌻2ₓ}∂ [1 2 3]ₓ    ⍝ 1 2 4
```

[`∂`](https://bpl.fast.ai//glyphs/partial.html) runs `f` once. With more than one number in `X`, it records the linear steps of the derivative as `f` runs, then reads the Jacobian from that record: once for each result item, or once for each number in `X`, whichever is fewer. A gradient therefore costs a small multiple of one run of `f`, however many numbers `X` holds. [`∂`](https://bpl.fast.ai//glyphs/partial.html) differentiates a [map](../terms.md#term-map) inside `f` through the map's [batched call](../evaluation.md#functions-and-effects), on whole arrays. So does [`∂`](https://bpl.fast.ai//glyphs/partial.html) inside a map, as in `f∂¹`, with one batched call for all the cells. For a structural copy or a selection, such as a reversal, a transpose or windows, [`∂`](https://bpl.fast.ai//glyphs/partial.html) adds each item's gradient back to the position that the item came from. [`•explain`](../system/explain.md) reports each such accumulation, apart from one that adds a single ascending run of positions as a block, as the gradients of Take and Drop do. The record keeps the values those steps need until [`∂`](https://bpl.fast.ai//glyphs/partial.html) finishes. Its memory grows with the work that `f` does. A value that `f` writes to an outer name keeps no derivative after [`∂`](https://bpl.fast.ai//glyphs/partial.html) returns. [Automatic differentiation](../autodiff.md) explains how [`∂`](https://bpl.fast.ai//glyphs/partial.html) works.

[`∂`](https://bpl.fast.ai//glyphs/partial.html) treats a value that [`•detach`](../system/detach.md) gives as a constant.

Arithmetic, the mathematical glyphs, the circle codes, [`⌈`](https://bpl.fast.ai//glyphs/upstile.html) and [`⌊`](https://bpl.fast.ai//glyphs/downstile.html), structural and selection glyphs, Under, sorting, folds and scans, `+.×`, outer products, [`⌻`](https://bpl.fast.ai//glyphs/quad-jot.html), [`⊥`](https://bpl.fast.ai//glyphs/up-tack.html), monadic [`∨`](https://bpl.fast.ai//glyphs/vee.html) and [`∧`](https://bpl.fast.ai//glyphs/wedge.html) on real numbers, and [`•float`](../system/float.md) all differentiate, and so does any operator whose operands do. `x↦y` differentiates with respect to its start `x`. [`⌹`](https://bpl.fast.ai//glyphs/domino.html) and the determinant `-.×` differentiate at an invertible matrix, and `B⌹A` differentiates with respect to `B` for any matrix `A`. `X!Y` differentiates with respect to `Y` wherever it is defined, and with respect to `X` except at a negative whole `X` or `Y`, where it gives NaN. Comparisons, floor, ceiling, grade, [`ι`](https://bpl.fast.ai//glyphs/iota.html), [`⍸`](https://bpl.fast.ai//glyphs/iota-underbar.html), [`⊤`](https://bpl.fast.ai//glyphs/down-tack.html), [`¿`](https://bpl.fast.ai//glyphs/inverted-question.html) and [`ᵇ`](https://bpl.fast.ai//glyphs/superscript-b.html) have a zero derivative. [`∂`](https://bpl.fast.ai//glyphs/partial.html) treats a count, a position, a circle code and the end of a range as constants, and [`⍕`](https://bpl.fast.ai//glyphs/thorn.html) and `•json⁻` write the value without its derivative. [`∂`](https://bpl.fast.ai//glyphs/partial.html) doesn't differentiate complex numbers.

`f∂←g` attaches `g` as the derivative of the function that `f` names, as [assignment](../assignment.md#inverses-and-derivatives) describes. `g` is a JVP rule: `T g X` gives the derivative of `f` at `X` along the direction `T`, which has `X`'s structure. `g` must be linear in `T`. Calling `f` still runs `f`, and [`∂`](https://bpl.fast.ai//glyphs/partial.html) uses `g` wherever `f` is called with one argument, alone or inside another function. For a gradient, [`∂`](https://bpl.fast.ai//glyphs/partial.html) transposes the steps that `g` takes on `T`. A rule describes a function of one argument, so [`∂`](https://bpl.fast.ai//glyphs/partial.html) through a call of `f` with two arguments raises `DOMAIN`. Bind the left argument first, as in `bound←10↣f ◇ bound∂←g`. An attached rule does not change `f⁻`, which still inverts `f`. [`⇄`](right-left-arrows.md) differentiates with [`∂`](https://bpl.fast.ai//glyphs/partial.html), and uses `g` as [`∂`](https://bpl.fast.ai//glyphs/partial.html) does.

```bpl
r←{ω×ω} ◇ r∂←{α×2×ω}
r 3                                    ⍝ 9
r∂3                                    ⍝ 6
{1+r ω}∂3                              ⍝ 6
s←{+/ω×ω} ◇ s∂←{+/α×2×ω} ◇ s∂ 1 2 3   ⍝ 2 4 6
```

## Errors

- `DOMAIN`: an argument or a result that isn't numeric, a nested result for an array argument, or a call with two arguments of a function that has an attached derivative
- `UNSUPPORTED`: a glyph or system function that [`∂`](https://bpl.fast.ai//glyphs/partial.html) can't differentiate, such as cis, a derivative of [`⌹`](https://bpl.fast.ai//glyphs/domino.html) or `-.×` with respect to a matrix that [`⌹`](domino.md) counts as singular, in a stack or alone, or a complex argument
- `SYNTAX`: a left argument for a function that takes only one argument, such as `C↣⌻`
