4,500
edits
m (Text replacement - "</source>" to "</syntaxhighlight>") |
m (Text replacement - "<source" to "<syntaxhighlight") |
||
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 < | {{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]] < | 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. | ||
< | <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 18: | Line 18: | ||
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). | ||
< | <syntaxhighlight lang=apl> | ||
⎕←M←3 4⍴⎕A | ⎕←M←3 4⍴⎕A | ||
ABCD | ABCD | ||
Line 35: | Line 35: | ||
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: | ||
< | <syntaxhighlight lang=apl> | ||
⎕←M←2 3 4⍴⎕A | ⎕←M←2 3 4⍴⎕A | ||
ABCD | ABCD | ||
Line 68: | Line 68: | ||
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. | ||
< | <syntaxhighlight lang=apl> | ||
⎕←M←3 4⍴⍳4 | ⎕←M←3 4⍴⍳4 | ||
1 2 3 4 | 1 2 3 4 | ||
Line 79: | Line 79: | ||
</syntaxhighlight>{{Works in|[[Dyalog APL]]}} | </syntaxhighlight>{{Works in|[[Dyalog APL]]}} | ||
< | <syntaxhighlight lang=j> | ||
i.3 4 | i.3 4 | ||
0 1 2 3 | 0 1 2 3 | ||
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 < | 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 < | 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. | ||
< | <syntaxhighlight lang=apl> | ||
RotateAxis ← { ⍝ R ← X (K RotateAxis) Y | RotateAxis ← { ⍝ R ← X (K RotateAxis) Y | ||
⎕IO←0 | ⎕IO←0 |