Transpose

From APL Wiki
Revision as of 14:32, 14 July 2020 by Adám Brudzewsky (talk | contribs) (Text replacement - "http://help.dyalog.com" to "https://help.dyalog.com")
Jump to navigation Jump to search

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

Dyadic usage

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, V⌷X⍉Y corresponds to V[X]⍉Y.

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

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 1⍉3 4⍴⎕A  ⍝ Extract diagonal from 3×4 matrix
AEI

      Y←?3 4 5 6 7⍴100
      X←3 2 3 1 2  ⍝ Left arg maps 5-dimensional Y to 3 dimensions
      ⍴X⍉Y         ⍝ Resulting shape is ⌊/¨6(4 7)(3 5)
6 4 3
      (X⍉Y)≡{⍵[X]⌷Y}¨⍳(⍴Y)⌊.+(⌊/⍬)×X∘.≠(1-⎕IO)+⍳⌈/X
1
Works in: Dyalog APL

External links

Documentation


APL built-ins [edit]
Primitives (Timeline) Functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare Root
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Non-Scalar
Structural ShapeReshapeTallyDepthRavelEnlistTableCatenateReverseRotateTransposeRazeMixSplitEncloseNestCut (K)PairLinkPartitioned EnclosePartition
Selection FirstPickTakeDropUniqueIdentityStopSelectReplicateExpandSet functions (IntersectionUnionWithout) ∙ Bracket indexingIndexCartesian ProductSort
Selector Index generatorGradeIndex OfInterval IndexIndicesDealPrefix and suffix vectors
Computational MatchNot MatchMembershipFindNub SieveEncodeDecodeMatrix InverseMatrix DivideFormatExecuteMaterialiseRange
Operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-BeamSpawnFunction axis
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductDeterminantPowerAtUnderRankDepthVariantStencilCutDirect definition (operator)
Quad names Index originComparison toleranceMigration levelAtomic vector