Reverse: Difference between revisions
Jump to navigation
Jump to search
→External links
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...") |
Miraheze>Marshall |
||
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> | ||