Take: Difference between revisions

Jump to navigation Jump to search
522 bytes added ,  09:07, 29 October 2019
m
Text replacement - "<code>" to "<source lang=apl inline>"
Miraheze>Adám Brudzewsky
(inline syntax coloring)
Miraheze>Adám Brudzewsky
m (Text replacement - "<code>" to "<source lang=apl inline>")
Line 61: Line 61:
== Description ==
== Description ==


In the expression <code>X↑Y</code>, <code>X</code> may be any array, and <code>Y</code> is a [[Simple array|simple]] [[numeric]] [[vector]] whose length is less than or equal to the [[rank]] of <code>Y</code>. 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. <code>X</code> 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, <code>Y</code> is also subject to [[scalar rank extension]]: if it is scalar then it will be extended so its rank is the length <code>≢X</code>.
In the expression <source lang=apl inline>X↑Y</code>, <source lang=apl inline>X</code> may be any array, and <source lang=apl inline>Y</code> is a [[Simple array|simple]] [[numeric]] [[vector]] whose length is less than or equal to the [[rank]] of <source lang=apl inline>Y</code>. 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</code> 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</code> 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</code>.


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


For each modified axis the result length along that axis is equal to the corresponding element of <code>|X</code>. If the original element in <code>X</code> 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 <source lang=apl inline>|X</code>. If the original element in <source lang=apl inline>X</code> 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 <code>(|X)≤(≢X)↑⍴Y</code>), then the result is a [[subarray]] of <code>Y</code>. Not all subarrays can be produced in this way: only those which 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 <source lang=apl inline>(|X)≤(≢X)↑⍴Y</code>), then the result is a [[subarray]] of <source lang=apl inline>Y</code>. Not all subarrays can be produced in this way: only those which align with one boundary of the argument along each axis.


=== Axis specification ===
=== Axis specification ===
Line 93: Line 93:
{{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 <code>⊢⊤(⍳×/)</code> 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 <source lang=apl inline>⊢⊤(⍳×/)</code> in place of [[Iota]] and changing the two subsequent uses of [[Each]] to Rank 1.


== History ==
== History ==


In [[A Programming Language]], prefix and suffix operations were described using the syntax <code>⍺<sup>j</sup>/x</code> to take the first <code>j</code> elements of vector <code>x</code> and <code>⍵<sup>j</sup>/x</code> for the last <code>j</code> elements. This combined a use of the special prefix and suffix vectors <code>⍺<sup>j</sup>(n)</code> and <code>⍵<sup>j</sup>(n)</code> with [[Compress|compression]], with the length <code>n</code> inferred based on the length of <code>x</code>. The symbol <code>↑</code> was used for vector [[Rotate]], while <code>↓</code> rotated in the opposite direction.<ref>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 <source lang=apl inline>⍺<sup>j</sup>/x</code> to take the first <source lang=apl inline>j</code> elements of vector <source lang=apl inline>x</code> and <source lang=apl inline>⍵<sup>j</sup>/x</code> for the last <source lang=apl inline>j</code> elements. This combined a use of the special prefix and suffix vectors <source lang=apl inline>⍺<sup>j</sup>(n)</code> and <source lang=apl inline>⍵<sup>j</sup>(n)</code> with [[Compress|compression]], with the length <source lang=apl inline>n</code> inferred based on the length of <source lang=apl inline>x</code>. The symbol <source lang=apl inline>↑</code> was used for vector [[Rotate]], while <source lang=apl inline>↓</code> rotated in the opposite direction.<ref>Iverson, K.E. (1962). A Programming Language. Wiley. ISBN 978-0-471-43014-8.</ref>


Take using the symbol <code>↑</code> was absent from the first version of [[APL\360]]<ref>Falkoff, A.D., and K.E. Iverson. [https://www.jsoftware.com/papers/APL360TerminalSystem.htm "The APL\360 Terminal System"]. Research Report RC-1922, IBM, 1967-10-16.</ref> but was introduced by 1968<ref>Falkoff, A.D., and K.E. Iverson, "[http://keiapl.org/archive/APL360_UsersMan_Aug1968.pdf APL\360 User's Manual]". IBM, August 1968.</ref>.
Take using the symbol <source lang=apl inline>↑</code> was absent from the first version of [[APL\360]]<ref>Falkoff, A.D., and K.E. Iverson. [https://www.jsoftware.com/papers/APL360TerminalSystem.htm "The APL\360 Terminal System"]. Research Report RC-1922, IBM, 1967-10-16.</ref> but was introduced by 1968<ref>Falkoff, A.D., and K.E. Iverson, "[http://keiapl.org/archive/APL360_UsersMan_Aug1968.pdf APL\360 User's Manual]". IBM, August 1968.</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 118: Line 118:
| [[SHARP APL]], [[Dyalog APL]], [[NARS2000]] || {{Yes}}          || {{Yes}}        || {{Yes}}
| [[SHARP APL]], [[Dyalog APL]], [[NARS2000]] || {{Yes}}          || {{Yes}}        || {{Yes}}
|-
|-
| [[ngn/apl]], [[J]] (<code>{.</code>)        || {{Yes}}          || {{Yes}}        || {{No}}
| [[ngn/apl]], [[J]] (<source lang=apl inline>{.</code>)        || {{Yes}}          || {{Yes}}        || {{No}}
|-
|-
| [[dzaima/APL]]                              || {{No}}          || {{Yes}}        || {{No}}
| [[dzaima/APL]]                              || {{No}}          || {{Yes}}        || {{No}}

Navigation menu