Transpose: Difference between revisions

Jump to navigation Jump to search
42 bytes added ,  05:13, 29 November 2022
m
Line 41: 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 55: 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