Transpose

Transpose (⍉) is a tool for rearranging the axes of an array 𝕩. Without a left argument, it moves the first axis to the end, while a left argument can specify an arbitrary rearrangement. Both cases are tweaked relative to APL to align better with the leading axis model and make common operations easier.

Transpose basics

The name for the primitive ⍉ comes from the Transpose operation on matrices. Given a matrix as an array of rank 2, ⍉ will transpose it:

↗️
    ⊒ mat ← 2β€Ώ3 β₯Š ↕6
β”Œβ”€       
β•΅ 0 1 2  
  3 4 5  
        β”˜
    ⍉ mat
β”Œβ”€     
β•΅ 0 3  
  1 4  
  2 5  
      β”˜

Transpose is named this way because it exchanges the two axes of the matrix. Above you can see that while mat has shape 2β€Ώ3, ⍉mat has shape 3β€Ώ2, and we can also check that the element at index iβ€Ώj in mat is the same as the one at jβ€Ώi in ⍉mat:

↗️
    1β€Ώ0 βŠ‘ mat
3
    0β€Ώ1 βŠ‘ ⍉ mat
3

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 ⍉ 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 "tensors"β€”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.

Transposing tensors

APL extends matrix transposition to any rank by reversing all axes for its monadic ⍉, but this generalization isn't very natural and is almost never used. The main reason for it is to maintain the equivalence a MP b ←→ b MPβŒΎβ‰ a, where MP ← +Λβˆ˜Γ—βŽ‰1β€Ώβˆž 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.

BQN's transpose takes the first axis of 𝕩 and moves it to the end.

↗️
    β‰’ a23456 ← ↕2β€Ώ3β€Ώ4β€Ώ5β€Ώ6
⟨ 2 3 4 5 6 ⟩

    β‰’ ⍉ a23456
⟨ 3 4 5 6 2 ⟩

In terms of the index-ordered elements as given by Deshape (β₯Š), 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:

↗️
    a322 ← 3β€Ώ2β€Ώ2β₯Šβ†•12
    β‹ˆβŸœβ‰ a322
β”Œβ”€                      
Β· β”Œβ”€        β”Œβ”€          
  β•Ž  0  1   β•Ž 0 4  8    
     2  3     1 5  9    
                        
     4  5     2 6 10    
     6  7     3 7 11    
                     β”˜  
     8  9               
    10 11               
          β”˜             
                       β”˜

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 β₯ŠΛ˜ a322:

↗️
    β‹ˆβŸœβ‰ β₯ŠΛ˜ a322
β”Œβ”€                          
Β· β”Œβ”€            β”Œβ”€          
  β•΅ 0 1  2  3   β•΅ 0 4  8    
    4 5  6  7     1 5  9    
    8 9 10 11     2 6 10    
              β”˜   3 7 11    
                         β”˜  
                           β”˜

To exchange multiple axes, use the Repeat 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 ⍉).

↗️
    β‰’ β‰βŸ3 a23456
⟨ 5 6 2 3 4 ⟩

    β‰’ ⍉⁼ a23456
⟨ 6 2 3 4 5 ⟩

In fact, we have β‰’β‰βŸk a ←→ kβŒ½β‰’a for any whole number k and array a.

To move axes other than the first, use the Rank modifier in order to leave initial axes untouched. A rank of k>0 transposes only the last k axes while a rank of k<0 ignores the first |k axes.

↗️
    β‰’ β‰βŽ‰3 a23456
⟨ 2 3 5 6 4 ⟩

And of course, Rank and Repeat can be combined to do more complicated transpositions: move a set of contiguous axes with any starting point and length to the end.

↗️
    β‰’ β‰βΌβŽ‰Β―1 a23456
⟨ 2 6 3 4 5 ⟩

Using these forms (and the Rank function), we can state BQN's generalized matrix product swapping rule:

a MP b  ←→  β‰βŸ(1-=a) (⍉b) MP (⍉⁼a)

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 MP ≑ MPβŒΎβ‰Λœ holds in BQN on rank 2.

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]:

↗️
    β‰’ β‰βΌβŽ‰Β―2 ⍉ a23456  # Restrict Transpose to the first three axes
⟨ 3 4 2 5 6 ⟩

In a case like this the dyadic version of ⍉, called Reorder Axes, is much easier.

Reorder Axes

Transpose also allows a left argument that specifies a permutation of 𝕩's axes. For each index p←iβŠ‘π•¨ in the left argument, axis i of 𝕩 is used for axis p of the result. Multiple argument axes can be sent to the same result axis, in which case that axis goes along a diagonal of 𝕩, and the result will have a lower rank than 𝕩 (see the next section).

↗️
    β‰’ 1β€Ώ3β€Ώ2β€Ώ0β€Ώ4 ⍉ a23456
⟨ 5 2 4 3 6 ⟩

    β‰’ 1β€Ώ2β€Ώ2β€Ώ0β€Ώ0 ⍉ a23456  # Don't worry too much about this case though
⟨ 5 2 3 ⟩

Since this kind of rearrangement can be counterintuitive, it's often easier to use ⍉⁼ when specifying all axes. If p≑○≠≒a, then we have β‰’p⍉⁼a ←→ pβŠβ‰’a.

↗️
    β‰’ 1β€Ώ3β€Ώ2β€Ώ0β€Ώ4 ⍉⁼ a23456
⟨ 3 5 4 2 6 ⟩

BQN makes one further extension, which is to allow only some axes to be specified (this is the only difference in dyadic ⍉ relative to APL). Then 𝕨 will be matched up with leading axes of 𝕩. Those axes are moved according to 𝕨, and remaining axes are placed in order into the gaps between them.

↗️
    β‰’ 0β€Ώ2β€Ώ4 ⍉ a23456
⟨ 2 5 3 6 4 ⟩

In particular, the case with only one axis 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.

↗️
    β‰’ 2 ⍉ a23456  # Restrict Transpose to the first three axes
⟨ 3 4 2 5 6 ⟩

Finally, it's worth noting that, as monadic Transpose moves the first axis to the end, it's equivalent to Reorder Axes with a "default" left argument: (=-1Λ™)βŠΈβ‰.

Taking diagonals

When 𝕨 contains an axis index more than once, the corresponding axes of 𝕩 will all be sent to that axis of the result. This isn't a special case: it follows the same rule that iβŠ‘π•¨β‰π•© is (π•¨βŠi)βŠ‘π•©. Only the result shape has to be adjusted for this case: the length along a result axis is the minimum of all the axes of 𝕩 that go into it, because any indices outside this range will be out of bounds along at least one axis.

A bit abstract. This rule is almost always used simply as 0β€Ώ0⍉𝕩 to get the main diagonal of a matrix.

↗️
    ⊒ a ← 3β€Ώ5β₯Š'a'+↕15
β”Œβ”€       
β•΅"abcde  
  fghij  
  klmno" 
        β”˜

    0β€Ώ0 ⍉ a
"agm"

    ⟨2βŸ©βŠ‘0β€Ώ0⍉a  # Single index into result
'm'
    ⟨2,2βŸ©βŠ‘a    # is like a doubled index into a
'm'

Definitions

Here we define the two valences of Transpose more precisely.

An atom right argument to Transpose or Reorder Axes is always enclosed to get an array before doing anything else.

Monadic Transpose is identical to (=-1Λ™)βŠΈβ‰, except that if 𝕩 is a unit it's returned unchanged (after enclosing, if it's an atom) rather than giving an error.

In Reorder Axes, 𝕨 is a number or numeric array of rank 1 or less, and 𝕨≀○≠≒𝕩. Define the result rank r←(=𝕩)-+Β΄Β¬βˆŠπ•¨ to be the rank of 𝕩 minus the number of duplicate entries in 𝕨. We require βˆ§Β΄π•¨<r. Bring 𝕨 to full length by appending the missing indices: π•¨βˆΎβ†©π•¨(¬∘∊˜/⊒)↕r. Now the result shape is defined to be βŒŠΒ΄Β¨π•¨βŠ”β‰’π•©. Element iβŠ‘z of the result z is element (π•¨βŠi)βŠ‘π•© of the argument.