Bracket indexing: Difference between revisions

Jump to navigation Jump to search
108 bytes added ,  21:58, 10 September 2022
m
Text replacement - "<source" to "<syntaxhighlight"
m (postfix→postcircumfix)
m (Text replacement - "<source" to "<syntaxhighlight")
Line 1: Line 1:
{{Built-in|Bracket indexing|<nowiki>[]</nowiki>}}, or simply '''Indexing''', is a special [[primitive function]] which uses the postcircumfix notation <source lang=apl inline>X[Y]</source> instead of a normal prefix function. The result of <source lang=apl inline>X[Y]</source> is an array formed with items of X extracted by the [[index]] specification Y.
{{Built-in|Bracket indexing|<nowiki>[]</nowiki>}}, or simply '''Indexing''', is a special [[primitive function]] which uses the postcircumfix notation <syntaxhighlight lang=apl inline>X[Y]</source> instead of a normal prefix function. The result of <syntaxhighlight lang=apl inline>X[Y]</source> is an array formed with items of X extracted by the [[index]] specification Y.


== Indexing modes ==
== Indexing modes ==
Line 5: Line 5:
=== Simple indexing ===
=== Simple indexing ===


Most APL implementations support only this mode of indexing. In its simplest form, <source lang=apl inline>X[Y]</source> on vector X and scalar Y extracts the item of X at index Y. In general, Y can be an array of any shape, with each item being a valid index in X; then <source lang=apl inline>X[Y]</source> is a Y-shaped array which contains the indexed results.
Most APL implementations support only this mode of indexing. In its simplest form, <syntaxhighlight lang=apl inline>X[Y]</source> on vector X and scalar Y extracts the item of X at index Y. In general, Y can be an array of any shape, with each item being a valid index in X; then <syntaxhighlight lang=apl inline>X[Y]</source> is a Y-shaped array which contains the indexed results.


<source lang=apl>
<syntaxhighlight lang=apl>
       'ABCDE'[2]
       'ABCDE'[2]
B
B
Line 15: Line 15:
</source>
</source>


For higher-[[rank]] array X with rank n, the notation <source lang=apl inline>X[Y1;Y2;...;Yn]</source> selects the indexes of X over each axis. If some <source lang=apl inline>Yk</source> is omitted, it implies all indices of k-th axis is selected, which is equivalent to specifying <source lang=apl inline>⍳(⍴X)[k]</source>. The resulting shape is the concatenation of shapes of Y1, Y2, ..., Yn.
For higher-[[rank]] array X with rank n, the notation <syntaxhighlight lang=apl inline>X[Y1;Y2;...;Yn]</source> selects the indexes of X over each axis. If some <syntaxhighlight lang=apl inline>Yk</source> is omitted, it implies all indices of k-th axis is selected, which is equivalent to specifying <syntaxhighlight lang=apl inline>⍳(⍴X)[k]</source>. The resulting shape is the concatenation of shapes of Y1, Y2, ..., Yn.


<source lang=apl>
<syntaxhighlight lang=apl>
       ⎕←A←2 3 4⍴10×⍳24
       ⎕←A←2 3 4⍴10×⍳24
  10  20  30  40
  10  20  30  40
Line 39: Line 39:
</source>
</source>


The major limitation of this indexing mode is that it only supports rectangular selection. For example, it is not possible to form <source lang=apl inline>X[1;1],X[2;2]</source> from a matrix X by single indexing.
The major limitation of this indexing mode is that it only supports rectangular selection. For example, it is not possible to form <syntaxhighlight lang=apl inline>X[1;1],X[2;2]</source> from a matrix X by single indexing.


=== Choose indexing ===
=== Choose indexing ===
Line 45: Line 45:
In this mode, the index specification Y is a [[depth]]-2 [[nested array]]. Each item of Y is a vector whose length is the rank of X, and the result is a collection of items of X selected by each item of Y.
In this mode, the index specification Y is a [[depth]]-2 [[nested array]]. Each item of Y is a vector whose length is the rank of X, and the result is a collection of items of X selected by each item of Y.


<source lang=apl>
<syntaxhighlight lang=apl>
       M
       M
10 20 30 40
10 20 30 40
Line 68: Line 68:
In this mode, Y is a depth-3 nested array. Each item of Y is a vector of nested vectors which specify the index at each level of nesting (which is equivalent to the indexing by [[Pick]]). This allows to extract multiple items from a deeply nested array with a single indexing operation.
In this mode, Y is a depth-3 nested array. Each item of Y is a vector of nested vectors which specify the index at each level of nesting (which is equivalent to the indexing by [[Pick]]). This allows to extract multiple items from a deeply nested array with a single indexing operation.


<source lang=apl>
<syntaxhighlight lang=apl>
       G←('ABC' 1)('DEF' 2)('GHI' 3)('JKL' 4)
       G←('ABC' 1)('DEF' 2)('GHI' 3)('JKL' 4)
       G←2 3⍴G,('MNO' 5)('PQR' 6)
       G←2 3⍴G,('MNO' 5)('PQR' 6)

Navigation menu