Index

From APL Wiki
Revision as of 22:05, 10 September 2022 by Adám Brudzewsky (talk | contribs) (Text replacement - "</source>" to "</syntaxhighlight>")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
This page is about the array theory concept. See Index (function), Indexing, Indices, Index Generator, Index of, and Interval Index for operations named after indices.

In the APL array model, an index is a vector of integers which indicates an element of an array. The term "index" may also refer to a scalar index along an axis, or (particularly in leading axis theory) the index of a cell. Indices are subject to index origin in languages which have such a concept.

Element index

The correspondence between indices and elements of an array is one of the foundational concepts of APL. The indices for an array encapsulate its multidimensional structure by defining its axes, and also define the ordering within each axis. One definition of an array is simply a map (function) which specifies an element for each index, where the possible indices are determined by the array's shape.

An index is a vector of integers each of which is greater than or equal to the index origin. For a particular array, the index of an element has length equal to that array's rank, and it is less than the array's shape plus the index origin in every element. In APL we can write

(⍴i) ≡ ⍴⍴A
∧/ (⎕IO≤i) ∧ (i<(⍴A)-⎕IO)

to describe the possible indices i for an array A.

The elements of an array then correspond one-to-one with valid indices for that array (while two elements may have the same value as arrays, they are still considered to be different elements). Using Squad indexing, the element at index i is the sole element of scalar i⌷A.

When indexing into a scalar array, there is only one valid index: the empty numeric vector ⍴0, or Zilde. There are no valid indices for an empty array, because it has no elements.

Index of an axis

The indices and shape of an array both contain one element for each axis. Because these objects are vectors, each has an element ordering and indices of its own. These indices are called axis indices, and in addition to their implicit use in ordering the shape and index vectors, they are also used directly to refer to axes, for example in dyadic Transpose and when specifying a function axis.

In some cases, such as Laminate, function axis may allow a non-integer value. Such a value indicates an axis that exists in the result but not in the argument. It must lie strictly between ⎕IO-1 and ⎕IO+r for an array of rank r. For a specified axis i, the axis will be inserted between axes ⌊i and ⌈i, or before every axis if ⌊i is not a valid axis and after every axis if ⌈i is not a valid axis (for an empty array, both exceptions hold).

Ravel index

The ravel of an array can be used to define another kind of index selecting one of its elements. The ravel index is a scalar (although it could equivalently be defined as a singleton vector) which corresponds to an ordinary index by the relation r⌷A i⌷,A where r is the ravel index and i the ordinary index of an element in A. Using Encode and Decode we may write r (⍴A)⊥i and i (⍴A)⊤r assuming index origin 0.

The correspondence between indices and ravel indices is governed by ravel order. For more information, see Axis ordering.

Index along an axis

Each of the values in an index corresponds to one axis of the indexed array. When considered in isolation, one of these values (a scalar number) is called an index along the corresponding axis. Selecting from the array using this index produces an array whose rank is one smaller than the initial array (and it cannot be done to a scalar, as there are no axes along which to index). This array is sometimes called a "slice" or hyperplane of the array. Selecting on the first axis gives a major cell, one kind of hyperplane.

The set of possible indices into an array as a whole is the Cartesian product of the possible indices into each axis.

Index of a cell

In leading axis theory an array's shape may be split in two with leading axes forming the frame and trailing axes forming the shape of each cell. A vector of indices for the axes in the frame only selects a cell of the array. Squad indexing supports this kind of selection using a short simple left argument. Using a frame with the same length as that argument, the given indices correspond to leading axes of the right argument—the ones in the frame—and those axes are not present in the result. All the indices into a cell, corresponding to trailing axes, are implicit, and those axes are unchanged.

External links


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 searchFirst-class function
Errors LIMIT ERRORRANK ERRORSYNTAX ERRORDOMAIN ERRORLENGTH ERRORINDEX ERRORVALUE ERROREVOLUTION ERROR