aboutsummaryrefslogtreecommitdiff
path: root/doc/transpose.md
diff options
context:
space:
mode:
authorMarshall Lochbaum <mwlochbaum@gmail.com>2020-12-20 15:04:05 -0500
committerMarshall Lochbaum <mwlochbaum@gmail.com>2020-12-20 15:04:05 -0500
commit9c04cd5285572698f8393cb2f1110bb92b3ea5d3 (patch)
treedf321ed1390e01c3c664e7dc99f63cafdaf4c088 /doc/transpose.md
parentbcff427614bf4e7f377a1c90408c9b1c3f6d70b0 (diff)
Update transpose doc and add a more basic introduction
Diffstat (limited to 'doc/transpose.md')
-rw-r--r--doc/transpose.md28
1 files changed, 21 insertions, 7 deletions
diff --git a/doc/transpose.md b/doc/transpose.md
index f8593e5a..ab573390 100644
--- a/doc/transpose.md
+++ b/doc/transpose.md
@@ -4,30 +4,44 @@
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.
+## Transpose basics
+
+The name for the primitive `⍉` comes from the [Transpose](https://en.wikipedia.org/wiki/Transpose) operation on matrices. Given a matrix as an array of rank 2, `⍉` will transpose it:
+
+ ⊢ mat ← 2‿3 ⥊ ↕6
+ ⍉ mat
+
+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](indices.md) `i‿j` in `mat` is the same as the one at `j‿i` in `⍉mat`:
+
+ 1‿0 ⊑ mat
+ 0‿1 ⊑ ⍉ mat
+
+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.
+
## 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.
+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 its argument and moves it to the end.
≢ a23456 ← ↕2‿3‿4‿5‿6
≢ ⍉ a23456
-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:
+In terms of the argument data 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
-But, reading in ravel order, the argument and result have exactly the same element ordering as for the rank 2 matrix `⥊˘ a322`:
+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
-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 `⍉`).
+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 `⍉`).
≢ ⍉⍟3 a23456
≢ ⍉⁼ a23456
-In fact, we have `≢⍉⍟k a ←→ k⌽≢a` for any number `k` and array `a`.
+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.
@@ -39,9 +53,9 @@ And of course, Rank and Power can be combined to do more complicated transpositi
Using these forms, we can state BQN's generalized matrix product swapping rule:
- a MP b ←→ ⍉⍟(=a) a ⍉⁼⊸MP⟜⍉ b
+ 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, and APL's rule holds in BQN.
+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.
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]: