diff options
Diffstat (limited to 'doc')
| -rw-r--r-- | doc/couple.md | 2 | ||||
| -rw-r--r-- | doc/indices.md | 2 | ||||
| -rw-r--r-- | doc/leading.md | 57 |
3 files changed, 42 insertions, 19 deletions
diff --git a/doc/couple.md b/doc/couple.md index 51309b18..e62e581c 100644 --- a/doc/couple.md +++ b/doc/couple.md @@ -24,7 +24,7 @@ Merge is effectively a generalization of Solo and Couple, since Solo is `{>β¨π ## Merge and array theory -In all cases what these functions do is more like reinterpreting existing data than creating new information. In fact, if we ignore the shape and look at the ravels of the arrays involved in a call to Merge, we find that it just [joins](join.md) them together. Essentially, Merge is a request to ensure that the inner arrays (which, being independent elements, could be any sort of "ragged" array) can fit together in an array, and then to consider them to be such an array. For this reason, Merge (or a virtual analogue) is used to combine the result cells when calling a function with Rank into a single array. +In all cases what these functions do is more like reinterpreting existing data than creating new information. In fact, if we ignore the shape and look at the deshaped arrays involved in a call to Merge, we find that it just [joins](join.md) them together. Essentially, Merge is a request to ensure that the inner arrays (which, being independent elements, could be any sort of "ragged" array) can fit together in an array, and then to consider them to be such an array. For this reason, Merge (or a virtual analogue) is used to combine the result cells when calling a function with Rank into a single array. β₯ > a β₯ β₯Β¨ a diff --git a/doc/indices.md b/doc/indices.md index 1b857466..21abc8be 100644 --- a/doc/indices.md +++ b/doc/indices.md @@ -50,6 +50,6 @@ To match this format, Range (`β`) could be changed to return a flat array when The most interesting feature would be that Select could still allow `π¨` to be nested. In this case each element of `π¨` would be an array with cell indices for its rows as before. However, each row can now index along multiple axes, allowing some adjacent axes to be dependent while others remain independent. This nicely unifies scatter-point and per-axis selection, and allows a mix of the two. However, it doesn't allow total freedom, as non-adjacent axes can't be combined except by also mixing in all axes in between. -Group (`β`) could accept the same index format for its index argument `π¨`. Each depth-1 array in `π¨` would correspond to multiple axes in the outer result array, but only a single axis in `π©` and inner arrays. Because the ravel ordering of indices must be used to order cells of inner arrays, this modification is not quite as clean as the change to Select. It's also not so clearly useful, as the same results can be obtained by using atomic indices and reshaping the result. +Group (`β`) could accept the same index format for its index argument `π¨`. Each depth-1 array in `π¨` would correspond to multiple axes in the outer result array, but only a single axis in `π©` and inner arrays. Because the index ordering of indices must be used to order cells of inner arrays, this modification is not quite as clean as the change to Select. It's also not so clearly useful, as the same results can be obtained by using atomic indices and reshaping the result. Overall it seems to me that the main use of cell indices of the type discussed here is for the Select primitive, and the other cases are somewhat contrived and awkward. So I've chosen not to support it in BQN at all. diff --git a/doc/leading.md b/doc/leading.md index 57cd017b..347d0b65 100644 --- a/doc/leading.md +++ b/doc/leading.md @@ -2,79 +2,94 @@ # The leading axis convention -Several primitive functions manipulate the right argument, or sometimes both arguments, of an [array](array.md) along one or more axes. According to the [leading axis model](https://aplwiki.com/wiki/Leading_axis_theory), it's best to make the primitives operate on initial axes, because the Rank modifier then allows it to apply to later axes as well. Here we'll see how this pattern works in BQN. +Several primitive functions manipulate the right argument, or sometimes both arguments, of an [array](array.md) along one or more axes. According to the [leading axis model](https://aplwiki.com/wiki/Leading_axis_theory), it's best to make the primitives operate on initial axes, because the [Rank modifier](rank.md) then allows it to apply to later axes as well. Here we'll see how this pattern works in BQN. ## Monadic functions ### Manipulating cells -Most non-arithmetic monadic functions work only on the first axis of the argumentβthat is, they treat it as a list of its major cells. The function [Length](shape.md) (`β `) counts these major cells, while [Prefixes](prefixes.md) (`β`), Suffixes (`β`), [Reverse](reverse.md) (`β½`), and [First Cell](select.md) (`β`) move them around. The [Insert](fold.md#insert) (`Λ`) and [Scan](scan.md) (`` ` ``) modifiers also yield functions that work along the first axis; in contrast, [Fold](fold.md) (`Β΄`) requires `π©` to be a list, as it works on elements. +Most monadic functions that deal with structure at all (that is, not arithmetic) work only on the first axis of the argument. Usually, they treat it as a list of its [major cells](array.md#cells). The function [Length](shape.md) (`β `) counts these major cells, while [Prefixes](prefixes.md) (`β`), Suffixes (`β`), [Reverse](reverse.md) (`β½`), and [First Cell](select.md) (`β`) move them around. The [Insert](fold.md#insert) (`Λ`) and [Scan](scan.md) (`` ` ``) modifiers also yield functions that work along the first axis; [Fold](fold.md) (`Β΄`) requires `π©` to be a list but does go along the first (only) axis of that list. β’ a β 3βΏ2 β₯ "abcdef" # An array with three major cells + β a # Get the first major cell + β½ a # Reverse the cells + β£` a # Replicate the first cell -To use these functions on another axis, use the Rank (`β`) or Cells (`Λ`) modifier to find the one you want. For a rank 2 array like `a`, the most you'll ever need is a single `Λ`, because a function works on axis 0 by default, and there's only one other axis. +To use these functions on another axis, use the [Rank](rank.md#rank) (`β`) or [Cells](rank.md#cells) (`Λ`) modifier to find the one you want. For a rank 2 array like `a`, the most you'll ever need is a single `Λ`, because after the leading one there's only one other axis. βΛ a # First column + β½Λ a # Swap the columns + β£`Λ a # Replicate along rows -In these three cases above, the results are the same as you would get from [transposing](transpose.md) before and after (this has no effect on the result of `βΛ`, since it has rank 1). But in the following cases, the structure is quite different: `βa` is a list of matrices while `βΛa` is a matrix of lists. This is because the functions `β`, `β½`, and `` β£` `` leave the trailing axis structure intact (`β` removes one axis); taking into account that Rank or Cells always preserves the leading or frame axes, all axes are preserved (except the one removed by `β`). In contrast, Prefixes or Suffixes will push some axes down in depth, and the number of axes that are pushed down in this way changes with the rank of application. More precisely, these functions move axes after the first from the argument itself to result elements, and create two axes from the first axis, with one of them forming the sole result axis and the other joining the rest as an element axis. +In these three cases above, the results are the same as you would get from [transposing](transpose.md) before and after (which does nothing to the rank-1 result of `βΛ`, but that's what's wanted). But in the following cases, the structure is quite different: `βa` is a list of matrices while `βΛa` is a matrix of lists. This is because the functions `β`, `β½`, and `` β£` `` leave the trailing axis structure intact (`β` removes one axis); taking into account that Rank or Cells always preserves the leading or frame axes, all axes are preserved (except the one removed by `β`). But Prefixes or Suffixes move axes after the first from the whole of `π©` to elements of the result, pushing them down in depth, and Rank won't undo this sort of structural change. β a # Prefixes of a: ranks 1|2 + βΛ a # Prefixes of rows: ranks 2|1 + βΎΛ a # Join the cells + βΎΛΛ a # Join-insert is a no-op on lists [Solo](couple.md) (`β`), something of a maverick, manages to act on *zero* leading axes of `π©` by creating the first axis of the *result* instead. Because it doesn't need any axis to work, it can go in front of either axis but also past the last one by working with rank 0, a case where most array functions would give an error. β’ β a # Solo adds a length-1 axis + a β‘ β β a # First Cell undoes this + β’ βΛ a # Solo can insert the axis deeperβ¦ + β’ ββ0 a # β¦or deeper still. ### Comparing cells -The functions in the last section manipulate cells in the same way regardless of what data they contain. Other functions compare cells to each other, either testing whether they match or how they are ordered relative to one another. The two [Grade](order.md) functions `ββ`, and the [self-search](selfcmp.md) functions Classify (`β`), Mark Firsts (`β`), and Occurrence Count (`β`), each give a list result, with one number for each cell. We can see below that Occurrence Count returns the same results even as we make the argument cells more complicated, because the changes made preserve the matching of cells. +The functions in the last section manipulate cells in the same way regardless of what data they contain. Other functions compare cells to each other, either testing whether they match or how they are ordered relative to one another. The two [Grade](order.md#grade) functions `ββ`, and the [self-search](selfcmp.md) functions Classify (`β`), Mark Firsts (`β`), and Occurrence Count (`β`), each give a list result, with one number for each cell. We can see below that [Occurrence Count](selfcmp.md#occurrence-count) returns the same results even as we make the argument cells more complicated, because the changes made preserve the matching of cells. s β "abracadabra" + β s + β βΛ s + β s βΎβ0βΏ1 "suffix" -The two Sort functions `β§β¨` and Deduplicate (`β·`) move cells around based on their ordering. The length of Deduplicate's result depends on how many unique cells the argument has, so you'd better be careful if you want to apply it to argument cells! However, the result of sorting has the same shape as the argument, so it can always safely be applied at any rank, for example to the rows of an array. +The two [Sort](order.md#sort) functions `β§β¨` and [Deduplicate](selfcmp.md#deduplicate) (`β·`) move cells around based on their ordering. The length of Deduplicate's result depends on how many unique cells the argument has, so you'd better be careful if you want to apply it to argument cells! However, the result of sorting has the same shape as the argument, so it can always safely be applied at any rank, for example to the rows of an array. β’ b β 4βΏ5 β₯ β4 + β¨Λ b ### Other monadic functions -Not all functions work on the first axis in a straightforward manner. [Transpose](transpose.md) `β` moves the first axis to the end, so while it focuses on the first one, it shifts every axis of `π©`. [Join](join.md) `βΎ` also works on every axis of its argument, and applies to the leading axes of `π©`'s *elements* instead: these leading inner axes are matched up with the outer axes, and trailing inner axes are allowed but the elements must have rank at least as high as the argument array. +Not all functions work on the first axis in a straightforward manner. [Transpose](transpose.md) `β` moves the first axis of `π©` to the end, so while it focuses on the first one, it shifts every other axis too. [Join](join.md) `βΎ` also works on every axis of its argument, and applies to the leading axes of `π©`'s *elements* instead: these leading inner axes are matched up with the outer axes, and trailing inner axes are allowed but the elements must have rank (after extension) at least as high as the argument array. -The other two monadic functions that work on high-rank arguments are [Deshape](reshape.md#deshape) (`β₯`) and [First](pick.md#first) (`β`). These treat `π©` as one long list, ordered by its element indices. This ordering privileges leading axes (in fact, it is the reason for the choice of leading axes in the leading axis convention), but these functions can't really be said to work on leading axes: they apply to all axes. +The other two monadic functions that work on high-rank arguments are [Deshape](reshape.md#deshape) (`β₯`) and [First](pick.md#first) (`β`). These treat `π©` as one long list, ordered by its element indices. This ordering privileges leading axes (in fact, it's the reason for the choice of leading axes in the leading axis convention), but these functions can't really be said to work on leading axes: they apply to all axes. -The [Each](map.md) (`Β¨`) and [Table](map.md#table) (`β`) modifiers return functions which are the same in the monadic case. These functions simply go through all elements of the argument array without regard for its multi-dimensional structure (the operand is applied to elements in index order, matching Deshape; this matters if it has side effects). Similarly, monadic arithmetic functions do not have any sort of leading axis dependence. +The [Each](map.md) (`Β¨`) and [Table](map.md#table) (`β`) modifiers behave the same in the monadic case: they go through all elements of `π©` without regard for its multi-dimensional structure (in index order, matching Deshape; this matters if it has side effects). Similarly, monadic arithmetic functions don't have any sort of leading axis dependence. ## Dyadic functions -For dyadic functions the pattern of working on only one argument axis is not so common. Only two functions can be said to follow it roughly: [Join to](join.md) (`βΎ`) combines two arrays along one axis, using the first axis of both arguments if they have the same rank and of the higher-rank argument if they differ by one. [Couple](couple.md) (`β`), like Solo, does not manipulate the argument axes but adds a result axis. There are also some functions that can't be limited to leading axes: [Reshape](reshape.md) (`β₯`) treats `π©` as one long list, and [Pick](pick.md) (`β`) requires each index to be as long as `π©`'s rank, because it selects elements and not cells from `π©`. +For dyadic functions the pattern of working on only one argument axis is not so common. Only two functions can be said to follow it roughly: [Join to](join.md) (`βΎ`) combines two arrays along one axis, using the first axis of both arguments if they have the same rank and of the higher-rank argument if they differ by one. [Couple](couple.md) (`β`), like Solo, doesn't manipulate the argument axes but adds a result axis. There are also some functions that can't be limited to leading axes: [Pick](pick.md) (`β`) requires each index to be as long as `π©`'s rank, because it selects elements and not cells from `π©`, and [Reshape](reshape.md) (`β₯`) treats `π©` as one long list. In fact I think I [got Reshape wrong](../commentary/problems.md#deshape-and-reshape-cant-ignore-trailing-axes) by rejecting J's leading axis form, but it's too late to go back on that, especially given that it wouldn't make much sense for it to share the glyph `β₯` with Deshape. ### Multiple axes -Instead of always working on a single axis, many dyadic functions work on one axis by default, but also allow a left argument with multiple elements corresponding to leading axes of `π©`. To decide which of the two possibilities applies, these functions test the depth of `π¨`, a convention that is discussed in the [depth](depth.md#testing-depth-for-multiple-axis-primitives) documentation. A left argument that applies to one axis has a particular depth; `π¨` can also be a list of such arguments. +Instead of always working on a single axis, many dyadic functions work on one axis by default, but also allow a left argument with multiple elements corresponding to leading axes of `π©`. To decide which of the two possibilities applies, these functions test the depth of `π¨`, a convention that is discussed [in the depth documentation](depth.md#testing-depth-for-multiple-axis-primitives). A left argument that applies to one axis has a particular depth; `π¨` can also be a list of such arguments. | Single-axis depth | Functions |-------------------|---------- | 0 | `ββββ½β` | 1 | `/ββ` -Functions such as Take and Drop use a single number per axis. When `π¨` is a list of numbers, they apply to initial axes. Observing the operation of [Rotate](reverse.md#rotate) on the result of [Range](range.md) is instructive: +Functions such as Take and Drop use a single number per axis. When `π¨` is a list of numbers, they apply to initial axes. The operation of [Rotate](reverse.md#rotate) on the result of [Range](range.md) is instructive: 2βΏ1 β½ β3βΏ5 -The array is shifted once to the left and twice upward, so that the first index (by ravel order) is now `β2βΏ1β½β3βΏ5 ββ 2βΏ1`. To see how values are matched to leading axes, we can look at how [Drop](take.md) changes the shape of its argument: +The array is shifted once to the left and twice upward, so that the first index (by index order) is now `β2βΏ1β½β3βΏ5 ββ 2βΏ1`. To see how values are matched to leading axes, we can look at how [Drop](take.md) changes the shape of its argument: β’ 3βΏ2 β 7βΏ7βΏ7βΏ7β₯"abc" @@ -82,21 +97,29 @@ Functions with single-axis depth 1 tend to be more complicated; see for example ### Leading axis agreement -[Arithmetic](arithmetic.md) functions, and the [Each](map.md#each) (`Β¨`) and [Depth](depth.md#the-depth-modifier) (`β`) modifiers, use leading axis agreement to match their arguments together. All axes of the lower-rank argument are matched with the leading axes of the higher-rank one, and axes matched together must have the same length. After pairing axes in this way, a single element of the lower-rank argument might correspond to any number of elements of the higher-rank one. It's reused for each of those corresponding elements. +[Arithmetic](arithmetic.md) functions, and the [Each](map.md#each) (`Β¨`) and [Depth](depth.md#the-depth-modifier) (`β`) modifiers, use leading axis agreement to match their arguments together. It's a bit like NumPy or Julia broadcasting, but these mostly match trailing, not leading, axes. In BQN, all axes of the lower-rank argument are matched with the leading axes of the higher-rank one, and axes matched together must have the same length. After pairing axes in this way, a single element of the lower-rank argument might correspond to any number of elements of the higher-rank one. It's reused for each of those corresponding elements. β’ x β 3βΏ2βΏ4 β₯ β60 # A rank-3 array + 100βΏ0βΏ200 + x # 0-cells paired with 2-cells + +That's shape `3βΏ2βΏ4` matched with shape `β¨3β©`: the leading `3` agrees. Now to match with `3βΏ2`: + β’ c β 100 Γ 3 =βββ 2 # A rank-2 array to add + c + x # 0-cells paired with 1-cells + +And of course, identical shapes agree: + x + x # Pairwise addition -If one argument is a unit, that is, it has no axes, then leading axis agreement reduces to APL's "scalar extension" (where "scalar" is equivalent to BQN's "unit"), where a single unit is matched with an entire array by repeating it at every application. A unit always agrees with any other array under leading axis agreement because it has no axes whose lengths would need to be checked. +If one argument is a [unit](enclose.md#whats-a-unit), that is, it has no axes, then leading axis agreement reduces to APL's "scalar extension" (where "scalar" is equivalent to BQN's "unit"), where a single unit is matched with an entire array by repeating it at every application. A unit always agrees with any other array under leading axis agreement, because it has no axes whose lengths would need to be checked. -With leading axis agreement, there are `k+1` shapes for arrays that can be added (or any other function with Each) to a given array `x` without changing its rank. These are precisely the prefixes of `β’x`, with ranks from `0` to `k` inclusive. Arrays with larger rank can also be used as the other argument, but then the result shape will match that argument and not `x`. +With leading axis agreement, there are `k+1` shapes for arrays that can be added (or any other function with Each) to a given array `a` without changing its rank. These are precisely the prefixes of `β’a`, with ranks from `0` to `k` inclusive. Arrays with larger rank can also be used as the other argument, but then the result shape will match that argument and not `a`. ### Search functions -The [search functions](search.md), Index of (`β`), Progressive Index of (`β`), and Member of (`β`), and also [Bins](order.md#bins) (`ββ`), look through cells of one argument to find cells of the other. Find (`β·`) also does a search, but a slightly different one: it tries to find *slices* of cells of `π©` that match `π¨`. +The [search functions](search.md) Index of (`β`), Progressive Index of (`β`), and Member of (`β`), and also [Bins](order.md#bins) (`ββ`), look through cells of one argument to find cells of the other. [Find](find.md) (`β·`) also does a search, but a slightly different one: it tries to find *slices* of cells of `π©` that match `π¨`. | Search in | Search for | Functions |-----------|------------|---------- |
