trusted
183
edits
Miraheze>Marshall (Created page with "{{Primitive|⌽ ⊖|Reverse}} is a monadic function which reorders elements of the argument to go in the opposite direction along a specified axis. The name Revers...") |
m (→APL model: Fix alignment) |
||
(10 intermediate revisions by 6 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 <source lang=apl inline>⌽</source>, which reverses along the last axis, while <source lang=apl inline>⊖</source>, 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>⊖</source> with the [[Rank operator]]. | ||
== Examples == | == Examples == | ||
Line 28: | Line 28: | ||
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> | ||
⎕←a←2 3 6⍴⍳36 | |||
1 2 3 4 5 6 | 1 2 3 4 5 6 | ||
7 8 9 10 11 12 | 7 8 9 10 11 12 | ||
Line 80: | Line 80: | ||
⌽1.1 | ⌽1.1 | ||
1.1 | 1.1 | ||
</source> | |||
== Description == | |||
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>⌽</source>, or the first axis for <source lang=apl inline>⊖</source>. | |||
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. | |||
=== APL model === | |||
The reverse of a vector <source lang=apl inline>x</source> may be written in any APL, assuming <source lang=apl inline>⎕IO←0</source>, as <source lang=apl inline>x[(¯1+⍴x)-⍳⍴x]</source>. To reverse an arbitrary array [[Squad indexing]] with axis (or [[Rank operator|rank]]) is helpful. | |||
<source lang=apl> | |||
ReverseAxis ← { | |||
⎕IO←0 | |||
0=≢⍴⍵: ⍵ ⍝ Return a scalar unchanged | |||
⍺ ← ¯1+≢⍴⍵ ⍝ Assume last axis | |||
l ← ⍺ ⌷ ⍴⍵ ⍝ Length of reversed axis | |||
(⊂l-1+⍳l) ⌷[⍺] ⍵ ⍝ Reverse with indexing | |||
} | |||
</source> | </source> | ||
Line 90: | Line 111: | ||
=== Documentation === | === Documentation === | ||
* [http://help.dyalog.com/latest/ | * [http://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) | ||
{{APL built-ins}}[[Category:Primitive functions]][[Category:Functions with first- and last-axis forms]] |