<<   >>

30. Dyadic Transpose

Dyadic transpose, x⍉y , is probably one of the last primitives to be mastered for an APLer, but is actually straightforward to describe. Assign a distinct letter for each unique integer in x :
    0   1   2   3  
i   j   k   l
If z←x⍉y , then z[i;j;k;…] equals y indexed by the letters corresponding to elements of x . For example:

   y← ? 5 13 19 17 11 ⍴ 100
   x←   2  1  2  0  1
   ⍝    k  j  k  i  j

   z←x⍉y

   i←?17 ⋄ j←?11 ⋄ k←?5

   z[i;j;k] = y[k;j;k;i;j]
1
   z[i;j;k] = y[⊂⍎¨'ijk'[x]]
1

From the above it can be seen that:

  • the rank   of z is 0⌈1+⌈/x
  • the shape of z is (⍴y)⌊.+(⌈/⍴y)×x∘.≠⍳0⌈1+⌈/x   [92]

And from that it is but a short step to a full model of x⍉y , essentially a problem of producing the ravelled representation from the strided representation [93]. The version here is from [49f], rendered in Dyalog APL:

   transpose←{(,⍵)⌷⍨⊂(↑,¨⍳(⍴⍵)⌊.+(⌈/⍴⍵)×~b)+.×(⍴⍵)⊥b←⍺∘.=⍳0⌈1+⌈/⍺}

   (x⍉y) ≡ x transpose y
1

The definition is briefer in an APL that supports infinity:

   transpose←{(,⍵)⌷⍨⊂(↑,¨⍳(⍴⍵)⌊.+∞×~b)+.×(⍴⍵)⊥b←⍺∘.=⍳0⌈1+⌈/⍺}

Postscript: Recent work on the problem derived a shorter and simpler function [137]:

   transpose←{⍵[(⊂⊂⍺)⌷¨,¨⍳⍺[⍋⍺]{⌊/⍵}⌸(⍴⍵)[⍋⍺]]}