Transpose: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
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>).

Latest revision as of 05:13, 29 November 2022

Transpose () is an ambivalent primitive function which reorders the axes of the right argument array. The name Transpose comes from the fact that monadic application to a matrix yields its transpose. The dyadic usage is sometimes called Rearrange Axes, which better reflects the behavior of the function.

Examples

Monadic usage

Monadic Transpose reverses the axes of the right argument. When applied to a matrix, the result is its transpose. Scalar and vector arguments are unaffected.

      3 3⍴⍳9
1 2 3
4 5 6
7 8 9
      3 3⍴⍳9  ⍝ Transpose of a matrix
1 4 7
2 5 8
3 6 9
      ⍴⍉3 4 5⎕A  ⍝ The first axis goes last, the last axis comes first
5 4 3
      1 2 3  ⍝ Unaffected
1 2 3

The transpose of a matrix could also be achieved if Monadic Transpose had rotated the axes, rather than reversed them, but the design was chosen so keep the matrix identity (m+.×n) (n)+.×⍉m or (m+.×n) n+.×m for arrays of all ranks.[1]

Dyadic usage

"Dyadic transpose, xy, is probably one of the last primitives to be mastered for an APLer, but is actually straightforward to describe."[2]

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 /X∊⍳(1-⎕IO)+⌈/X 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, VXY corresponds to V[X]Y.

      X3 1 2
      Y3 4 5⎕A
      XY  ⍝ First axis goes to third (X[1] is 3), second goes to first (X[2] is 1), etc.
4 5 3
      1 2 3XY  ⍝ or (X⍉Y)[1;2;3]
P
      1 2 3[X]Y  ⍝ or Y[3;1;2]
P

Duplicates in the left argument

When X contains duplicates, the result has rank (1-⎕IO)+⌈/X. 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 (Y).+(/)×X∘.(1-⎕IO)+⍳⌈/X, then creating the array of its multi-dimensional indices , then modify each index and fetch the corresponding elements of Y {[X]Y}¨.

      1 13 4⎕A  ⍝ Extract diagonal from 3×4 matrix
AEI

      Y?3 4 5 6 7100
      X3 2 3 1 2  ⍝ Left arg maps 5-dimensional Y to 3 dimensions
      XY         ⍝ Resulting shape is ⌊/¨6(4 7)(3 5)
6 4 3
      (XY){[X]Y}¨(Y).+(/)×X∘.(1-⎕IO)+⍳⌈/X
1
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 Up ().

The reason for the design of 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.[3]

External links

Tutorials

Documentation

References

  1. Falkoff, Adin and Ken Iverson. The Design of APL. Formal manipulation. IBM Journal of Research and Development. Volume 17. Number 4. July 1973.
  2. Roger Hui. dyadic transpose, a personal history. Dyalog Forums. 22 May 2020.
  3. Morten Kromberg. Message 57439754ff. APL Orchard. 25 Mar 2021.


APL built-ins [edit]
Primitive functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare Root
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Non-Scalar
Structural ShapeReshapeTallyDepthRavelEnlistTableCatenateReverseRotateTransposeRazeMixSplitEncloseNestCut (K)PairLinkPartitioned EnclosePartition
Selection FirstPickTakeDropUniqueIdentitySelectReplicateExpandSet functions (IntersectionUnionWithout) ∙ Bracket indexingIndex
Selector Index generatorGradeIndex OfInterval IndexIndicesDeal
Computational MatchNot MatchMembershipFindNub SieveEncodeDecodeMatrix InverseMatrix DivideFormatExecuteMaterialiseRange
Primitive operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-BeamSpawnFunction axis
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductPowerAtUnderRankDepthVariantStencilCut (J)
Quad names
Arrays Index originMigration levelAtomic vector
Functions Name classCase convertUnicode convert
Operators SearchReplace