Transpose: Difference between revisions

Jump to navigation Jump to search
1,981 bytes added ,  05:13, 29 November 2022
m
(7 intermediate revisions by the same user not shown)
Line 7: Line 7:
[[Monadic]] Transpose reverses the axes of the right argument. When applied to a [[matrix]], the result is its [[wikipedia:transpose|transpose]]. [[Scalar]] and [[vector]] arguments are unaffected.
[[Monadic]] Transpose reverses the axes of the right argument. When applied to a [[matrix]], the result is its [[wikipedia:transpose|transpose]]. [[Scalar]] and [[vector]] arguments are unaffected.


<source lang=apl>
<syntaxhighlight lang=apl>
       3 3⍴⍳9
       3 3⍴⍳9
1 2 3
1 2 3
Line 20: Line 20:
       ⍉1 2 3  ⍝ Unaffected
       ⍉1 2 3  ⍝ Unaffected
1 2 3
1 2 3
</source>
</syntaxhighlight>
The transpose of a matrix could also be achieved if Monadic Transpose had [[rotate]]d the axes, rather than reversed them, but the design was chosen so keep the matrix identity <syntaxhighlight lang=apl inline>(m+.×n) ≡ ⍉(⍉n)+.×⍉m</syntaxhighlight> or <syntaxhighlight lang=apl inline>(m+.×n) ≡ n+.×⍢⍉m</syntaxhighlight> for [[array]]s of all [[rank]]s.<ref>[[Adin Falkoff|Falkoff, Adin]] and [[Ken Iverson]]. ''The Design of APL''. [https://www.jsoftware.com/papers/APLDesign1.htm#6 Formal manipulation]. IBM Journal of Research and Development. Volume 17. Number 4. July 1973.</ref>


=== Dyadic usage ===
=== Dyadic usage ===
{| class=wikitable style="width:50%;float:right"
{| class=wikitable style="width:50%;float:right"
|{{quote | "Dyadic transpose, <source lang=apl inline>x⍉y</source>, is probably one of the last primitives to be mastered for an APLer, but is actually straightforward to describe."<ref>[[Roger Hui]]. [https://forums.dyalog.com/viewtopic.php?f=30&t=1648 ''dyadic transpose, a personal history'']. Dyalog Forums. 22 May 2020.</ref>}}
|{{quote | "Dyadic transpose, <syntaxhighlight lang=apl inline>x⍉y</syntaxhighlight>, is probably one of the last primitives to be mastered for an APLer, but is actually straightforward to describe."<ref>[[Roger Hui]]. [https://forums.dyalog.com/viewtopic.php?f=30&t=1648 ''dyadic transpose, a personal history'']. Dyalog Forums. 22 May 2020.</ref>}}
|}
|}
For [[dyadic]] usage, the left argument X must be a [[vector]] whose length equals the [[rank]] of the right argument Y, and the elements must form a range so that <source lang=apl inline>∧/X∊⍳(1-⎕IO)+⌈/X</source> is satisfied.
For [[dyadic]] usage, the left argument X must be a [[vector]] whose length equals the [[rank]] of the right argument Y, and the elements must form a range so that <syntaxhighlight lang=apl inline>∧/X∊⍳(1-⎕IO)+⌈/X</syntaxhighlight> is satisfied.


If all values in X are unique (X forms a [[permutation]] over the axes of Y), the axes are reordered by X so that N-th element of X specifies the new position for the N-th axis of Y. This means that, given a multi-dimensional [[index]] V of an element in the resulting array, <source lang=apl inline>V⌷X⍉Y</source> corresponds to <source lang=apl inline>V[X]⍉Y</source>.
If all values in X are unique (X forms a [[permutation]] over the axes of Y), the axes are reordered by X so that N-th element of X specifies the new position for the N-th axis of Y. This means that, given a multi-dimensional [[index]] V of an element in the resulting array, <syntaxhighlight lang=apl inline>V⌷X⍉Y</syntaxhighlight> corresponds to <syntaxhighlight lang=apl inline>V[X]⍉Y</syntaxhighlight>.


<source lang=apl>
<syntaxhighlight lang=apl>
       X←3 1 2
       X←3 1 2
       Y←3 4 5⍴⎕A
       Y←3 4 5⍴⎕A
Line 39: Line 40:
       1 2 3[X]⌷Y  ⍝ or Y[3;1;2]
       1 2 3[X]⌷Y  ⍝ or Y[3;1;2]
P
P
</source>
</syntaxhighlight>
==== Duplicates in the left argument ====
When X contains duplicates, the result has rank <syntaxhighlight lang=apl inline>(1-⎕IO)+⌈/X</syntaxhighlight>. For the axes of Y that map to the same resulting axis, only the elements where the indices are equal over those axes are collected. This has the effect of extracting diagonal elements. If the axes are of unequal length, the resulting axis has the length of the shortest of them. This operation can be modeled as computing the resulting shape <syntaxhighlight lang=apl inline>(⍴Y)⌊.+(⌊/⍬)×X∘.≠(1-⎕IO)+⍳⌈/X</syntaxhighlight>, then [[Index Generator|creating the array of its multi-dimensional indices]] <syntaxhighlight lang=apl inline>⍳</syntaxhighlight>, then modify each index and fetch the corresponding elements of Y <syntaxhighlight lang=apl inline>{⍵[X]⌷Y}¨</syntaxhighlight>.


When X contains duplicates, the result has rank <source lang=apl inline>(1-⎕IO)+⌈/X</source>. For the axes of Y that map to the same resulting axis, only the elements where the indices are equal over those axes are collected. This has the effect of extracting diagonal elements. If the axes are of unequal length, the resulting axis has the length of the shortest of them. This operation can be modeled as computing the resulting shape <source lang=apl inline>(⍴Y)⌊.+(⌊/⍬)×X∘.≠(1-⎕IO)+⍳⌈/X</source>, then [[Index Generator|creating the array of its multi-dimensional indices]] <source lang=apl inline>⍳</source>, then modify each index and fetch the corresponding elements of Y <source lang=apl inline>{⍵[X]⌷Y}¨</source>.
<syntaxhighlight lang=apl>
 
<source lang=apl>
       1 1⍉3 4⍴⎕A  ⍝ Extract diagonal from 3×4 matrix
       1 1⍉3 4⍴⎕A  ⍝ Extract diagonal from 3×4 matrix
AEI
AEI
Line 53: Line 54:
       (X⍉Y)≡{⍵[X]⌷Y}¨⍳(⍴Y)⌊.+(⌊/⍬)×X∘.≠(1-⎕IO)+⍳⌈/X
       (X⍉Y)≡{⍵[X]⌷Y}¨⍳(⍴Y)⌊.+(⌊/⍬)×X∘.≠(1-⎕IO)+⍳⌈/X
1
1
</source>{{Works in|[[Dyalog APL]]}}
</syntaxhighlight>{{Works in|[[Dyalog APL]]}}
 
== Issues ==
A common mistake in employing dyadic transpose is the "intuitive" interpretation of the left argument as if gives the order in which you want to select dimensions of the right argument for the result. In fact, it gives the new position of each of the dimensions. It is possible to convert between these two representations by "inverting" the permutation with monadic [[Grade|Grade Up]] (<syntaxhighlight lang=apl inline>⍋</syntaxhighlight>).
 
The reason for the design of <syntaxhighlight lang=apl inline>⍉</syntaxhighlight> being as it is, is that it allows you to select diagonals by giving one or more dimensions equal mapping, whereas simply selecting dimensions from the right would not allow that. It is therefore the more complete of the two options.<ref>[[Morten Kromberg]]. Message {{M|57439754}}ff. [[APL Orchard]]. 25 Mar 2021.</ref>
 
== External links ==
=== Tutorials ===
* [https://chat.stackexchange.com/rooms/52405/conversation/lesson-10-apl-functions--#41867568 APL Cultivation]
* [https://xpqz.github.io/learnapl/dyadictrn.html Learning APL]
* [https://dyalog.tv/Webinar/?v=zBqdeDJPPRc Dyalog Webinar]


=== Documentation ===
=== Documentation ===
Line 61: Line 73:
* J [https://www.jsoftware.com/help/dictionary/d232.htm Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/barco NuVoc]
* J [https://www.jsoftware.com/help/dictionary/d232.htm Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/barco NuVoc]
* [https://mlochbaum.github.io/BQN/doc/transpose.html BQN]
* [https://mlochbaum.github.io/BQN/doc/transpose.html BQN]
== References ==
== References ==
<references/>
<references/>


{{APL built-ins}}[[Category:Primitive functions]]
{{APL built-ins}}[[Category:Primitive functions]]

Navigation menu