aboutsummaryrefslogtreecommitdiff
path: root/docs/doc/transpose.html
blob: f86029a7def8f7584295083ddbbb11318dbc866a (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
<head>
  <link href="../favicon.ico" rel="shortcut icon" type="image/x-icon"/>
  <link href="../style.css" rel="stylesheet"/>
  <title>BQN: Transpose</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="transpose">Transpose</h1>
<p>As in APL, Transpose (<code><span class='Function'></span></code>) is a tool for rearranging the axes of an array. BQN's version is tweaked to align better with the leading axis model and make common operations easier.</p>
<h2 id="transpose-basics">Transpose basics</h2>
<p>The name for the primitive <code><span class='Function'></span></code> comes from the <a href="https://en.wikipedia.org/wiki/Transpose">Transpose</a> operation on matrices. Given a matrix as an array of rank 2, <code><span class='Function'></span></code> will transpose it:</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIG1hdCDihpAgMuKAvzMg4qWKIOKGlTYK4o2JIG1hdA==">↗️</a><pre>    <span class='Function'></span> <span class='Value'>mat</span> <span class='Gets'></span> <span class='Number'>2</span><span class='Ligature'></span><span class='Number'>3</span> <span class='Function'></span> <span class='Function'></span><span class='Number'>6</span>
┌─       
╵ 0 1 2  
  3 4 5  
        ┘
    <span class='Function'></span> <span class='Value'>mat</span>
┌─     
╵ 0 3  
  1 4  
  2 5  
      ┘
</pre>
<p>Transpose is named this way because it exchanges the two axes of the matrix. Above you can see that while <code><span class='Value'>mat</span></code> has shape <code><span class='Number'>2</span><span class='Ligature'></span><span class='Number'>3</span></code>, <code><span class='Function'></span><span class='Value'>mat</span></code> has shape <code><span class='Number'>3</span><span class='Ligature'></span><span class='Number'>2</span></code>, and we can also check that the element at <a href="indices.html">index</a> <code><span class='Value'>i</span><span class='Ligature'></span><span class='Value'>j</span></code> in <code><span class='Value'>mat</span></code> is the same as the one at <code><span class='Value'>j</span><span class='Ligature'></span><span class='Value'>i</span></code> in <code><span class='Function'></span><span class='Value'>mat</span></code>:</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MeKAvzAg4oqRIG1hdAow4oC/MSDiipEg4o2JIG1hdA==">↗️</a><pre>    <span class='Number'>1</span><span class='Ligature'></span><span class='Number'>0</span> <span class='Function'></span> <span class='Value'>mat</span>
3
    <span class='Number'>0</span><span class='Ligature'></span><span class='Number'>1</span> <span class='Function'></span> <span class='Function'></span> <span class='Value'>mat</span>
3
</pre>
<p>With two axes the only interesting operation of this sort is to swap them (and with one or zero axes there's nothing interesting to do, and <code><span class='Function'></span></code> just returns the argument array). But a BQN programmer may well want to work with higher-rank arrays—although such a programmer might call them &quot;tensors&quot;—and this means there are many more ways to rearrange the axes. Transpose extends to high-rank arrays to allow some useful special cases as well as completely general axis rearrangement, as described below.</p>
<h2 id="monadic-transpose">Monadic Transpose</h2>
<p>APL extends matrix transposition to any rank by reversing all axes for its monadic <code><span class='Function'></span></code>, but this generalization isn't very natural and is almost never used. The main reason for it is to maintain the equivalence <code><span class='Value'>a</span> <span class='Function'>MP</span> <span class='Value'>b</span> <span class='Gets'>←→</span> <span class='Value'>b</span> <span class='Function'>MP</span><span class='Modifier2'></span><span class='Function'></span> <span class='Value'>a</span></code>, where <code><span class='Function'>MP</span> <span class='Gets'></span> <span class='Function'>+</span><span class='Modifier'>˝</span><span class='Modifier2'></span><span class='Function'>×</span><span class='Modifier2'></span><span class='Number'>1</span><span class='Ligature'></span><span class='Number'></span></code> is the generalized matrix product. But even here APL's Transpose is suspect. It does much more work than it needs to, as we'll see.</p>
<p>BQN's transpose takes the first axis of its argument and moves it to the end.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omiIGEyMzQ1NiDihpAg4oaVMuKAvzPigL804oC/NeKAvzYK4omiIOKNiSBhMjM0NTY=">↗️</a><pre>    <span class='Function'></span> <span class='Value'>a23456</span> <span class='Gets'></span> <span class='Function'></span><span class='Number'>2</span><span class='Ligature'></span><span class='Number'>3</span><span class='Ligature'></span><span class='Number'>4</span><span class='Ligature'></span><span class='Number'>5</span><span class='Ligature'></span><span class='Number'>6</span>
⟨ 2 3 4 5 6 ⟩
    <span class='Function'></span> <span class='Function'></span> <span class='Value'>a23456</span>
⟨ 3 4 5 6 2 ⟩
</pre>
<p>In terms of the argument data as given by Deshape (<code><span class='Function'></span></code>), this looks like a simple 2-dimensional transpose: one axis is exchanged with a compound axis made up of the other axes. Here we transpose a rank 3 matrix:</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YTMyMiDihpAgM+KAvzLigL8y4qWK4oaVMTIK4omN4peLPOKfnOKNiSBhMzIy">↗️</a><pre>    <span class='Value'>a322</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'>2</span><span class='Function'>⥊↕</span><span class='Number'>12</span>
    <span class='Function'></span><span class='Modifier2'></span><span class='Function'>&lt;</span><span class='Modifier2'></span><span class='Function'></span> <span class='Value'>a322</span>
┌─                      
· ┌─        ┌─          
  ╎  0  1   ╎ 0 4  8    
     2  3     1 5  9    
                        
     4  5     2 6 10    
     6  7     3 7 11    
                     ┘  
     8  9               
    10 11               
          ┘             
                       ┘
</pre>
<p>But, ignoring the whitespace and going in reading order, the argument and result have exactly the same element ordering as for the rank 2 matrix <code><span class='Function'></span><span class='Modifier'>˘</span> <span class='Value'>a322</span></code>:</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omN4peLPOKfnOKNiSDipYrLmCBhMzIy">↗️</a><pre>    <span class='Function'></span><span class='Modifier2'></span><span class='Function'>&lt;</span><span class='Modifier2'></span><span class='Function'></span> <span class='Function'></span><span class='Modifier'>˘</span> <span class='Value'>a322</span>
┌─                          
· ┌─            ┌─          
  ╵ 0 1  2  3   ╵ 0 4  8    
    4 5  6  7     1 5  9    
    8 9 10 11     2 6 10    
              ┘   3 7 11    
                         ┘  
                           ┘
</pre>
<p>To exchange multiple axes, use the Power modifier. A negative power moves axes in the other direction, just like how Rotate handles negative left arguments. In particular, to move the last axis to the front, use Undo (as you might expect, this exactly inverts <code><span class='Function'></span></code>).</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omiIOKNieKNnzMgYTIzNDU2CuKJoiDijYnigbwgYTIzNDU2">↗️</a><pre>    <span class='Function'></span> <span class='Function'></span><span class='Modifier2'></span><span class='Number'>3</span> <span class='Value'>a23456</span>
⟨ 5 6 2 3 4 ⟩
    <span class='Function'></span> <span class='Function'></span><span class='Modifier'></span> <span class='Value'>a23456</span>
⟨ 6 2 3 4 5 ⟩
</pre>
<p>In fact, we have <code><span class='Function'>≢⍉</span><span class='Modifier2'></span><span class='Value'>k</span> <span class='Value'>a</span> <span class='Gets'>←→</span> <span class='Value'>k</span><span class='Function'>⌽≢</span><span class='Value'>a</span></code> for any whole number <code><span class='Value'>k</span></code> and array <code><span class='Value'>a</span></code>.</p>
<p>To move axes other than the first, use the Rank modifier in order to leave initial axes untouched. A rank of <code><span class='Value'>k</span><span class='Function'>&gt;</span><span class='Number'>0</span></code> transposes only the last <code><span class='Value'>k</span></code> axes while a rank of <code><span class='Value'>k</span><span class='Function'>&lt;</span><span class='Number'>0</span></code> ignores the first <code><span class='Function'>|</span><span class='Value'>k</span></code> axes.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omiIOKNieKOiTMgYTIzNDU2">↗️</a><pre>    <span class='Function'></span> <span class='Function'></span><span class='Modifier2'></span><span class='Number'>3</span> <span class='Value'>a23456</span>
⟨ 2 3 5 6 4 ⟩
</pre>
<p>And of course, Rank and Power can be combined to do more complicated transpositions: move a set of contiguous axes with any starting point and length to the end.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omiIOKNieKBvOKOicKvMSBhMjM0NTY=">↗️</a><pre>    <span class='Function'></span> <span class='Function'></span><span class='Modifier'></span><span class='Modifier2'></span><span class='Number'>¯1</span> <span class='Value'>a23456</span>
⟨ 2 6 3 4 5 ⟩
</pre>
<p>Using these forms, we can state BQN's generalized matrix product swapping rule:</p>
<pre><span class='Value'>a</span> <span class='Function'>MP</span> <span class='Value'>b</span>  <span class='Gets'>←→</span>  <span class='Function'></span><span class='Modifier2'></span><span class='Paren'>(</span><span class='Number'>1</span><span class='Function'>-=</span><span class='Value'>a</span><span class='Paren'>)</span> <span class='Paren'>(</span><span class='Function'></span><span class='Value'>b</span><span class='Paren'>)</span> <span class='Function'>MP</span> <span class='Paren'>(</span><span class='Function'></span><span class='Modifier'></span><span class='Value'>a</span><span class='Paren'>)</span>
</pre>
<p>Certainly not as concise as APL's version, but not a horror either. BQN's rule is actually more parsimonious in that it only performs the axis exchanges necessary for the computation: it moves the two axes that will be paired with the matrix product into place before the product, and directly exchanges all axes afterwards. Each of these steps is equivalent in terms of data movement to a matrix transpose, the simplest nontrivial transpose to perform. Also remember that for two-dimensional matrices both kinds of transposition are the same, so that APL's simpler rule <code><span class='Function'>MP</span> <span class='Function'></span> <span class='Function'>MP</span><span class='Modifier2'></span><span class='Function'></span><span class='Modifier'>˜</span></code> holds in BQN.</p>
<p>Axis permutations of the types we've shown generate the complete permutation group on any number of axes, so you could produce any transposition you want with the right sequence of monadic transpositions with Rank. However, this can be unintuitive and tedious. What if you want to transpose the first three axes, leaving the rest alone? With monadic Transpose you have to send some axes to the end, then bring them back to the beginning. For example [following four or five failed tries]:</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omiIOKNieKBvOKOicKvMiDijYkgYTIzNDU2ICAjIFJlc3RyaWN0IFRyYW5zcG9zZSB0byB0aGUgZmlyc3QgdGhyZWUgYXhlcw==">↗️</a><pre>    <span class='Function'></span> <span class='Function'></span><span class='Modifier'></span><span class='Modifier2'></span><span class='Number'>¯2</span> <span class='Function'></span> <span class='Value'>a23456</span>  <span class='Comment'># Restrict Transpose to the first three axes
</span>⟨ 3 4 2 5 6 ⟩
</pre>
<p>In a case like this BQN's Dyadic transpose is much easier.</p>
<h2 id="dyadic-transpose">Dyadic Transpose</h2>
<p>Transpose also allows a left argument that specifies a permutation of the right argument's axes. For each index <code><span class='Value'>p</span><span class='Gets'></span><span class='Value'>i</span><span class='Function'></span><span class='Value'>𝕨</span></code> in the left argument, axis <code><span class='Value'>i</span></code> of the argument is used for axis <code><span class='Value'>p</span></code> of the result. Multiple argument axes can be sent to the same result axis, in which case that axis goes along a diagonal of the argument array, and the result will have a lower rank than the argument.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omiIDHigL8z4oC/MuKAvzDigL80IOKNiSBhMjM0NTYK4omiIDHigL8y4oC/MuKAvzDigL8wIOKNiSBhMjM0NTYgICMgRG9uJ3Qgd29ycnkgdG9vIG11Y2ggYWJvdXQgdGhpcyBjYXNlIHRob3VnaA==">↗️</a><pre>    <span class='Function'></span> <span class='Number'>1</span><span class='Ligature'></span><span class='Number'>3</span><span class='Ligature'></span><span class='Number'>2</span><span class='Ligature'></span><span class='Number'>0</span><span class='Ligature'></span><span class='Number'>4</span> <span class='Function'></span> <span class='Value'>a23456</span>
⟨ 5 2 4 3 6 ⟩
    <span class='Function'></span> <span class='Number'>1</span><span class='Ligature'></span><span class='Number'>2</span><span class='Ligature'></span><span class='Number'>2</span><span class='Ligature'></span><span class='Number'>0</span><span class='Ligature'></span><span class='Number'>0</span> <span class='Function'></span> <span class='Value'>a23456</span>  <span class='Comment'># Don't worry too much about this case though
</span>⟨ 5 2 3 ⟩
</pre>
<p>Since this kind of rearrangement can be counterintuitive, it's often easier to use <code><span class='Function'></span><span class='Modifier'></span></code> when specifying all axes. If <code><span class='Value'>p</span><span class='Function'></span><span class='Modifier2'></span><span class='Function'>≠≢</span><span class='Value'>a</span></code>, then we have <code><span class='Function'></span><span class='Value'>p</span><span class='Function'></span><span class='Modifier'></span><span class='Value'>a</span> <span class='Gets'>←→</span> <span class='Value'>p</span><span class='Function'>⊏≢</span><span class='Value'>a</span></code>.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omiIDHigL8z4oC/MuKAvzDigL80IOKNieKBvCBhMjM0NTY=">↗️</a><pre>    <span class='Function'></span> <span class='Number'>1</span><span class='Ligature'></span><span class='Number'>3</span><span class='Ligature'></span><span class='Number'>2</span><span class='Ligature'></span><span class='Number'>0</span><span class='Ligature'></span><span class='Number'>4</span> <span class='Function'></span><span class='Modifier'></span> <span class='Value'>a23456</span>
⟨ 3 5 4 2 6 ⟩
</pre>
<p>So far, all like APL. BQN makes one little extension, which is to allow only some axes to be specified. The left argument will be matched up with leading axes of the right argument. Those axes are moved according to the left argument, and remaining axes are placed in order into the gaps between them.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omiIDDigL8y4oC/NCDijYkgYTIzNDU2">↗️</a><pre>    <span class='Function'></span> <span class='Number'>0</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='Value'>a23456</span>
⟨ 2 5 3 6 4 ⟩
</pre>
<p>In particular, the case with only one argument specified is interesting. Here, the first axis ends up at the given location. This gives us a much better solution to the problem at the end of the last section.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omiIDIg4o2JIGEyMzQ1NiAgIyBSZXN0cmljdCBUcmFuc3Bvc2UgdG8gdGhlIGZpcnN0IHRocmVlIGF4ZXM=">↗️</a><pre>    <span class='Function'></span> <span class='Number'>2</span> <span class='Function'></span> <span class='Value'>a23456</span>  <span class='Comment'># Restrict Transpose to the first three axes
</span>⟨ 3 4 2 5 6 ⟩
</pre>
<p>Finally, it's worth noting that, as monadic Transpose moves the first axis to the end, it's equivalent to dyadic Transpose with a &quot;default&quot; left argument: <code><span class='Paren'>(</span><span class='Function'>=-</span><span class='Number'>1</span><span class='Modifier'>˙</span><span class='Paren'>)</span><span class='Modifier2'></span><span class='Function'></span></code>.</p>
<h2 id="definitions">Definitions</h2>
<p>Here we define the two valences of Transpose more precisely.</p>
<p>Monadic transpose is identical to <code><span class='Paren'>(</span><span class='Function'>=-</span><span class='Number'>1</span><span class='Modifier'>˙</span><span class='Paren'>)</span><span class='Modifier2'></span><span class='Function'></span></code>, except that if the argument is a unit it is returned unchanged rather than giving an error.</p>
<p>An atom right argument to dyadic Transpose is always enclosed to get an array before doing anything else.</p>
<p>In dyadic Transpose, the left argument is a number or numeric array of rank 1 or less, and <code><span class='Value'>𝕨</span><span class='Function'></span><span class='Modifier2'></span><span class='Function'>≠≢</span><span class='Value'>𝕩</span></code>. Define the result rank <code><span class='Value'>r</span><span class='Gets'></span><span class='Paren'>(</span><span class='Function'>=</span><span class='Value'>𝕩</span><span class='Paren'>)</span><span class='Function'>-+</span><span class='Modifier'>´</span><span class='Function'>¬∊</span><span class='Value'>𝕨</span></code> to be the argument rank minus the number of duplicate entries in the left argument. We require <code><span class='Function'></span><span class='Modifier'>´</span><span class='Value'>𝕨</span><span class='Function'>&lt;</span><span class='Value'>r</span></code>. Bring <code><span class='Value'>𝕨</span></code> to full length by appending the missing indices: <code><span class='Value'>𝕨</span><span class='Function'></span><span class='Gets'></span><span class='Value'>𝕨</span><span class='Paren'>(</span><span class='Function'>¬</span><span class='Modifier2'></span><span class='Function'></span><span class='Modifier'>˜</span><span class='Function'>/⊢</span><span class='Paren'>)</span><span class='Function'></span><span class='Value'>r</span></code>. Now the result shape is defined to be <code><span class='Function'></span><span class='Modifier'>´¨</span><span class='Value'>𝕨</span><span class='Function'>⊔≢</span><span class='Value'>𝕩</span></code>. Element <code><span class='Value'>i</span><span class='Function'></span><span class='Value'>z</span></code> of the result <code><span class='Value'>z</span></code> is element <code><span class='Paren'>(</span><span class='Value'>𝕨</span><span class='Function'></span><span class='Value'>i</span><span class='Paren'>)</span><span class='Function'></span><span class='Value'>𝕩</span></code> of the argument.</p>