4,577
edits
m (Text replacement - " ⊢( *[^∘])" to " ⎕←$1") |
m (Text replacement - "<source" to "<syntaxhighlight") |
||
(5 intermediate revisions by 3 users not shown) | |||
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 < | {{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 <syntaxhighlight lang=apl inline>⌽</syntaxhighlight>, which reverses along the last axis, while <syntaxhighlight 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 <syntaxhighlight lang=apl inline>⊖</syntaxhighlight> with the [[Rank operator]]. | ||
== Examples == | == Examples == | ||
Reverse flips a [[matrix]] horizontally, changing elements to go right to left, and Reverse First flip vertically, so the elements are reordered top to bottom. | Reverse flips a [[matrix]] horizontally, changing elements to go right to left, and Reverse First flip vertically, so the elements are reordered top to bottom. | ||
< | <syntaxhighlight lang=apl> | ||
x ← 10 20 30 ∘.+ ⍳6 | x ← 10 20 30 ∘.+ ⍳6 | ||
x | x | ||
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. | ||
< | <syntaxhighlight lang=apl> | ||
⌽ 'Backwards text' | ⌽ 'Backwards text' | ||
txet sdrawkcaB | txet sdrawkcaB | ||
⊖⌽ '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. | ||
< | <syntaxhighlight lang=apl> | ||
⎕←a←2 3 6⍴⍳36 | ⎕←a←2 3 6⍴⍳36 | ||
1 2 3 4 5 6 | 1 2 3 4 5 6 | ||
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 < | 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 <syntaxhighlight lang=apl inline>k</syntaxhighlight> reverses the first axis of each <syntaxhighlight lang=apl inline>k</syntaxhighlight>-cell, or the <syntaxhighlight lang=apl inline>1+r-k</syntaxhighlight>'th axis of a [[rank]]-<syntaxhighlight lang=apl inline>r</syntaxhighlight> array. | ||
< | <syntaxhighlight lang=apl> | ||
⌽⍤2⊢a ⍝ Same as ⌽ | ⌽⍤2⊢a ⍝ Same as ⌽ | ||
5 4 3 2 1 0 | 5 4 3 2 1 0 | ||
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. | ||
< | <syntaxhighlight 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 < | 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 <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 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 < | The reverse of a vector <syntaxhighlight lang=apl inline>x</syntaxhighlight> may be written in any APL, assuming <syntaxhighlight lang=apl inline>⎕IO←0</syntaxhighlight>, as <syntaxhighlight lang=apl inline>x[(¯1+⍴x)-⍳⍴x]</syntaxhighlight>. To reverse an arbitrary array [[Squad indexing]] with axis (or [[Rank operator|rank]]) is helpful. | ||
< | <syntaxhighlight lang=apl> | ||
ReverseAxis ← { | ReverseAxis ← { | ||
⎕IO←0 | ⎕IO←0 | ||
Line 99: | Line 99: | ||
⍺ ← ¯1+≢⍴⍵ ⍝ Assume last axis | ⍺ ← ¯1+≢⍴⍵ ⍝ Assume last axis | ||
l ← ⍺ ⌷ ⍴⍵ ⍝ Length of reversed axis | l ← ⍺ ⌷ ⍴⍵ ⍝ Length of reversed axis | ||
( | (⊂l-1+⍳l) ⌷[⍺] ⍵ ⍝ Reverse with indexing | ||
} | } | ||
</ | </syntaxhighlight> | ||
== External links == | == External links == | ||
Line 111: | Line 111: | ||
=== Documentation === | === Documentation === | ||
* [ | * [https://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Reverse.htm Dyalog] | ||
* [http://microapl.com/apl_help/ch_020_020_510.htm APLX] | * [http://microapl.com/apl_help/ch_020_020_510.htm APLX] | ||
* J [https://www.jsoftware.com/help/dictionary/d231.htm Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/bardot NuVoc] (only first-axis reverse exists) | * J [https://www.jsoftware.com/help/dictionary/d231.htm Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/bardot NuVoc] (only first-axis reverse exists) | ||
* [https://mlochbaum.github.io/BQN/doc/reverse.html 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]] |