diff options
| author | Marshall Lochbaum <mwlochbaum@gmail.com> | 2022-06-25 22:20:19 -0400 |
|---|---|---|
| committer | Marshall Lochbaum <mwlochbaum@gmail.com> | 2022-06-25 22:20:24 -0400 |
| commit | c618ade174cc2b4e428457751ad8dd01130c2239 (patch) | |
| tree | 2c0840b92204d77ec982a6cf7cb6a1e4f738545f /doc | |
| parent | e62c37d34604f6a2293e981d2fd986729e70d2c9 (diff) | |
Back to editing docs
Diffstat (limited to 'doc')
| -rw-r--r-- | doc/under.md | 12 | ||||
| -rw-r--r-- | doc/undo.md | 8 | ||||
| -rw-r--r-- | doc/windows.md | 63 |
3 files changed, 42 insertions, 41 deletions
diff --git a/doc/under.md b/doc/under.md index 78e09ea1..60935da4 100644 --- a/doc/under.md +++ b/doc/under.md @@ -60,15 +60,15 @@ A *structural function* is one that moves elements around without performing com 1⊸⌽⌾(⊏˘) a -When used with Under, the function `1⊸⌽` applies to the first column, rotating it. The result of `𝔽` needs to be compatible with the selection function, so Rotate works but trying to remove an element is no good: +When used with Under, the function `1⊸⌽` applies to the first column, [rotating](reverse.md#rotate) it. The result of `𝔽` needs to be compatible with the selection function, so Rotate works but trying to drop an element is no good: 1⊸↓⌾(⊏˘) a -BQN can detect lots of structural functions when written in [tacit](tacit.md) form; see the list of functions [in the spec](../spec/inferred.md#required-structural-inverses). You can also include computations on the shape. For example, here's a function to reverse the first half of a list. +BQN can detect lots of structural functions when written [tacitly](tacit.md); see the list of recognized forms [in the spec](../spec/inferred.md#required-structural-inverses). You can also include computations on the shape. For example, here's a function to reverse the first half of a list. ⌽⌾(⊢↑˜≠÷2˙) "abcdef" -But you can't use a computation that uses array values, such as `10⊸+⌾((<⊸5)⊸/)` to add 10 to each element below 5. This is because Under can change the array values, so that the function `𝔾` doesn't select the same elements before and after applying it (at the same time, Under can't change array structure, or at least not the parts that matter to `𝔾`). To use a dynamic selection function, compute the mask or indices based on a copy of the argument and use those as part of `𝔾`. +But you can't use a computation that uses array values, such as `10⊸+⌾((<⊸5)⊸/)` to add 10 to each element below 5. This is because Under can change the array values, so that the function `𝔾` doesn't select the same elements before and after applying it (contrarily, Under can't change array structure, or at least not the parts that matter to `𝔾`). To use a dynamic selection function, compute the mask or indices based on a copy of the argument and use those as part of `𝔾`. {10⊸+⌾((𝕩<5)⊸/)𝕩} 3‿8‿2‿2‿6 @@ -92,17 +92,17 @@ Under is the idiomatic way to do a round-to-nearest function: ⌊⌾(10⊸×) 3.524‿6.799‿2.031 -See how it works? `⌊` rounds down to an integer, but we can get it to round down to a decimal by first multiplying by 10 (single decimals are now integers), then rounding, then undoing that multiplication. A related idea is to not just round but produce a range. Suppose I want the arithmetic progression 4, 7, 10, ... <20. If I had the right range `↕n`, then it would be `4+3×↕n`, or `(4+3×⊢)↕n`. By using the *inverse* of this transformation function on the desired endpoint, I can make sure it's applied on the way out, and BQN figures out what to do on the way in as if by magic. +See how it works? `⌊` rounds down to an integer, but we can get it to round down to a decimal by first multiplying by 10 (so that single decimals become integers), then rounding, then undoing that multiplication. A related idea is to not just round but produce a range. Suppose I want the arithmetic progression 4, 7, 10, ... <20. If I had the right range `↕n`, then it would be `4+3×↕n`, or `(4+3×⊢)↕n`. By using the *inverse* of this transformation function on the desired endpoint, I can make sure it's applied on the way out, and BQN figures out what to do on the way in as if by magic. ↕∘⌈⌾((4+3×⊢)⁼) 20 -Well, really it's a bit of simple algebra, but if it wants to wear a pointy hat and wave a wand around I won't judge. +Well, really it's some simple algebra, but if it wants to wear a pointy hat and wave a wand around I won't judge. ## Left argument When called dyadically, Under applies `𝔽` dyadically, like [Over](compose.md#over). This doesn't affect the undoing part of Under, which still tries to put the result of `𝔽` back into `𝕩` for structural Under or invert `𝔾` for computational. In fact, `𝕨 𝔽⌾𝔾 𝕩` is equivalent to `(𝔾𝕨)˙⊸𝔽⌾𝔾 𝕩` so no exciting language stuff is happening here at all. -But you can still do some cool stuff with it! One pattern is simply to set `𝔽` to `⊣`, the [identity](identity.md) function that just returns its left argument. Now structural Under will replace everything that `𝔾` selects from `𝕩` with the corresponding values in `𝕨`. Here's an example that replaces elements with indices `1` and `2`. +But you can still do cool things with it! One pattern is simply to set `𝔽` to `⊣`, the [identity](identity.md) function that just returns its left argument. Now structural Under will replace everything that `𝔾` selects from `𝕩` with the corresponding values in `𝕨`. Here's an example that replaces elements with indices `1` and `2`. "abcd" ⊣⌾(1‿2⊸⊏) "0123" diff --git a/doc/undo.md b/doc/undo.md index 4afa2c00..fe27fa37 100644 --- a/doc/undo.md +++ b/doc/undo.md @@ -18,7 +18,7 @@ Here it undoes a function to decrement the last character by incrementing that c ## The rules -If `𝔽` can be inverted exactly, then Undo just does that. However, there are also some other functions that BQN inverts. For example, the squaring function `ט` has both a positive and a negative inverse, and yet: +If `𝔽` can be inverted exactly, then Undo just does that (or tries). However, there are also some other functions that BQN inverts. For example, the squaring function `ט` has both a positive and a negative inverse, and yet: ט ¯3 ט⁼ ט ¯3 # It's not the same! @@ -40,6 +40,7 @@ A few notable inverses are the [logarithm](arithmetic.md#basic-arithmetic) `⋆ Structural functions like [Take](take.md) and [shifts](shift.md) that remove elements from `𝕩` can't be inverted, because given the result there's no way to know what the elements should be. However, there are two special cases that have inverses defined despite losing data: these are `⊣⁼` and `k⁼` where `k` is a constant (a data type, or `k˙`). For these, `𝕩` is required to [match](match.md) the always returned value `𝕨` or `k`, and this value is also used for the result—even though any result would be valid, as these functions ignore `𝕩`. 3 ⊣⁼ 4 + 3 ⊣⁼ 3 ## Undo headers @@ -51,4 +52,7 @@ Of course BQN will never be able to invert all the functions you could write (if 𝕊⁼𝕩: 𝕩÷1-𝕩 } -The above function could also be defined with the automatically invertible `1⊸+⌾÷`, but maybe there's a numerical reason to use the definition above. Like a normal header, an undo header reflects the normal use, but it includes `⁼` and possibly `˜` addition to the function and arguments. +The above function could also be defined with the automatically invertible `1⊸+⌾÷`, but maybe there's a numerical reason to use the definition above. Like a normal header, an undo header reflects the normal use, but it includes `⁼` and possibly `˜` addition to the function and arguments. Any header that includes a function can have these modifiers added—even a fancy modifier header like `𝔽_m1⁼a‿b:` or plain label like `𝕊⁼:`. The three forms are `𝕊⁼𝕩:`, `𝕨𝕊⁼𝕩:`, and `𝕨𝕊˜⁼𝕩:`, and the two dyadic forms are also used to derive `k⊸F⁼𝕩` and `F⟜k⁼𝕩` with a constant `k`. + + G ← { 𝕊˜⁼:𝕩-2×𝕨 ; 𝕨+2×𝕩 } + G⟜2⁼ 7 diff --git a/doc/windows.md b/doc/windows.md index 379bf644..6c009143 100644 --- a/doc/windows.md +++ b/doc/windows.md @@ -2,75 +2,72 @@ # Windows -In BQN, it's strongly preferred to use functions, and not modifiers, for array manipulation. Functions are simpler as they have fewer moving parts. They are more concrete, since the array results can always be viewed right away. They are easier to implement with reasonable performance as well, since there is no need to recognize many possible function operands as special cases. +The Windows function returns all slices, or contiguous subarrays, with shape (well, shape prefix) `𝕨` from `𝕩`. It might also be seen as sliding a moving window along `𝕩`. -The Window function replaces APL's Windowed Reduction, J's more general Infix operator, and Dyalog's Stencil, which is adapted from one case of J's Cut operator. +This function replaces APL's Windowed Reduction, J's more general Infix operator, and Dyalog APL's Stencil, which is adapted from one case of J's Cut operator. In BQN, it's strongly preferred to use functions, and not modifiers, for array manipulation. Functions are simpler with fewer moving parts, and more concrete, since the array results can always be viewed right away. -## Definition +## Basic case -We'll start with the one-axis case. Here Window's left argument is a number between `0` and `1+≠𝕩`. The result is composed of slices of `𝕩` (contiguous sections of major cells) with length `𝕨`, starting at each possible index in order. +We'll start with the one-axis case. Here `𝕨` is a number between `0` and `1+≠𝕩`. The result is composed of slices of `𝕩` (contiguous sections of [major cells](array.md#cells)) with length `𝕨`, starting at each possible index in order. 5↕"abcdefg" There are `1+(≠𝕩)-𝕨`, or `(≠𝕩)¬𝕨`, of these sections, because the starting index must be at least `0` and at most `(≠𝕩)-𝕨`. Another way to find this result is to look at the number of cells in or before a given slice: there are always `𝕨` in the slice and there are only `≠𝕩` in total, so the number of slices is the range [spanned](logic.md) by these two endpoints. -You can take a slice of an array `𝕩` that has length `l` and starts at index `i` using [Take](take.md) with Drop or [Rotate](reverse.md#rotate): `l↑i↓𝕩` or `l↑i⌽𝕩`. The [Prefixes](prefixes.md) function returns all the slices that end at the end of the array (`(≠𝕩)=i+l`), and Suffixes gives the slices that start at the beginning (`i=0`). Windows gives yet another collection of slices: the ones that have a fixed length `l=𝕨`. Selecting one cell from its result gives you the slice starting at that cell's index: +A single slice of an array `𝕩` with length `l` and starting index `i` is `l↑i↓𝕩`, using [Take and Drop](take.md). The [Prefixes](prefixes.md) function returns all the slices that end at the end of the array (`(≠𝕩)=i+l`), and Suffixes gives the slices that start at the beginning (`i=0`). Windows gives yet another collection of slices: the ones that have a fixed length `l=𝕨`. Selecting one cell from its result gives the slice starting at that cell's index: 2⊏5↕"abcdefg" - 5↑2↓"abcdefg" - -Windows differs from Prefixes and Suffixes in that it doesn't add a layer of nesting (it doesn't enclose each slice). This is possible because the slices have a fixed size. -### Multiple dimensions + 5↑2↓"abcdefg" -The above description applies to a higher-rank right argument. As an example, we'll look at two-row slices of a shape `3‿4` array. For convenience, we will enclose each slice. Note that slices always have the same rank as the argument array. +Windows differs from Prefixes and Suffixes in that it doesn't add a layer of nesting (it doesn't enclose each slice). This is possible because the slices have a fixed size, so they fit together as cells of an array. - <⎉2 2↕"0123"∾"abcd"≍"ABCD" +## Windowed reduction -Passing a list as the left argument to Windows takes slices along any number of leading axes. Here are all the shape `2‿2` slices: +Windows can be followed up with [Insert](fold.md#insert) on each slice to give a windowed reduction or fold. Here we take running sums of 3 values. - <⎉2 2‿2↕"0123"∾"abcd"≍"ABCD" + +˝˘3↕ ⟨2,6,0,1,4,3⟩ -The slices are naturally arranged along multiple dimensions according to their starting index. Once again the equivalence `i⊏l↕x` ←→ `l↑i↓x` holds, provided `i` and `l` have the same length. +A common task is to act on windows with an initial or final element so the total length stays the same. When using windows of length 2, the best way to accomplish this is with a [shift](shift.md) `«` or `»`. If the window length is longer or variable, then a trick with Windows works better: add the elements, and then use windows matching the original length. Here we invert Plus [Scan](scan.md) `` +` ``, which requires we take pairwise differences starting at initial value 0. -If `𝕨` has length `0`, then `𝕩` is not sliced along any dimensions. The only slice that results—the entire argument—is then arranged along an additional zero dimensions. In the end, the result is `𝕩`, unchanged. + -⟜(0»⊢) +` 3‿2‿1‿1 -### More formally + (-˜˝≠↕0∾⊢) +` 3‿2‿1‿1 -`𝕩` is an array. `𝕨` is a number, or numeric list or unit, with `𝕨≤○≠≢𝕩`. The result `z` has shape `𝕨∾¬⟜𝕨⌾((≠𝕨)⊸↑)≢𝕩`, and element `i⊑z` is `𝕩⊑˜(≠𝕨)(↑+⌾((≠𝕨)⊸↑)↓)i`. +With Windows, we can modify the 3-element running sum from before to keep the length constant by starting with two zeros. -Using [Group](group.md) we could also write `i⊑z` ←→ `𝕩⊑˜(𝕨∾○(↕∘≠)≢𝕩) +´¨∘⊔ i`. + (+˝≠↕(2⥊0)⊸∾) ⟨2,6,0,1,4,3⟩ ## Symmetry -Let's look at an earlier example, along with its [Transpose](transpose.md) (`⍉`). +Let's look at the first example, paired with its [Transpose](transpose.md) (`⍉`). - {⟨𝕩,⍉𝕩⟩}5↕"abcdefg" + ⋈⟜⍉ 5↕"abcdefg" -Although the two arrays have different shapes, they are identical where they overlap. +Although the two arrays have different shapes, they're identical in the 3×3 region where they overlap. - ≡○(3‿3⊸↑)⟜⍉5↕"abcdefg" + ≡○(3‿3⊸↑)⟜⍉ 5↕"abcdefg" -In other words, the i'th element of slice j is the same as the j'th element of slice i: it is the `i+j`'th element of the argument. So transposing still gives a possible result of Windows, but with a different slice length. +More concretely, the `i`th element of slice `j` is the same as the `j`th element of slice `i`: it's the `i+j`th element of the argument. So transposing still gives a possible result of Windows, but with a different slice length. The two lengths are related by [Span](logic.md), which converts between length and number of slices. {(5↕𝕩)≡⍉(3↕𝕩)}"abcdefg" -In general, we need a more complicated transpose—swapping the first set of `≠𝕨` axes with the second set. Note again the use of [Span](logic.md), our slice-length to slice-number converter. + (≠"abcdefg") ¬ 3 - {((5‿6¬2‿2)↕𝕩) ≡ 2‿3⍉(2‿2↕𝕩)} ↕5‿6‿7 +## Multiple dimensions -## Applications +The right argument can have rank more than 1, and it's viewed as a list of major cells following [leading axis](leading.md) principles. As an example, Windows can take two-row slices of a shape `3‿4` array. -Windows can be followed up with a [reduction](fold.md#insert) on each slice to give a windowed reduction. Here we take running sums of 3 values. + 2↕["0123","abcd","ABCD"] - +˝˘3↕ ⟨2,6,0,1,4,3⟩ + <⎉2 2↕["0123","abcd","ABCD"] -A common task is to act on windows with an initial or final element so the total length stays the same. When using windows of length 2, the best way to accomplish this is with a [shift](shift.md) `«` or `»`. If the window length is longer or variable, then a trick with Windows works better: add the elements, and then use windows matching the original length. Here we invert Plus [Scan](scan.md) `` +` ``, which requires we take pairwise differences starting at initial value 0. +In the second version we've enclosed each slice with `<⎉2` for viewing—a slice has rank 2, the same as `𝕩`. Passing a list as the left argument to Windows takes slices along any number of leading axes. Here are all the shape `2‿2` slices: - -⟜(0»⊢) +` 3‿2‿1‿1 + <⎉2 2‿2↕["0123","abcd","ABCD"] - (-˜˝≠↕0∾⊢) +` 3‿2‿1‿1 +The slices are naturally arranged along multiple dimensions according to their starting index. Once again the equivalence `i⊏l↕x` ←→ `l↑i↓x` holds, provided `i` and `l` have the same length. -With Windows, we can modify the 3-element running sum from before to keep the length constant by starting with two zeros. +If `𝕨` has length `0`, then `𝕩` is not sliced along any dimensions. The only slice that results—the entire argument—is then arranged along an additional zero dimensions. In the end, the result is `𝕩`, unchanged. - (+˝≠↕(2⥊0)⊸∾) ⟨2,6,0,1,4,3⟩ +Here's a more formal definition: `𝕩` is an array. `𝕨` is a number, or numeric list or unit, with `𝕨≤○≠≢𝕩`. The result `z` has shape `𝕨∾¬⟜𝕨⌾((≠𝕨)⊸↑)≢𝕩`, and element `i⊑z` is `i⊑z` ←→ `𝕩⊑˜+´¨(𝕨∾○(↕∘≠)≢𝕩)⊔i`. |
