4,494
edits
(→Documentation: BQN link) |
m (Text replacement - "</source>" to "</syntaxhighlight>") |
||
Line 1: | Line 1: | ||
{{Built-ins|Reverse|⌽|⊖}} is a [[monadic function]] which reorders [[elements]] of the argument to go in the opposite direction along a specified [[axis]]. The name Reverse is typically used for the primitive <source lang=apl inline>⌽</ | {{Built-ins|Reverse|⌽|⊖}} is a [[monadic function]] which reorders [[elements]] of the argument to go in the opposite direction along a specified [[axis]]. The name Reverse is typically used for the primitive <source lang=apl inline>⌽</syntaxhighlight>, which reverses along the last axis, while <source lang=apl inline>⊖</syntaxhighlight>, which reverses along the first axis, is called "Reverse First", "Reverse-down", or similar. In APLs with [[function axis]], either form may use a specified axis which overrides this default choice of axis. In the [[leading axis model]], specifying an axis is discouraged in favor of using <source lang=apl inline>⊖</syntaxhighlight> with the [[Rank operator]]. | ||
== Examples == | == Examples == | ||
Line 18: | Line 18: | ||
21 22 23 24 25 26 | 21 22 23 24 25 26 | ||
11 12 13 14 15 16 | 11 12 13 14 15 16 | ||
</ | </syntaxhighlight> | ||
Since a [[vector]] only has one axis, any form of Reverse will reverse along this axis. | Since a [[vector]] only has one axis, any form of Reverse will reverse along this axis. | ||
<source lang=apl> | <source lang=apl> | ||
Line 25: | Line 25: | ||
⊖⌽ 'Backwards text' ⍝ One reverse undoes the other | ⊖⌽ 'Backwards text' ⍝ One reverse undoes the other | ||
Backwards text | Backwards text | ||
</ | </syntaxhighlight> | ||
Reverse with a [[specified axis]] can reverse along any of the three dimensions of the array below. | Reverse with a [[specified axis]] can reverse along any of the three dimensions of the array below. | ||
<source lang=apl> | <source lang=apl> | ||
Line 46: | Line 46: | ||
│13 14 15 16 17 18│19 20 21 22 23 24│36 35 34 33 32 31│ | │13 14 15 16 17 18│19 20 21 22 23 24│36 35 34 33 32 31│ | ||
└─────────────────┴─────────────────┴─────────────────┘ | └─────────────────┴─────────────────┴─────────────────┘ | ||
</ | </syntaxhighlight> | ||
The [[Rank operator]] can also be used to reverse along a particular axis. While Rank has no effect on Reverse (last), Reverse First with rank <source lang=apl inline>k</ | The [[Rank operator]] can also be used to reverse along a particular axis. While Rank has no effect on Reverse (last), Reverse First with rank <source lang=apl inline>k</syntaxhighlight> reverses the first axis of each <source lang=apl inline>k</syntaxhighlight>-cell, or the <source lang=apl inline>1+r-k</syntaxhighlight>'th axis of a [[rank]]-<source lang=apl inline>r</syntaxhighlight> array. | ||
<source lang=apl> | <source lang=apl> | ||
⌽⍤2⊢a ⍝ Same as ⌽ | ⌽⍤2⊢a ⍝ Same as ⌽ | ||
Line 75: | Line 75: | ||
│13 14 15 16 17 18│19 20 21 22 23 24│36 35 34 33 32 31│ | │13 14 15 16 17 18│19 20 21 22 23 24│36 35 34 33 32 31│ | ||
└─────────────────┴─────────────────┴─────────────────┘ | └─────────────────┴─────────────────┴─────────────────┘ | ||
</ | </syntaxhighlight> | ||
Reversing a [[scalar]] has no effect: there are no axes to reverse along. | Reversing a [[scalar]] has no effect: there are no axes to reverse along. | ||
<source lang=apl> | <source lang=apl> | ||
⌽1.1 | ⌽1.1 | ||
1.1 | 1.1 | ||
</ | </syntaxhighlight> | ||
== Description == | == Description == | ||
Line 86: | Line 86: | ||
In languages with [[function axis]], exactly one argument axis may be specified. | In languages with [[function axis]], exactly one argument axis may be specified. | ||
Reversing a [[scalar]] always yields that scalar unchanged. Otherwise, Reverse operates on a particular axis of its argument. This axis is the specified axis if one is given, and otherwise the last axis for <source lang=apl inline>⌽</ | Reversing a [[scalar]] always yields that scalar unchanged. Otherwise, Reverse operates on a particular axis of its argument. This axis is the specified axis if one is given, and otherwise the last axis for <source lang=apl inline>⌽</syntaxhighlight>, or the first axis for <source lang=apl inline>⊖</syntaxhighlight>. | ||
The result array has the same [[shape]] and [[elements]] as the argument array, but the elements go in the opposite direction along the reversal axis: the first in the argument is last in the result, and so on. Consequently if the length of this axis is 0 or 1 then reversing has no effect. | The result array has the same [[shape]] and [[elements]] as the argument array, but the elements go in the opposite direction along the reversal axis: the first in the argument is last in the result, and so on. Consequently if the length of this axis is 0 or 1 then reversing has no effect. | ||
Line 92: | Line 92: | ||
=== APL model === | === APL model === | ||
The reverse of a vector <source lang=apl inline>x</ | The reverse of a vector <source lang=apl inline>x</syntaxhighlight> may be written in any APL, assuming <source lang=apl inline>⎕IO←0</syntaxhighlight>, as <source lang=apl inline>x[(¯1+⍴x)-⍳⍴x]</syntaxhighlight>. To reverse an arbitrary array [[Squad indexing]] with axis (or [[Rank operator|rank]]) is helpful. | ||
<source lang=apl> | <source lang=apl> | ||
ReverseAxis ← { | ReverseAxis ← { | ||
Line 101: | Line 101: | ||
(⊂l-1+⍳l) ⌷[⍺] ⍵ ⍝ Reverse with indexing | (⊂l-1+⍳l) ⌷[⍺] ⍵ ⍝ Reverse with indexing | ||
} | } | ||
</ | </syntaxhighlight> | ||
== External links == | == External links == |