diff options
Diffstat (limited to 'docs/doc/replicate.html')
| -rw-r--r-- | docs/doc/replicate.html | 233 |
1 files changed, 233 insertions, 0 deletions
diff --git a/docs/doc/replicate.html b/docs/doc/replicate.html new file mode 100644 index 00000000..752fc7e1 --- /dev/null +++ b/docs/doc/replicate.html @@ -0,0 +1,233 @@ +<head> + <link href="../favicon.ico" rel="shortcut icon" type="image/x-icon"/> + <link href="../style.css" rel="stylesheet"/> + <title>BQN: Indices and Replicate</title> +</head> +<div class="nav"><a href="https://github.com/mlochbaum/BQN">BQN</a> / <a href="../index.html">main</a> / <a href="index.html">doc</a></div> +<h1 id="indices-and-replicate">Indices and Replicate</h1> +<svg viewBox='-134.4 -37.4 595.2 198'> + <g font-family='BQN,monospace' font-size='18px' text-anchor='middle'> + <rect class='code' stroke-width='1.5' rx='12' x='-100.8' y='-28.6' width='528' height='180.4'/> + <g class='bluegreen' stroke-width='0' opacity='0.2'> + <rect x='-91.2' y='-15.4' width='508.8' height='30.8'/> + <rect x='-91.2' y='77' width='508.8' height='30.8'/> + </g> + <g font-size='24px' text-anchor='end'> + <text dy='0.29em' x='-33.6' y='0'><tspan class='Value'>𝕨</tspan></text> + <text dy='0.29em' x='-33.6' y='35.2'><tspan class='Value'>𝕩</tspan></text> + <text dy='0.29em' x='-33.6' y='92.4'><tspan class='Value'>𝕨</tspan><tspan class='Function'>/</tspan><tspan class='Value'>𝕩</tspan></text> + <text dy='0.29em' x='-33.6' y='127.6'><tspan class='Function'>/</tspan><tspan class='Value'>𝕨</tspan></text> + </g> + <text dy='0.29em' x='0' y='0'><tspan class='String'>'r'</tspan></text> + <text dy='0.29em' x='48' y='0'><tspan class='String'>'e'</tspan></text> + <text dy='0.29em' x='96' y='0'><tspan class='String'>'p'</tspan></text> + <text dy='0.29em' x='144' y='0'><tspan class='String'>'l'</tspan></text> + <text dy='0.29em' x='192' y='0'><tspan class='String'>'i'</tspan></text> + <text dy='0.29em' x='240' y='0'><tspan class='String'>'c'</tspan></text> + <text dy='0.29em' x='288' y='0'><tspan class='String'>'a'</tspan></text> + <text dy='0.29em' x='336' y='0'><tspan class='String'>'t'</tspan></text> + <text dy='0.29em' x='384' y='0'><tspan class='String'>'e'</tspan></text> + <text dy='0.29em' x='0' y='35.2'><tspan class='Number'>0</tspan></text> + <text dy='0.29em' x='48' y='35.2'><tspan class='Number'>1</tspan></text> + <text dy='0.29em' x='96' y='35.2'><tspan class='Number'>1</tspan></text> + <text dy='0.29em' x='144' y='35.2'><tspan class='Number'>0</tspan></text> + <text dy='0.29em' x='192' y='35.2'><tspan class='Number'>3</tspan></text> + <text dy='0.29em' x='240' y='35.2'><tspan class='Number'>2</tspan></text> + <text dy='0.29em' x='288' y='35.2'><tspan class='Number'>0</tspan></text> + <text dy='0.29em' x='336' y='35.2'><tspan class='Number'>0</tspan></text> + <text dy='0.29em' x='384' y='35.2'><tspan class='Number'>0</tspan></text> + <text dy='0.29em' x='0' y='92.4'><tspan class='String'>'e'</tspan></text> + <text dy='0.29em' x='48' y='92.4'><tspan class='String'>'p'</tspan></text> + <text dy='0.29em' x='96' y='92.4'><tspan class='String'>'i'</tspan></text> + <text dy='0.29em' x='144' y='92.4'><tspan class='String'>'i'</tspan></text> + <text dy='0.29em' x='192' y='92.4'><tspan class='String'>'i'</tspan></text> + <text dy='0.29em' x='240' y='92.4'><tspan class='String'>'c'</tspan></text> + <text dy='0.29em' x='288' y='92.4'><tspan class='String'>'c'</tspan></text> + <text dy='0.29em' x='0' y='127.6'><tspan class='Number'>1</tspan></text> + <text dy='0.29em' x='48' y='127.6'><tspan class='Number'>2</tspan></text> + <text dy='0.29em' x='96' y='127.6'><tspan class='Number'>4</tspan></text> + <text dy='0.29em' x='144' y='127.6'><tspan class='Number'>4</tspan></text> + <text dy='0.29em' x='192' y='127.6'><tspan class='Number'>4</tspan></text> + <text dy='0.29em' x='240' y='127.6'><tspan class='Number'>5</tspan></text> + <text dy='0.29em' x='288' y='127.6'><tspan class='Number'>5</tspan></text> + <g stroke-linecap='round' stroke='currentColor' opacity='0.7'> + <g stroke-width='0.4'> + <line x1='0' x2='0' y1='9.9' y2='23.1'/> + <line x1='144' x2='144' y1='9.9' y2='23.1'/> + <line x1='288' x2='288' y1='9.9' y2='23.1'/> + <line x1='336' x2='336' y1='9.9' y2='23.1'/> + <line x1='384' x2='384' y1='9.9' y2='23.1'/> + </g> + <g stroke-width='1.5'> + <line x1='48' x2='48' y1='9.9' y2='23.1'/> + <line x1='96' x2='96' y1='9.9' y2='23.1'/> + <line x1='192' x2='192' y1='9.9' y2='23.1'/> + <line x1='192' x2='192' y1='9.9' y2='23.1'/> + <line x1='192' x2='192' y1='9.9' y2='23.1'/> + <line x1='240' x2='240' y1='9.9' y2='23.1'/> + <line x1='240' x2='240' y1='9.9' y2='23.1'/> + <line x1='48' x2='2.4' y1='45.1' y2='80.3'/> + <line x1='96' x2='50.4' y1='45.1' y2='80.3'/> + <line x1='192' x2='100.8' y1='45.1' y2='80.3'/> + <line x1='192' x2='146.4' y1='45.1' y2='80.3'/> + <line x1='192' x2='192' y1='45.1' y2='80.3'/> + <line x1='240' x2='240' y1='45.1' y2='80.3'/> + <line x1='240' x2='285.6' y1='45.1' y2='80.3'/> + <line x1='0' x2='0' y1='102.3' y2='115.5'/> + <line x1='48' x2='48' y1='102.3' y2='115.5'/> + <line x1='96' x2='96' y1='102.3' y2='115.5'/> + <line x1='144' x2='144' y1='102.3' y2='115.5'/> + <line x1='192' x2='192' y1='102.3' y2='115.5'/> + <line x1='240' x2='240' y1='102.3' y2='115.5'/> + <line x1='288' x2='288' y1='102.3' y2='115.5'/> + </g> + </g> + </g> +</svg> + +<p>The functions Indices and Replicate are used to copy or filter data. They might be described as transforming a <a href="https://en.wikipedia.org/wiki/Run-length_encoding">run-length encoding</a> into unencoded form. On the other hand, Indices might be described as giving a sparse representation of <code><span class='Value'>𝕩</span></code>, which is smaller if <code><span class='Value'>𝕩</span></code> mostly consists of zeros.</p> +<p>BQN doesn't have any of the various features used in APL to add fills to the result of Replicate, like negative numbers in <code><span class='Value'>𝕨</span></code> or an Expand (<code><span class='Value'>\</span></code>) primitive. An alternative to Expand is to use Replicate with structural Under (<code><span class='Modifier2'>⌾</span></code>) to insert values into an array of fills.</p> +<h2 id="replicate">Replicate</h2> +<p>Given a list of natural numbers <code><span class='Value'>𝕨</span></code>, Replicate repeats each major cell in <code><span class='Value'>𝕩</span></code> the corresponding number of times. That is, <code><span class='Value'>𝕨</span></code> and <code><span class='Value'>𝕩</span></code> must have the same length, and the result includes <code><span class='Value'>i</span><span class='Function'>⊑</span><span class='Value'>𝕨</span></code> copies of each cell <code><span class='Value'>i</span><span class='Function'>⊏</span><span class='Value'>𝕩</span></code>, in order.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MuKAvzHigL8w4oC/MiAvICJhYmNkIgoK4oqiIGEg4oaQID4iYWEwIuKAvyJiYjEi4oC/ImNjMiLigL8iZGQzIgoKMuKAvzHigL8w4oC/MiAvIGE=">↗️</a><pre> <span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>2</span> <span class='Function'>/</span> <span class='String'>"abcd"</span> +"aabdd" + + <span class='Function'>⊢</span> <span class='Value'>a</span> <span class='Gets'>←</span> <span class='Function'>></span><span class='String'>"aa0"</span><span class='Ligature'>‿</span><span class='String'>"bb1"</span><span class='Ligature'>‿</span><span class='String'>"cc2"</span><span class='Ligature'>‿</span><span class='String'>"dd3"</span> +┌─ +╵"aa0 + bb1 + cc2 + dd3" + ┘ + + <span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>2</span> <span class='Function'>/</span> <span class='Value'>a</span> +┌─ +╵"aa0 + aa0 + bb1 + dd3 + dd3" + ┘ +</pre> +<p>It's also allowed for <code><span class='Value'>𝕨</span></code> to be a single number (or enclosed number: it just needs to be a unit and not a list). In this case every cell of <code><span class='Value'>𝕩</span></code> is repeated that number of times.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MyAvICJjb3B5Ig==">↗️</a><pre> <span class='Number'>3</span> <span class='Function'>/</span> <span class='String'>"copy"</span> +"cccooopppyyy" +</pre> +<p>When <code><span class='Value'>𝕨</span></code> is a list of booleans, a cell is never repeated more than once, meaning that each cell of <code><span class='Value'>𝕩</span></code> is either left out (0), or kept in (1). If <code><span class='Function'>Fn</span></code> is a function with a boolean result, <code><span class='Function'>Fn</span><span class='Modifier'>¨</span><span class='Modifier2'>⊸</span><span class='Function'>/</span></code> filters elements of a list according to <code><span class='Function'>Fn</span></code>.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MeKAvzHigL8w4oC/MOKAvzHigL8wIC8gImZpbHRlciIKCuKJpOKfnCdpJyAiZmlsdGVyIgoK4omk4p+cJ2kn4oq4LyAiZmlsdGVyIg==">↗️</a><pre> <span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span> <span class='Function'>/</span> <span class='String'>"filter"</span> +"fie" + + <span class='Function'>≤</span><span class='Modifier2'>⟜</span><span class='String'>'i'</span> <span class='String'>"filter"</span> +⟨ 1 1 0 0 1 0 ⟩ + + <span class='Function'>≤</span><span class='Modifier2'>⟜</span><span class='String'>'i'</span><span class='Modifier2'>⊸</span><span class='Function'>/</span> <span class='String'>"filter"</span> +"fie" +</pre> +<p>Here <code><span class='Function'>≤</span><span class='Modifier2'>⟜</span><span class='String'>'i'</span></code> is a pervasive function, so there's no need to add <code><span class='Modifier'>¨</span></code>. Similarly, to filter major cells of an array, <code><span class='Function'>Fn</span><span class='Modifier'>˘</span><span class='Modifier2'>⊸</span><span class='Function'>/</span></code> could be used, applying <code><span class='Function'>Fn</span></code> to one major cell at a time.</p> +<p>A similar pattern applies to Replicate as well. The function below tests which input characters are double quotes, but by adding one it changes the result to 1 for each non-quote character and 2 for quotes (but source code and display also double quotes here, so the input string has only two <code><span class='String'>"</span></code>s and the output has four).</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=ezErJyInPfCdlal94oq4LyAiZm9yICIiZXNjYXBpbmciIiBxdW90ZXMi">↗️</a><pre> <span class='Brace'>{</span><span class='Number'>1</span><span class='Function'>+</span><span class='String'>'"'</span><span class='Function'>=</span><span class='Value'>𝕩</span><span class='Brace'>}</span><span class='Modifier2'>⊸</span><span class='Function'>/</span> <span class='String'>"for ""escaping"" quotes"</span> +"for """"escaping"""" quotes" +</pre> +<h3 id="compound-replicate">Compound Replicate</h3> +<p>If <code><span class='Value'>𝕨</span></code> has <a href="depth.html">depth</a> two, then its elements give the amounts to copy along each <a href="leading.html">leading axis</a> of <code><span class='Value'>𝕩</span></code>.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGIg4oaQIDLigL81IOKliiDihpUxMAoK4p+oMuKAvzAsIDHigL8w4oC/MOKAvzHigL8x4p+pIC8gYgoKMuKAvzAgLyAx4oC/MOKAvzDigL8x4oC/MeKKuC/LmCBi">↗️</a><pre> <span class='Function'>⊢</span> <span class='Value'>b</span> <span class='Gets'>←</span> <span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>5</span> <span class='Function'>⥊</span> <span class='Function'>↕</span><span class='Number'>10</span> +┌─ +╵ 0 1 2 3 4 + 5 6 7 8 9 + ┘ + + <span class='Bracket'>⟨</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Separator'>,</span> <span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Bracket'>⟩</span> <span class='Function'>/</span> <span class='Value'>b</span> +┌─ +╵ 0 3 4 + 0 3 4 + ┘ + + <span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>0</span> <span class='Function'>/</span> <span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Modifier2'>⊸</span><span class='Function'>/</span><span class='Modifier'>˘</span> <span class='Value'>b</span> +┌─ +╵ 0 3 4 + 0 3 4 + ┘ +</pre> +<p>Here the <code><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>0</span></code> indicates that the first row of <code><span class='Value'>b</span></code> is copied twice and the second is ignored, while <code><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>1</span></code> picks out three entries from that row. <code><span class='Value'>𝕩</span></code> can also have more axes than elements of <code><span class='Value'>𝕨</span></code>, and the trailing ones aren't changed, just like the simpler case. However, <code><span class='Value'>𝕨</span></code> has to have at least as many elements as <code><span class='Value'>𝕩</span></code> has axes (so <code><span class='Paren'>(</span><span class='Function'>≠</span><span class='Value'>𝕨</span><span class='Paren'>)</span><span class='Function'>≥=</span><span class='Value'>𝕩</span></code>), and each element has to have the same length as the corresponding axis in <code><span class='Value'>𝕩</span></code>—or it can be a unit, as shown below.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4p+oPDIsPDPin6kgLyBi">↗️</a><pre> <span class='Bracket'>⟨</span><span class='Function'><</span><span class='Number'>2</span><span class='Separator'>,</span><span class='Function'><</span><span class='Number'>3</span><span class='Bracket'>⟩</span> <span class='Function'>/</span> <span class='Value'>b</span> +┌─ +╵ 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 + 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 + 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 + 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 + ┘ +</pre> +<p>Above, both elements of <code><span class='Value'>𝕨</span></code> are enclosed numbers. An individual element doesn't have to be enclosed, but I don't recommend this, since if <em>none</em> of them are enclosed, then <code><span class='Value'>𝕨</span></code> will have depth 1 and it will be interpreted as replicating along the first axis only.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4p+oMiwz4p+pIC8gYg==">↗️</a><pre> <span class='Bracket'>⟨</span><span class='Number'>2</span><span class='Separator'>,</span><span class='Number'>3</span><span class='Bracket'>⟩</span> <span class='Function'>/</span> <span class='Value'>b</span> +┌─ +╵ 0 1 2 3 4 + 0 1 2 3 4 + 5 6 7 8 9 + 5 6 7 8 9 + 5 6 7 8 9 + ┘ +</pre> +<p>The example above has a different result from the previous one! The function <code><span class='Function'><</span><span class='Modifier'>¨</span><span class='Modifier2'>⊸</span><span class='Function'>/</span></code> could be used in place of <code><span class='Function'>/</span></code> to replicate each axis by a different number.</p> +<p>If <code><span class='Value'>𝕨</span></code> is <code><span class='Bracket'>⟨⟩</span></code>, then it has depth 1, but is handled with the multidimensional case anyway, giving a result of <code><span class='Value'>𝕩</span></code>. The one-dimensional case could also only ever return <code><span class='Value'>𝕩</span></code>, but it would be required to have length 0, so this convention still only extends the simple case.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YiDiiaEg4p+o4p+pIC8gYg==">↗️</a><pre> <span class='Value'>b</span> <span class='Function'>≡</span> <span class='Bracket'>⟨⟩</span> <span class='Function'>/</span> <span class='Value'>b</span> +1 +</pre> +<h2 id="indices">Indices</h2> +<p>The monadic form of <code><span class='Function'>/</span></code> is much simpler than the dyadic one, with no multidimensional case or mismatched argument ranks. <code><span class='Value'>𝕩</span></code> must be a list of natural numbers, and <code><span class='Function'>/</span><span class='Value'>𝕩</span></code> is the list <code><span class='Value'>𝕩</span><span class='Function'>/↕≠</span><span class='Value'>𝕩</span></code>. Its elements are the indices for <code><span class='Value'>𝕩</span></code>, with index <code><span class='Value'>i</span></code> repeated <code><span class='Value'>i</span><span class='Function'>⊑</span><span class='Value'>𝕩</span></code> times.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=LyAz4oC/MOKAvzHigL8y">↗️</a><pre> <span class='Function'>/</span> <span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>2</span> +⟨ 0 0 0 2 3 3 ⟩ +</pre> +<p>A unit argument isn't allowed, and isn't very useful: for example, <code><span class='Function'>/</span><span class='Number'>6</span></code> might indicate an array of six zeros, but this can be written <code><span class='Function'>/⥊</span><span class='Number'>6</span></code> or <code><span class='Number'>6</span><span class='Function'>⥊</span><span class='Number'>0</span></code> with hardly any extra effort.</p> +<p>When <code><span class='Value'>𝕨</span></code> has rank 1, <code><span class='Value'>𝕨</span><span class='Function'>/</span><span class='Value'>𝕩</span></code> is equivalent to <code><span class='Value'>𝕨</span><span class='Function'>/</span><span class='Modifier2'>⊸</span><span class='Function'>⊏</span><span class='Value'>𝕩</span></code>. Of course, this isn't the only use of Indices. It also gets along well with <a href="group.html">Group</a>: for example, <code><span class='Function'>/</span><span class='Modifier2'>⊸</span><span class='Function'>⊔</span></code> groups <code><span class='Value'>𝕩</span></code> according to a list of lengths <code><span class='Value'>𝕨</span></code>.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MuKAvzXigL8w4oC/MSAv4oq44oqUICJBQkNERUZHSCI=">↗️</a><pre> <span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>5</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span> <span class='Function'>/</span><span class='Modifier2'>⊸</span><span class='Function'>⊔</span> <span class='String'>"ABCDEFGH"</span> +⟨ "AB" "CDEFG" ⟨⟩ "H" ⟩ +</pre> +<p>This function will fail to include trailing empty arrays; the modification <code><span class='Paren'>(</span><span class='Function'>/∾</span><span class='Modifier2'>⟜</span><span class='Number'>1</span><span class='Paren'>)</span><span class='Modifier2'>⊸</span><span class='Function'>⊔</span></code> fixes this and ensures the result always has as many elements as <code><span class='Value'>𝕨</span></code>.</p> +<p>If <code><span class='Value'>𝕩</span></code> is boolean then <code><span class='Function'>/</span><span class='Value'>𝕩</span></code> contains all the indices where a 1 appears in <code><span class='Value'>𝕩</span></code>. Applying <code><span class='Function'>-</span><span class='Modifier2'>⟜</span><span class='Function'>»</span></code> to the result gives the distance from each 1 to the previous, or to the start of the list, another potentially useful function.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=LyAw4oC/MeKAvzDigL8x4oC/MOKAvzDigL8w4oC/MOKAvzHigL8wCgot4p+cwrsgLyAw4oC/MeKAvzDigL8x4oC/MOKAvzDigL8w4oC/MOKAvzHigL8w">↗️</a><pre> <span class='Function'>/</span> <span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span> +⟨ 1 3 8 ⟩ + + <span class='Function'>-</span><span class='Modifier2'>⟜</span><span class='Function'>»</span> <span class='Function'>/</span> <span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span> +⟨ 1 2 5 ⟩ +</pre> +<p>With more effort we can also use <code><span class='Function'>/</span></code> to analyze groups of 1s in the argument (and of course all these methods can be applied to 0s instead, by first flipping the values with <code><span class='Function'>¬</span></code>). First we highlight the start and end of each group by comparing the list with a shifted copy of itself. Or rather, we'll first place a 0 at the front and then at the end, in order to detect when a group starts at the beginning of the list or ends at the end (there's also a <a href="shift.html">shift</a>-based version, <code><span class='Function'>≠</span><span class='Modifier2'>⟜</span><span class='Function'>«</span><span class='Number'>0</span><span class='Function'>∾</span><span class='Value'>𝕩</span></code>).</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MCAo4oi+4omN4oi+y5wpIDDigL8x4oC/MeKAvzHigL8w4oC/MOKAvzHigL8w4oC/MeKAvzHigL8wCgowICjiiL7iiaDiiL7LnCkgMOKAvzHigL8x4oC/MeKAvzDigL8w4oC/MeKAvzDigL8x4oC/MeKAvzAKCi8gMCjiiL7iiaDiiL7LnCkgMOKAvzHigL8x4oC/MeKAvzDigL8w4oC/MeKAvzDigL8x4oC/MeKAvzA=">↗️</a><pre> <span class='Number'>0</span> <span class='Paren'>(</span><span class='Function'>∾≍∾</span><span class='Modifier'>˜</span><span class='Paren'>)</span> <span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span> +┌─ +╵ 0 0 1 1 1 0 0 1 0 1 1 0 + 0 1 1 1 0 0 1 0 1 1 0 0 + ┘ + + <span class='Number'>0</span> <span class='Paren'>(</span><span class='Function'>∾≠∾</span><span class='Modifier'>˜</span><span class='Paren'>)</span> <span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span> +⟨ 0 1 0 0 1 0 1 1 1 0 1 0 ⟩ + + <span class='Function'>/</span> <span class='Number'>0</span><span class='Paren'>(</span><span class='Function'>∾≠∾</span><span class='Modifier'>˜</span><span class='Paren'>)</span> <span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span> +⟨ 1 4 6 7 8 10 ⟩ +</pre> +<p>So now we have the indices of each transition from 0 to 1 or 1 to 0, in an extended list with 0 added at the beginning and end. The first index has to be for a 0 to 1 transition, because we forced the first value to be a 0, and then the next can only be 1 to 0, then 0 to 1, and so on until the last, which must be 1 to 0 because the last value is also 0.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=LcucYMuYIOKImOKAvzLipYovIDAo4oi+4omg4oi+y5wpIDDigL8x4oC/MeKAvzHigL8w4oC/MOKAvzHigL8w4oC/MeKAvzHigL8w">↗️</a><pre> <span class='Function'>-</span><span class='Modifier'>˜`˘</span> <span class='Modifier2'>∘</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Function'>⥊/</span> <span class='Number'>0</span><span class='Paren'>(</span><span class='Function'>∾≠∾</span><span class='Modifier'>˜</span><span class='Paren'>)</span> <span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span> +┌─ +╵ 1 3 + 6 1 + 8 2 + ┘ +</pre> +<p>This means the transitions can be grouped exactly in pairs, the beginning and end of each group. Reshape with a <a href="reshape.html#computed-lengths">computed length</a> <code><span class='Modifier2'>∘</span><span class='Ligature'>‿</span><span class='Number'>2</span></code> groups these pairs, and then a scan <code><span class='Function'>-</span><span class='Modifier'>˜`˘</span></code> can be used to convert the start/end format to start/length if wanted.</p> +<h3 id="inverse">Inverse</h3> +<p>The result of Indices <code><span class='Function'>/</span><span class='Value'>n</span></code> is an ordered list of natural numbers, where the number <code><span class='Value'>i</span></code> appears <code><span class='Value'>i</span><span class='Function'>⊑</span><span class='Value'>n</span></code> times. Given an ordered list of natural numbers <code><span class='Value'>k</span></code>, the <em>inverse</em> of indices returns a corresponding <code><span class='Value'>n</span></code>: one where the value <code><span class='Value'>i</span><span class='Function'>⊑</span><span class='Value'>n</span></code> is the number of times <code><span class='Value'>i</span></code> appears in <code><span class='Value'>k</span></code>.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=LyAz4oC/MuKAvzEKCi/igbwgMOKAvzDigL8w4oC/MeKAvzHigL8y">↗️</a><pre> <span class='Function'>/</span> <span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>1</span> +⟨ 0 0 0 1 1 2 ⟩ + + <span class='Function'>/</span><span class='Modifier'>⁼</span> <span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>2</span> +⟨ 3 2 1 ⟩ +</pre> +<p>Finding how many times each index appears in a list of indices is often a useful thing to do, and there are a few ways to do it:</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=K8udy5ggKOKGlTUpID3ijJwgMuKAvzLigL804oC/MeKAvzLigL8wICAjIEluZWZmaWNpZW50CgriiaDCqOKKlCAy4oC/MuKAvzTigL8x4oC/MuKAvzAKCi/igbziiKcgMuKAvzLigL804oC/MeKAvzLigL8w">↗️</a><pre> <span class='Function'>+</span><span class='Modifier'>˝˘</span> <span class='Paren'>(</span><span class='Function'>↕</span><span class='Number'>5</span><span class='Paren'>)</span> <span class='Function'>=</span><span class='Modifier'>⌜</span> <span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>4</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>0</span> <span class='Comment'># Inefficient +</span>⟨ 1 1 3 0 1 ⟩ + + <span class='Function'>≠</span><span class='Modifier'>¨</span><span class='Function'>⊔</span> <span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>4</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>0</span> +⟨ 1 1 3 0 1 ⟩ + + <span class='Function'>/</span><span class='Modifier'>⁼</span><span class='Function'>∧</span> <span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>4</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>0</span> +⟨ 1 1 3 0 1 ⟩ +</pre> +<p>For <code><span class='Function'>/</span><span class='Modifier'>⁼</span></code> to work, the argument has to be sorted: otherwise it won't be a valid result of <code><span class='Function'>/</span></code>. But sorting with <code><span class='Function'>∧</span></code> is no problem, and <code><span class='Function'>/</span><span class='Modifier'>⁼</span><span class='Function'>∧</span></code> will probably be faster than <code><span class='Function'>≠</span><span class='Modifier'>¨</span><span class='Function'>⊔</span></code> in the absence of special handling for either combination.</p> |
