aboutsummaryrefslogtreecommitdiff
path: root/docs/doc/leading.html
blob: b3b008c88b253cbd06cade8cf003db0dca7fd356 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<head>
  <link href="../favicon.ico" rel="shortcut icon" type="image/x-icon"/>
  <link href="../style.css" rel="stylesheet"/>
  <title>BQN: The leading axis convention</title>
</head>
<div class="nav">(<a href="https://github.com/mlochbaum/BQN">github</a>) / <a href="../index.html">BQN</a> / <a href="index.html">doc</a></div>
<h1 id="the-leading-axis-convention"><a class="header" href="#the-leading-axis-convention">The leading axis convention</a></h1>
<p>Several primitive functions manipulate the right argument, or sometimes both arguments, of an <a href="array.html">array</a> 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 <a href="rank.html">Rank modifier</a> 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"><a class="header" href="#monadic-functions">Monadic functions</a></h2>
<h3 id="manipulating-cells"><a class="header" href="#manipulating-cells">Manipulating cells</a></h3>
<p>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 <a href="array.html#cells">major cells</a>. The function <a href="shape.html">Length</a> (<code><span class='Function'></span></code>) counts these major cells, while <a href="prefixes.html">Prefixes</a> (<code><span class='Function'></span></code>), Suffixes (<code><span class='Function'></span></code>), <a href="reverse.html">Reverse</a> (<code><span class='Function'></span></code>), and <a href="select.html#first-cell">First Cell</a> (<code><span class='Function'></span></code>) move them around. The <a href="fold.html#insert">Insert</a> (<code><span class='Modifier'>˝</span></code>) and <a href="scan.html">Scan</a> (<code><span class='Modifier'>`</span></code>) modifiers also yield functions that work along the first axis; <a href="fold.html">Fold</a> (<code><span class='Modifier'>´</span></code>) requires <code><span class='Value'>𝕩</span></code> to be a list but does go along the first (only) axis of that list.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGEg4oaQIDPigL8yIOKliiAiYWJjZGVmIiAgIyBBbiBhcnJheSB3aXRoIHRocmVlIG1ham9yIGNlbGxzCgriio8gYSAgICAgICAgICAgICAgICAgICAjIEdldCB0aGUgZmlyc3QgbWFqb3IgY2VsbAoK4oy9IGEgICAgICAgICAgICAgICAgICAgIyBSZXZlcnNlIHRoZSBjZWxscwoK4oqjYCBhICAgICAgICAgICAgICAgICAgIyBSZXBsaWNhdGUgdGhlIGZpcnN0IGNlbGw=">↗️</a><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'>&quot;abcdef&quot;</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 <a href="rank.html#rank">Rank</a> (<code><span class='Modifier2'></span></code>) or <a href="rank.html#cells">Cells</a> (<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 after the leading one there's only one other axis.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqPy5ggYSAgICAgICAgICAgICAgICAgICMgRmlyc3QgY29sdW1uCgrijL3LmCBhICAgICAgICAgICAgICAgICAgIyBTd2FwIHRoZSBjb2x1bW5zCgriiqNgy5ggYSAgICAgICAgICAgICAgICAgIyBSZXBsaWNhdGUgYWxvbmcgcm93cw==">↗️</a><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 <a href="transpose.html">transposing</a> before and after (which does nothing to the rank-1 result of <code><span class='Function'></span><span class='Modifier'>˘</span></code>, but that's what's wanted). 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>). But Prefixes or Suffixes move axes after the first from the whole of <code><span class='Value'>𝕩</span></code> to elements of the result, pushing them down in depth, and Rank won't undo this sort of structural change.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oaRIGEgICAgICAgICAgICAgICAgICAgIyBQcmVmaXhlcyBvZiBhOiAgICByYW5rcyAxfDIKCuKGkcuYIGEgICAgICAgICAgICAgICAgICAjIFByZWZpeGVzIG9mIHJvd3M6IHJhbmtzIDJ8MQoK4oi+y50gYSAgICAgICAgICAgICAgICAgICMgSm9pbiB0aGUgY2VsbHMKCuKIvsudy5ggYSAgICAgICAgICAgICAgICAgIyBKb2luLWluc2VydCBpcyBhIG5vLW9wIG9uIGxpc3Rz">↗️</a><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><a href="couple.html">Solo</a> (<code><span class='Function'></span></code>), something of a maverick, manages to act on <em>zero</em> leading axes of <code><span class='Value'>𝕩</span></code> 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>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omiIOKJjSBhICAgICAgICAgICAgICAgICAjIFNvbG8gYWRkcyBhIGxlbmd0aC0xIGF4aXMKCmEg4omhIOKKjyDiiY0gYSAgICAgICAgICAgICAjIEZpcnN0IENlbGwgdW5kb2VzIHRoaXMKCuKJoiDiiY3LmCBhICAgICAgICAgICAgICAgICMgU29sbyBjYW4gaW5zZXJ0IHRoZSBheGlzIGRlZXBlcuKApgoK4omiIOKJjeKOiTAgYSAgICAgICAgICAgICAgICMg4oCmb3IgZGVlcGVyIHN0aWxsLg==">↗️</a><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"><a class="header" href="#comparing-cells">Comparing cells</a></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 <a href="order.html#grade">Grade</a> functions <code><span class='Function'>⍋⍒</span></code>, and the <a href="selfcmp.html">self-search</a> functions Classify (<code><span class='Function'></span></code>), Mark Firsts (<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 <a href="selfcmp.html#occurrence-count">Occurrence Count</a> returns the same results even as we make the argument cells more complicated, because the changes made preserve the matching of cells.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=cyDihpAgImFicmFjYWRhYnJhIgoK4oqSIHMKCuKKkiDiiY3LmCBzCgriipIgcyDiiL7ijokw4oC/MSAic3VmZml4Ig==">↗️</a><pre>    <span class='Value'>s</span> <span class='Gets'></span> <span class='String'>&quot;abracadabra&quot;</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'>&quot;suffix&quot;</span>
⟨ 0 0 0 1 0 2 0 3 1 1 4 ⟩
</pre>
<p>The two <a href="order.html#sort">Sort</a> functions <code><span class='Function'>∧∨</span></code> and <a href="selfcmp.html#deduplicate">Deduplicate</a> (<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>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGIg4oaQIDTigL81IOKliiDihpU0CgriiKjLmCBi">↗️</a><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"><a class="header" href="#other-monadic-functions">Other monadic functions</a></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 of <code><span class='Value'>𝕩</span></code> to the end, so while it focuses on the first one, it shifts every other axis too. <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 <code><span class='Value'>𝕩</span></code>'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 (after extension) at least as high as the argument array.</p>
<p>The other two monadic functions that work on high-rank arguments are <a href="reshape.html#deshape">Deshape</a> (<code><span class='Function'></span></code>) and <a href="pick.html#first">First</a> (<code><span class='Function'></span></code>). These treat <code><span class='Value'>𝕩</span></code> 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.</p>
<p>The <a href="map.html">Each</a> (<code><span class='Modifier'>¨</span></code>) and <a href="map.html#table">Table</a> (<code><span class='Modifier'></span></code>) modifiers behave the same in the monadic case: they go through all elements of <code><span class='Value'>𝕩</span></code> 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.</p>
<h2 id="dyadic-functions"><a class="header" href="#dyadic-functions">Dyadic functions</a></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: <a href="join.html">Join to</a> (<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. <a href="couple.html">Couple</a> (<code><span class='Function'></span></code>), 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: <a href="pick.html">Pick</a> (<code><span class='Function'></span></code>) requires each index to be as long as <code><span class='Value'>𝕩</span></code>'s rank, because it selects elements and not cells from <code><span class='Value'>𝕩</span></code>, and <a href="reshape.html">Reshape</a> (<code><span class='Function'></span></code>) treats <code><span class='Value'>𝕩</span></code> as one long list. In fact I think I <a href="../commentary/problems.html#deshape-and-reshape-cant-ignore-trailing-axes">got Reshape wrong</a> 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 <code><span class='Function'></span></code> with Deshape.</p>
<h3 id="multiple-axes"><a class="header" href="#multiple-axes">Multiple axes</a></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 <code><span class='Value'>𝕩</span></code>. To decide which of the two possibilities applies, these functions test the depth of <code><span class='Value'>𝕨</span></code>, a convention that is discussed <a href="depth.html#testing-depth-for-multiple-axis-primitives">in the depth documentation</a>. A left argument that applies to one axis has a particular depth; <code><span class='Value'>𝕨</span></code> 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 <code><span class='Value'>𝕨</span></code> is a list of numbers, they apply to initial axes. The operation of <a href="reverse.html#rotate">Rotate</a> on the result of <a href="range.html">Range</a> is instructive:</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MuKAvzEg4oy9IOKGlTPigL81">↗️</a><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 index 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 <a href="take.html">Drop</a> changes the shape of its argument:</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omiIDPigL8yIOKGkyA34oC/N+KAvzfigL834qWKImFiYyI=">↗️</a><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'>&quot;abc&quot;</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"><a class="header" href="#leading-axis-agreement">Leading axis agreement</a></h3>
<p><a href="arithmetic.html">Arithmetic</a> functions, and the <a href="map.html#each">Each</a> (<code><span class='Modifier'>¨</span></code>) and <a href="depth.html#the-depth-modifier">Depth</a> (<code><span class='Modifier2'></span></code>) 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.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIHgg4oaQIDPigL8y4oC/NCDipYog4oaVNjAgICAgICMgQSByYW5rLTMgYXJyYXkKCjEwMOKAvzDigL8yMDAgKyB4ICAgICAgICAgIyAwLWNlbGxzIHBhaXJlZCB3aXRoIDItY2VsbHM=">↗️</a><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='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  
                  ┘
</pre>
<p>That's shape <code><span class='Number'>3</span><span class='Ligature'></span><span class='Number'>2</span><span class='Ligature'></span><span class='Number'>4</span></code> matched with shape <code><span class='Bracket'></span><span class='Number'>3</span><span class='Bracket'></span></code>: the leading <code><span class='Number'>3</span></code> agrees. Now to match with <code><span class='Number'>3</span><span class='Ligature'></span><span class='Number'>2</span></code>:</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGMg4oaQIDEwMCDDlyAzID3ijJzil4vihpUgMiAgIyBBIHJhbmstMiBhcnJheSB0byBhZGQKCmMgKyB4ICAgICAgICAgICAgICAgICAjIDAtY2VsbHMgcGFpcmVkIHdpdGggMS1jZWxscw==">↗️</a><pre>    <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='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  
                  ┘
</pre>
<p>And of course, identical shapes agree:</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=eCArIHggICAgICAgICAgICAgICAgICMgUGFpcndpc2UgYWRkaXRpb24=">↗️</a><pre>    <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 <a href="enclose.html#whats-a-unit">unit</a>, that is, it has no axes, then leading axis agreement reduces to APL's &quot;scalar extension&quot; (where &quot;scalar&quot; is equivalent to BQN's &quot;unit&quot;), 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.</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'>a</span></code> without changing its rank. These are precisely the prefixes of <code><span class='Function'></span><span class='Value'>a</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'>a</span></code>.</p>
<h3 id="search-functions"><a class="header" href="#search-functions">Search functions</a></h3>
<p>The <a href="search.html">search functions</a> 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>), and also <a href="order.html#bins">Bins</a> (<code><span class='Function'>⍋⍒</span></code>), look through cells of one argument to find cells of the other. <a href="find.html">Find</a> (<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 <code><span class='Value'>𝕩</span></code> that match <code><span class='Value'>𝕨</span></code>.</p>
<table>
<thead>
<tr>
<th>Search in</th>
<th>Search 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 searched-in argument is treated as a list of its major cells, and the searched-for argument is considered a collection of cells with the same rank. See the <a href="search.html#higher-ranks">search function documentation</a>.</p>