From 2afb23928e1984d475cc460e1672e8f6fa0e4dbe Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Wed, 11 Aug 2021 17:21:31 -0400 Subject: Allow clicking on header to get fragment link --- docs/doc/indices.html | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'docs/doc/indices.html') diff --git a/docs/doc/indices.html b/docs/doc/indices.html index fe67492d..8ed4317e 100644 --- a/docs/doc/indices.html +++ b/docs/doc/indices.html @@ -4,7 +4,7 @@ BQN: Indices -

Indices

+

Indices

One-dimensional arrays such as K lists or Python arrays have only one kind of index, a single number that refers to an element. For multidimensional arrays using the leading axis theory, there are several types of indexing that can be useful. Historically, nested APL designs have equivocated between these, which I believe can lead to subtle errors when programming. BQN focuses on single-number (atomic) indices, which can refer to list elements or array major cells (or more generally indexing along any particular axis). When using atomic indices to select elements, the indexed array has to be a list. In contrast, elements of any array can be indicated by list indices with length equal to that array's rank. Only two BQN primitives use these list indices: Range (↕), which returns an array of them if given a list argument, and Pick (βŠ‘), where the depth-1 components of an array left argument are list indices.

The following functions take or return indices. Except where marked, the indices are in the result; this is by far the most common type of index use. Group (βŠ”) is given two rows as it falls into both cases. Note that in the result case, there is usually no possibility for the programmer to select the format of indices. Instead, the language should be carefully designed to make sure that the kind of index returned is as useful as possible.

@@ -86,15 +86,15 @@

In Dyadic Transpose (⍉), 𝕨 is made up of indices into axes of 𝕩. Since array shape is 1-dimensional, there is only one sensible choice for these elements, a single number each.

-

Element indices

+

Element indices

In general, the index of an element of an array is a list whose length matches the array rank. It is also possible to use a number for an index into a list, as the list index is a singleton, but this must be kept consistent with the rest of the language. NARS-family APLs make the Index Generator (↕ in BQN) return a numeric list when the argument has length 1 but a nested array otherwise. This means that the depth of the result depends on the shape of the argument, inverting the typical hierarchy. BQN shouldn't have such an inconsistency.

Functions Range (↕), Indices (/), Group (βŠ”), and Pick (βŠ‘) naturally deal with element indices. Each of these can be defined to use list indices. However, this usually rules out the possibility of using atomic indices, which makes these functions harder to use both with generic array manipulation and with the major cell indices discussed in the next section. For this reason BQN restricts βŠ” and / to use atomic indices, which comes with the requirement that the arguments to Group and Indices, and the result of Group Indices, must be lists. For dyadic Group the depth-1 elements of 𝕨 are arrays of indices along axes of the result (multi-axis documentation). This means each axis of 𝕩 can only be related to one axis of the result.

Unlike / and βŠ”, ↕ and βŠ‘ do use list element indices. For ↕ this is because the output format can be controlled by the argument format: if passed a single number, the result uses atomic indices (so it's a numeric list); if passed a list, it uses list indices and the result has depth 2 (the result depth is always one greater than the argument depth). For βŠ‘, list indices are chosen because Select (⊏) handles atomic indices well already. When selecting multiple elements from a list, they would typically have to be placed in an array, which is equivalent to ⊏ with a numeric list 𝕨. An atom 𝕨 in Pick is converted to a list, so it can be used to select a single element if only one is wanted. To select multiple elements, βŠ‘ uses each depth-1 array in 𝕨 as an index and replaces it with that element from the right argument. Because this uses elements as elements (not cells), it is impossible to have conformability errors where elements do not fit together (invalid index errors are of course still possible). Atoms also cannot be used in this context, as it would create ambiguity: is a one-element list an index, or does it contain an index?

-

Major cell indices

+

Major cell indices

One of the successes of the leading axis model is to introduce a kind of index for multidimensional arrays that is easier to work with than list indices. The model introduces cells, where a cell index is a list of any length up to the containing array's rank. General cell indices are discussed in the next section; first we introduce a special case, indices into major cells or Β―1-cells. These cells naturally form a list, so the index of a major cell is a single number. Such an index can also be considered to select along the first axis, since an index along any axis is a single number.

Ordering functions ⍋⍒ and search/self-search functions βŠβŠ’ that depend on cell ordering only really make sense with major cell indices: while other indices have an ordering, it's not very natural. Note that ⊐ only uses the ordering in an incidental way, because it's defined to return the first index where a cell in 𝕩 is found. A mathematician would be more interested in a "pre-image" function that returns the set of all indices where a particular value appears. However, programming usefulness and consistency with the other search functions makes searching for the first index a reasonable choice.

Only one other functionβ€”but an important one!β€”deals with cells rather than elements: Select (⊏). Select allows either a simple first-axis case where 𝕨 has depth 1 or less (a depth-0 argument is automatically enclosed), and a multi-axis case where it is a list of depth-1 elements. In each case the depth-1 arrays index along a single axis.

-

General cell indices

+

General cell indices

BQN does not use general cell indices directly, but it is useful to consider how they might work, and how a programmer might implement functions that use them in BQN if needed. The functions /, βŠ”, and ⊏ are the ones that can work with indices for multidimensional arrays but don't already. Here we will examine how multidimensional versions would work.

A cell index into an array of rank r is a numeric list of length l≀r, which then refers to a cell of rank r-l. In BQN, the cell at index i of array a is i<¨⊸⊏a.

Because the shape of a cell index relates to the shape of the indexed array, it makes sense not to enclose cell indices, instead treating them as rows of an index array. A definition for ⊏ for depth-1 left arguments of rank at least 1 follows: replace each row of the left argument with the indexed cell of the right, yielding a result with the same depth as the right argument and shape 𝕨((Β―1β†“βŠ£)∾(Β―1β†‘βŠ£)βŠΈβ†“)○≒𝕩.

-- cgit v1.2.3