Take: Difference between revisions

Jump to navigation Jump to search
279 bytes added ,  11:03, 11 September 2022
m
Text replacement - "<source" to "<syntaxhighlight"
m (Text replacement - "</source>" to "</syntaxhighlight>")
m (Text replacement - "<source" to "<syntaxhighlight")
Line 4: Line 4:


Take may be used to get the first few, or last few, elements of a vector:
Take may be used to get the first few, or last few, elements of a vector:
<source lang=apl>
<syntaxhighlight lang=apl>
       3 ↑ 5 4 3 2 1
       3 ↑ 5 4 3 2 1
5 4 3
5 4 3
Line 13: Line 13:
=== Overtaking ===
=== Overtaking ===
A length which is larger than the argument length causes [[Fill element|fills]] to be inserted. The alignment remains the same: if two different positive arguments are used to take from an array, the one which is closer to zero gives a [[prefix]] of the other result. If they are both negative, it is a [[suffix]] instead. When Take makes an axis longer, it is said to "overtake" along that axis.
A length which is larger than the argument length causes [[Fill element|fills]] to be inserted. The alignment remains the same: if two different positive arguments are used to take from an array, the one which is closer to zero gives a [[prefix]] of the other result. If they are both negative, it is a [[suffix]] instead. When Take makes an axis longer, it is said to "overtake" along that axis.
<source lang=apl>
<syntaxhighlight lang=apl>
       8 ↑ 5 4 3 2 1
       8 ↑ 5 4 3 2 1
5 4 3 2 1 0 0 0
5 4 3 2 1 0 0 0
Line 22: Line 22:
=== Truncation ===
=== Truncation ===
A higher-[[rank]] array can be shortened by using a left argument with one element for each axis:
A higher-[[rank]] array can be shortened by using a left argument with one element for each axis:
<source lang=apl>
<syntaxhighlight lang=apl>
       ¯2 3↑⍳4 5
       ¯2 3↑⍳4 5
┌───┬───┬───┐
┌───┬───┬───┐
Line 33: Line 33:
=== Leading axes ===
=== Leading axes ===
In languages with the SHARP APL extension, the left argument can be shortened. This causes leading axes of the right argument to be modified while trailing axes are ignored.
In languages with the SHARP APL extension, the left argument can be shortened. This causes leading axes of the right argument to be modified while trailing axes are ignored.
<source lang=apl>
<syntaxhighlight lang=apl>
       ¯2↑⍳4 5
       ¯2↑⍳4 5
┌───┬───┬───┬───┬───┐
┌───┬───┬───┬───┬───┐
Line 44: Line 44:
=== Function axis ===
=== Function axis ===
An [[Function axis|axis]] may be specified to apply left argument elements to specific axes of the right argument. Here the last axis is specified in order to take two columns of the argument.
An [[Function axis|axis]] may be specified to apply left argument elements to specific axes of the right argument. Here the last axis is specified in order to take two columns of the argument.
<source lang=apl>
<syntaxhighlight lang=apl>
       ¯2↑[2]⍳4 5
       ¯2↑[2]⍳4 5
┌───┬───┐
┌───┬───┐
Line 57: Line 57:
</syntaxhighlight>
</syntaxhighlight>
{{Works in|[[Dyalog APL]]}}
{{Works in|[[Dyalog APL]]}}
If the [[Rank operator]] is available then <source lang=apl inline>¯2↑⍤1⍳4 5</syntaxhighlight> is an equivalent expression.
If the [[Rank operator]] is available then <syntaxhighlight lang=apl inline>¯2↑⍤1⍳4 5</syntaxhighlight> is an equivalent expression.


== Description ==
== Description ==


In the expression <source lang=apl inline>X↑Y</syntaxhighlight>, <source lang=apl inline>X</syntaxhighlight> may be any array, and <source lang=apl inline>Y</syntaxhighlight> is a [[Simple array|simple]] [[numeric]] [[vector]] whose length is less than or equal to the [[rank]] of <source lang=apl inline>Y</syntaxhighlight>. Many APLs require the length to be exactly equal; however, an extension by [[SHARP APL]] to allow a shorter left argument has been widely adopted by recent APLs. <source lang=apl inline>X</syntaxhighlight> may also be a scalar, in which case it is treated as a one-element vector in an instance of [[scalar rank extension]]. In some APLs, <source lang=apl inline>Y</syntaxhighlight> is also subject to [[scalar rank extension]]: if it is scalar then it will be extended so its rank is the length <source lang=apl inline>≢X</syntaxhighlight>.
In the expression <syntaxhighlight lang=apl inline>X↑Y</syntaxhighlight>, <syntaxhighlight lang=apl inline>X</syntaxhighlight> may be any array, and <syntaxhighlight lang=apl inline>Y</syntaxhighlight> is a [[Simple array|simple]] [[numeric]] [[vector]] whose length is less than or equal to the [[rank]] of <syntaxhighlight lang=apl inline>Y</syntaxhighlight>. Many APLs require the length to be exactly equal; however, an extension by [[SHARP APL]] to allow a shorter left argument has been widely adopted by recent APLs. <syntaxhighlight lang=apl inline>X</syntaxhighlight> may also be a scalar, in which case it is treated as a one-element vector in an instance of [[scalar rank extension]]. In some APLs, <syntaxhighlight lang=apl inline>Y</syntaxhighlight> is also subject to [[scalar rank extension]]: if it is scalar then it will be extended so its rank is the length <syntaxhighlight lang=apl inline>≢X</syntaxhighlight>.


Elements of <source lang=apl inline>X</syntaxhighlight> are matched with axes of <source lang=apl inline>Y</syntaxhighlight> with the same [[index]], that is, the left argument corresponds to [[Leading axis theory|leading axes]] of the right. The trailing axes of <source lang=apl inline>Y</syntaxhighlight> which are not matched in this way are unchanged by Take; this may also be modelled by extending <source lang=apl inline>X</syntaxhighlight> using the lengths of those axes.
Elements of <syntaxhighlight lang=apl inline>X</syntaxhighlight> are matched with axes of <syntaxhighlight lang=apl inline>Y</syntaxhighlight> with the same [[index]], that is, the left argument corresponds to [[Leading axis theory|leading axes]] of the right. The trailing axes of <syntaxhighlight lang=apl inline>Y</syntaxhighlight> which are not matched in this way are unchanged by Take; this may also be modelled by extending <syntaxhighlight lang=apl inline>X</syntaxhighlight> using the lengths of those axes.


For each modified axis the result length along that axis is equal to the corresponding element of <source lang=apl inline>|X</syntaxhighlight>. If the original element in <source lang=apl inline>X</syntaxhighlight> is positive then the result is aligned with the argument at the beginning of that axis, and if that element is negative they are aligned at the end. If it is zero then the result is empty, so both are true. Elements from the right argument are used in the result until the result is filled. If the argument axis is shorter than the result axis then [[Fill element|fills]] are used once it is exhausted. If the result is empty, its [[prototype]] is the same as the right argument's.
For each modified axis the result length along that axis is equal to the corresponding element of <syntaxhighlight lang=apl inline>|X</syntaxhighlight>. If the original element in <syntaxhighlight lang=apl inline>X</syntaxhighlight> is positive then the result is aligned with the argument at the beginning of that axis, and if that element is negative they are aligned at the end. If it is zero then the result is empty, so both are true. Elements from the right argument are used in the result until the result is filled. If the argument axis is shorter than the result axis then [[Fill element|fills]] are used once it is exhausted. If the result is empty, its [[prototype]] is the same as the right argument's.


If the result is no larger than the right argument along each axis (equivalently, no fills are used, or <source lang=apl inline>(|X)≤(≢X)↑⍴Y</syntaxhighlight>), then the result is a [[subarray]] of <source lang=apl inline>Y</syntaxhighlight>. Not all subarrays can be produced in this way: only those which have the same rank and align with one boundary of the argument along each axis.
If the result is no larger than the right argument along each axis (equivalently, no fills are used, or <syntaxhighlight lang=apl inline>(|X)≤(≢X)↑⍴Y</syntaxhighlight>), then the result is a [[subarray]] of <syntaxhighlight lang=apl inline>Y</syntaxhighlight>. Not all subarrays can be produced in this way: only those which have the same rank and align with one boundary of the argument along each axis.


=== Axis specification ===
=== Axis specification ===
Line 85: Line 85:
The following [[dfn]] models Take as defined by [[Dyalog APL]] but with no axis specification or error checking. It is implemented by construction a [[nested array]] of indices and using these to select from the right argument, with prototypes used for out-of-range indices. It explicitly includes [[scalar rank extension]] for the right argument and the SHARP APL extension; if these extensions are not wanted those lines can be removed. [[Scalar rank extension]] of the left argument is inherited from [[Iota]] and [[scalar function]] extension.
The following [[dfn]] models Take as defined by [[Dyalog APL]] but with no axis specification or error checking. It is implemented by construction a [[nested array]] of indices and using these to select from the right argument, with prototypes used for out-of-range indices. It explicitly includes [[scalar rank extension]] for the right argument and the SHARP APL extension; if these extensions are not wanted those lines can be removed. [[Scalar rank extension]] of the left argument is inherited from [[Iota]] and [[scalar function]] extension.


<source lang=apl>
<syntaxhighlight lang=apl>
Take ← {
Take ← {
     ⎕IO←0                        ⍝ For index comparisons
     ⎕IO←0                        ⍝ For index comparisons
Line 101: Line 101:
{{Works in|[[Dyalog APL]]}}
{{Works in|[[Dyalog APL]]}}


This definition could be converted to work in a [[Flat array model|flat]] APL with the [[Rank operator]] by using an [[odometer function]] like <source lang=apl inline>⊢⊤(⍳×/)</syntaxhighlight> in place of [[Iota]] and changing the two subsequent uses of [[Each]] to Rank 1.
This definition could be converted to work in a [[Flat array model|flat]] APL with the [[Rank operator]] by using an [[odometer function]] like <syntaxhighlight lang=apl inline>⊢⊤(⍳×/)</syntaxhighlight> in place of [[Iota]] and changing the two subsequent uses of [[Each]] to Rank 1.


== History ==
== History ==
Line 107: Line 107:
In [[A Programming Language]], [[prefix]] and [[suffix]] operations were described using the syntax <math>\alpha^j/x</math> to take the first <math>j</math> elements of vector <math>x</math> and <math>\omega^j/x</math> for the last <math>j</math> elements. This combined a use of the special prefix and suffix vectors <math>\alpha^j(n)</math> and <math>\omega^j(n)</math> with [[Compress|compression]], with the length <math>n</math> inferred based on the length of <math>x</math>. The symbol <math>\uparrow</math> was used for vector [[Rotate]], while <math>\downarrow</math> rotated in the opposite direction.<ref>[[Ken Iverson|Iverson, K.E.]] (1962). A Programming Language. Wiley. ISBN 978-0-471-43014-8.</ref>
In [[A Programming Language]], [[prefix]] and [[suffix]] operations were described using the syntax <math>\alpha^j/x</math> to take the first <math>j</math> elements of vector <math>x</math> and <math>\omega^j/x</math> for the last <math>j</math> elements. This combined a use of the special prefix and suffix vectors <math>\alpha^j(n)</math> and <math>\omega^j(n)</math> with [[Compress|compression]], with the length <math>n</math> inferred based on the length of <math>x</math>. The symbol <math>\uparrow</math> was used for vector [[Rotate]], while <math>\downarrow</math> rotated in the opposite direction.<ref>[[Ken Iverson|Iverson, K.E.]] (1962). A Programming Language. Wiley. ISBN 978-0-471-43014-8.</ref>


The first version of [[APL\360]]<ref>[[Adin Falkoff|Falkoff, A.D.]], and [[Ken Iverson|K.E. Iverson]]. [https://www.jsoftware.com/papers/APL360TerminalSystem.htm "The APL\360 Terminal System"]. Research Report RC-1922, IBM, 1967-10-16.</ref> followed Iverson notation in defining prefix (<source lang=apl inline>n ⍺ j</syntaxhighlight>) and suffix (<source lang=apl inline>n ⍵ j</syntaxhighlight>) vectors but not Take, even though the arrow symbols were no longer used for Rotate. For a vector <source lang=apl inline>x</syntaxhighlight>, what is now <source lang=apl inline>j↑x</syntaxhighlight> would have been written <source lang=apl inline>((⍴x)⍺j)/x</syntaxhighlight> while <source lang=apl inline>(-j)↑x</syntaxhighlight> would be <source lang=apl inline>((⍴x)⍵j)/x</syntaxhighlight>. The functions Take and Drop using arrow symbols were introduced by 1968.<ref>[[Adin Falkoff|Falkoff, A.D.]], and [[Ken Iverson|K.E. Iverson]], "[http://keiapl.org/archive/APL360_UsersMan_Aug1968.pdf APL\360 User's Manual]". [[IBM]], August 1968.</ref> They were implemented for arrays (not just vectors), and extended to allow the left argument to be larger than the right argument's shape (introducing overtaking and [[fill element]]s) in 1970.<ref>"[[IBM|I.B.M.]] Report". [[APL Quote-Quad]] Volume 2, Number 1. 1970-04.</ref>
The first version of [[APL\360]]<ref>[[Adin Falkoff|Falkoff, A.D.]], and [[Ken Iverson|K.E. Iverson]]. [https://www.jsoftware.com/papers/APL360TerminalSystem.htm "The APL\360 Terminal System"]. Research Report RC-1922, IBM, 1967-10-16.</ref> followed Iverson notation in defining prefix (<syntaxhighlight lang=apl inline>n ⍺ j</syntaxhighlight>) and suffix (<syntaxhighlight lang=apl inline>n ⍵ j</syntaxhighlight>) vectors but not Take, even though the arrow symbols were no longer used for Rotate. For a vector <syntaxhighlight lang=apl inline>x</syntaxhighlight>, what is now <syntaxhighlight lang=apl inline>j↑x</syntaxhighlight> would have been written <syntaxhighlight lang=apl inline>((⍴x)⍺j)/x</syntaxhighlight> while <syntaxhighlight lang=apl inline>(-j)↑x</syntaxhighlight> would be <syntaxhighlight lang=apl inline>((⍴x)⍵j)/x</syntaxhighlight>. The functions Take and Drop using arrow symbols were introduced by 1968.<ref>[[Adin Falkoff|Falkoff, A.D.]], and [[Ken Iverson|K.E. Iverson]], "[http://keiapl.org/archive/APL360_UsersMan_Aug1968.pdf APL\360 User's Manual]". [[IBM]], August 1968.</ref> They were implemented for arrays (not just vectors), and extended to allow the left argument to be larger than the right argument's shape (introducing overtaking and [[fill element]]s) in 1970.<ref>"[[IBM|I.B.M.]] Report". [[APL Quote-Quad]] Volume 2, Number 1. 1970-04.</ref>


The [[Function axis|axis]] specification for Take was defined in [[APL2]]. It is shared by [[SHARP APL]] and [[Rationalized APL]], and continues to be supported in [[Dyalog APL]].
The [[Function axis|axis]] specification for Take was defined in [[APL2]]. It is shared by [[SHARP APL]] and [[Rationalized APL]], and continues to be supported in [[Dyalog APL]].
Line 126: Line 126:
| [[SHARP APL]], [[Dyalog APL]], [[NARS2000]] || {{Yes}}          || {{Yes}}        || {{Yes}}
| [[SHARP APL]], [[Dyalog APL]], [[NARS2000]] || {{Yes}}          || {{Yes}}        || {{Yes}}
|-
|-
| [[A+]], [[ngn/apl]], [[J]] (<source lang=j inline>{.</syntaxhighlight>), [[BQN]] || {{Yes}}          || {{Yes}}        || {{No}}
| [[A+]], [[ngn/apl]], [[J]] (<syntaxhighlight lang=j inline>{.</syntaxhighlight>), [[BQN]] || {{Yes}}          || {{Yes}}        || {{No}}
|-
|-
| [[dzaima/APL]]                              || {{No}}          || {{Yes}}        || {{No}}
| [[dzaima/APL]]                              || {{No}}          || {{Yes}}        || {{No}}

Navigation menu