Transpose: Difference between revisions

Jump to navigation Jump to search
645 bytes added ,  05:13, 29 November 2022
m
m (Text replacement - "<source" to "<syntaxhighlight")
(One intermediate revision by the same user not shown)
Line 21: Line 21:
1 2 3
1 2 3
</syntaxhighlight>
</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 ===
Line 40: Line 41:
P
P
</syntaxhighlight>
</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 <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>.


Line 54: Line 55:
1
1
</syntaxhighlight>{{Works in|[[Dyalog APL]]}}
</syntaxhighlight>{{Works in|[[Dyalog APL]]}}
== Issues ==
== 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>).
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>).

Navigation menu