Rotate

From APL Wiki
(Redirected from Rotate First)
Jump to navigation Jump to search

Rotate (, ) is a dyadic primitive function which "rotates" the elements of the right argument around a specified axis. The name Rotate is typically used for the primitive , which rotates along the last axis, while , which rotates along the first axis, is called "Rotate First" or similar. APLs with function axis allow to choose a different axis from the default one. In the leading axis model, specifying an axis is discouraged in favor of using with the Rank operator.

Rotate and Rotate First share the glyphs and with monadic functions Reverse and Reverse First, respectively.

Examples

The left argument is usually restricted to a scalar. Rotate on a vector right argument rotates the elements to the left, wrapping around as necessary. The left argument can be large or negative.

      31 2 3 4 5 6 7
4 5 6 7 1 2 3
      81 2 3 4 5 6 7
2 3 4 5 6 7 1
      ¯41 2 3 4 5 6 7
4 5 6 7 1 2 3

Rotate on a matrix rotates the elements around horizontally (to the left), while Rotate First does vertically (upwards).

      M3 4⎕A
ABCD
EFGH
IJKL
      2M  ⍝ Horizontally rotate twice
CDAB
GHEF
KLIJ
      1M  ⍝ Vertically rotate once
EFGH
IJKL
ABCD

Higher-rank arrays can be rotated on an arbitrary axis if function axis or Rank operator is supported:

      M2 3 4⎕A
ABCD
EFGH
IJKL
    
MNOP
QRST
UVWX
      (1[1]M)(1[2]M)(1[3]M)  ⍝ Rotate once over 1st, 2nd, or 3rd axis, using function axis
┌────┬────┬────┐
MNOPEFGHBCDA
QRSTIJKLFGHE
UVWXABCDJKLI
            
ABCDQRSTNOPM
EFGHUVWXRSTQ
IJKLMNOPVWXU
└────┴────┴────┘
      (13M)(12M)(11M)  ⍝ Same as above, using Rotate First with Rank
┌────┬────┬────┐
MNOPEFGHBCDA
QRSTIJKLFGHE
UVWXABCDJKLI
            
ABCDQRSTNOPM
EFGHUVWXRSTQ
IJKLMNOPVWXU
└────┴────┴────┘

Usage of non-scalar X differs between implementations. Dyalog APL allows to specify different amount of rotation for different "columns" or 1-dimensional subarrays which Rotate is applied to. J allows to specify rotations for multiple leading axes at once.

      M3 4⍴⍳4
1 2 3 4
1 2 3 4
1 2 3 4
      1 3 2M  ⍝ Rotate 1st row once, 2nd row thrice, and 3rd row twice
2 3 4 1
4 1 2 3
3 4 1 2
Works in: Dyalog APL
      i.3 4
0 1  2  3
4 5  6  7
8 9 10 11
      1 2|.i.3 4  NB. Rotate vertically once, horizontally twice
 6  7 4 5
10 11 8 9
 2  3 0 1
Works in: J

Description

In languages with function axis, exactly one argument axis may be specified.

Rotating a scalar always yields that scalar unchanged. Otherwise, Rotate operates on a particular axis of its right argument. This axis is the specified axis if one is given, and otherwise the last axis for , or the first axis for .

The result array has the same shape and elements as the right argument array, but the elements cyclically move around the rotation axis. Consequently if the length of this axis is 0 or 1 then rotation has no effect.

APL model

The rotation of a vector Y by X units may be written in any APL, assuming ⎕IO0, as Y[(Y)|X+⍳⍴Y]. To rotate an arbitrary array Squad indexing with axis (or rank) is helpful.

RotateAxis  {                  ⍝ R ← X (K RotateAxis) Y
    ⎕IO0
    0=≢⍴⍵:                     ⍝ Return a scalar unchanged
    l  ⍺⍺                   ⍝ Length of rotation axis
    (l|+⍳l) [⍺⍺]            ⍝ Rotate with indexing
}

External links

Lessons

Documentation


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, Atop, Over) ∙ Inner ProductPowerAtUnderRankDepthVariantStencilCut (J)
Quad names
Arrays Index originMigration levelAtomic vector
Functions Case convertUnicode convert
Operators SearchReplace