Subarray: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
m (Array relationships category)
(A cell ''is'' a part with lesser rank.)
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
:''The term sub-array may also be used to refer to an array [[cell]].''
:''The term sub-array may also be used to refer part of an array with lesser [[rank]] (i.e. a [[cell]]) or [[depth]].''


A '''subarray''' (or '''sub-array''') of an array is another array which can be formed by selecting a contiguous section along each [[axis]]. The subarrays of an array are all those that result from [[indexing]] that array by a full set of [[index]] arrays each of which is either a [[scalar]] or a [[vector]] of sequential indices. The term subarray is used loosely in APL and rarely defined directly. Depending on context, scalar indices may be allowed only for [[Axis ordering|leading]] axes or not at all. If scalar indices are not allowed than a subarray shares every [[axis]] with the array that contains it and has equal [[rank]] to it; we might call such subarrays ''equal-rank'' subarrays. Otherwise a subarray has rank less than or equal to its containing array.
A '''subarray''' (or '''sub-array''') of an array is another array which can be formed by selecting a contiguous section along each [[axis]]. The subarrays of an array are all those that result from [[Bracket indexing|indexing]] that array by a full set of [[index]] arrays each of which is either a [[scalar]] or a [[vector]] of sequential indices. The term subarray is used loosely in APL and rarely defined directly. Depending on context, scalar indices may be allowed only for [[Axis ordering|leading]] axes or not at all. If scalar indices are not allowed than a subarray shares every [[axis]] with the array that contains it and has equal [[rank]] to it; we might call such subarrays ''equal-rank'' subarrays. Otherwise a subarray has rank less than or equal to its containing array.


A vector subarray of a [[vector]] is a subvector. In languages outside of the APL family which have only 1-dimensional arrays, "subarray" may have this meaning: for example, the [[wikipedia:maximum subarray problem|maximum subarray problem]] is usually taken to apply to 1-dimensional arrays only. The term "slice" may also be used to refer to a subvector outside of APL.
A vector subarray of a [[vector]] is a subvector. In languages outside of the APL family which have only 1-dimensional arrays, "subarray" may have this meaning: for example, the [[wikipedia:maximum subarray problem|maximum subarray problem]] is usually taken to apply to 1-dimensional arrays only. The term "slice" may also be used to refer to a subvector outside of APL.


The [[Find]] (<source lang=apl inline>⍷</source>) function searches for occurrences of the left argument as a subarray of the right argument. For Find, a subarray may have leading axes dropped but not others. We might also say that Find searches for the left argument after extending its [[shape]] with leading 1s, or that it searches for the left argument as a subarray of any equal-[[rank]] [[cell]] of the right argument.
The [[Find]] (<syntaxhighlight lang=apl inline>⍷</syntaxhighlight>) function searches for occurrences of the left argument as a subarray of the right argument. For Find, a subarray may have leading axes dropped but not others. We might also say that Find searches for the left argument after extending its [[shape]] with leading 1s, or that it searches for the left argument as a subarray of any equal-[[rank]] [[cell]] of the right argument.
<source lang=apl>
<syntaxhighlight lang=apl>
       ⊢A ← 5 4⍴⎕A
       ⎕←A ← 5 4⍴⎕A
ABCD
ABCD
EFGH
EFGH
Line 37: Line 37:
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
</source>
</syntaxhighlight>


Special types of equal-rank subarrays include:
Special types of equal-rank subarrays include:

Latest revision as of 09:27, 14 September 2022

The term sub-array may also be used to refer part of an array with lesser rank (i.e. a cell) or depth.

A subarray (or sub-array) of an array is another array which can be formed by selecting a contiguous section along each axis. The subarrays of an array are all those that result from indexing that array by a full set of index arrays each of which is either a scalar or a vector of sequential indices. The term subarray is used loosely in APL and rarely defined directly. Depending on context, scalar indices may be allowed only for leading axes or not at all. If scalar indices are not allowed than a subarray shares every axis with the array that contains it and has equal rank to it; we might call such subarrays equal-rank subarrays. Otherwise a subarray has rank less than or equal to its containing array.

A vector subarray of a vector is a subvector. In languages outside of the APL family which have only 1-dimensional arrays, "subarray" may have this meaning: for example, the maximum subarray problem is usually taken to apply to 1-dimensional arrays only. The term "slice" may also be used to refer to a subvector outside of APL.

The Find () function searches for occurrences of the left argument as a subarray of the right argument. For Find, a subarray may have leading axes dropped but not others. We might also say that Find searches for the left argument after extending its shape with leading 1s, or that it searches for the left argument as a subarray of any equal-rank cell of the right argument.

      ⎕←A ← 5 4⍴⎕A
ABCD
EFGH
IJKL
MNOP
QRST
      (2 3⍴'FGHJKL') ⍷ A
0 0 0 0
0 1 0 0
0 0 0 0
0 0 0 0
0 0 0 0
      'MN' ⍷ A            ⍝ Found in a row
0 0 0 0
0 0 0 0
0 0 0 0
1 0 0 0
0 0 0 0
      'CGKO' ⍷ A          ⍝ Not found in a column
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
      (⍪'CGKO') ⍷ A       ⍝ Found after adding last axis
0 0 1 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0

Special types of equal-rank subarrays include:

General subarrays include:

Reduction and the related operators Scan and Windowed Reduction reduce along 1-dimensional subarrays of the right argument. The vectors used in a particular reduction can be formed by taking a scalar index along each axis except the reduction axis, and selecting from the reduction axis according to the operator used. For a reduction the entire axis is used while for scans prefixes are used and windowed reductions take subsections of the length specified by the left argument.


APL features [edit]
Built-ins Primitives (functions, operators) ∙ Quad name
Array model ShapeRankDepthBoundIndex (Indexing) ∙ AxisRavelRavel orderElementScalarVectorMatrixSimple scalarSimple arrayNested arrayCellMajor cellSubarrayEmpty arrayPrototype
Data types Number (Boolean, Complex number) ∙ Character (String) ∙ BoxNamespaceFunction array
Concepts and paradigms Conformability (Scalar extension, Leading axis agreement) ∙ Scalar function (Pervasion) ∙ Identity elementComplex floorArray ordering (Total) ∙ Tacit programming (Function composition, Close composition) ∙ GlyphLeading axis theoryMajor cell search
Errors LIMIT ERRORRANK ERRORSYNTAX ERRORDOMAIN ERRORLENGTH ERRORINDEX ERRORVALUE ERROREVOLUTION ERROR