Indexing: Difference between revisions
m (Text replacement - " ⊢( *[^∘])" to " ⎕←$1") Tags: Mobile edit Mobile web edit |
m (Text replacement - "<source" to "<syntaxhighlight") |
||
(One intermediate revision by the same user not shown) | |||
Line 5: | Line 5: | ||
== Rectangular indexing == | == Rectangular indexing == | ||
'''Rectangular indexing''' selects indices along each [[axis]] of a given array. This indexing mode is usually implemented as [[Bracket indexing]] < | '''Rectangular indexing''' selects indices along each [[axis]] of a given array. This indexing mode is usually implemented as [[Bracket indexing]] <syntaxhighlight lang=apl inline>X[Y]</syntaxhighlight> and [[Index (function)|Squad indexing]] <syntaxhighlight lang=apl inline>Y⌷X</syntaxhighlight>. Rectangular indexing has an important property: If rectangular indexing is applied to a [[matrix]], the elements at four corners of a rectangle still form a rectangle in the result. This property generalizes to four elements of a higher-[[rank]] array for that share an index over all but two axes. | ||
< | <syntaxhighlight lang=apl> | ||
⎕←mat←10⊥¨⍳3 4 | ⎕←mat←10⊥¨⍳3 4 | ||
11 12 13 14 | 11 12 13 14 | ||
Line 19: | Line 19: | ||
21 24 | 21 24 | ||
31 34 | 31 34 | ||
</ | </syntaxhighlight> | ||
=== Relationship to leading axis theory === | === Relationship to leading axis theory === | ||
Line 25: | Line 25: | ||
For implementations that support [[leading axis theory]], it is common to select [[cell|cells]] or especially [[major cell|major cells]] of an array. In these implementations, [[Index (function)|Squad]] can be used with short left [[argument]] so that the omitted trailing axes can be selected as-is. A few implementations also support [[Select]], the primitive dedicated to selecting major cells. | For implementations that support [[leading axis theory]], it is common to select [[cell|cells]] or especially [[major cell|major cells]] of an array. In these implementations, [[Index (function)|Squad]] can be used with short left [[argument]] so that the omitted trailing axes can be selected as-is. A few implementations also support [[Select]], the primitive dedicated to selecting major cells. | ||
< | <syntaxhighlight lang=apl> | ||
(⊂1 1 2)⌷mat | (⊂1 1 2)⌷mat | ||
11 12 13 14 | 11 12 13 14 | ||
11 12 13 14 | 11 12 13 14 | ||
21 22 23 24 | 21 22 23 24 | ||
</ | </syntaxhighlight> | ||
=== Replicate as a special case === | === Replicate as a special case === | ||
Various forms of [[Replicate]] can be used to select one or more indices over a single axis. < | Various forms of [[Replicate]] can be used to select one or more indices over a single axis. <syntaxhighlight lang=apl inline>X/Y</syntaxhighlight> applies to the last axis, <syntaxhighlight lang=apl inline>X⌿Y</syntaxhighlight> to the first, and <syntaxhighlight lang=apl inline>X/[K]Y</syntaxhighlight> applies to the K-th axis. All the other axes are kept intact. The main difference from other indexing primitives is that X does not specify the indices; instead, it specifies how many copies of the corresponding indices to include in the result. | ||
< | <syntaxhighlight lang=apl> | ||
2 1 0⌿mat | 2 1 0⌿mat | ||
11 12 13 14 | 11 12 13 14 | ||
11 12 13 14 | 11 12 13 14 | ||
21 22 23 24 | 21 22 23 24 | ||
</ | </syntaxhighlight> | ||
== Non-rectangular indexing == | == Non-rectangular indexing == | ||
Line 47: | Line 47: | ||
'''Non-rectangular indexing''' selects multi-dimensional indices from the entire array. This only works with [[nested array model]] because the index specification must contain [[vector|vectors]] as its elements. A few APL implementations support this form of indexing as [[Bracket indexing#Choose indexing|a mode of Bracket indexing]]. | '''Non-rectangular indexing''' selects multi-dimensional indices from the entire array. This only works with [[nested array model]] because the index specification must contain [[vector|vectors]] as its elements. A few APL implementations support this form of indexing as [[Bracket indexing#Choose indexing|a mode of Bracket indexing]]. | ||
< | <syntaxhighlight lang=apl> | ||
mat[2 2⍴(1 1)(2 2)(1 3)(2 4)] | mat[2 2⍴(1 1)(2 2)(1 3)(2 4)] | ||
11 22 | 11 22 | ||
13 24 | 13 24 | ||
</ | </syntaxhighlight> | ||
== Deep indexing == | == Deep indexing == | ||
Line 57: | Line 57: | ||
In [[nested array model]], a need to access items through layers of nesting arises. '''Deep indexing''' uses a vector of indices over multiple layers to fetch a deeply nested item. This mode of indexing is supported by [[Pick]], and as [[Bracket indexing#Reach indexing|a mode of Bracket indexing]] in a few implementations. | In [[nested array model]], a need to access items through layers of nesting arises. '''Deep indexing''' uses a vector of indices over multiple layers to fetch a deeply nested item. This mode of indexing is supported by [[Pick]], and as [[Bracket indexing#Reach indexing|a mode of Bracket indexing]] in a few implementations. | ||
< | <syntaxhighlight lang=apl> | ||
⎕←G←2 3⍴('ABC' 1)('DEF' 2)('GHI' 3)('JKL' 4)('MNO' 5)('PQR' 6) | ⎕←G←2 3⍴('ABC' 1)('DEF' 2)('GHI' 3)('JKL' 4)('MNO' 5)('PQR' 6) | ||
┌───────┬───────┬───────┐ | ┌───────┬───────┬───────┐ | ||
Line 72: | Line 72: | ||
((2 1)1 2)⊃G | ((2 1)1 2)⊃G | ||
K | K | ||
</ | </syntaxhighlight> | ||
{{APL features}}[[Category:Arrays]] | {{APL features}}[[Category:Arrays]] |
Latest revision as of 22:13, 10 September 2022
- This page is about the concept of extracting items from an array. See Index for the concept of a location in an array. See Bracket indexing, Index (function), Pick, and Select for the primitives designed for indexing.
In the APL array model, indexing refers to the operation of extracting one or more elements from an array based on some specification of indices. All indexing operations are subject to index origin in languages which have such a concept.
Rectangular indexing
Rectangular indexing selects indices along each axis of a given array. This indexing mode is usually implemented as Bracket indexing X[Y]
and Squad indexing Y⌷X
. Rectangular indexing has an important property: If rectangular indexing is applied to a matrix, the elements at four corners of a rectangle still form a rectangle in the result. This property generalizes to four elements of a higher-rank array for that share an index over all but two axes.
⎕←mat←10⊥¨⍳3 4 11 12 13 14 21 22 23 24 31 32 33 34 mat[1 1 2;] 11 12 13 14 11 12 13 14 21 22 23 24 mat[2 3;1 4] 21 24 31 34
Relationship to leading axis theory
For implementations that support leading axis theory, it is common to select cells or especially major cells of an array. In these implementations, Squad can be used with short left argument so that the omitted trailing axes can be selected as-is. A few implementations also support Select, the primitive dedicated to selecting major cells.
(⊂1 1 2)⌷mat 11 12 13 14 11 12 13 14 21 22 23 24
Replicate as a special case
Various forms of Replicate can be used to select one or more indices over a single axis. X/Y
applies to the last axis, X⌿Y
to the first, and X/[K]Y
applies to the K-th axis. All the other axes are kept intact. The main difference from other indexing primitives is that X does not specify the indices; instead, it specifies how many copies of the corresponding indices to include in the result.
2 1 0⌿mat 11 12 13 14 11 12 13 14 21 22 23 24
Non-rectangular indexing
Non-rectangular indexing selects multi-dimensional indices from the entire array. This only works with nested array model because the index specification must contain vectors as its elements. A few APL implementations support this form of indexing as a mode of Bracket indexing.
mat[2 2⍴(1 1)(2 2)(1 3)(2 4)] 11 22 13 24
Deep indexing
In nested array model, a need to access items through layers of nesting arises. Deep indexing uses a vector of indices over multiple layers to fetch a deeply nested item. This mode of indexing is supported by Pick, and as a mode of Bracket indexing in a few implementations.
⎕←G←2 3⍴('ABC' 1)('DEF' 2)('GHI' 3)('JKL' 4)('MNO' 5)('PQR' 6) ┌───────┬───────┬───────┐ │┌───┬─┐│┌───┬─┐│┌───┬─┐│ ││ABC│1│││DEF│2│││GHI│3││ │└───┴─┘│└───┴─┘│└───┴─┘│ ├───────┼───────┼───────┤ │┌───┬─┐│┌───┬─┐│┌───┬─┐│ ││JKL│4│││MNO│5│││PQR│6││ │└───┴─┘│└───┴─┘│└───┴─┘│ └───────┴───────┴───────┘ ((2 1)1)⊃G JKL ((2 1)1 2)⊃G K
APL features [edit] | |
---|---|
Built-ins | Primitives (functions, operators) ∙ Quad name |
Array model | Shape ∙ Rank ∙ Depth ∙ Bound ∙ Index (Indexing) ∙ Axis ∙ Ravel ∙ Ravel order ∙ Element ∙ Scalar ∙ Vector ∙ Matrix ∙ Simple scalar ∙ Simple array ∙ Nested array ∙ Cell ∙ Major cell ∙ Subarray ∙ Empty array ∙ Prototype |
Data types | Number (Boolean, Complex number) ∙ Character (String) ∙ Box ∙ Namespace ∙ Function array |
Concepts and paradigms | Conformability (Scalar extension, Leading axis agreement) ∙ Scalar function (Pervasion) ∙ Identity element ∙ Complex floor ∙ Array ordering (Total) ∙ Tacit programming (Function composition, Close composition) ∙ Glyph ∙ Leading axis theory ∙ Major cell search ∙ First-class function |
Errors | LIMIT ERROR ∙ RANK ERROR ∙ SYNTAX ERROR ∙ DOMAIN ERROR ∙ LENGTH ERROR ∙ INDEX ERROR ∙ VALUE ERROR ∙ EVOLUTION ERROR |