Drop: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
m (13 revisions imported: Migrate from miraheze)
m (Text replacement - "</source>" to "</syntaxhighlight>")
 
(4 intermediate revisions by 2 users not shown)
Line 4: Line 4:


Use Drop to remove leading or trailing [[elements]] from a [[vector]]:
Use Drop to remove leading or trailing [[elements]] from a [[vector]]:
<source lang=apl>
<syntaxhighlight lang=apl>
       3 ↓ 5 4 3 2 1
       3 ↓ 5 4 3 2 1
2 1
2 1
       ¯3 ↓ 5 4 3 2 1
       ¯3 ↓ 5 4 3 2 1
5 4
5 4
</source>
</syntaxhighlight>


Removing more elements than the length of the axis gives an [[empty]] result:
Removing more elements than the length of the axis gives an [[empty]] result:
<source lang=apl>
<syntaxhighlight lang=apl>
       ¯8 ↓ 5 4 3 2 1
       ¯8 ↓ 5 4 3 2 1


</source>
</syntaxhighlight>
{{Works in|[[Dyalog APL]], [[ngn/apl]]}}
{{Works in|[[Dyalog APL]], [[ngn/apl]]}}


When the right argument is a multidimensional array, one element from the left argument is used for each axis. In a language that supports [[axis specification]] for Take, or allows a short left argument, you can specify drop amounts for only some axes: other axes will remain unchanged.
When the right argument is a multidimensional array, one element from the left argument is used for each axis. In a language that supports [[axis specification]] for Take, or allows a short left argument, you can specify drop amounts for only some axes: other axes will remain unchanged.
<source lang=apl>
<syntaxhighlight lang=apl>
       2 3 ↓ ⍳4 5
       2 3 ↓ ⍳4 5
┌───┬───┐
┌───┬───┐
Line 26: Line 26:
│4 4│4 5│
│4 4│4 5│
└───┴───┘
└───┴───┘
</source>
</syntaxhighlight>


== Description ==
== Description ==
Line 32: Line 32:
The arguments to Drop must be valid arguments to [[Take]], and elements of the left argument are matched to [[axes]] of the right in the same way. Like Take, the left argument is subject to [[scalar rank extension]], and in some APLs the right is as well.
The arguments to Drop must be valid arguments to [[Take]], and elements of the left argument are matched to [[axes]] of the right in the same way. Like Take, the left argument is subject to [[scalar rank extension]], and in some APLs the right is as well.


Each axis with a matching left argument element <source lang=apl inline>d</source> is modified to remove <source lang=apl inline>|d</source> indices, or all of them if <source lang=apl inline>|d</source> is larger than the length of the axis. Indices are removed starting from the beginning if <source lang=apl inline>d</source> is positive and starting from the end if it is negative (if <source lang=apl inline>d</source> is 0, then no indices are removed). Equivalently, for an axis of length <source lang=apl inline>l</source>, <source lang=apl inline>l-l⌊|d</source> axes are retained—not dropped—from the axis starting from the opposite side: the end if <source lang=apl inline>d</source> is positive and the beginning otherwise. If <source lang=apl inline>|d</source> is greater than or equal to <source lang=apl inline>l</source> for any axis, then the result is [[empty array|empty]] and shares a [[prototype]] with the argument.
Each axis with a matching left argument element <syntaxhighlight lang=apl inline>d</syntaxhighlight> is modified to remove <syntaxhighlight lang=apl inline>|d</syntaxhighlight> indices, or all of them if <syntaxhighlight lang=apl inline>|d</syntaxhighlight> is larger than the length of the axis. Indices are removed starting from the beginning if <syntaxhighlight lang=apl inline>d</syntaxhighlight> is positive and starting from the end if it is negative (if <syntaxhighlight lang=apl inline>d</syntaxhighlight> is 0, then no indices are removed). Equivalently, for an axis of length <syntaxhighlight lang=apl inline>l</syntaxhighlight>, <syntaxhighlight lang=apl inline>l-l⌊|d</syntaxhighlight> axes are retained—not dropped—from the axis starting from the opposite side: the end if <syntaxhighlight lang=apl inline>d</syntaxhighlight> is positive and the beginning otherwise. If <syntaxhighlight lang=apl inline>|d</syntaxhighlight> is greater than or equal to <syntaxhighlight lang=apl inline>l</syntaxhighlight> for any axis, then the result is [[empty array|empty]] and shares a [[prototype]] with the argument.


=== APL Model ===
=== APL Model ===


Drop can be modelled using Take with a complementary length:
Drop can be modelled using Take with a complementary length:
<source lang=apl>
<syntaxhighlight lang=apl>
Drop ← {
Drop ← {
     s ← ⍴⍵
     s ← ⍴⍵
Line 44: Line 44:
     ((sׯ1*⍺>0) + (-s)⌈s⌊⍺) ↑ ⍵
     ((sׯ1*⍺>0) + (-s)⌈s⌊⍺) ↑ ⍵
}
}
</source>
</syntaxhighlight>
The code works by modifying the left argument using the right argument's shape. First, the shape is used to restrict the left argument's range (<source lang=apl inline>(-s)⌈s⌊⍺</source>) so that the maximum number of indices dropped is the length of the axis. Then, the shape is added or subtracted so that the left argument's sign is flipped.
The code works by modifying the left argument using the right argument's shape. First, the shape is used to restrict the left argument's range (<syntaxhighlight lang=apl inline>(-s)⌈s⌊⍺</syntaxhighlight>) so that the maximum number of indices dropped is the length of the axis. Then, the shape is added or subtracted so that the left argument's sign is flipped.


This implementation inherits all argument extensions from Take. The two middle lines are used to support right argument scalar rank extension and the [[SHARP APL]] short left argument extension. If Take does not have these extensions then they have no effect and can be removed.
This implementation inherits all argument extensions from Take. The two middle lines are used to support right argument scalar rank extension and the [[SHARP APL]] short left argument extension. If Take does not have these extensions then they have no effect and can be removed.
Line 57: Line 57:
=== Documentation ===
=== Documentation ===


* [http://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Drop.htm Dyalog] ([http://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Drop%20with%20Axes.htm with axis])
* [https://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Drop.htm Dyalog] ([https://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Drop%20with%20Axes.htm with axis])


* [http://microapl.com/apl_help/ch_020_020_570.htm APLX]
* [http://microapl.com/apl_help/ch_020_020_570.htm APLX]


* [https://www.jsoftware.com/help/dictionary/d531.htm J Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/curlyrtdot#dyadic J NuVoc] (as <source lang=apl inline>}.</source>)
* [https://www.jsoftware.com/help/dictionary/d531.htm J Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/curlyrtdot#dyadic J NuVoc] (as <syntaxhighlight lang=apl inline>}.</syntaxhighlight>)
{{APL built-ins}}
 
* [https://mlochbaum.github.io/BQN/doc/take.html BQN]
{{APL built-ins}}[[Category:Primitive functions]]

Latest revision as of 21:23, 10 September 2022

Drop () is a primitive function which removes part of each axis of an array. The removed portions are exactly those that Take would include in its result. Drop is subject to the same extensions as Take, and a detailed description of those extensions and their history is given in that article rather than this one. Unlike Take, Drop never uses fill elements and the result is always a subarray of the argument.

Examples

Use Drop to remove leading or trailing elements from a vector:

      3 ↓ 5 4 3 2 1
2 1
      ¯3 ↓ 5 4 3 2 1
5 4

Removing more elements than the length of the axis gives an empty result:

      ¯8 ↓ 5 4 3 2 1
Works in: Dyalog APL, ngn/apl

When the right argument is a multidimensional array, one element from the left argument is used for each axis. In a language that supports axis specification for Take, or allows a short left argument, you can specify drop amounts for only some axes: other axes will remain unchanged.

      2 3 ↓ ⍳4 5
┌───┬───┐
│3 4│3 5│
├───┼───┤
│4 4│4 5│
└───┴───┘

Description

The arguments to Drop must be valid arguments to Take, and elements of the left argument are matched to axes of the right in the same way. Like Take, the left argument is subject to scalar rank extension, and in some APLs the right is as well.

Each axis with a matching left argument element d is modified to remove |d indices, or all of them if |d is larger than the length of the axis. Indices are removed starting from the beginning if d is positive and starting from the end if it is negative (if d is 0, then no indices are removed). Equivalently, for an axis of length l, l-l⌊|d axes are retained—not dropped—from the axis starting from the opposite side: the end if d is positive and the beginning otherwise. If |d is greater than or equal to l for any axis, then the result is empty and shares a prototype with the argument.

APL Model

Drop can be modelled using Take with a complementary length:

Drop ← {
    s ← ⍴⍵
    s ,← ((0=≢s)×≢⍺)⍴1          ⍝ Scalar rank extension
    s ← (≢⍺)↑s                  ⍝ SHARP APL extension
    ((sׯ1*⍺>0) + (-s)⌈s⌊⍺) ↑ ⍵
}

The code works by modifying the left argument using the right argument's shape. First, the shape is used to restrict the left argument's range ((-s)⌈s⌊⍺) so that the maximum number of indices dropped is the length of the axis. Then, the shape is added or subtracted so that the left argument's sign is flipped.

This implementation inherits all argument extensions from Take. The two middle lines are used to support right argument scalar rank extension and the SHARP APL short left argument extension. If Take does not have these extensions then they have no effect and can be removed.

External links

Lessons

Documentation

APL built-ins [edit]
Primitives (Timeline) Functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare Root
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 axis
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductDeterminantPowerAtUnderRankDepthVariantStencilCutDirect definition (operator)
Quad names Index originComparison toleranceMigration levelAtomic vector