diff options
Diffstat (limited to 'docs/doc/leading.html')
| -rw-r--r-- | docs/doc/leading.html | 223 |
1 files changed, 223 insertions, 0 deletions
diff --git a/docs/doc/leading.html b/docs/doc/leading.html new file mode 100644 index 00000000..fba0d4c4 --- /dev/null +++ b/docs/doc/leading.html @@ -0,0 +1,223 @@ +<head><link href="../style.css" rel="stylesheet"/></head> +<div class="nav"><a href="https://github.com/mlochbaum/BQN">BQN</a></div> +<h1 id="the-leading-axis-convention">The leading axis convention</h1> +<p>Several primitive functions manipulate the right argument, or sometimes both arguments, along one or more axes. According to the <a href="https://aplwiki.com/wiki/Leading_axis_theory">leading axis model</a>, 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.</p> +<h2 id="monadic-functions">Monadic functions</h2> +<h3 id="manipulating-cells">Manipulating cells</h3> +<p>Most non-scalar 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 (<code><span class='Function'>≠</span></code>) counts these major cells, while Prefixes (<code><span class='Function'>↑</span></code>), Suffixes (<code><span class='Function'>↓</span></code>), Reverse (<code><span class='Function'>⌽</span></code>), and First Cell (<code><span class='Function'>⊏</span></code>) move them around. The Insert (<code><span class='Modifier'>˝</span></code>) and Scan (<code><span class='Modifier'>`</span></code>) modifiers also yield functions that work along the first axis; in contrast, Reduce (<code><span class='Modifier'>´</span></code>) requires its argument to be a list, as it works on elements.</p> +<pre> <span class='Function'>⊢</span> <span class='Value'>a</span> <span class='Gets'>←</span> <span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>2</span> <span class='Function'>⥊</span> <span class='String'>"abcdef"</span> <span class='Comment'># An array with three major cells +</span>┌─ +╵"ab + cd + ef" + ┘ + <span class='Function'>⊏</span> <span class='Value'>a</span> <span class='Comment'># Get the first major cell +</span>"ab" + <span class='Function'>⌽</span> <span class='Value'>a</span> <span class='Comment'># Reverse the cells +</span>┌─ +╵"ef + cd + ab" + ┘ + <span class='Function'>⊣</span><span class='Modifier'>`</span> <span class='Value'>a</span> <span class='Comment'># Replicate the first cell +</span>┌─ +╵"ab + ab + ab" + ┘ +</pre> +<p>To use these functions on another axis, use the Rank (<code><span class='Modifier2'>⎉</span></code>) or Cells (<code><span class='Modifier'>˘</span></code>) modifier to find the one you want. For a rank 2 array like <code><span class='Value'>a</span></code>, the most you'll ever need is a single <code><span class='Modifier'>˘</span></code>, because a function works on axis 0 by default, and there's only one other axis.</p> +<pre> <span class='Function'>⊏</span><span class='Modifier'>˘</span> <span class='Value'>a</span> <span class='Comment'># First column +</span>"ace" + <span class='Function'>⌽</span><span class='Modifier'>˘</span> <span class='Value'>a</span> <span class='Comment'># Swap the columns +</span>┌─ +╵"ba + dc + fe" + ┘ + <span class='Function'>⊣</span><span class='Modifier'>`˘</span> <span class='Value'>a</span> <span class='Comment'># Replicate along rows +</span>┌─ +╵"aa + cc + ee" + ┘ +</pre> +<p>In these three cases above, the results are the same as you would get from transposing before and after (this has no effect on the result of <code><span class='Function'>⊏</span><span class='Modifier'>˘</span></code>, since it has rank 1). But in the following cases, the structure is quite different: <code><span class='Function'>↑</span><span class='Value'>a</span></code> is a list of matrices while <code><span class='Function'>↑</span><span class='Modifier'>˘</span><span class='Value'>a</span></code> is a matrix of lists. This is because the functions <code><span class='Function'>⊏</span></code>, <code><span class='Function'>⌽</span></code>, and <code><span class='Function'>⊣</span><span class='Modifier'>`</span></code> leave the trailing axis structure intact (<code><span class='Function'>⊏</span></code> 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 <code><span class='Function'>⊏</span></code>). In contrast, Prefixes or Suffixes pushes 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.</p> +<pre> <span class='Function'>↑</span> <span class='Value'>a</span> <span class='Comment'># Prefixes of a: ranks 1|2 +</span>┌─ +· 0‿2⥊⟨⟩ ┌─ ┌─ ┌─ + ╵"ab" ╵"ab ╵"ab + ┘ cd" cd + ┘ ef" + ┘ + ┘ + <span class='Function'>↑</span><span class='Modifier'>˘</span> <span class='Value'>a</span> <span class='Comment'># Prefixes of rows: ranks 2|1 +</span>┌─ +╵ ⟨⟩ "a" "ab" + ⟨⟩ "c" "cd" + ⟨⟩ "e" "ef" + ┘ + <span class='Function'>∾</span><span class='Modifier'>˝</span> <span class='Value'>a</span> <span class='Comment'># Join the cells +</span>"abcdef" + <span class='Function'>∾</span><span class='Modifier'>˝˘</span> <span class='Value'>a</span> <span class='Comment'># Join-insert is a no-op on lists +</span>┌─ +╵"ab + cd + ef" + ┘ +</pre> +<p>Solo (<code><span class='Function'>≍</span></code>), something of a maverick, manages to act on <em>zero</em> leading axes of its argument by creating the first axis of the <em>result</em> 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.</p> +<pre> <span class='Function'>≢</span> <span class='Function'>≍</span> <span class='Value'>a</span> <span class='Comment'># Solo adds a length-1 axis +</span>⟨ 1 3 2 ⟩ + <span class='Value'>a</span> <span class='Function'>≡</span> <span class='Function'>⊏</span> <span class='Function'>≍</span> <span class='Value'>a</span> <span class='Comment'># First Cell undoes this +</span>1 + <span class='Function'>≢</span> <span class='Function'>≍</span><span class='Modifier'>˘</span> <span class='Value'>a</span> <span class='Comment'># Solo can insert the axis deeper… +</span>⟨ 3 1 2 ⟩ + <span class='Function'>≢</span> <span class='Function'>≍</span><span class='Modifier2'>⎉</span><span class='Number'>0</span> <span class='Value'>a</span> <span class='Comment'># …or deeper still. +</span>⟨ 3 2 1 ⟩ +</pre> +<h3 id="comparing-cells">Comparing cells</h3> +<p>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 functions <code><span class='Function'>⍋⍒</span></code>, and the self-comparison functions Unique Mask (<code><span class='Function'>∊</span></code>) and Occurrence Count (<code><span class='Function'>⊒</span></code>), 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.</p> +<pre> <span class='Value'>s</span> <span class='Gets'>←</span> <span class='String'>"abracadabra"</span> + <span class='Function'>⊒</span> <span class='Value'>s</span> +⟨ 0 0 0 1 0 2 0 3 1 1 4 ⟩ + <span class='Function'>⊒</span> <span class='Function'>≍</span><span class='Modifier'>˘</span> <span class='Value'>s</span> +⟨ 0 0 0 1 0 2 0 3 1 1 4 ⟩ + <span class='Function'>⊒</span> <span class='Value'>s</span> <span class='Function'>∾</span><span class='Modifier2'>⎉</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span> <span class='String'>"suffix"</span> +⟨ 0 0 0 1 0 2 0 3 1 1 4 ⟩ +</pre> +<p>The two Sort functions <code><span class='Function'>∧∨</span></code> and Deduplicate (<code><span class='Function'>⍷</span></code>) 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.</p> +<pre> <span class='Function'>⊢</span> <span class='Value'>b</span> <span class='Gets'>←</span> <span class='Number'>4</span><span class='Ligature'>‿</span><span class='Number'>5</span> <span class='Function'>⥊</span> <span class='Function'>↕</span><span class='Number'>4</span> +┌─ +╵ 0 1 2 3 0 + 1 2 3 0 1 + 2 3 0 1 2 + 3 0 1 2 3 + ┘ + <span class='Function'>∨</span><span class='Modifier'>˘</span> <span class='Value'>b</span> +┌─ +╵ 3 2 1 0 0 + 3 2 1 1 0 + 3 2 2 1 0 + 3 3 2 1 0 + ┘ +</pre> +<h3 id="other-monadic-functions">Other monadic functions</h3> +<p>Not all functions work on the first axis in a straightforward manner. <a href="transpose.html">Transpose</a> <code><span class='Function'>⍉</span></code> moves the first axis to the end, so while it focuses on the first one, it shifts every axis of the argument. <a href="join.html">Join</a> <code><span class='Function'>∾</span></code> also works on every axis of its argument, and applies to the leading axes of the argument's <em>elements</em> 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.</p> +<p>The other two monadic functions that work on high-rank arguments are Deshape (<code><span class='Function'>⥊</span></code>) and First (<code><span class='Function'>⊑</span></code>). These treat the argument 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.</p> +<p>The Each (<code><span class='Modifier'>¨</span></code>) and Table (<code><span class='Modifier'>⌜</span></code>) 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 scalar functions do not have any sort of leading axis dependence.</p> +<h2 id="dyadic-functions">Dyadic functions</h2> +<p>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 (<code><span class='Function'>∾</span></code>) 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 (<code><span class='Function'>≍</span></code>), 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 (<code><span class='Function'>⥊</span></code>) treats the argument as one long list, and Pick (<code><span class='Function'>⊑</span></code>) requires each index to be as long as the right argument's rank, because it selects elements and not cells from the right argument.</p> +<h3 id="multiple-axes">Multiple axes</h3> +<p>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 the right argument. To decide which of the two possibilities applies, these functions test the left argument depth, a convention that is discussed in the <a href="depth.html#testing-depth-for-multiple-axis-primitives">depth</a> documentation. A left argument that applies to one axis has a particular depth; the argument can also be a list of such arguments.</p> +<table> +<thead> +<tr> +<th>Single-axis depth</th> +<th>Functions</th> +</tr> +</thead> +<tbody> +<tr> +<td>0</td> +<td><code><span class='Function'>↑↓↕⌽⍉</span></code></td> +</tr> +<tr> +<td>1</td> +<td><code><span class='Function'>/⊏⊔</span></code></td> +</tr> +</tbody> +</table> +<p>Functions such as Take and Drop use a single number per axis. When the left argument is a list of numbers, they apply to initial axes. Observing the operation of Rotate on the result of Range is instructive:</p> +<pre> <span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>1</span> <span class='Function'>⌽</span> <span class='Function'>↕</span><span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>5</span> +┌─ +╵ ⟨ 2 1 ⟩ ⟨ 2 2 ⟩ ⟨ 2 3 ⟩ ⟨ 2 4 ⟩ ⟨ 2 0 ⟩ + ⟨ 0 1 ⟩ ⟨ 0 2 ⟩ ⟨ 0 3 ⟩ ⟨ 0 4 ⟩ ⟨ 0 0 ⟩ + ⟨ 1 1 ⟩ ⟨ 1 2 ⟩ ⟨ 1 3 ⟩ ⟨ 1 4 ⟩ ⟨ 1 0 ⟩ + ┘ +</pre> +<p>The array is shifted once to the left and twice upward, so that the first index (by ravel order) is now <code><span class='Function'>⊑</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Function'>⌽↕</span><span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>5</span> <span class='Gets'>←→</span> <span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>1</span></code>. To see how values are matched to leading axes, we can look at how Drop changes the shape of its argument:</p> +<pre> <span class='Function'>≢</span> <span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>2</span> <span class='Function'>↓</span> <span class='Number'>7</span><span class='Ligature'>‿</span><span class='Number'>7</span><span class='Ligature'>‿</span><span class='Number'>7</span><span class='Ligature'>‿</span><span class='Number'>7</span><span class='Function'>⥊</span><span class='String'>"abc"</span> +⟨ 4 5 7 7 ⟩ +</pre> +<p>Functions with single-axis depth 1 tend to be more complicated; see for example <a href="group.html#multidimensional-grouping">Group</a>.</p> +<h3 id="leading-axis-agreement">Leading axis agreement</h3> +<p>Scalar functions, and the Each (<code><span class='Modifier'>¨</span></code>) and Depth (<code><span class='Modifier2'>⚇</span></code>) 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.</p> +<pre> <span class='Function'>⊢</span> <span class='Value'>x</span> <span class='Gets'>←</span> <span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>4</span> <span class='Function'>⥊</span> <span class='Function'>↕</span><span class='Number'>60</span> <span class='Comment'># A rank-3 array +</span>┌─ +╎ 0 1 2 3 + 4 5 6 7 + + 8 9 10 11 + 12 13 14 15 + + 16 17 18 19 + 20 21 22 23 + ┘ + <span class='Number'>100</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>200</span> <span class='Function'>+</span><span class='Modifier'>¨</span> <span class='Value'>x</span> <span class='Comment'># 0-cells paired with 2-cells +</span>┌─ +╎ 100 101 102 103 + 104 105 106 107 + + 8 9 10 11 + 12 13 14 15 + + 216 217 218 219 + 220 221 222 223 + ┘ + <span class='Function'>⊢</span> <span class='Value'>c</span> <span class='Gets'>←</span> <span class='Number'>100</span> <span class='Function'>×</span> <span class='Number'>3</span> <span class='Function'>=</span><span class='Modifier'>⌜</span><span class='Modifier2'>○</span><span class='Function'>↕</span> <span class='Number'>2</span> <span class='Comment'># A rank-2 array to add +</span>┌─ +╵ 100 0 + 0 100 + 0 0 + ┘ + <span class='Value'>c</span> <span class='Function'>+</span><span class='Modifier'>¨</span> <span class='Value'>x</span> <span class='Comment'># 0-cells paired with 1-cells +</span>┌─ +╎ 100 101 102 103 + 4 5 6 7 + + 8 9 10 11 + 112 113 114 115 + + 16 17 18 19 + 20 21 22 23 + ┘ + <span class='Value'>x</span> <span class='Function'>+</span> <span class='Value'>x</span> <span class='Comment'># Pairwise addition +</span>┌─ +╎ 0 2 4 6 + 8 10 12 14 + + 16 18 20 22 + 24 26 28 30 + + 32 34 36 38 + 40 42 44 46 + ┘ +</pre> +<p>If one argument is a scalar, that is, it has no axes, then leading axis agreement reduces to "scalar extension", where a single scalar is matched with an entire array by repeating it at every application. A scalar always agrees with any other array under leading axis agreement because it has no axes whose lengths would need to be checked.</p> +<p>With leading axis agreement, there are <code><span class='Value'>k</span><span class='Function'>+</span><span class='Number'>1</span></code> shapes for arrays that can be added (or any other function with Each) to a given array <code><span class='Value'>x</span></code> without changing its rank. These are precisely the prefixes of <code><span class='Function'>≢</span><span class='Value'>x</span></code>, with ranks from <code><span class='Number'>0</span></code> to <code><span class='Value'>k</span></code> inclusive. Arrays with larger rank can also be used as the other argument, but then the result shape will match that argument and not <code><span class='Value'>x</span></code>.</p> +<h3 id="search-functions">Search functions</h3> +<p>The search functions Bins (<code><span class='Function'>⍋⍒</span></code>), Index of (<code><span class='Function'>⊐</span></code>), Progressive Index of (<code><span class='Function'>⊒</span></code>), and Member of (<code><span class='Function'>∊</span></code>) look through cells of one argument to find cells of the other. Find (<code><span class='Function'>⍷</span></code>) also does a search, but a slightly different one: it tries to find <em>slices</em> of cells of its right argument that match the left argument.</p> +<table> +<thead> +<tr> +<th>Searching through</th> +<th>Look for</th> +<th>Functions</th> +</tr> +</thead> +<tbody> +<tr> +<td><code><span class='Value'>𝕨</span></code></td> +<td><code><span class='Value'>𝕩</span></code></td> +<td><code><span class='Function'>⍋⍒⊐⊒</span></code></td> +</tr> +<tr> +<td><code><span class='Value'>𝕩</span></code></td> +<td><code><span class='Value'>𝕨</span></code></td> +<td><code><span class='Function'>∊⍷</span></code></td> +</tr> +</tbody> +</table> +<p>For all of these functions but Find, the argument to search through is treated as a list of its major cells. It is the rank of these major cells—let's call this rank <code><span class='Value'>c</span></code>—that determines how the other argument is treated. That argument must have rank at least <code><span class='Value'>c</span></code>, and it is treated as an array of <code><span class='Value'>c</span></code>-cells. For example, if the left argument to <code><span class='Function'>⍋</span></code> is a matrix, then each 1-cell or row of the right argument is treated independently, and each one yields one number in the result: a 0-cell. The result rank of <code><span class='Function'>⍋</span></code> is always <code><span class='Value'>𝕨</span><span class='Function'>¬</span><span class='Modifier2'>○</span><span class='Function'>=</span><span class='Value'>𝕩</span></code>.</p> + |
