aboutsummaryrefslogtreecommitdiff
path: root/docs/doc/reshape.html
blob: 963d03a3452c35de40fd93df0f5cf7217227012a (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
<head>
  <link href="../favicon.ico" rel="shortcut icon" type="image/x-icon"/>
  <link href="../style.css" rel="stylesheet"/>
  <title>BQN: Deshape and Reshape</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="deshape-and-reshape"><a class="header" href="#deshape-and-reshape">Deshape and Reshape</a></h1>
<svg viewBox='-64 -15 608 377'>
  <g fill='currentColor' stroke-linecap='round' font-family='BQN,monospace'>
    <rect class='code' stroke-width='1.5' rx='12' x='0' y='0' width='480' height='347'/>
    <g font-size='22px' fill='currentColor' opacity='0.9'><text dy='0.33em' x='23' y='-2'>Index order</text></g>
    <path stroke='#3b285c' fill='none' stroke-width='4' stroke-linejoin='round' opacity='0.5' d='M28 51L452 51L28 87L452 87L28 159L452 159L28 195L452 195L28 267L452 267L28 303L452 303M68 39l24 12l-24 12M196 147l24 12l-24 12M388 291l24 12l-24 12'/>
    <path class='bluegreen' stroke-width='3' style='fill:none' opacity='0.7' d='M441 324h20v-20M49 30h-30v30m0 16v12'/>
    <g font-size='18px' text-anchor='end'>
      <text dy='0.33em' x='64' y='51'><tspan class='Number'>100</tspan></text>
      <text dy='0.33em' x='128' y='51'><tspan class='Number'>101</tspan></text>
      <text dy='0.33em' x='192' y='51'><tspan class='Number'>102</tspan></text>
      <text dy='0.33em' x='256' y='51'><tspan class='Number'>103</tspan></text>
      <text dy='0.33em' x='320' y='51'><tspan class='Number'>104</tspan></text>
      <text dy='0.33em' x='384' y='51'><tspan class='Number'>105</tspan></text>
      <text dy='0.33em' x='448' y='51'><tspan class='Number'>106</tspan></text>
      <text dy='0.33em' x='64' y='87'><tspan class='Number'>150</tspan></text>
      <text dy='0.33em' x='128' y='87'><tspan class='Number'>151</tspan></text>
      <text dy='0.33em' x='192' y='87'><tspan class='Number'>152</tspan></text>
      <text dy='0.33em' x='256' y='87'><tspan class='Number'>153</tspan></text>
      <text dy='0.33em' x='320' y='87'><tspan class='Number'>154</tspan></text>
      <text dy='0.33em' x='384' y='87'><tspan class='Number'>155</tspan></text>
      <text dy='0.33em' x='448' y='87'><tspan class='Number'>156</tspan></text>
      <text dy='0.33em' x='64' y='159'><tspan class='Number'>0</tspan></text>
      <text dy='0.33em' x='128' y='159'><tspan class='Number'>1</tspan></text>
      <text dy='0.33em' x='192' y='159'><tspan class='Number'>2</tspan></text>
      <text dy='0.33em' x='256' y='159'><tspan class='Number'>3</tspan></text>
      <text dy='0.33em' x='320' y='159'><tspan class='Number'>4</tspan></text>
      <text dy='0.33em' x='384' y='159'><tspan class='Number'>5</tspan></text>
      <text dy='0.33em' x='448' y='159'><tspan class='Number'>6</tspan></text>
      <text dy='0.33em' x='64' y='195'><tspan class='Number'>50</tspan></text>
      <text dy='0.33em' x='128' y='195'><tspan class='Number'>51</tspan></text>
      <text dy='0.33em' x='192' y='195'><tspan class='Number'>52</tspan></text>
      <text dy='0.33em' x='256' y='195'><tspan class='Number'>53</tspan></text>
      <text dy='0.33em' x='320' y='195'><tspan class='Number'>54</tspan></text>
      <text dy='0.33em' x='384' y='195'><tspan class='Number'>55</tspan></text>
      <text dy='0.33em' x='448' y='195'><tspan class='Number'>56</tspan></text>
      <text dy='0.33em' x='64' y='267'><tspan class='Number'>200</tspan></text>
      <text dy='0.33em' x='128' y='267'><tspan class='Number'>201</tspan></text>
      <text dy='0.33em' x='192' y='267'><tspan class='Number'>202</tspan></text>
      <text dy='0.33em' x='256' y='267'><tspan class='Number'>203</tspan></text>
      <text dy='0.33em' x='320' y='267'><tspan class='Number'>204</tspan></text>
      <text dy='0.33em' x='384' y='267'><tspan class='Number'>205</tspan></text>
      <text dy='0.33em' x='448' y='267'><tspan class='Number'>206</tspan></text>
      <text dy='0.33em' x='64' y='303'><tspan class='Number'>250</tspan></text>
      <text dy='0.33em' x='128' y='303'><tspan class='Number'>251</tspan></text>
      <text dy='0.33em' x='192' y='303'><tspan class='Number'>252</tspan></text>
      <text dy='0.33em' x='256' y='303'><tspan class='Number'>253</tspan></text>
      <text dy='0.33em' x='320' y='303'><tspan class='Number'>254</tspan></text>
      <text dy='0.33em' x='384' y='303'><tspan class='Number'>255</tspan></text>
      <text dy='0.33em' x='448' y='303'><tspan class='Number'>256</tspan></text>
    </g>
  </g>
</svg>

<p>The glyph <code><span class='Function'></span></code> indicates BQN's facilities to reflow the data in an array, giving it a different shape. Its monadic form, Deshape, simply removes all shape information, returning a list of all the elements from the array in reading order. With a left argument, <code><span class='Function'></span></code> is called Reshape and is a more versatile tool for rearranging the data in an array into the desired shape.</p>
<p>Because of its dependence on the reading order of an array, Reshape is less fundamental than other array operations. Using Reshape in the central computations of a program can be a sign of imperfect usage of arrays. For example, it may be useful to use Reshape to create a constant array or repeat a sequence of values several times, but the same task might also be accomplished more simply with <a href="map.html#table">Table</a> <code><span class='Modifier'></span></code>, or by taking advantage of <a href="leading.html#leading-axis-agreement">leading axis agreement</a> in arithmetic primitives.</p>
<h2 id="deshape"><a class="header" href="#deshape">Deshape</a></h2>
<p>The result of Deshape is a list containing the same elements as the argument.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGEg4oaQICvijJzCtCDin6gxMDDigL8yMDAsIDMw4oC/NDAsIDXigL824oC/N+KfqQoK4qWKIGE=">↗️</a><pre>    <span class='Function'></span> <span class='Value'>a</span> <span class='Gets'></span> <span class='Function'>+</span><span class='Modifier'>⌜´</span> <span class='Bracket'></span><span class='Number'>100</span><span class='Ligature'></span><span class='Number'>200</span><span class='Separator'>,</span> <span class='Number'>30</span><span class='Ligature'></span><span class='Number'>40</span><span class='Separator'>,</span> <span class='Number'>5</span><span class='Ligature'></span><span class='Number'>6</span><span class='Ligature'></span><span class='Number'>7</span><span class='Bracket'></span>
┌─             
╎ 135 136 137  
  145 146 147  
               
  235 236 237  
  245 246 247  
              ┘

    <span class='Function'></span> <span class='Value'>a</span>
⟨ 135 136 137 145 146 147 235 236 237 245 246 247 ⟩
</pre>
<p>The elements are ordered in reading order—left to right, then top to bottom. This means that leading axes &quot;matter more&quot; for ordering: if one element comes earlier in the first axis but later in the second than some other element, it will come first in the result. In another view, elements are ordered according to their <a href="indices.html">indices</a>. In other words, deshaping the array of indices for an array will always give a <a href="order.html">sorted</a> array.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oaV4omiYQoK4o2LIOKliiDihpXiiaJh">↗️</a><pre>    <span class='Function'>↕≢</span><span class='Value'>a</span>
┌─                               
╎ ⟨ 0 0 0 ⟩ ⟨ 0 0 1 ⟩ ⟨ 0 0 2 ⟩  
  ⟨ 0 1 0 ⟩ ⟨ 0 1 1 ⟩ ⟨ 0 1 2 ⟩  
                                 
  ⟨ 1 0 0 ⟩ ⟨ 1 0 1 ⟩ ⟨ 1 0 2 ⟩  
  ⟨ 1 1 0 ⟩ ⟨ 1 1 1 ⟩ ⟨ 1 1 2 ⟩  
                                ┘

    <span class='Function'></span> <span class='Function'></span> <span class='Function'>↕≢</span><span class='Value'>a</span>
⟨ 0 1 2 3 4 5 6 7 8 9 10 11 ⟩
</pre>
<p>This ordering is also known as <em>row-major</em> order.</p>
<p>Deshape turns a unit argument into a single-element list, automatically <a href="enclose.html">enclosing</a> it if it's an atom. However, if you know <code><span class='Value'>𝕩</span></code> is a unit, a more principled way to turn it into a list is to apply <a href="couple.html">Solo</a> (<code><span class='Function'></span></code>), which adds a length-1 axis before any other axes. If you ever add axes to the data format, Solo is more likely to continue working after this transition, unless there's a reason the result should always be a list.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4qWKIDIK4omNIDI=">↗️</a><pre>    <span class='Function'></span> <span class='Number'>2</span>
⟨ 2 ⟩
    <span class='Function'></span> <span class='Number'>2</span>
⟨ 2 ⟩
</pre>
<h2 id="reshape"><a class="header" href="#reshape">Reshape</a></h2>
<p>While Deshape removes all shape information from its argument array, Reshape adds shape information back based on the left argument. Reshape ignores the shape of its original argument, treating it like a list of elements as though it were deshaped initially.</p>
<p>The left argument of Reshape gives the shape of the result, except that one entry can be left unspecified for BQN to fill in. We'll look at the cases where a full shape is given first.</p>
<h3 id="matching-lengths"><a class="header" href="#matching-lengths">Matching lengths</a></h3>
<p>If the number of elements implied by the given shape—that is, <code><span class='Function'>×</span><span class='Modifier'>´</span><span class='Value'>𝕨</span></code>—is equal to the number of elements in <code><span class='Value'>𝕩</span></code>, then <code><span class='Value'>𝕩</span></code> is simply rearranged to match that shape. The element list is kept the same, so that the deshaped result matches the deshaped right argument.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YQoKNuKAvzIg4qWKIGEKCijipYphKSDiiaEg4qWKIDbigL8y4qWKYQ==">↗️</a><pre>    <span class='Value'>a</span>
┌─             
╎ 135 136 137  
  145 146 147  
               
  235 236 237  
  245 246 247  
              ┘

    <span class='Number'>6</span><span class='Ligature'></span><span class='Number'>2</span> <span class='Function'></span> <span class='Value'>a</span>
┌─         
╵ 135 136  
  137 145  
  146 147  
  235 236  
  237 245  
  246 247  
          ┘

    <span class='Paren'>(</span><span class='Function'></span><span class='Value'>a</span><span class='Paren'>)</span> <span class='Function'></span> <span class='Function'></span> <span class='Number'>6</span><span class='Ligature'></span><span class='Number'>2</span><span class='Function'></span><span class='Value'>a</span>
1
</pre>
<p>One common use is to generate an array with a specified shape that counts up from 0 in reading order, a reshaped <a href="range.html">Range</a>. The idiomatic phrase to do this is <code><span class='Function'></span><span class='Modifier2'></span><span class='Paren'>(</span><span class='Function'>↕×</span><span class='Modifier'>´</span><span class='Paren'>)</span></code>, since it doesn't require writing the shape and its product separately.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MuKAvzcg4qWKIOKGlTE0CuKliuKfnCjihpXDl8K0KSAy4oC/Nw==">↗️</a><pre>    <span class='Number'>2</span><span class='Ligature'></span><span class='Number'>7</span> <span class='Function'></span> <span class='Function'></span><span class='Number'>14</span>
┌─                   
╵ 0 1 2  3  4  5  6  
  7 8 9 10 11 12 13  
                    ┘
    <span class='Function'></span><span class='Modifier2'></span><span class='Paren'>(</span><span class='Function'>↕×</span><span class='Modifier'>´</span><span class='Paren'>)</span> <span class='Number'>2</span><span class='Ligature'></span><span class='Number'>7</span>
┌─                   
╵ 0 1 2  3  4  5  6  
  7 8 9 10 11 12 13  
                    ┘
</pre>
<h3 id="non-matching-lengths"><a class="header" href="#non-matching-lengths">Non-matching lengths</a></h3>
<p>If <code><span class='Value'>𝕨</span></code> implies a smaller number of elements than are present initially, then only the initial elements of <code><span class='Value'>𝕩</span></code> are used. Here the result stops at <code><span class='Number'>237</span></code>, three-quarters of the way through <code><span class='Value'>a</span></code>, because at that point the result is filled up.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=M+KAvzMg4qWKIGE=">↗️</a><pre>    <span class='Number'>3</span><span class='Ligature'></span><span class='Number'>3</span> <span class='Function'></span> <span class='Value'>a</span>
┌─             
╵ 135 136 137  
  145 146 147  
  235 236 237  
              ┘
</pre>
<p>If <code><span class='Value'>𝕨</span></code> implies a larger number of elements, then elements of <code><span class='Value'>𝕩</span></code> are reused cyclically. Below, we reach the last element <code><span class='Number'>247</span></code> and start over at <code><span class='Number'>135</span></code>. If <code><span class='Value'>𝕩</span></code> doesn't have any elements to start with, you'll get an error as there aren't any elements available.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MTUg4qWKIGEKCjQg4qWKIOKGlTA=">↗️</a><pre>    <span class='Number'>15</span> <span class='Function'></span> <span class='Value'>a</span>
⟨ 135 136 137 145 146 147 235 236 237 245 246 247 135 136 137 ⟩

    <span class='Number'>4</span> <span class='Function'></span> <span class='Function'></span><span class='Number'>0</span>
<span class='Error'>Error: ⥊: Empty 𝕩 and non-empty result</span>
</pre>
<p>Reshape is the idiomatic way to make an array filled with a constant value (that is, where all elements are the same) when you know what shape it should have. For an atom element, reshape it directly; for an arbitrary element, first <a href="enclose.html">enclose</a> it to create a unit, and then reshape it.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=M+KAvzQg4qWKIDAKCjUg4qWKIDwgInN0cmluZyI=">↗️</a><pre>    <span class='Number'>3</span><span class='Ligature'></span><span class='Number'>4</span> <span class='Function'></span> <span class='Number'>0</span>
┌─         
╵ 0 0 0 0  
  0 0 0 0  
  0 0 0 0  
          ┘

    <span class='Number'>5</span> <span class='Function'></span> <span class='Function'>&lt;</span> <span class='String'>&quot;string&quot;</span>
⟨ "string" "string" "string" "string" "string" ⟩
</pre>
<h3 id="computed-lengths"><a class="header" href="#computed-lengths">Computed lengths</a></h3>
<p>What if you want to reshape an array into, say, rows of length 2, but don't want to write out the number of rows?</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oiY4oC/MiDipYogImFBZUVpSW9PdVUi">↗️</a><pre>    <span class='Modifier2'></span><span class='Ligature'></span><span class='Number'>2</span> <span class='Function'></span> <span class='String'>&quot;aAeEiIoOuU&quot;</span>
┌─    
╵"aA  
  eE  
  iI  
  oO  
  uU" 
     ┘
</pre>
<p>Above, the length given is <code><span class='Modifier2'></span></code>, a special value that indicates that a length that fits the argument should be computed. In fact, Reshape has four different special values that can be used. Every one works the same for a case like the one above, where the rest of the shape divides the argument length evenly. They differ in how they handle uneven cases, where the required length would fall between two whole numbers.</p>
<ul>
<li><code><span class='Modifier2'></span></code> says the length must be an exact fit, and gives an error in such a case.</li>
<li><code><span class='Function'></span></code> rounds the length down, so that some elements are discarded.</li>
<li><code><span class='Function'></span></code> rounds the length up, repeating elements to make up the difference.</li>
<li><code><span class='Function'></span></code> rounds the length up, but uses the argument's fill for the needed extra elements.</li>
</ul>
<p>These values are just BQN primitives of course. They're not called by Reshape or anything like that; the primitives are just chosen to suggest the corresponding functionality.</p>
<p>Here's an example of the four cases. If we try to turn five elements into two rows, <code><span class='Modifier2'></span></code> gives an error, <code><span class='Function'></span></code> drops the last element, <code><span class='Function'></span></code> uses the first element again, and <code><span class='Function'></span></code> uses a fill element (like <code><span class='Number'>6</span><span class='Function'></span><span class='String'>&quot;abcde&quot;</span></code> would).</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MuKAv+KImCDipYogImFiY2RlIgoKMuKAv+KMiiDipYogImFiY2RlIgoKMuKAv+KMvSDipYogImFiY2RlIgoKMuKAv+KGkSDipYogImFiY2RlIg==">↗️</a><pre>    <span class='Number'>2</span><span class='Ligature'></span><span class='Modifier2'></span> <span class='Function'></span> <span class='String'>&quot;abcde&quot;</span>
<span class='Error'>Error: ⥊: Shape must be exact when reshaping with ∘</span>

    <span class='Number'>2</span><span class='Ligature'></span><span class='Function'></span> <span class='Function'></span> <span class='String'>&quot;abcde&quot;</span>
┌─    
╵"ab  
  cd" 
     ┘

    <span class='Number'>2</span><span class='Ligature'></span><span class='Function'></span> <span class='Function'></span> <span class='String'>&quot;abcde&quot;</span>
┌─     
╵"abc  
  dea" 
      ┘

    <span class='Number'>2</span><span class='Ligature'></span><span class='Function'></span> <span class='Function'></span> <span class='String'>&quot;abcde&quot;</span>
┌─     
╵"abc  
  de " 
      ┘
</pre>
<p>A computed length can be useful to input an array without using nested notation: for example, if you have a table with rows of three elements, you might write it as one long list, using <code><span class='Modifier2'></span><span class='Ligature'></span><span class='Number'>3</span><span class='Function'></span><span class='Bracket'></span><span class='Value'></span><span class='Bracket'></span></code> to get it back to the appropriate shape. <code><span class='Modifier2'></span></code> is definitely the value to use here, as it will check that you haven't missed an element or something like that.</p>
<p>Computed Reshape might also be used in actual data processing: for example, to sum a list in groups of four, you might first reshape it using <code><span class='Function'></span><span class='Ligature'></span><span class='Number'>4</span></code> for the shape, then average the rows. Here the code <code><span class='Function'></span></code> is useful because added fill elements of <code><span class='Number'>0</span></code> won't change the sum, so that if the last group doesn't have four elements (<code><span class='Number'>9</span><span class='Ligature'></span><span class='Number'>7</span></code> below), it will still be summed correctly.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=K8K0y5gg4oaR4oC/NCDipYog4p+oMCwyLDEsMSwgNSw5LDYsNCwgMywzLDMsMywgOSw34p+p">↗️</a><pre>    <span class='Function'>+</span><span class='Modifier'>´˘</span> <span class='Function'></span><span class='Ligature'></span><span class='Number'>4</span> <span class='Function'></span> <span class='Bracket'></span><span class='Number'>0</span><span class='Separator'>,</span><span class='Number'>2</span><span class='Separator'>,</span><span class='Number'>1</span><span class='Separator'>,</span><span class='Number'>1</span><span class='Separator'>,</span> <span class='Number'>5</span><span class='Separator'>,</span><span class='Number'>9</span><span class='Separator'>,</span><span class='Number'>6</span><span class='Separator'>,</span><span class='Number'>4</span><span class='Separator'>,</span> <span class='Number'>3</span><span class='Separator'>,</span><span class='Number'>3</span><span class='Separator'>,</span><span class='Number'>3</span><span class='Separator'>,</span><span class='Number'>3</span><span class='Separator'>,</span> <span class='Number'>9</span><span class='Separator'>,</span><span class='Number'>7</span><span class='Bracket'></span>
⟨ 4 24 12 16 ⟩
</pre>
<p>Computed Reshape can even be used with <a href="under.html#structural-under">structural Under</a>. Only the <code><span class='Modifier2'></span></code> case really makes sense, although <code><span class='Function'></span></code>, which leaves trailing elements unchanged, could conceivably be useful. Below, we split one argument into three groups and <a href="reverse.html">reverse</a> their order, and reverse groups of three in another.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oy94oy+KDPigL/iiJjiirjipYopIOKGlTE1CgrijL3ijL4o4oiY4oC/M+KKuOKliikgIm5vbHlyaWNzaGVyZSI=">↗️</a><pre>    <span class='Function'></span><span class='Modifier2'></span><span class='Paren'>(</span><span class='Number'>3</span><span class='Ligature'></span><span class='Modifier2'>∘⊸</span><span class='Function'></span><span class='Paren'>)</span> <span class='Function'></span><span class='Number'>15</span>
⟨ 10 11 12 13 14 5 6 7 8 9 0 1 2 3 4 ⟩

    <span class='Function'></span><span class='Modifier2'></span><span class='Paren'>(</span><span class='Modifier2'></span><span class='Ligature'></span><span class='Number'>3</span><span class='Modifier2'></span><span class='Function'></span><span class='Paren'>)</span> <span class='String'>&quot;nolyricshere&quot;</span>
"erecshyrinol"
</pre>