Take

From APL Wiki
Revision as of 11:09, 17 October 2019 by 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...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Take () is a primitive function which shortens or extends an array along one or more 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.

Description

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

Elements of X are matched with axes of Y with the same index, that is, the left argument corresponds to leading axes of the right. The trailing axes of Y which are not matched in this way are unchanged by Take; this may also be modelled by extending X using the lengths of those axes.

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

Axis specification

When Take is called with axis, the axis determines how elements of the left argument correspond to axes of the right argument. The left argument and axis are required to have rank no more than 1 and are treated as vectors. Their lengths must match, and be less than or equal to the rank of the right argument. Then each element of the left argument applies to the right argument axis given by the corresponding element of the axis vector. Each axis may only be specified once, and unspecified axes are left unchanged.

History

Take was present in APL\360, but not in A Programming Language, where the symbol () was used for Rotate.