Reverse: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
(→‎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>⌽</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]].
{{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
</source>
</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
</source>
</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│
└─────────────────┴─────────────────┴─────────────────┘
└─────────────────┴─────────────────┴─────────────────┘
</source>
</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</source> reverses the first axis of each <source lang=apl inline>k</source>-cell, or the <source lang=apl inline>1+r-k</source>'th axis of a [[rank]]-<source lang=apl inline>r</source> array.
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│
└─────────────────┴─────────────────┴─────────────────┘
└─────────────────┴─────────────────┴─────────────────┘
</source>
</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
</source>
</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>⌽</source>, or the first axis for <source lang=apl inline>⊖</source>.
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</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.
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
}
}
</source>
</syntaxhighlight>


== External links ==
== External links ==

Revision as of 21:20, 10 September 2022

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

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. <source lang=apl>

     x ← 10 20 30 ∘.+ ⍳6
     x

11 12 13 14 15 16 21 22 23 24 25 26 31 32 33 34 35 36

     ⌽x

16 15 14 13 12 11 26 25 24 23 22 21 36 35 34 33 32 31

     ⊖x

31 32 33 34 35 36 21 22 23 24 25 26 11 12 13 14 15 16 </syntaxhighlight> Since a vector only has one axis, any form of Reverse will reverse along this axis. <source lang=apl>

     ⌽ 'Backwards text'

txet sdrawkcaB

     ⊖⌽ 'Backwards text'       ⍝ One reverse undoes the other

Backwards text </syntaxhighlight> Reverse with a specified axis can reverse along any of the three dimensions of the array below. <source lang=apl>

     ⎕←a←2 3 6⍴⍳36
1  2  3  4  5  6
7  8  9 10 11 12

13 14 15 16 17 18

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

     (⌽[1]a)(⌽[2]a)(⌽[3]a)

┌─────────────────┬─────────────────┬─────────────────┐ │19 20 21 22 23 24│13 14 15 16 17 18│ 6 5 4 3 2 1│ │25 26 27 28 29 30│ 7 8 9 10 11 12│12 11 10 9 8 7│ │31 32 33 34 35 36│ 1 2 3 4 5 6│18 17 16 15 14 13│ │ │ │ │ │ 1 2 3 4 5 6│31 32 33 34 35 36│24 23 22 21 20 19│ │ 7 8 9 10 11 12│25 26 27 28 29 30│30 29 28 27 26 25│ │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</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>

     ⌽⍤2⊢a                     ⍝ Same as ⌽
5  4  3  2  1  0

11 10 9 8 7 6 17 16 15 14 13 12

23 22 21 20 19 18 29 28 27 26 25 24 35 34 33 32 31 30

     ⊖⍤2⊢a                    ⍝ Same as ⌽[2]

12 13 14 15 16 17

6  7  8  9 10 11
0  1  2  3  4  5

30 31 32 33 34 35 24 25 26 27 28 29 18 19 20 21 22 23

     (⊖⍤3⊢a)(⊖⍤2⊢a)(⊖⍤1⊢a)

┌─────────────────┬─────────────────┬─────────────────┐ │19 20 21 22 23 24│13 14 15 16 17 18│ 6 5 4 3 2 1│ │25 26 27 28 29 30│ 7 8 9 10 11 12│12 11 10 9 8 7│ │31 32 33 34 35 36│ 1 2 3 4 5 6│18 17 16 15 14 13│ │ │ │ │ │ 1 2 3 4 5 6│31 32 33 34 35 36│24 23 22 21 20 19│ │ 7 8 9 10 11 12│25 26 27 28 29 30│30 29 28 27 26 25│ │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. <source lang=apl>

     ⌽1.1

1.1 </syntaxhighlight>

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>⌽</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.

APL model

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) 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

} </syntaxhighlight>

External links

Lessons

Documentation


APL built-ins [edit]
Primitives (Timeline) Functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare RootRound
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Non-Scalar
Structural ShapeReshapeTallyDepthRavelEnlistTableCatenateReverseRotateTransposeRazeMixSplitEncloseNestCut (K)PairLinkPartitioned EnclosePartition
Selection FirstPickTakeDropUniqueIdentityStopSelectReplicateExpandSet functions (IntersectionUnionWithout) ∙ Bracket indexingIndexCartesian ProductSort
Selector Index generatorGradeIndex OfInterval IndexIndicesDealPrefix and suffix vectors
Computational MatchNot MatchMembershipFindNub SieveEncodeDecodeMatrix InverseMatrix DivideFormatExecuteMaterialiseRange
Operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-BeamSpawnFunction axisIdentity (Null, Ident)
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductDeterminantPowerAtUnderRankDepthVariantStencilCutDirect definition (operator)Identity (Lev, Dex)
Quad names Index originComparison toleranceMigration levelAtomic vector