Take: Difference between revisions

Jump to navigation Jump to search
2,219 bytes added ,  12:21, 17 October 2019
Examples
Miraheze>Marshall
(Created page with "Take (<code>↑</code>) is a primitive function which shortens or extends an array along one or more axes. The vector left argument indicates the lengths of r...")
 
Miraheze>Marshall
(Examples)
Line 1: Line 1:
Take (<code>↑</code>) is a [[primitive function]] which shortens or extends an array along one or more [[Axis|axes]]. The [[vector]] left argument indicates the lengths of result axes, with a sign to denote whether elements should be taken starting from the beginning or end of each axis. Take was introduced in [[APL\360]] with the requirement that the left argument length match the right argument rank, and was extended in [[SHARP APL]] to allow short left arguments. It is closely related to [[Drop]], which removes the parts of each axis that Take would include.
Take (<code>↑</code>) is a [[primitive function]] which shortens or extends an array along one or more [[Axis|axes]]. The [[vector]] left argument indicates the lengths of result axes, with a sign to denote whether elements should be taken starting from the beginning or end of each axis. Take was introduced in [[APL\360]] with the requirement that the left argument length match the right argument rank, and was extended in [[SHARP APL 19.0]] to allow short left arguments. It is closely related to [[Drop]], which removes the parts of each axis that Take would include.
 
== Examples ==
 
Take may be used to get the first few, or last few, elements of a vector:
<pre class=apl>
      3 ↑ 5 4 3 2 1
5 4 3
      ¯3 ↑ 5 4 3 2 1
3 2 1
</pre>
{{Works in|[[Dyalog APL]], [[dzaima/APL]], [[ngn/APL]]}}
The left argument to length specifies a length, and not an index. It does not depend on [[index origin]].
 
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.
<pre>
      8 ↑ 5 4 3 2 1
5 4 3 2 1 0 0 0
      ¯8 ↑ 5 4 3 2 1
0 0 0 5 4 3 2 1
</pre>
{{Works in|[[Dyalog APL]], [[ngn/APL]]}}
 
A higher-[[rank]] array can be shortened by using a left argument with one element for each axis:
<pre>
      ¯2 3↑⍳4 5
┌───┬───┬───┐
│3 1│3 2│3 3│
├───┼───┼───┤
│4 1│4 2│4 3│
└───┴───┴───┘
</pre>
{{Works in|[[Dyalog APL]], [[dzaima/APL]], [[ngn/APL]]}}
 
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.
<pre>
      ¯2↑⍳4 5
┌───┬───┬───┬───┬───┐
│3 1│3 2│3 3│3 4│3 5│
├───┼───┼───┼───┼───┤
│4 1│4 2│4 3│4 4│4 5│
└───┴───┴───┴───┴───┘
</pre>
{{Works in|[[Dyalog APL]], [[ngn/APL]]}}
 
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.
<pre>
      ¯2↑[2]⍳4 5
┌───┬───┐
│1 4│1 5│
├───┼───┤
│2 4│2 5│
├───┼───┤
│3 4│3 5│
├───┼───┤
│4 4│4 5│
└───┴───┘
</pre>
{{Works in|[[Dyalog APL]]}}
If the [[Rank operator]] is available then <code>¯2↑⍤1⍳4 5</code> is an equivalent expression.


== 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. If <code>X</code> is a scalar, it may also be accepted and 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 <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>.


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 <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.
Anonymous user

Navigation menu