From 229e2cd2f5c78b13c483a8559dead2c8f31d8e42 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Sat, 18 Jul 2020 18:26:52 -0400 Subject: Terminology changes: subject, 1/2-modifier, Box/Unbox to Enclose/Merge, blocks --- docs/doc/transpose.html | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'docs/doc/transpose.html') diff --git a/docs/doc/transpose.html b/docs/doc/transpose.html index d2629546..43b51b48 100644 --- a/docs/doc/transpose.html +++ b/docs/doc/transpose.html @@ -2,7 +2,7 @@

Transpose

As in APL, Transpose () 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.

Monadic Transpose

-

Transposing a matrix exchanges its axes, mirroring it across the diagonal. APL extends the function to any rank by reversing all axes, 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 ←→ a MP b, 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.

+

Transposing a matrix exchanges its axes, mirroring it across the diagonal. APL extends the function to any rank by reversing all axes, 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 ←→ a MP b, 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 its argument and moves it to the end.

     a23456  23456
 [ 2 3 4 5 6 ]
@@ -11,7 +11,7 @@
 

On the argument's ravel, 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  322⥊↕12
-    < a322
+    < a322
 
            
      0  1     0 4  8
@@ -26,7 +26,7 @@
                        
 

But, reading in ravel order, the argument and result have exactly the same element ordering as for the rank 2 matrix ˘ a322:

-
    < ˘ a322
+
    < ˘ a322
 
                
     0 1  2  3     0 4  8
@@ -36,27 +36,27 @@
                          
                            
 
-

To exchange multiple axes, use the Power operator. Like Rotate, a negative power will move axes in the other direction. In particular, to move the last axis to the front, use Inverse (as you might expect, this exactly inverts ).

-
     3 a23456
+

To exchange multiple axes, use the Power modifier. Like Rotate, a negative power will move axes in the other direction. In particular, to move the last axis to the front, use Inverse (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 number k and array a.

-

To move axes other than the first, use the Rank operator 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
+

In fact, we have ≢⍉k a ←→ k⌽≢a for any 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 Power 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
+
     ¯1 a23456
 [ 2 6 3 4 5 ]
 

Using these forms, we can state BQN's generalized matrix product swapping rule:

-
a MP b  ←→  (≠≢a) a MP b
+
a MP b  ←→  (≠≢a) a MP b
 

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, and APL's rule holds in BQN.

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
+
     ¯2  a23456  # Restrict Transpose to the first three axes
 [ 3 4 2 5 6 ]
 

In a case like this BQN's Dyadic transpose is much easier.

@@ -67,7 +67,7 @@ 12200 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 pa ←→ p⊏≢a.

+

Since this kind of rearrangement can be counterintuitive, it's often easier to use when specifying all axes. If p≠≢a, then we have pa ←→ p⊏≢a.

     13204  a23456
 [ 3 5 4 2 6 ]
 
@@ -79,10 +79,10 @@
     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 dyadic Transpose with a "default" left argument: (≢-1˜).

+

Finally, it's worth noting that, as monadic Transpose moves the first axis to the end, it's equivalent to dyadic Transpose with a "default" left argument: (≢-1˜).

Definitions

Here we define the two valences of Transpose more precisely.

-

A non-array right argument to Transpose is always boxed to get a scalar array before doing anything else.

-

Monadic transpose is identical to (≢-1˜), except that for scalar arguments it returns the array unchanged rather than giving an error.

-

In Dyadic transpose, the left argument is a number or numeric array of rank 1 or less, and 𝕨≠≢𝕩. Define the result rank r(≠≢𝕩)-+´¬∊𝕨 to be the argument rank minus the number of duplicate entries in the left argument. We require ´𝕨<r. Bring 𝕨 to full length by appending the missing indices: 𝕨𝕨(¬˜/⊢)r. Now the result shape is defined to be ´¨𝕨⊔≢𝕩. Element iz of the result z is element (𝕨i)𝕩 of the argument.

+

A non-array right argument to Transpose is always enclosed to get a scalar array before doing anything else.

+

Monadic transpose is identical to (≢-1˜), except that for scalar arguments it returns the array unchanged rather than giving an error.

+

In Dyadic transpose, the left argument is a number or numeric array of rank 1 or less, and 𝕨≠≢𝕩. Define the result rank r(≠≢𝕩)-+´¬∊𝕨 to be the argument rank minus the number of duplicate entries in the left argument. We require ´𝕨<r. Bring 𝕨 to full length by appending the missing indices: 𝕨𝕨(¬˜/⊢)r. Now the result shape is defined to be ´¨𝕨⊔≢𝕩. Element iz of the result z is element (𝕨i)𝕩 of the argument.

-- cgit v1.2.3