Rotate: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
m (Text replacement - "http://help.dyalog.com" to "https://help.dyalog.com")
Tags: Mobile edit Mobile web edit
m (Text replacement - "<source" to "<syntaxhighlight")
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Built-ins|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 <source lang=apl inline>⌽</source>, which rotates along the last axis, while <source lang=apl inline>⊖</source>, 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 <source lang=apl inline>⊖</source> with the [[Rank operator]].
{{Built-ins|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 <syntaxhighlight lang=apl inline>⌽</syntaxhighlight>, which rotates along the last axis, while <syntaxhighlight lang=apl inline>⊖</syntaxhighlight>, 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 <syntaxhighlight lang=apl inline>⊖</syntaxhighlight> with the [[Rank operator]].


Rotate and Rotate First share the [[glyph|glyphs]] <source lang=apl inline>⌽</source> and <source lang=apl inline>⊖</source> with monadic functions [[Reverse]] and Reverse First, respectively.
Rotate and Rotate First share the [[glyph|glyphs]] <syntaxhighlight lang=apl inline>⌽</syntaxhighlight> and <syntaxhighlight lang=apl inline>⊖</syntaxhighlight> with monadic functions [[Reverse]] and Reverse First, respectively.


== Examples ==
== Examples ==
Line 7: Line 7:
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.
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.


<source lang=apl>
<syntaxhighlight lang=apl>
       3⌽1 2 3 4 5 6 7
       3⌽1 2 3 4 5 6 7
4 5 6 7 1 2 3
4 5 6 7 1 2 3
Line 14: Line 14:
       ¯4⌽1 2 3 4 5 6 7
       ¯4⌽1 2 3 4 5 6 7
4 5 6 7 1 2 3
4 5 6 7 1 2 3
</source>
</syntaxhighlight>


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


<source lang=apl>
<syntaxhighlight lang=apl>
       ⎕←M←3 4⍴⎕A
       ⎕←M←3 4⍴⎕A
ABCD
ABCD
Line 31: Line 31:
IJKL
IJKL
ABCD
ABCD
</source>
</syntaxhighlight>


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


<source lang=apl>
<syntaxhighlight lang=apl>
       ⎕←M←2 3 4⍴⎕A
       ⎕←M←2 3 4⍴⎕A
ABCD
ABCD
Line 64: Line 64:
│IJKL│MNOP│VWXU│
│IJKL│MNOP│VWXU│
└────┴────┴────┘
└────┴────┴────┘
</source>
</syntaxhighlight>


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


<source lang=apl>
<syntaxhighlight lang=apl>
       ⎕←M←3 4⍴⍳4
       ⎕←M←3 4⍴⍳4
1 2 3 4
1 2 3 4
Line 77: Line 77:
4 1 2 3
4 1 2 3
3 4 1 2
3 4 1 2
</source>{{Works in|[[Dyalog APL]]}}
</syntaxhighlight>{{Works in|[[Dyalog APL]]}}


<source lang=j>
<syntaxhighlight lang=j>
       i.3 4
       i.3 4
0 1  2  3
0 1  2  3
Line 88: Line 88:
10 11 8 9
10 11 8 9
  2  3 0 1
  2  3 0 1
</source>{{Works in|[[J]]}}
</syntaxhighlight>{{Works in|[[J]]}}


== Description ==
== Description ==
Line 94: Line 94:
In languages with [[function axis]], exactly one argument axis may be specified.
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 <source lang=apl inline>⌽</source>, or the first axis for <source lang=apl inline>⊖</source>.
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 <syntaxhighlight lang=apl inline>⌽</syntaxhighlight>, or the first axis for <syntaxhighlight lang=apl inline>⊖</syntaxhighlight>.


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.
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.
Line 100: Line 100:
=== APL model ===
=== APL model ===


The rotation of a vector <source lang=apl inline>Y</source> by <source lang=apl inline>X</source> units may be written in any APL, assuming <source lang=apl inline>⎕IO←0</source>, as <source lang=apl inline>Y[(⍴Y)|X+⍳⍴Y]</source>. To rotate an arbitrary array [[Squad indexing]] with axis (or [[Rank operator|rank]]) is helpful.
The rotation of a vector <syntaxhighlight lang=apl inline>Y</syntaxhighlight> by <syntaxhighlight lang=apl inline>X</syntaxhighlight> units may be written in any APL, assuming <syntaxhighlight lang=apl inline>⎕IO←0</syntaxhighlight>, as <syntaxhighlight lang=apl inline>Y[(⍴Y)|X+⍳⍴Y]</syntaxhighlight>. To rotate an arbitrary array [[Squad indexing]] with axis (or [[Rank operator|rank]]) is helpful.
<source lang=apl>
<syntaxhighlight lang=apl>
RotateAxis ← {                  ⍝ R ← X (K RotateAxis) Y
RotateAxis ← {                  ⍝ R ← X (K RotateAxis) Y
     ⎕IO←0
     ⎕IO←0
Line 108: Line 108:
     (⊂l|⍺+⍳l) ⌷[⍺⍺] ⍵          ⍝ Rotate with indexing
     (⊂l|⍺+⍳l) ⌷[⍺⍺] ⍵          ⍝ Rotate with indexing
}
}
</source>
</syntaxhighlight>


== External links ==
== External links ==
Line 121: Line 121:
* [http://microapl.com/apl_help/ch_020_020_520.htm APLX]
* [http://microapl.com/apl_help/ch_020_020_520.htm APLX]
* J [https://www.jsoftware.com/help/dictionary/d231.htm Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/bardot#dyadic NuVoc] (only first-axis rotate exists)
* J [https://www.jsoftware.com/help/dictionary/d231.htm Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/bardot#dyadic NuVoc] (only first-axis rotate exists)
* [https://mlochbaum.github.io/BQN/doc/reverse.html#rotate BQN]


{{APL built-ins}}[[Category:Primitive functions]][[Category:Functions with first- and last-axis forms]]
{{APL built-ins}}[[Category:Primitive functions]][[Category:Functions with first- and last-axis forms]]

Latest revision as of 22:15, 10 September 2022

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.

      3⌽1 2 3 4 5 6 7
4 5 6 7 1 2 3
      8⌽1 2 3 4 5 6 7
2 3 4 5 6 7 1
      ¯4⌽1 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).

      ⎕←M←3 4⍴⎕A
ABCD
EFGH
IJKL
      2⌽M  ⍝ Horizontally rotate twice
CDAB
GHEF
KLIJ
      1⊖M  ⍝ Vertically rotate once
EFGH
IJKL
ABCD

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

      ⎕←M←2 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
┌────┬────┬────┐
│MNOP│EFGH│BCDA│
│QRST│IJKL│FGHE│
│UVWX│ABCD│JKLI│
│    │    │    │
│ABCD│QRST│NOPM│
│EFGH│UVWX│RSTQ│
│IJKL│MNOP│VWXU│
└────┴────┴────┘
      (1⊖⍤3⊢M)(1⊖⍤2⊢M)(1⊖⍤1⊢M)  ⍝ Same as above, using Rotate First with Rank
┌────┬────┬────┐
│MNOP│EFGH│BCDA│
│QRST│IJKL│FGHE│
│UVWX│ABCD│JKLI│
│    │    │    │
│ABCD│QRST│NOPM│
│EFGH│UVWX│RSTQ│
│IJKL│MNOP│VWXU│
└────┴────┴────┘

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.

      ⎕←M←3 4⍴⍳4
1 2 3 4
1 2 3 4
1 2 3 4
      1 3 2⌽M  ⍝ 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 ⎕IO←0, as Y[(⍴Y)|X+⍳⍴Y]. To rotate an arbitrary array Squad indexing with axis (or rank) is helpful.

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

External links

Lessons

Documentation


APL built-ins [edit]
Primitives (Timeline) Functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare RootRound
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 axisIdentity (Null, Ident)
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductDeterminantPowerAtUnderRankDepthVariantStencilCutDirect definition (operator)Identity (Lev, Dex)
Quad names Index originComparison toleranceMigration levelAtomic vector